OSDN Git Service

am 60d04edf: am e7c9540b: Refresh date display at midnight.
[android-x86/packages-apps-DeskClock.git] / src / com / android / deskclock / DeskClock.java
index 73a7448..e45f38a 100644 (file)
@@ -29,6 +29,7 @@ import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
 import android.content.res.Configuration;
 import android.content.res.Resources;
+import android.database.ContentObserver;
 import android.database.Cursor;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.ColorDrawable;
@@ -94,13 +95,11 @@ public class DeskClock extends Activity {
     // Alarm action for midnight (so we can update the date display).
     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.
-    private final long QUERY_WEATHER_DELAY = 5 * 60 * 1000; // 5 min
+    // Interval between forced polls of the weather widget.
+    private final long QUERY_WEATHER_DELAY = 60 * 60 * 1000; // 1 hr
 
     // Delay before engaging the burn-in protection mode (green-on-black).
-    private final long SCREEN_SAVER_TIMEOUT = 5* 60 * 1000; // 10 min
+    private final long SCREEN_SAVER_TIMEOUT = 5 * 60 * 1000; // 5 min
 
     // Repositioning delay in screen saver.
     private final long SCREEN_SAVER_MOVE_DELAY = 60 * 1000; // 1 min
@@ -167,8 +166,6 @@ public class DeskClock extends Activity {
 
     private boolean mLaunchedFromDock = false;
 
-    private int mIdleTimeoutEpoch = 0;
-
     private Random mRNG;
 
     private PendingIntent mMidnightIntent;
@@ -207,15 +204,21 @@ public class DeskClock extends Activity {
             } else if (m.what == UPDATE_WEATHER_DISPLAY_MSG) {
                 updateWeatherDisplay();
             } else if (m.what == SCREEN_SAVER_TIMEOUT_MSG) {
-                if (m.arg1 == mIdleTimeoutEpoch) {
-                    saveScreen();
-                }
+                saveScreen();
             } else if (m.what == SCREEN_SAVER_MOVE_MSG) {
                 moveScreenSaver();
             }
         }
     };
 
