Loading src/com/android/deskclock/Screensaver.java +37 −12 Original line number Original line Diff line number Diff line Loading @@ -16,29 +16,18 @@ package com.android.deskclock; package com.android.deskclock; import android.animation.Animator; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.TimeInterpolator; import android.content.BroadcastReceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Context; import android.content.Intent; import android.content.Intent; import android.content.IntentFilter; import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Configuration; import android.database.ContentObserver; import android.database.ContentObserver; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; import android.os.Handler; import android.os.Handler; import android.preference.PreferenceManager; import android.preference.PreferenceManager; import android.provider.Settings; import android.provider.Settings; import android.service.dreams.DreamService; import android.service.dreams.DreamService; import android.util.Log; import android.util.Log; import android.view.View; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.view.animation.DecelerateInterpolator; import com.android.deskclock.Utils.ScreensaverMoveSaverRunnable; import com.android.deskclock.Utils.ScreensaverMoveSaverRunnable; Loading @@ -62,6 +51,31 @@ public class Screensaver extends DreamService { } } }; }; // Thread that runs every midnight and refreshes the date. private final Runnable mMidnightUpdater = new Runnable() { @Override public void run() { Utils.updateDate(mDateFormat, mDateFormatForAccessibility, mContentView); Utils.setMidnightUpdater(mHandler, mMidnightUpdater); } }; /** * Receiver to handle time reference changes. */ private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action != null && (action.equals(Intent.ACTION_TIME_CHANGED) || action.equals(Intent.ACTION_TIMEZONE_CHANGED))) { Utils.updateDate(mDateFormat, mDateFormatForAccessibility, mContentView); Utils.refreshAlarm(Screensaver.this, mContentView); Utils.setMidnightUpdater(mHandler, mMidnightUpdater); } } }; public Screensaver() { public Screensaver() { if (DEBUG) Log.d(TAG, "Screensaver allocated"); if (DEBUG) Log.d(TAG, "Screensaver allocated"); mMoveSaverRunnable = new ScreensaverMoveSaverRunnable(mHandler); mMoveSaverRunnable = new ScreensaverMoveSaverRunnable(mHandler); Loading Loading @@ -97,6 +111,13 @@ public class Screensaver extends DreamService { layoutClockSaver(); layoutClockSaver(); // Setup handlers for time reference changes and date updates. IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_TIME_CHANGED); filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); registerReceiver(mIntentReceiver, filter); Utils.setMidnightUpdater(mHandler, mMidnightUpdater); getContentResolver().registerContentObserver( getContentResolver().registerContentObserver( Settings.System.getUriFor(Settings.System.NEXT_ALARM_FORMATTED), Settings.System.getUriFor(Settings.System.NEXT_ALARM_FORMATTED), false, false, Loading @@ -111,6 +132,10 @@ public class Screensaver extends DreamService { mHandler.removeCallbacks(mMoveSaverRunnable); mHandler.removeCallbacks(mMoveSaverRunnable); getContentResolver().unregisterContentObserver(mSettingsContentObserver); getContentResolver().unregisterContentObserver(mSettingsContentObserver); // Tear down handlers for time reference changes and date updates. Utils.resetMidnightUpdater(mHandler, mMidnightUpdater); unregisterReceiver(mIntentReceiver); } } private void setClockStyle() { private void setClockStyle() { Loading src/com/android/deskclock/Utils.java +24 −0 Original line number Original line Diff line number Diff line Loading @@ -41,6 +41,7 @@ import android.provider.Settings; import android.text.TextUtils; import android.text.TextUtils; import android.text.format.DateFormat; import android.text.format.DateFormat; import android.text.format.DateUtils; import android.text.format.DateUtils; import android.text.format.Time; import android.view.MenuItem; import android.view.MenuItem; import android.view.View; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.view.animation.AccelerateInterpolator; Loading Loading @@ -397,6 +398,29 @@ public class Utils { return startAlarmOnQuarterHour(context); return startAlarmOnQuarterHour(context); } } // Setup a thread that starts at midnight plus one second. The extra second is added to ensure // the date has changed. public static void setMidnightUpdater(Handler handler, Runnable runnable) { String timezone = TimeZone.getDefault().getID(); if (handler == null || runnable == null || timezone == null) { return; } long now = System.currentTimeMillis(); Time time = new Time(timezone); time.set(now); long runInMillis = ((24 - time.hour) * 3600 - time.minute * 60 - time.second + 1) * 1000; handler.removeCallbacks(runnable); handler.postDelayed(runnable, runInMillis); } // Stop the midnight update thread public static void resetMidnightUpdater(Handler handler, Runnable runnable) { if (handler == null || runnable == null) { return; } handler.removeCallbacks(runnable); } /** /** * For screensavers to set whether the digital or analog clock should be displayed. * For screensavers to set whether the digital or analog clock should be displayed. * Returns the view to be displayed. * Returns the view to be displayed. Loading Loading
src/com/android/deskclock/Screensaver.java +37 −12 Original line number Original line Diff line number Diff line Loading @@ -16,29 +16,18 @@ package com.android.deskclock; package com.android.deskclock; import android.animation.Animator; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.TimeInterpolator; import android.content.BroadcastReceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Context; import android.content.Intent; import android.content.Intent; import android.content.IntentFilter; import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.res.Configuration; import android.content.res.Configuration; import android.database.ContentObserver; import android.database.ContentObserver; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; import android.os.Handler; import android.os.Handler; import android.preference.PreferenceManager; import android.preference.PreferenceManager; import android.provider.Settings; import android.provider.Settings; import android.service.dreams.DreamService; import android.service.dreams.DreamService; import android.util.Log; import android.util.Log; import android.view.View; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.view.animation.DecelerateInterpolator; import com.android.deskclock.Utils.ScreensaverMoveSaverRunnable; import com.android.deskclock.Utils.ScreensaverMoveSaverRunnable; Loading @@ -62,6 +51,31 @@ public class Screensaver extends DreamService { } } }; }; // Thread that runs every midnight and refreshes the date. private final Runnable mMidnightUpdater = new Runnable() { @Override public void run() { Utils.updateDate(mDateFormat, mDateFormatForAccessibility, mContentView); Utils.setMidnightUpdater(mHandler, mMidnightUpdater); } }; /** * Receiver to handle time reference changes. */ private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action != null && (action.equals(Intent.ACTION_TIME_CHANGED) || action.equals(Intent.ACTION_TIMEZONE_CHANGED))) { Utils.updateDate(mDateFormat, mDateFormatForAccessibility, mContentView); Utils.refreshAlarm(Screensaver.this, mContentView); Utils.setMidnightUpdater(mHandler, mMidnightUpdater); } } }; public Screensaver() { public Screensaver() { if (DEBUG) Log.d(TAG, "Screensaver allocated"); if (DEBUG) Log.d(TAG, "Screensaver allocated"); mMoveSaverRunnable = new ScreensaverMoveSaverRunnable(mHandler); mMoveSaverRunnable = new ScreensaverMoveSaverRunnable(mHandler); Loading Loading @@ -97,6 +111,13 @@ public class Screensaver extends DreamService { layoutClockSaver(); layoutClockSaver(); // Setup handlers for time reference changes and date updates. IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_TIME_CHANGED); filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); registerReceiver(mIntentReceiver, filter); Utils.setMidnightUpdater(mHandler, mMidnightUpdater); getContentResolver().registerContentObserver( getContentResolver().registerContentObserver( Settings.System.getUriFor(Settings.System.NEXT_ALARM_FORMATTED), Settings.System.getUriFor(Settings.System.NEXT_ALARM_FORMATTED), false, false, Loading @@ -111,6 +132,10 @@ public class Screensaver extends DreamService { mHandler.removeCallbacks(mMoveSaverRunnable); mHandler.removeCallbacks(mMoveSaverRunnable); getContentResolver().unregisterContentObserver(mSettingsContentObserver); getContentResolver().unregisterContentObserver(mSettingsContentObserver); // Tear down handlers for time reference changes and date updates. Utils.resetMidnightUpdater(mHandler, mMidnightUpdater); unregisterReceiver(mIntentReceiver); } } private void setClockStyle() { private void setClockStyle() { Loading
src/com/android/deskclock/Utils.java +24 −0 Original line number Original line Diff line number Diff line Loading @@ -41,6 +41,7 @@ import android.provider.Settings; import android.text.TextUtils; import android.text.TextUtils; import android.text.format.DateFormat; import android.text.format.DateFormat; import android.text.format.DateUtils; import android.text.format.DateUtils; import android.text.format.Time; import android.view.MenuItem; import android.view.MenuItem; import android.view.View; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.view.animation.AccelerateInterpolator; Loading Loading @@ -397,6 +398,29 @@ public class Utils { return startAlarmOnQuarterHour(context); return startAlarmOnQuarterHour(context); } } // Setup a thread that starts at midnight plus one second. The extra second is added to ensure // the date has changed. public static void setMidnightUpdater(Handler handler, Runnable runnable) { String timezone = TimeZone.getDefault().getID(); if (handler == null || runnable == null || timezone == null) { return; } long now = System.currentTimeMillis(); Time time = new Time(timezone); time.set(now); long runInMillis = ((24 - time.hour) * 3600 - time.minute * 60 - time.second + 1) * 1000; handler.removeCallbacks(runnable); handler.postDelayed(runnable, runInMillis); } // Stop the midnight update thread public static void resetMidnightUpdater(Handler handler, Runnable runnable) { if (handler == null || runnable == null) { return; } handler.removeCallbacks(runnable); } /** /** * For screensavers to set whether the digital or analog clock should be displayed. * For screensavers to set whether the digital or analog clock should be displayed. * Returns the view to be displayed. * Returns the view to be displayed. Loading