Move all time ticks to bg handlers, and post
This will hopefully avoid blaming sysui for ANRs when the system is hosed. Test: Manual, build, push, wait for time to change Change-Id: I1661ac1a997ad8917b449dd175229d8b77f583c9
This commit is contained in:
@@ -390,6 +390,18 @@ public class DateTimeView extends TextView {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @hide
|
||||
*/
|
||||
public static void setReceiverHandler(Handler handler) {
|
||||
ReceiverInfo ri = sReceiverInfo.get();
|
||||
if (ri == null) {
|
||||
ri = new ReceiverInfo();
|
||||
sReceiverInfo.set(ri);
|
||||
}
|
||||
ri.setHandler(handler);
|
||||
}
|
||||
|
||||
private static class ReceiverInfo {
|
||||
private final ArrayList<DateTimeView> mAttachedViews = new ArrayList<DateTimeView>();
|
||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||
@@ -416,35 +428,46 @@ public class DateTimeView extends TextView {
|
||||
}
|
||||
};
|
||||
|
||||
private Handler mHandler = new Handler();
|
||||
|
||||
public void addView(DateTimeView v) {
|
||||
final boolean register = mAttachedViews.isEmpty();
|
||||
mAttachedViews.add(v);
|
||||
if (register) {
|
||||
register(getApplicationContextIfAvailable(v.getContext()));
|
||||
synchronized (mAttachedViews) {
|
||||
final boolean register = mAttachedViews.isEmpty();
|
||||
mAttachedViews.add(v);
|
||||
if (register) {
|
||||
register(getApplicationContextIfAvailable(v.getContext()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void removeView(DateTimeView v) {
|
||||
mAttachedViews.remove(v);
|
||||
if (mAttachedViews.isEmpty()) {
|
||||
unregister(getApplicationContextIfAvailable(v.getContext()));
|
||||
synchronized (mAttachedViews) {
|
||||
mAttachedViews.remove(v);
|
||||
if (mAttachedViews.isEmpty()) {
|
||||
unregister(getApplicationContextIfAvailable(v.getContext()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void updateAll() {
|
||||
final int count = mAttachedViews.size();
|
||||
for (int i = 0; i < count; i++) {
|
||||
mAttachedViews.get(i).clearFormatAndUpdate();
|
||||
synchronized (mAttachedViews) {
|
||||
final int count = mAttachedViews.size();
|
||||
for (int i = 0; i < count; i++) {
|
||||
DateTimeView view = mAttachedViews.get(i);
|
||||
view.post(() -> view.clearFormatAndUpdate());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
long getSoonestUpdateTime() {
|
||||
long result = Long.MAX_VALUE;
|
||||
final int count = mAttachedViews.size();
|
||||
for (int i = 0; i < count; i++) {
|
||||
final long time = mAttachedViews.get(i).mUpdateTimeMillis;
|
||||
if (time < result) {
|
||||
result = time;
|
||||
synchronized (mAttachedViews) {
|
||||
final int count = mAttachedViews.size();
|
||||
for (int i = 0; i < count; i++) {
|
||||
final long time = mAttachedViews.get(i).mUpdateTimeMillis;
|
||||
if (time < result) {
|
||||
result = time;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@@ -461,11 +484,21 @@ public class DateTimeView extends TextView {
|
||||
filter.addAction(Intent.ACTION_TIME_CHANGED);
|
||||
filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
|
||||
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
|
||||
context.registerReceiver(mReceiver, filter);
|
||||
context.registerReceiver(mReceiver, filter, null, mHandler);
|
||||
}
|
||||
|
||||
void unregister(Context context) {
|
||||
context.unregisterReceiver(mReceiver);
|
||||
}
|
||||
|
||||
public void setHandler(Handler handler) {
|
||||
mHandler = handler;
|
||||
synchronized (mAttachedViews) {
|
||||
if (!mAttachedViews.isEmpty()) {
|
||||
unregister(mAttachedViews.get(0).getContext());
|
||||
register(mAttachedViews.get(0).getContext());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user