Merge "Fix permissions on WindowManagerService.showAssistant()" into klp-dev
diff --git a/core/java/android/view/IWindowManager.aidl b/core/java/android/view/IWindowManager.aidl
index aea2799c..caf9c8b 100644
--- a/core/java/android/view/IWindowManager.aidl
+++ b/core/java/android/view/IWindowManager.aidl
@@ -249,12 +249,6 @@
     boolean isSafeModeEnabled();
 
     /**
-     * Tell keyguard to show the assistant (Intent.ACTION_ASSIST) after asking for the user's
-     * credentials.
-     */
-    void showAssistant();
-
-    /**
      * Sets the display magnification callbacks. These callbacks notify
      * the client for contextual changes related to display magnification.
      *
diff --git a/core/java/android/view/WindowManagerPolicy.java b/core/java/android/view/WindowManagerPolicy.java
index e116662..79c0b3c 100644
--- a/core/java/android/view/WindowManagerPolicy.java
+++ b/core/java/android/view/WindowManagerPolicy.java
@@ -1176,12 +1176,6 @@
     public void dump(String prefix, PrintWriter writer, String[] args);
 
     /**
-     * Ask keyguard to invoke the assist intent after dismissing keyguard
-     * {@link android.content.Intent#ACTION_ASSIST}
-     */
-    public void showAssistant();
-
-    /**
      * Returns whether a given window type can be magnified.
      *
      * @param windowType The window type.
diff --git a/packages/SystemUI/src/com/android/systemui/SearchPanelView.java b/packages/SystemUI/src/com/android/systemui/SearchPanelView.java
index c32f741..c7f0e17 100644
--- a/packages/SystemUI/src/com/android/systemui/SearchPanelView.java
+++ b/packages/SystemUI/src/com/android/systemui/SearchPanelView.java
@@ -46,6 +46,7 @@
 import com.android.systemui.statusbar.BaseStatusBar;
 import com.android.systemui.statusbar.CommandQueue;
 import com.android.systemui.statusbar.StatusBarPanel;
+import com.android.systemui.statusbar.phone.KeyguardTouchDelegate;
 import com.android.systemui.statusbar.phone.PhoneStatusBar;
 
 public class SearchPanelView extends FrameLayout implements
@@ -88,11 +89,7 @@
 
         if (isKeyguardShowing) {
             // Have keyguard show the bouncer and launch the activity if the user succeeds.
-            try {
-                mWm.showAssistant();
-            } catch (RemoteException e) {
-                // too bad, so sad...
-            }
+            KeyguardTouchDelegate.getInstance(getContext()).showAssistant();
             onAnimationStarted();
         } else {
             // Otherwise, keyguard isn't showing so launch it from here.
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardTouchDelegate.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardTouchDelegate.java
index 1221a55..5c55f0d 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardTouchDelegate.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardTouchDelegate.java
@@ -23,7 +23,7 @@
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.os.UserHandle;
-import android.util.Log;
+import android.util.Slog;
 import android.view.MotionEvent;
 
 import com.android.internal.policy.IKeyguardExitCallback;
@@ -41,7 +41,9 @@
     static final String KEYGUARD_PACKAGE = "com.android.keyguard";
     static final String KEYGUARD_CLASS = "com.android.keyguard.KeyguardService";
 
-    IKeyguardService mService;
+    private static KeyguardTouchDelegate sInstance;
+
+    private volatile IKeyguardService mService;
 
     protected static final boolean DEBUG = false;
     protected static final String TAG = "KeyguardTouchDelegate";
@@ -49,83 +51,121 @@
     private final ServiceConnection mKeyguardConnection = new ServiceConnection() {
         @Override
         public void onServiceConnected(ComponentName name, IBinder service) {
-            Log.v(TAG, "Connected to keyguard");
+            Slog.v(TAG, "Connected to keyguard");
             mService = IKeyguardService.Stub.asInterface(service);
 
         }
 
         @Override
         public void onServiceDisconnected(ComponentName name) {
-            Log.v(TAG, "Disconnected from keyguard");
+            Slog.v(TAG, "Disconnected from keyguard");
             mService = null;
+            sInstance = null; // force reconnection if this goes away
         }
 
     };
 
-    public KeyguardTouchDelegate(Context context) {
+    private KeyguardTouchDelegate(Context context) {
         Intent intent = new Intent();
         intent.setClassName(KEYGUARD_PACKAGE, KEYGUARD_CLASS);
         if (!context.bindServiceAsUser(intent, mKeyguardConnection,
                 Context.BIND_AUTO_CREATE, UserHandle.OWNER)) {
-            if (DEBUG) Log.v(TAG, "*** Keyguard: can't bind to " + KEYGUARD_CLASS);
+            if (DEBUG) Slog.v(TAG, "*** Keyguard: can't bind to " + KEYGUARD_CLASS);
         } else {
-            if (DEBUG) Log.v(TAG, "*** Keyguard started");
+            if (DEBUG) Slog.v(TAG, "*** Keyguard started");
         }
     }
 
+    public static KeyguardTouchDelegate getInstance(Context context) {
+        if (sInstance == null) {
+            sInstance = new KeyguardTouchDelegate(context);
+        }
+        return sInstance;
+    }
+
     public boolean isSecure() {
-        boolean secure = false;
-        if (mService != null) {
+        final IKeyguardService service = mService;
+        if (service != null) {
             try {
-                secure = mService.isSecure();
+                return service.isSecure();
             } catch (RemoteException e) {
-                Log.e(TAG, "RemoteException calling keyguard.isSecure()!", e);
+                Slog.e(TAG, "RemoteException calling keyguard.isSecure()!", e);
             }
         } else {
-            Log.w(TAG, "isSecure(): NO SERVICE!");
+            Slog.w(TAG, "isSecure(): NO SERVICE!");
         }
-        return secure;
+        return false;
     }
 
     public boolean dispatch(MotionEvent event) {
-        if (mService != null) {
+        final IKeyguardService service = mService;
+        if (service != null) {
             try {
-                mService.dispatch(event);
+                service.dispatch(event);
+                return true;
             } catch (RemoteException e) {
                 // What to do?
-                Log.e(TAG, "RemoteException sending event to keyguard!", e);
-                return false;
+                Slog.e(TAG, "RemoteException sending event to keyguard!", e);
             }
-            return true;
         } else {
-            Log.w(TAG, "dispatch(event): NO SERVICE!");
+            Slog.w(TAG, "dispatch(event): NO SERVICE!");
+        }
+        return false;
+    }
+
+    public boolean isInputRestricted() {
+        final IKeyguardService service = mService;
+        if (service != null) {
+            try {
+                return service.isInputRestricted();
+            } catch (RemoteException e) {
+                Slog.w(TAG , "Remote Exception", e);
+            }
+        } else {
+            Slog.w(TAG, "isInputRestricted(): NO SERVICE!");
+        }
+        return false;
+    }
+
+    public boolean isShowingAndNotHidden() {
+        final IKeyguardService service = mService;
+        if (service != null) {
+            try {
+                return service.isShowingAndNotHidden();
+            } catch (RemoteException e) {
+                Slog.w(TAG , "Remote Exception", e);
+            }
+        } else {
+            Slog.w(TAG, "isShowingAndNotHidden(): NO SERVICE!");
         }
         return false;
     }
 
     public void showAssistant() {
-        if (mService != null) {
+        final IKeyguardService service = mService;
+        if (service != null) {
             try {
-                mService.showAssistant();
+                service.showAssistant();
             } catch (RemoteException e) {
                 // What to do?
-                Log.e(TAG, "RemoteException launching assistant!", e);
+                Slog.e(TAG, "RemoteException launching assistant!", e);
             }
         } else {
-            Log.w(TAG, "dispatch(event): NO SERVICE!");
+            Slog.w(TAG, "showAssistant(event): NO SERVICE!");
         }
     }
 
     public void launchCamera() {
-        if (mService != null) {
+        final IKeyguardService service = mService;
+        if (service != null) {
             try {
-                mService.launchCamera();
+                service.launchCamera();
             } catch (RemoteException e) {
                 // What to do?
-                Log.e(TAG, "RemoteException launching camera!", e);
+                Slog.e(TAG, "RemoteException launching camera!", e);
             }
         } else {
-            Log.w(TAG, "dispatch(event): NO SERVICE!");
+            Slog.w(TAG, "dispatch(event): NO SERVICE!");
         }
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
index 596fac6..04885f0 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarView.java
@@ -88,7 +88,6 @@
 
     // used to disable the camera icon in navbar when disabled by DPM
     private boolean mCameraDisabledByDpm;
-    KeyguardTouchDelegate mKeyguardTouchDelegate;
 
     private final OnTouchListener mCameraTouchListener = new OnTouchListener() {
         @Override
@@ -112,7 +111,7 @@
                     }
                     break;
             }
-            return mKeyguardTouchDelegate.dispatch(event);
+            return KeyguardTouchDelegate.getInstance(getContext()).dispatch(event);
         }
     };
 
@@ -155,8 +154,6 @@
 
         mBarTransitions = new NavigationBarTransitions(this);
 
-        mKeyguardTouchDelegate = new KeyguardTouchDelegate(mContext);
-
         mCameraDisabledByDpm = isCameraDisabledByDpm();
         watchForDevicePolicyChanges();
     }
@@ -341,7 +338,7 @@
                 final int disabledFlags = dpm.getKeyguardDisabledFeatures(null, userId);
                 final  boolean disabledBecauseKeyguardSecure =
                         (disabledFlags & DevicePolicyManager.KEYGUARD_DISABLE_SECURE_CAMERA) != 0
-                        && mKeyguardTouchDelegate.isSecure();
+                        && KeyguardTouchDelegate.getInstance(getContext()).isSecure();
                 return dpm.getCameraDisabled(null) || disabledBecauseKeyguardSecure;
             } catch (RemoteException e) {
                 Log.e(TAG, "Can't get userId", e);
@@ -426,9 +423,9 @@
 
     protected void launchForAccessibilityClick(View v) {
         if (v == getCameraButton()) {
-            mKeyguardTouchDelegate.launchCamera();
+            KeyguardTouchDelegate.getInstance(getContext()).launchCamera();
         } else if (v == getSearchLight()) {
-            mKeyguardTouchDelegate.showAssistant();
+            KeyguardTouchDelegate.getInstance(getContext()).showAssistant();
         }
     }
 
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
index 9f9b6d6..a5fd1d7 100644
--- a/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
@@ -5127,11 +5127,6 @@
     }
 
     @Override
-    public void showAssistant() {
-        mKeyguardDelegate.showAssistant();
-    }
-
-    @Override
     public boolean canMagnifyWindow(int windowType) {
         switch (windowType) {
             case WindowManager.LayoutParams.TYPE_INPUT_METHOD:
diff --git a/policy/src/com/android/internal/policy/impl/keyguard/KeyguardServiceWrapper.java b/policy/src/com/android/internal/policy/impl/keyguard/KeyguardServiceWrapper.java
index 5e299ee..83be1a8 100644
--- a/policy/src/com/android/internal/policy/impl/keyguard/KeyguardServiceWrapper.java
+++ b/policy/src/com/android/internal/policy/impl/keyguard/KeyguardServiceWrapper.java
@@ -181,11 +181,7 @@
     }
 
     public void showAssistant() {
-        try {
-            mService.showAssistant();
-        } catch (RemoteException e) {
-            Slog.w(TAG , "Remote Exception", e);
-        }
+        // Not used by PhoneWindowManager
     }
 
     public void dispatch(MotionEvent event) {
diff --git a/services/java/com/android/server/wm/WindowManagerService.java b/services/java/com/android/server/wm/WindowManagerService.java
index e330f8b..680b44e 100644
--- a/services/java/com/android/server/wm/WindowManagerService.java
+++ b/services/java/com/android/server/wm/WindowManagerService.java
@@ -10142,16 +10142,6 @@
         return mSafeMode;
     }
 
-    @Override
-    public void showAssistant() {
-        // TODO: What permission?
-        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DEVICE_POWER)
-                != PackageManager.PERMISSION_GRANTED) {
-            return;
-        }
-        mPolicy.showAssistant();
-    }
-
     void dumpPolicyLocked(PrintWriter pw, String[] args, boolean dumpAll) {
         pw.println("WINDOW MANAGER POLICY STATE (dumpsys window policy)");
         mPolicy.dump("    ", pw, args);
diff --git a/tools/layoutlib/bridge/src/android/view/IWindowManagerImpl.java b/tools/layoutlib/bridge/src/android/view/IWindowManagerImpl.java
index f0c3a75..fd7a645 100644
--- a/tools/layoutlib/bridge/src/android/view/IWindowManagerImpl.java
+++ b/tools/layoutlib/bridge/src/android/view/IWindowManagerImpl.java
@@ -458,11 +458,6 @@
     }
 
     @Override
-    public void showAssistant() {
-
-    }
-
-    @Override
     public IBinder getFocusedWindowToken() {
         // TODO Auto-generated method stub
         return null;