Use FLAG_SHOW_WHEN_LOCKED window flag to disable the keyguard in the phone app.

This replaces use of KeyguardManager.disableKeyguard() and reenableKeyguard().
Fixes bug b/2287830 "The phone is accessible for unauthorized user even when pattern lock is enabled"

Change-Id: I1a52144c16f0e6f2079c78241751ce632f1357e3
Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/src/com/android/phone/EmergencyDialer.java b/src/com/android/phone/EmergencyDialer.java
index 037fd9b..129e301 100644
--- a/src/com/android/phone/EmergencyDialer.java
+++ b/src/com/android/phone/EmergencyDialer.java
@@ -51,8 +51,8 @@
  * activity from apps/Contacts) that:
  *   1. Allows ONLY emergency calls to be dialed
  *   2. Disallows voicemail functionality
- *   3. Handles manually enabling/disabling the keyguard (since we get
- *      launched directly from the lock screen).
+ *   3. Uses the FLAG_SHOW_WHEN_LOCKED flag to allow this activity to stay
+ *      in front of the keyguard.
  *
  * TODO: Even though this is an ultra-simplified version of the normal
  * dialer, there's still lots of code duplication between this class and
@@ -149,6 +149,9 @@
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
 
+        // set this flag so this activity will stay in front of the keyguard
+        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
+
         // Set the content view
         setContentView(R.layout.emergency_dialer);
 
@@ -424,9 +427,6 @@
         return false;
     }
 
-    /**
-     * turn off keyguard on start.
-     */
     @Override
     protected void onResume() {
         super.onResume();
@@ -449,27 +449,22 @@
             }
         }
 
-        // Turn keyguard off and set the poke lock timeout to medium.  There is
-        // no need to do anything with the wake lock.
-        if (DBG) Log.d(LOG_TAG, "turning keyguard off, set to long timeout");
+        // Disable the status bar and set the poke lock timeout to medium.
+        // There is no need to do anything with the wake lock.
+        if (DBG) Log.d(LOG_TAG, "disabling status bar, set to long timeout");
         PhoneApp app = (PhoneApp) getApplication();
-        app.disableKeyguard();
         app.disableStatusBar();
         app.setScreenTimeout(PhoneApp.ScreenTimeoutDuration.MEDIUM);
 
         updateDialAndDeleteButtonStateEnabledAttr();
     }
 
-    /**
-     * turn on keyguard on pause.
-     */
     @Override
     public void onPause() {
-        // Turn keyguard back on and set the poke lock timeout to default.  There
-        // is no need to do anything with the wake lock.
-        if (DBG) Log.d(LOG_TAG, "turning keyguard back on and closing the dialer");
+        // Reenable the status bar and set the poke lock timeout to default.
+        // There is no need to do anything with the wake lock.
+        if (DBG) Log.d(LOG_TAG, "reenabling status bar and closing the dialer");
         PhoneApp app = (PhoneApp) getApplication();
-        app.reenableKeyguard();
         app.reenableStatusBar();
         app.setScreenTimeout(PhoneApp.ScreenTimeoutDuration.DEFAULT);
 
diff --git a/src/com/android/phone/InCallScreen.java b/src/com/android/phone/InCallScreen.java
index 977ebb5..432273e 100755
--- a/src/com/android/phone/InCallScreen.java
+++ b/src/com/android/phone/InCallScreen.java
@@ -558,6 +558,9 @@
 
         super.onCreate(icicle);
 
+        // set this flag so this activity will stay in front of the keyguard
+        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
+
         final PhoneApp app = PhoneApp.getInstance();
         app.setInCallScreenInstance(this);
 
@@ -678,13 +681,6 @@
 
         final PhoneApp app = PhoneApp.getInstance();
 
-        // Disable the keyguard the entire time the InCallScreen is
-        // active.  (This is necessary only for the case of receiving an
-        // incoming call while the device is locked; we need to disable
-        // the keyguard so you can answer the call and use the in-call UI,
-        // but we always re-enable the keyguard as soon as you leave this
-        // screen (see onPause().))
-        app.disableKeyguard();
         app.disableStatusBar();
 
         // Touch events are never considered "user activity" while the
@@ -928,9 +924,6 @@
                 }
             }, 500);
 
-        // The keyguard was disabled the entire time the InCallScreen was
-        // active (see onResume()).  Re-enable it now.
-        app.reenableKeyguard();
         app.reenableStatusBar();
 
         // Make sure we revert the poke lock and wake lock when we move to
