OSDN Git Service

Re-schedule screen saver mode when returning to normal.
[android-x86/packages-apps-DeskClock.git] / src / com / android / deskclock / DeskClock.java
index 1471c41..cfb599e 100644 (file)
@@ -165,9 +165,7 @@ public class DeskClock extends Activity {
     private int mBatteryLevel = -1;
     private boolean mPluggedIn = false;
 
-    private boolean mInDock = false;
-
-    private int mIdleTimeoutEpoch = 0;
+    private boolean mLaunchedFromDock = false;
 
     private Random mRNG;
 
@@ -183,6 +181,16 @@ public class DeskClock extends Activity {
                 handleBatteryUpdate(
                     intent.getIntExtra("status", BATTERY_STATUS_UNKNOWN),
                     intent.getIntExtra("level", 0));
+            } else if (Intent.ACTION_DOCK_EVENT.equals(action)) {
+                int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, -1);
+                if (DEBUG) Log.d(LOG_TAG, "ACTION_DOCK_EVENT, state=" + state);
+                if (state == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
+                    if (mLaunchedFromDock) {
+                        // moveTaskToBack(false);
+                        finish();
+                    }
+                    mLaunchedFromDock = false;
+                }
             }
         }
     };
@@ -196,9 +204,7 @@ 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();
             }
@@ -253,6 +259,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");
@@ -261,6 +275,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();
     }
 
@@ -389,35 +406,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);
@@ -515,7 +518,6 @@ public class DeskClock extends Activity {
         if (mDimmed) {
             winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
             winParams.dimAmount = DIM_BEHIND_AMOUNT_DIMMED;
-            winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
 
             // show the window tint
             tintView.startAnimation(AnimationUtils.loadAnimation(this,
@@ -524,7 +526,6 @@ public class DeskClock extends Activity {
         } else {
             winParams.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
             winParams.dimAmount = DIM_BEHIND_AMOUNT_NORMAL;
-            winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
 
             // hide the window tint
             tintView.startAnimation(AnimationUtils.loadAnimation(this,
@@ -536,9 +537,19 @@ public class DeskClock extends Activity {
     }
 
     @Override
+    public void onNewIntent(Intent newIntent) {
+        super.onNewIntent(newIntent);
+        if (DEBUG) Log.d(LOG_TAG, "onNewIntent with intent: " + newIntent);
+
+        // update our intent so that we can consult it to determine whether or
+        // not the most recent launch was via a dock event 
+        setIntent(newIntent);
+    }
+
+    @Override
     public void onResume() {
         super.onResume();
-        if (DEBUG) Log.d(LOG_TAG, "onResume");
+        if (DEBUG) Log.d(LOG_TAG, "onResume with intent: " + getIntent());
 
         // reload the date format in case the user has changed settings
         // recently
@@ -547,43 +558,46 @@ public class DeskClock extends Activity {
         IntentFilter filter = new IntentFilter();
         filter.addAction(Intent.ACTION_DATE_CHANGED);
         filter.addAction(Intent.ACTION_BATTERY_CHANGED);
+        filter.addAction(Intent.ACTION_DOCK_EVENT);
         filter.addAction(ACTION_MIDNIGHT);
+        registerReceiver(mIntentReceiver, filter);
 
         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); // un-dim when resuming
+        // un-dim when resuming
+        mDimmed = false;
+        doDim(false);
+
         restoreScreen(); // disable screen saver
         refreshAll(); // will schedule periodic weather fetch
 
         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);
 
-        if (supportsWeather() && launchedFromDock && !mInDock) {
+        if (supportsWeather() && launchedFromDock && !mLaunchedFromDock) {
             // policy: fetch weather if launched via dock connection
             if (DEBUG) Log.d(LOG_TAG, "Device now docked; forcing weather to refresh right now");
             requestWeatherDataFetch();
         }
 
-        mInDock = launchedFromDock;
+        mLaunchedFromDock = launchedFromDock;
     }
 
     @Override
     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.