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.IntentFilter;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewParent;
import android.widget.TextView;
@@ -39,10 +40,7 @@ public class DateView extends TextView {
private final Date mCurrentTime = new Date();
private SimpleDateFormat mDateFormat;
private boolean mChangedLocale;
private boolean mAttachedToWindow;
private boolean mWindowVisible;
private boolean mUpdating;
private String mLastText;
private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
@Override
@@ -53,7 +51,8 @@ public class DateView extends TextView {
|| Intent.ACTION_TIMEZONE_CHANGED.equals(action)
|| 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();
}
@@ -67,80 +66,39 @@ public class DateView extends TextView {
@Override
protected void 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
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mAttachedToWindow = false;
setUpdates();
}
@Override
protected void onWindowVisibilityChanged(int visibility) {
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;
mDateFormat = null; // reload the locale next time
mContext.unregisterReceiver(mIntentReceiver);
}
protected void updateClock() {
if (mDateFormat == null || mChangedLocale) {
if (mDateFormat == null) {
final String dateFormat = getContext().getString(R.string.system_ui_date_pattern);
final Locale l = Locale.getDefault();
final String fmt = ICU.getBestDateTimePattern(dateFormat, l.toString());
mDateFormat = new SimpleDateFormat(fmt, l);
mChangedLocale = false;
}
mCurrentTime.setTime(System.currentTimeMillis());
setText(mDateFormat.format(mCurrentTime));
}
private boolean isVisible() {
View v = this;
while (true) {
if (v.getVisibility() != VISIBLE) {
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);
}
final String text = mDateFormat.format(mCurrentTime);
if (!text.equals(mLastText)) {
setText(text);
mLastText = text;
}
}
}