am 7eb5ce03: Merge "Update date format in panel on locale change." into klp-dev

* commit '7eb5ce03d9697caa2e9caf0437036a937d081e90':
  Update date format in panel on locale change.
This commit is contained in:
Daniel Sandler
2013-09-30 20:55:06 -07:00
committed by Android Git Automerger

View File

@@ -21,6 +21,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import android.view.View; import android.view.View;
import android.view.ViewParent; import android.view.ViewParent;
import android.widget.TextView; import android.widget.TextView;
@@ -39,10 +40,7 @@ public class DateView extends TextView {
private final Date mCurrentTime = new Date(); private final Date mCurrentTime = new Date();
private SimpleDateFormat mDateFormat; private SimpleDateFormat mDateFormat;
private boolean mChangedLocale; private String mLastText;
private boolean mAttachedToWindow;
private boolean mWindowVisible;
private boolean mUpdating;
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override @Override
@@ -53,7 +51,8 @@ public class DateView extends TextView {
|| Intent.ACTION_TIMEZONE_CHANGED.equals(action) || Intent.ACTION_TIMEZONE_CHANGED.equals(action)
|| Intent.ACTION_LOCALE_CHANGED.equals(action)) { || Intent.ACTION_LOCALE_CHANGED.equals(action)) {
if (Intent.ACTION_LOCALE_CHANGED.equals(action)) { if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
mChangedLocale = true; // need to get a fresh date format
mDateFormat = null;
} }
updateClock(); updateClock();
} }
@@ -67,80 +66,39 @@ public class DateView extends TextView {
@Override @Override
protected void onAttachedToWindow() { protected void onAttachedToWindow() {
super.onAttachedToWindow(); super.onAttachedToWindow();
mAttachedToWindow = true;
setUpdates(); IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
filter.addAction(Intent.ACTION_LOCALE_CHANGED);
mContext.registerReceiver(mIntentReceiver, filter, null, null);
updateClock();
} }
@Override @Override
protected void onDetachedFromWindow() { protected void onDetachedFromWindow() {
super.onDetachedFromWindow(); super.onDetachedFromWindow();
mAttachedToWindow = false;
setUpdates();
}
@Override mDateFormat = null; // reload the locale next time
protected void onWindowVisibilityChanged(int visibility) { mContext.unregisterReceiver(mIntentReceiver);
super.onWindowVisibilityChanged(visibility);
mWindowVisible = visibility == VISIBLE;
setUpdates();
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
setUpdates();
}
@Override
protected int getSuggestedMinimumWidth() {
// makes the large background bitmap not force us to full width
return 0;
} }
protected void updateClock() { protected void updateClock() {
if (mDateFormat == null || mChangedLocale) { if (mDateFormat == null) {
final String dateFormat = getContext().getString(R.string.system_ui_date_pattern); final String dateFormat = getContext().getString(R.string.system_ui_date_pattern);
final Locale l = Locale.getDefault(); final Locale l = Locale.getDefault();
final String fmt = ICU.getBestDateTimePattern(dateFormat, l.toString()); final String fmt = ICU.getBestDateTimePattern(dateFormat, l.toString());
mDateFormat = new SimpleDateFormat(fmt, l); mDateFormat = new SimpleDateFormat(fmt, l);
mChangedLocale = false;
} }
mCurrentTime.setTime(System.currentTimeMillis()); mCurrentTime.setTime(System.currentTimeMillis());
setText(mDateFormat.format(mCurrentTime));
}
private boolean isVisible() { final String text = mDateFormat.format(mCurrentTime);
View v = this; if (!text.equals(mLastText)) {
while (true) { setText(text);
if (v.getVisibility() != VISIBLE) { mLastText = text;
return false;
}
final ViewParent parent = v.getParent();
if (parent instanceof View) {
v = (View)parent;
} else {
return true;
}
}
}
private void setUpdates() {
boolean update = mAttachedToWindow && mWindowVisible && isVisible();
if (update != mUpdating) {
mUpdating = update;
if (update) {
// Register for Intent broadcasts for the clock and battery
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
filter.addAction(Intent.ACTION_LOCALE_CHANGED);
mContext.registerReceiver(mIntentReceiver, filter, null, null);
updateClock();
} else {
mContext.unregisterReceiver(mIntentReceiver);
}
} }
} }
} }