+    private final ContentObserver mContentObserver = new ContentObserver(mHandy) {
+        @Override
+        public void onChange(boolean selfChange) {
+            if (DEBUG) Log.d(LOG_TAG, "content observer notified that weather changed");
+            refreshWeather();
+        }
+    };
+
 
     private void moveScreenSaver() {
         moveScreenSaverTo(-1,-1);
@@ -264,6 +267,14 @@ public class DeskClock extends Activity {
         win.setAttributes(winParams);
     }
 
+    private void scheduleScreenSaver() {
+        // reschedule screen saver
+        mHandy.removeMessages(SCREEN_SAVER_TIMEOUT_MSG);
+        mHandy.sendMessageDelayed(
+            Message.obtain(mHandy, SCREEN_SAVER_TIMEOUT_MSG),
+            SCREEN_SAVER_TIMEOUT);
+    }
+
     private void restoreScreen() {
         if (!mScreenSaverMode) return;
         if (DEBUG) Log.d(LOG_TAG, "restoreScreen");
@@ -272,6 +283,9 @@ public class DeskClock extends Activity {
         doDim(false); // restores previous dim mode
         // policy: update weather info when returning from screen saver
         if (mPluggedIn) requestWeatherDataFetch();
+
+        scheduleScreenSaver();
+
         refreshAll();
     }
 
@@ -336,8 +350,7 @@ public class DeskClock extends Activity {
     private void requestWeatherDataFetch() {
         if (DEBUG) Log.d(LOG_TAG, "forcing the Genie widget to update weather now...");
         sendBroadcast(new Intent(ACTION_GENIE_REFRESH).putExtra("requestWeather", true));
-        // update the display with any new data
-        scheduleWeatherQueryDelayed(5000);
+        // we expect the result to show up in our content observer
     }
 
     private boolean supportsWeather() {
@@ -400,35 +413,21 @@ public class DeskClock extends Activity {
 
             mWeatherIconDrawable = mGenieResources.getDrawable(cur.getInt(
                 cur.getColumnIndexOrThrow("iconResId")));
-
+            mWeatherCurrentTemperatureString = String.format("%d\u00b0",
+                (cur.getInt(cur.getColumnIndexOrThrow("temperature"))));
+            mWeatherHighTemperatureString = String.format("%d\u00b0",
+                (cur.getInt(cur.getColumnIndexOrThrow("highTemperature"))));
+            mWeatherLowTemperatureString = String.format("%d\u00b0",
+                (cur.getInt(cur.getColumnIndexOrThrow("lowTemperature"))));
             mWeatherLocationString = cur.getString(
                 cur.getColumnIndexOrThrow("location"));
-
-            // any of these may be NULL
-            final int colTemp = cur.getColumnIndexOrThrow("temperature");
-            final int colHigh = cur.getColumnIndexOrThrow("highTemperature");
-            final int colLow = cur.getColumnIndexOrThrow("lowTemperature");
-
-            mWeatherCurrentTemperatureString =
-                cur.isNull(colTemp)
-                    ? "\u2014"
-                    : String.format("%d\u00b0", cur.getInt(colTemp));
-            mWeatherHighTemperatureString =
-                cur.isNull(colHigh)
-                    ? "\u2014"
-                    : String.format("%d\u00b0", cur.getInt(colHigh));
-            mWeatherLowTemperatureString =
-                cur.isNull(colLow)
-                    ? "\u2014"
-                    : String.format("%d\u00b0", cur.getInt(colLow));
         } else {
             Log.w(LOG_TAG, "No weather information available (cur="
                 + cur +")");
             mWeatherIconDrawable = null;
+            mWeatherHighTemperatureString = "";
+            mWeatherLowTemperatureString = "";
             mWeatherLocationString = getString(R.string.weather_fetch_failure);
-            mWeatherCurrentTemperatureString =
-                mWeatherHighTemperatureString =
-                mWeatherLowTemperatureString = "";
         }
 
         mHandy.sendEmptyMessage(UPDATE_WEATHER_DISPLAY_MSG);
@@ -570,6 +569,16 @@ public class DeskClock extends Activity {
         filter.addAction(ACTION_MIDNIGHT);
         registerReceiver(mIntentReceiver, filter);
 
+        // Listen for updates to weather data
+        Uri weatherNotificationUri = new Uri.Builder()
+            .scheme(android.content.ContentResolver.SCHEME_CONTENT)
+            .authority(WEATHER_CONTENT_AUTHORITY)
+            .path(WEATHER_CONTENT_PATH)
+            .build();
+        getContentResolver().registerContentObserver(
+            weatherNotificationUri, true, mContentObserver);
+
+        // Elaborate mechanism to find out when the day rolls over
         Calendar today = Calendar.getInstance();
         today.set(Calendar.HOUR_OF_DAY, 0);
         today.set(Calendar.MINUTE, 0);
@@ -592,10 +601,7 @@ public class DeskClock extends Activity {
 
         setWakeLock(mPluggedIn);
 
-        mIdleTimeoutEpoch++;
-        mHandy.sendMessageDelayed(
-            Message.obtain(mHandy, SCREEN_SAVER_TIMEOUT_MSG, mIdleTimeoutEpoch, 0),
-            SCREEN_SAVER_TIMEOUT);
+        scheduleScreenSaver();
 
         final boolean launchedFromDock
             = getIntent().hasCategory(Intent.CATEGORY_DESK_DOCK);
@@ -613,11 +619,15 @@ public class DeskClock extends Activity {
     public void onPause() {
         if (DEBUG) Log.d(LOG_TAG, "onPause");
 
-        // Turn off the screen saver. (But don't un-dim.)
+        // Turn off the screen saver and cancel any pending timeouts.
+        // (But don't un-dim.)
+        mHandy.removeMessages(SCREEN_SAVER_TIMEOUT_MSG);
         restoreScreen();
 
         // Other things we don't want to be doing in the background.
         unregisterReceiver(mIntentReceiver);
+        getContentResolver().unregisterContentObserver(mContentObserver);
+
         AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
         am.cancel(mMidnightIntent);
         unscheduleWeatherQuery();