@@ -2793,9 +2786,6 @@
             // Phone is idle!  We should exit this screen now.
             if (DBG) log("- delayedCleanupAfterDisconnect: phone is idle...");
 
-            // No need to re-enable keyguard or screen wake state here;
-            // that happens in onPause() when we actually exit.
-
             // And (finally!) exit from the in-call screen
             // (but not if we're already in the process of pausing...)
             if (mIsForegroundActivity) {
@@ -2822,8 +2812,6 @@
             // we don't need to keep the screen on.
             if (DBG) log("- delayedCleanupAfterDisconnect: staying on the InCallScreen...");
             if (DBG) PhoneUtils.dumpCallState(mPhone);
-            // No need to re-enable keyguard or screen wake state here;
-            // should be taken care of in onPhoneStateChanged();
         }
     }
 
diff --git a/src/com/android/phone/PhoneApp.java b/src/com/android/phone/PhoneApp.java
index 279ab43..e7f0872 100755
--- a/src/com/android/phone/PhoneApp.java
+++ b/src/com/android/phone/PhoneApp.java
@@ -184,8 +184,6 @@
     private PowerManager.WakeLock mPartialWakeLock;
     private PowerManager.WakeLock mProximityWakeLock;
     private KeyguardManager mKeyguardManager;
-    private KeyguardManager.KeyguardLock mKeyguardLock;
-    private int mKeyguardDisableCount;
     private StatusBarManager mStatusBarManager;
     private int mStatusBarDisableCount;
 
@@ -396,7 +394,6 @@
             if (DBG) Log.d(LOG_TAG, "mProximityWakeLock: " + mProximityWakeLock);
 
             mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
-            mKeyguardLock = mKeyguardManager.newKeyguardLock(LOG_TAG);
             mStatusBarManager = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
 
             // get a handle to the service so that we can use it later when we
@@ -724,45 +721,6 @@
     }
 
     /**
-     * Disables the keyguard.  This is used by the phone app to allow
-     * interaction with the Phone UI when the keyguard would otherwise be
-     * active (like receiving an incoming call while the device is
-     * locked.)
-     *
-     * Any call to this method MUST be followed (eventually)
-     * by a corresponding reenableKeyguard() call.
-     */
-    /* package */ void disableKeyguard() {
-        if (DBG) Log.d(LOG_TAG, "disable keyguard");
-        // if (DBG) Log.d(LOG_TAG, "disableKeyguard()...", new Throwable("stack dump"));
-        synchronized (mKeyguardLock) {
-            if (mKeyguardDisableCount++ == 0) {
-                mKeyguardLock.disableKeyguard();
-            }
-        }
-    }
-
-    /**
-     * Re-enables the keyguard after a previous disableKeyguard() call.
-     *
-     * Any call to this method MUST correspond to (i.e. be balanced with)
-     * a previous disableKeyguard() call.
-     */
-    /* package */ void reenableKeyguard() {
-        if (DBG) Log.d(LOG_TAG, "re-enable keyguard");
-        // if (DBG) Log.d(LOG_TAG, "reenableKeyguard()...", new Throwable("stack dump"));
-        synchronized (mKeyguardLock) {
-            if (mKeyguardDisableCount > 0) {
-                if (--mKeyguardDisableCount == 0) {
-                    mKeyguardLock.reenableKeyguard();
-                }
-            } else {
-                Log.e(LOG_TAG, "mKeyguardDisableCount is already zero");
-            }
-        }
-    }
-
-    /**
      * Disables the status bar.  This is used by the phone app when in-call UI is active.
      *
      * Any call to this method MUST be followed (eventually)
@@ -1153,8 +1111,6 @@
                     if (!mProximityWakeLock.isHeld()) {
                         if (DBG) Log.d(LOG_TAG, "updateProximitySensorMode: acquiring...");
                         mProximityWakeLock.acquire();
-                        // disable keyguard while we are using the proximity sensor
-                        disableKeyguard();
                     } else {
                         if (VDBG) Log.d(LOG_TAG, "updateProximitySensorMode: lock already held.");
                     }
@@ -1169,7 +1125,6 @@
                         int flags =
                             (screenOnImmediately ? 0 : PowerManager.WAIT_FOR_PROXIMITY_NEGATIVE);
                         mProximityWakeLock.release(flags);
-                        reenableKeyguard();
                     } else {
                         if (VDBG) {
                             Log.d(LOG_TAG, "updateProximitySensorMode: lock already released.");