OSDN Git Service

AlarmClock tweaks.
authorDaniel Sandler <dsandler@google.com>
Mon, 2 Nov 2009 21:23:52 +0000 (16:23 -0500)
committerDaniel Sandler <dsandler@google.com>
Mon, 2 Nov 2009 21:23:52 +0000 (16:23 -0500)
- No more layout jumps when switching between dim/undim
  modes
- Now registers itself as the "dock home"---that is, it
  hijacks the home softkey away from Launcher while the
  device is docked
- Shows "on top" of the keyguard; all exits, however, will
  return to the keyguard before completing
- Now uses the WindowManager to hold a wakelock when
  necessary
- Tweaked weather display (bringing the weather icon size
  under control)

AndroidManifest.xml
res/layout-land/desk_clock.xml
res/layout/desk_clock.xml
res/layout/desk_clock_buttons.xml
res/layout/desk_clock_weather.xml
src/com/android/deskclock/DeskClock.java

index fbfce2c..25ac74d 100644 (file)
                 android:label="@string/app_label"
                 android:theme="@android:style/Theme.Wallpaper.NoTitleBar"
                 android:icon="@drawable/ic_widget_analog_clock"
+                android:launchMode="singleInstance"
                 android:configChanges="orientation|keyboardHidden|keyboard|navigation">
                 >
+
+            <!-- while docked, this is our home application -->
+            <meta-data android:name="android.dock_home" android:value="true" />
+
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.DEFAULT" />
index 7079975..37f7c10 100644 (file)
@@ -25,6 +25,7 @@
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:orientation="vertical"
+        android:paddingTop="18dip"
         >
 
         <!-- Entire display region (everything but bottom buttons) -->
index 66f1e7c..06f45d1 100644 (file)
@@ -28,6 +28,7 @@
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:orientation="vertical"
+        android:paddingTop="18dip"
         >
 
         <!-- Entire display region (everything but bottom buttons) -->
index ad11cbe..35de5fe 100644 (file)
@@ -14,7 +14,6 @@
      limitations under the License.
 -->
 
-<!-- Buttons for the desk-mode alarm clock. -->
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="horizontal"
     android:layout_width="fill_parent"
index 71e80f5..762a32b 100644 (file)
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
-        android:layout_marginBottom="-4dip"
+        android:layout_marginBottom="-6dip"
         >
         <TextView android:id="@+id/weather_high_temperature"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:textSize="42sp"
+            android:textSize="50sp"
             android:textColor="#FFFFFFFF"
             android:shadowColor="#C0000000"
             android:shadowDx="0"
         <TextView android:id="@+id/weather_low_temperature"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:textSize="42sp"
-            android:textColor="#FFCCCCCC"
+            android:textSize="50sp"
+            android:textColor="#FFDDDDDD"
             android:shadowColor="#C0000000"
             android:shadowDx="0"
             android:shadowDy="0"
             android:shadowRadius="3.0"                        
             android:layout_marginRight="8dip"
-            android:layout_centerVertical="true"
             android:layout_toRightOf="@id/weather_high_temperature"
+            android:layout_alignBottom="@id/weather_high_temperature"
             />
         <ImageView android:id="@+id/weather_icon"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
+            android:layout_height="60sp"
+            android:layout_width="60sp"
+            android:gravity="left"
             android:layout_toRightOf="@id/weather_low_temperature"
-            android:layout_centerVertical="true"
-            android:paddingTop="2dip"
+            android:layout_alignBottom="@id/weather_low_temperature"
+            android:scaleType="centerInside"
             />
     </RelativeLayout>
     <TextView android:id="@+id/weather_location"
