Merge "Stop using TIME_TICK broadcast in TextClock" into sc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
a0a7d15be9
@@ -34,7 +34,6 @@ import android.icu.text.DateTimePatternGenerator;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.SystemClock;
|
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.text.format.DateFormat;
|
import android.text.format.DateFormat;
|
||||||
@@ -45,6 +44,10 @@ import android.view.inspector.InspectableProperty;
|
|||||||
|
|
||||||
import com.android.internal.R;
|
import com.android.internal.R;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
@@ -185,18 +188,29 @@ public class TextClock extends TextView {
|
|||||||
|
|
||||||
private final Runnable mTicker = new Runnable() {
|
private final Runnable mTicker = new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
if (mStopTicking) {
|
removeCallbacks(this);
|
||||||
|
if (mStopTicking || !mShouldRunTicker) {
|
||||||
return; // Test disabled the clock ticks
|
return; // Test disabled the clock ticks
|
||||||
}
|
}
|
||||||
onTimeChanged();
|
onTimeChanged();
|
||||||
|
|
||||||
long now = SystemClock.uptimeMillis();
|
Instant now = mTime.toInstant();
|
||||||
long next = now + (1000 - now % 1000);
|
ZoneId zone = mTime.getTimeZone().toZoneId();
|
||||||
|
|
||||||
Handler handler = getHandler();
|
ZonedDateTime nextTick;
|
||||||
if (handler != null) {
|
if (mHasSeconds) {
|
||||||
handler.postAtTime(mTicker, next);
|
nextTick = now.atZone(zone).plusSeconds(1).withNano(0);
|
||||||
|
} else {
|
||||||
|
nextTick = now.atZone(zone).plusMinutes(1).withSecond(0).withNano(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long millisUntilNextTick = Duration.between(now, nextTick.toInstant()).toMillis();
|
||||||
|
if (millisUntilNextTick <= 0) {
|
||||||
|
// This should never happen, but if it does, then tick again in a second.
|
||||||
|
millisUntilNextTick = 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
postDelayed(this, millisUntilNextTick);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -519,8 +533,7 @@ public class TextClock extends TextView {
|
|||||||
mHasSeconds = DateFormat.hasSeconds(mFormat);
|
mHasSeconds = DateFormat.hasSeconds(mFormat);
|
||||||
|
|
||||||
if (mShouldRunTicker && hadSeconds != mHasSeconds) {
|
if (mShouldRunTicker && hadSeconds != mHasSeconds) {
|
||||||
if (hadSeconds) getHandler().removeCallbacks(mTicker);
|
mTicker.run();
|
||||||
else mTicker.run();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -557,14 +570,10 @@ public class TextClock extends TextView {
|
|||||||
|
|
||||||
if (!mShouldRunTicker && isVisible) {
|
if (!mShouldRunTicker && isVisible) {
|
||||||
mShouldRunTicker = true;
|
mShouldRunTicker = true;
|
||||||
if (mHasSeconds) {
|
|
||||||
mTicker.run();
|
mTicker.run();
|
||||||
} else {
|
|
||||||
onTimeChanged();
|
|
||||||
}
|
|
||||||
} else if (mShouldRunTicker && !isVisible) {
|
} else if (mShouldRunTicker && !isVisible) {
|
||||||
mShouldRunTicker = false;
|
mShouldRunTicker = false;
|
||||||
getHandler().removeCallbacks(mTicker);
|
removeCallbacks(mTicker);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -592,7 +601,6 @@ public class TextClock extends TextView {
|
|||||||
private void registerReceiver() {
|
private void registerReceiver() {
|
||||||
final IntentFilter filter = new IntentFilter();
|
final IntentFilter filter = new IntentFilter();
|
||||||
|
|
||||||
filter.addAction(Intent.ACTION_TIME_TICK);
|
|
||||||
filter.addAction(Intent.ACTION_TIME_CHANGED);
|
filter.addAction(Intent.ACTION_TIME_CHANGED);
|
||||||
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
|
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user