diff options
author | Daniel Sandler <dsandler@google.com> | 2009-11-11 11:03:10 -0800 |
---|---|---|
committer | Daniel Sandler <dsandler@google.com> | 2009-11-11 11:03:10 -0800 |
commit | ba328cd4a23a13732467ba5533a94ac3d7cbbf4e (patch) | |
tree | 8e669a958cb9b3e727f96e05a95048b810b9d182 | |
parent | 8c7bbe7b1ec01f1d551d9e6c8e8edc4ce4f9c2a6 (diff) | |
download | android_packages_apps_DeskClock-ba328cd4a23a13732467ba5533a94ac3d7cbbf4e.tar.gz android_packages_apps_DeskClock-ba328cd4a23a13732467ba5533a94ac3d7cbbf4e.tar.bz2 android_packages_apps_DeskClock-ba328cd4a23a13732467ba5533a94ac3d7cbbf4e.zip |
Use an alarm to refresh the date at midnight.
Fixes http://b/2251125 (we were detecting user-initiated
date changes but not the natural rolling of one day into the
next).
-rw-r--r-- | src/com/android/deskclock/DeskClock.java | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/com/android/deskclock/DeskClock.java b/src/com/android/deskclock/DeskClock.java index 2f4cde985..0cb23c85e 100644 --- a/src/com/android/deskclock/DeskClock.java +++ b/src/com/android/deskclock/DeskClock.java @@ -17,7 +17,9 @@ package com.android.deskclock; import android.app.Activity; +import android.app.AlarmManager; import android.app.AlertDialog; +import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; @@ -74,6 +76,7 @@ import java.io.IOException; import java.io.InputStream; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.Random; @@ -89,6 +92,8 @@ public class DeskClock extends Activity { // Intent used to start the music player. private static final String MUSIC_NOW_PLAYING = "com.android.music.PLAYBACK_VIEWER"; + private static final String ACTION_MIDNIGHT = "com.android.deskclock.MIDNIGHT"; + // Interval between polls of the weather widget. Its refresh period is // likely to be much longer (~3h), but we want to pick up any changes // within 5 minutes. @@ -162,6 +167,8 @@ public class DeskClock extends Activity { private Random mRNG; + private PendingIntent mMidnightIntent; + private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -438,7 +445,9 @@ public class DeskClock extends Activity { } private void refreshDate() { - mDate.setText(mDateFormat.format(new Date())); + final Date now = new Date(); + if (DEBUG) Log.d(LOG_TAG, "refreshing date..." + now); + mDate.setText(mDateFormat.format(now)); } private void refreshAlarm() { @@ -470,7 +479,6 @@ public class DeskClock extends Activity { Window win = getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); - // secret! winParams.flags |= (WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); winParams.flags |= (WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); @@ -518,6 +526,13 @@ public class DeskClock extends Activity { IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_DATE_CHANGED); filter.addAction(Intent.ACTION_BATTERY_CHANGED); + filter.addAction(ACTION_MIDNIGHT); + + Calendar today = Calendar.getInstance(); + today.add(Calendar.DATE, 1); + mMidnightIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_MIDNIGHT), 0); + AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); + am.setRepeating(AlarmManager.RTC, today.getTimeInMillis(), AlarmManager.INTERVAL_DAY, mMidnightIntent); registerReceiver(mIntentReceiver, filter); doDim(false); @@ -553,6 +568,8 @@ public class DeskClock extends Activity { // Other things we don't want to be doing in the background. unregisterReceiver(mIntentReceiver); + AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); + am.cancel(mMidnightIntent); unscheduleWeatherFetch(); super.onPause(); |