index 59963a3..c94c362 100644 (file)
@@ -79,7 +79,7 @@ import java.util.Random;
  * DeskClock clock view for desk docks.
  */
 public class DeskClock extends Activity {
-    private static final boolean DEBUG = true;
+    private static final boolean DEBUG = false;
 
     private static final String LOG_TAG = "DeskClock";
 
@@ -133,18 +133,15 @@ public class DeskClock extends Activity {
 
     private DateFormat mDateFormat;
 
-    private int mBatteryLevel;
-    private boolean mPluggedIn;
+    private int mBatteryLevel = -1;
+    private boolean mPluggedIn = false;
 
-    private PowerManager.WakeLock mWakeLock;
     private int mIdleTimeoutEpoch = 0;
 
     private boolean mWeatherFetchScheduled = false;
 
     private Random mRNG;
 
-
-
     private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
@@ -221,6 +218,18 @@ public class DeskClock extends Activity {
         mHandy.sendEmptyMessageDelayed(SCREEN_SAVER_MOVE_MSG, SCREEN_SAVER_MOVE_DELAY);
     }
 
+    private void setWakeLock(boolean hold) {
+        if (DEBUG) Log.d(LOG_TAG, (hold ? "hold" : " releas") + "ing wake lock");
+        Window win = getWindow();
+        WindowManager.LayoutParams winParams = win.getAttributes();
+        winParams.flags |= WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
+        if (hold)
+            winParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
+        else
+            winParams.flags &= (~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
+        win.setAttributes(winParams);
+    }
+
     private void restoreScreen() {
         if (!mScreenSaverMode) return;
         mScreenSaverMode = false;
@@ -244,6 +253,10 @@ public class DeskClock extends Activity {
         winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
         win.setAttributes(winParams);
 
+        // give up any internal focus before we switch layouts
+        final View focused = getCurrentFocus();
+        if (focused != null) focused.clearFocus();
+
         setContentView(R.layout.desk_clock_saver);
 
         mTime = (DigitalClock) findViewById(R.id.time);
@@ -367,16 +380,13 @@ public class DeskClock extends Activity {
     // Adapted from KeyguardUpdateMonitor.java
     private void handleBatteryUpdate(int plugStatus, int batteryLevel) {
         final boolean pluggedIn = (plugStatus == BATTERY_STATUS_CHARGING || plugStatus == BATTERY_STATUS_FULL);
+        if (pluggedIn != mPluggedIn) {
+            setWakeLock(pluggedIn);
+        }
         if (pluggedIn != mPluggedIn || batteryLevel != mBatteryLevel) {
             mBatteryLevel = batteryLevel;
             mPluggedIn = pluggedIn;
             refreshBattery();
-
-            if (mPluggedIn) {
-                if (!mWakeLock.isHeld()) mWakeLock.acquire();
-            } else {
-                if (mWakeLock.isHeld()) mWakeLock.release();
-            }
         }
     }
 
@@ -427,6 +437,10 @@ 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);
+
         // dim the wallpaper somewhat (how much is determined below)
         winParams.flags |= (WindowManager.LayoutParams.FLAG_DIM_BEHIND);
 
@@ -454,6 +468,11 @@ public class DeskClock extends Activity {
     @Override
     public void onResume() {
         super.onResume();
+        // NB: To avoid situations where the user launches Alarm Clock and is
+        // surprised to find it in dim mode (because it was last used in dim
+        // mode, but that last use is long in the past), we always un-dim upon
+        // bringing the activity to the foregreound.
+        mDimmed = false;
 
         // reload the date format in case the user has changed settings
         // recently
@@ -465,8 +484,9 @@ public class DeskClock extends Activity {
         registerReceiver(mIntentReceiver, filter);
 
         doDim(false);
+        restoreScreen();
         refreshAll();
-        if (mPluggedIn && !mWakeLock.isHeld()) mWakeLock.acquire();
+        setWakeLock(mPluggedIn);
 
         mIdleTimeoutEpoch++;
         mHandy.sendMessageDelayed(
@@ -479,11 +499,14 @@ public class DeskClock extends Activity {
         super.onPause();
         unregisterReceiver(mIntentReceiver);
         unscheduleWeatherFetch();
-        if (mWakeLock.isHeld()) mWakeLock.release();
     }
 
 
     private void initViews() {
+        // give up any internal focus before we switch layouts
+        final View focused = getCurrentFocus();
+        if (focused != null) focused.clearFocus();
+
         setContentView(R.layout.desk_clock);
 
         mTime = (DigitalClock) findViewById(R.id.time);
@@ -567,11 +590,6 @@ public class DeskClock extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
 
-        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
-        mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
-            "DeskClock");
-        mWakeLock.acquire();
-
         mRNG = new Random();
 
         try {