OSDN Git Service

Dismiss the desk clock if it was launched by docking.
authorDaniel Sandler <dsandler@google.com>
Fri, 11 Dec 2009 07:07:36 +0000 (02:07 -0500)
committerDaniel Sandler <dsandler@google.com>
Fri, 11 Dec 2009 07:07:36 +0000 (02:07 -0500)
In other words: if the clock is behaving like a dock app
(launched by a dock event), it should finish() when the
device is removed from the dock.  If, on the other hand,
it's behaving like a regular app (launched from the
Launcher), it should ignore an un-dock event.

This change also removes support for entering the desk dock
via a dialer code (an unnecessary feature since the app can
always be invoked from the Launcher).

Fixes http://b/2302215, approved by hiroshi.

AndroidManifest.xml
src/com/android/deskclock/DeskClock.java
src/com/android/deskclock/DockEventReceiver.java [deleted file]

index b030918..293d0fe 100644 (file)
             </intent-filter>
             <meta-data android:name="android.appwidget.provider" android:resource="@xml/analog_appwidget" />
         </receiver>
-
-        <receiver android:name="DockEventReceiver">
-            
-            <intent-filter>
-                <action android:name="android.intent.action.DOCK_EVENT" />
-            </intent-filter>
-            
-            <intent-filter>
-                <action android:name="android.provider.Telephony.SECRET_CODE" />
-                <data android:scheme="android_secret_code" android:host="3375" /><!-- DESK -->
-            </intent-filter>
-            
-        </receiver>
     </application>
 </manifest>
 
index 88030cd..4c02174 100644 (file)
@@ -165,7 +165,7 @@ public class DeskClock extends Activity {
     private int mBatteryLevel = -1;
     private boolean mPluggedIn = false;
 
-    private boolean mInDock = false;
+    private boolean mLaunchedFromDock = false;
 
     private int mIdleTimeoutEpoch = 0;
 
@@ -183,6 +183,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;
+                }
             }
         }
     };
@@ -534,9 +544,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
@@ -545,14 +565,15 @@ 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);
 
         // un-dim when resuming
         mDimmed = false;
@@ -571,13 +592,13 @@ public class DeskClock extends Activity {
         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
diff --git a/src/com/android/deskclock/DockEventReceiver.java b/src/com/android/deskclock/DockEventReceiver.java
deleted file mode 100644 (file)
index bfc4673..0000000
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package com.android.deskclock;
-
-import android.app.Notification;
-import android.app.NotificationManager;
-import android.app.PendingIntent;
-import android.content.BroadcastReceiver;
-import android.content.ComponentName;
-import android.content.Context;
-import android.content.Intent;
-
-/**
- * BroadcastReceiver which receives {@link Intent#ACTION_DOCK_EVENT} events.
- * Launches the CarDockActivity if the device is placed into a car dock.
- *
- * TODO: This is the wrong way to launch, as this would cause contention
- * between multiple activities trying to launch if others did the same. Instead
- * register for a regular intent which should fire when placed into a car dock.
- */
-public class DockEventReceiver extends BroadcastReceiver {
-    @Override
-    public void onReceive(Context context, Intent intent) {        
-        Intent clockIntent = new Intent(Intent.ACTION_MAIN);
-        clockIntent.setComponent(
-                new ComponentName(context, DeskClock.class));
-        clockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        
-        String action = intent.getAction();
-        if (Intent.ACTION_DOCK_EVENT.equals(action)) {
-            // Code to control a sticky notification for the dock.
-            /*
-            NotificationManager notificationManager = (NotificationManager)
-                    context.getSystemService(Context.NOTIFICATION_SERVICE);
-            
-            int dockState = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, -1);
-            if (dockState == Intent.EXTRA_DOCK_STATE_DESK) {
-                Notification n = new Notification();
-                n.icon = R.drawable.notification;
-                n.defaults = Notification.DEFAULT_LIGHTS;
-                n.flags = Notification.FLAG_ONGOING_EVENT;
-                n.tickerText = context.getString(R.string.notification_title);
-                n.when = 0;
-                n.setLatestEventInfo(
-                        context,
-                        context.getString(R.string.notification_title),
-                        context.getString(R.string.notification_text),
-                        PendingIntent.getActivity(context, 0, clockIntent, 0));
-                notificationManager.notify(0, n);
-            } else if (dockState == Intent.EXTRA_DOCK_STATE_UNDOCKED) {
-                notificationManager.cancelAll();
-            }
-            */
-        } else if (android.provider.Telephony.Intents.SECRET_CODE_ACTION.equals(action)) {
-            // The user dialed *#*#DESK#*#*
-            context.startActivity(clockIntent);
-        }
-    }
-}