Merge "Accessibility in bad state after using SDK tool uiautomatorviewer." into lmp-dev
diff --git a/core/java/android/hardware/camera2/legacy/CameraDeviceUserShim.java b/core/java/android/hardware/camera2/legacy/CameraDeviceUserShim.java
index e19f587..fcf172c 100644
--- a/core/java/android/hardware/camera2/legacy/CameraDeviceUserShim.java
+++ b/core/java/android/hardware/camera2/legacy/CameraDeviceUserShim.java
@@ -336,8 +336,16 @@
         CameraInfo info = new CameraInfo();
         Camera.getCameraInfo(cameraId, info);
 
+        Camera.Parameters legacyParameters = null;
+        try {
+            legacyParameters = legacyCamera.getParameters();
+        } catch (RuntimeException e) {
+            throw new CameraRuntimeException(CameraAccessException.CAMERA_ERROR,
+                    "Unable to get initial parameters", e);
+        }
+
         CameraCharacteristics characteristics =
-                LegacyMetadataMapper.createCharacteristics(legacyCamera.getParameters(), info);
+                LegacyMetadataMapper.createCharacteristics(legacyParameters, info);
         LegacyCameraDevice device = new LegacyCameraDevice(
                 cameraId, legacyCamera, characteristics, threadCallbacks);
         return new CameraDeviceUserShim(cameraId, device, characteristics, init, threadCallbacks);
diff --git a/core/java/android/hardware/camera2/legacy/RequestThreadManager.java b/core/java/android/hardware/camera2/legacy/RequestThreadManager.java
index d27ca14..a7ea89c 100644
--- a/core/java/android/hardware/camera2/legacy/RequestThreadManager.java
+++ b/core/java/android/hardware/camera2/legacy/RequestThreadManager.java
@@ -328,6 +328,7 @@
         resetJpegSurfaceFormats(mCallbackOutputs);
         mPreviewOutputs.clear();
         mCallbackOutputs.clear();
+        mJpegSurfaceIds.clear();
         mPreviewTexture = null;
 
         int facing = mCharacteristics.get(CameraCharacteristics.LENS_FACING);
@@ -356,7 +357,14 @@
                 }
             }
         }
-        mParams = mCamera.getParameters();
+        try {
+            mParams = mCamera.getParameters();
+        } catch (RuntimeException e) {
+            Log.e(TAG, "Received device exception: ", e);
+            mDeviceState.setError(
+                CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE);
+            return;
+        }
 
         List<int[]> supportedFpsRanges = mParams.getSupportedPreviewFpsRange();
         int[] bestRange = getPhotoPreviewFpsRange(supportedFpsRanges);
@@ -780,7 +788,14 @@
                             if (DEBUG) {
                                 Log.d(TAG, "Params changed -- getting new Parameters from HAL.");
                             }
-                            mParams = mCamera.getParameters();
+                            try {
+                                mParams = mCamera.getParameters();
+                            } catch (RuntimeException e) {
+                                Log.e(TAG, "Received device exception: ", e);
+                                mDeviceState.setError(
+                                    CameraDeviceImpl.CameraDeviceCallbacks.ERROR_CAMERA_DEVICE);
+                                break;
+                            }
 
                             // Update parameters to the latest that we think the camera is using
                             mLastRequest.setParameters(mParams);
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index 440b1ec..ec4a53e 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -978,6 +978,19 @@
     public static final String EXTRA_INPUT_METHOD_ID = "input_method_id";
 
     /**
+     * Activity Extra: The device identifier to act upon.
+     * <p>
+     * This can be passed as an extra field in an Activity Intent with a single
+     * InputDeviceIdentifier. This field is used by some activities to jump straight into the
+     * settings for the given device.
+     * <p>
+     * Example: The {@link #INPUT_METHOD_SETTINGS} intent opens the keyboard layout dialog for the
+     * given device.
+     * @hide
+     */
+    public static final String EXTRA_INPUT_DEVICE_IDENTIFIER = "input_device_identifier";
+
+    /**
      * @hide
      * Activity Extra: Enable or disable Airplane Mode.
      * <p>
diff --git a/core/java/com/android/internal/widget/LockPatternUtils.java b/core/java/com/android/internal/widget/LockPatternUtils.java
index 1a2d6ce..85b58aa 100644
--- a/core/java/com/android/internal/widget/LockPatternUtils.java
+++ b/core/java/com/android/internal/widget/LockPatternUtils.java
@@ -780,9 +780,9 @@
      */
     public void saveLockPassword(String password, int quality, boolean isFallback, int userHandle) {
         try {
-            getLockSettings().setLockPassword(password, userHandle);
             DevicePolicyManager dpm = getDevicePolicyManager();
             if (!TextUtils.isEmpty(password)) {
+                getLockSettings().setLockPassword(password, userHandle);
                 int computedQuality = computePasswordQuality(password);
 
                 if (userHandle == UserHandle.USER_OWNER) {
@@ -860,6 +860,7 @@
                 setString(PASSWORD_HISTORY_KEY, passwordHistory, userHandle);
             } else {
                 // Empty password
+                getLockSettings().setLockPassword(null, userHandle);
                 if (userHandle == UserHandle.USER_OWNER) {
                     // Set the encryption password to default.
                     updateEncryptionPassword(StorageManager.CRYPT_TYPE_DEFAULT, null);
diff --git a/graphics/java/android/graphics/drawable/RippleBackground.java b/graphics/java/android/graphics/drawable/RippleBackground.java
index 4e709b5..faa89bf 100644
--- a/graphics/java/android/graphics/drawable/RippleBackground.java
+++ b/graphics/java/android/graphics/drawable/RippleBackground.java
@@ -106,7 +106,7 @@
 
     public void setup(int maxRadius, int color, float density) {
         mColorOpaque = color | 0xFF000000;
-        mColorAlpha = Color.alpha(color);
+        mColorAlpha = Color.alpha(color) / 2;
 
         if (maxRadius != RippleDrawable.RADIUS_AUTO) {
             mHasMaxRadius = true;
diff --git a/location/java/com/android/internal/location/GpsNetInitiatedHandler.java b/location/java/com/android/internal/location/GpsNetInitiatedHandler.java
index f0a2072..e9e475c 100644
--- a/location/java/com/android/internal/location/GpsNetInitiatedHandler.java
+++ b/location/java/com/android/internal/location/GpsNetInitiatedHandler.java
@@ -289,7 +289,7 @@
                         + " mPopupImmediately: " + mPopupImmediately
                         + " mInEmergency: " + getInEmergency());
 
-        if (getLocationEnabled() && !getInEmergency()) {
+        if (!getLocationEnabled() && !getInEmergency()) {
             // Location is currently disabled, ignore all NI requests.
             try {
                 mNetInitiatedListener.sendNiResponse(notif.notificationId,
diff --git a/policy/src/com/android/internal/policy/impl/PhoneWindow.java b/policy/src/com/android/internal/policy/impl/PhoneWindow.java
index 00026dc..42ee666 100644
--- a/policy/src/com/android/internal/policy/impl/PhoneWindow.java
+++ b/policy/src/com/android/internal/policy/impl/PhoneWindow.java
@@ -274,6 +274,8 @@
     private long mBackgroundFadeDurationMillis = -1;
     private Boolean mSharedElementsUseOverlay;
 
+    private Rect mTempRect;
+
     static class WindowManagerHolder {
         static final IWindowManager sWindowManager = IWindowManager.Stub.asInterface(
                 ServiceManager.getService("window"));
@@ -2892,37 +2894,53 @@
             // Show the status guard when the non-overlay contextual action bar is showing
             if (mActionModeView != null) {
                 if (mActionModeView.getLayoutParams() instanceof MarginLayoutParams) {
-                    MarginLayoutParams mlp = (MarginLayoutParams) mActionModeView.getLayoutParams();
+                    // Insets are magic!
+                    final MarginLayoutParams mlp = (MarginLayoutParams)
+                            mActionModeView.getLayoutParams();
                     boolean mlpChanged = false;
                     if (mActionModeView.isShown()) {
-                        final boolean nonOverlay = (getLocalFeatures()
-                                & (1 << FEATURE_ACTION_MODE_OVERLAY)) == 0;
-                        if (mlp.topMargin != insets.getSystemWindowInsetTop()) {
+                        if (mTempRect == null) {
+                            mTempRect = new Rect();
+                        }
+                        final Rect rect = mTempRect;
+
+                        // If the parent doesn't consume the insets, manually
+                        // apply the default system window insets.
+                        mContentParent.computeSystemWindowInsets(insets, rect);
+                        final int newMargin = rect.top == 0 ? insets.getSystemWindowInsetTop() : 0;
+                        if (mlp.topMargin != newMargin) {
                             mlpChanged = true;
                             mlp.topMargin = insets.getSystemWindowInsetTop();
 
-                            // Only show status guard for non-overlay modes.
-                            if (nonOverlay) {
-                                if (mStatusGuard == null) {
-                                    mStatusGuard = new View(mContext);
-                                    mStatusGuard.setBackgroundColor(mContext.getResources()
-                                            .getColor(R.color.input_method_navigation_guard));
-                                    addView(mStatusGuard, indexOfChild(mStatusColorView),
-                                            new LayoutParams(LayoutParams.MATCH_PARENT,
-                                                    mlp.topMargin,
-                                                    Gravity.START | Gravity.TOP));
-                                } else {
-                                    LayoutParams lp = (LayoutParams) mStatusGuard.getLayoutParams();
-                                    if (lp.height != mlp.topMargin) {
-                                        lp.height = mlp.topMargin;
-                                        mStatusGuard.setLayoutParams(lp);
-                                    }
+                            if (mStatusGuard == null) {
+                                mStatusGuard = new View(mContext);
+                                mStatusGuard.setBackgroundColor(mContext.getResources()
+                                        .getColor(R.color.input_method_navigation_guard));
+                                addView(mStatusGuard, indexOfChild(mStatusColorView),
+                                        new LayoutParams(LayoutParams.MATCH_PARENT,
+                                                mlp.topMargin, Gravity.START | Gravity.TOP));
+                            } else {
+                                final LayoutParams lp = (LayoutParams)
+                                        mStatusGuard.getLayoutParams();
+                                if (lp.height != mlp.topMargin) {
+                                    lp.height = mlp.topMargin;
+                                    mStatusGuard.setLayoutParams(lp);
                                 }
                             }
                         }
+
+                        // We only need to consume the insets if the action
+                        // mode is overlaid on the app content (e.g. it's
+                        // sitting in a FrameLayout, see
+                        // screen_simple_overlay_action_mode.xml).
+                        final boolean nonOverlay = (getLocalFeatures()
+                                & (1 << FEATURE_ACTION_MODE_OVERLAY)) == 0;
                         insets = insets.consumeSystemWindowInsets(
                                 false, nonOverlay /* top */, false, false);
-                        showStatusGuard = nonOverlay;
+
+                        // The action mode's theme may differ from the app, so
+                        // always show the status guard above it.
+                        showStatusGuard = true;
                     } else {
                         // reset top margin
                         if (mlp.topMargin != 0) {
diff --git a/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java b/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
index 008830d..b05d82b 100644
--- a/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
+++ b/services/accessibility/java/com/android/server/accessibility/AccessibilityManagerService.java
@@ -1169,7 +1169,8 @@
 
         // No enabled installed services => disable accessibility to avoid
         // sending accessibility events with no recipient across processes.
-        if (isEnabled && userState.mEnabledServices.isEmpty()) {
+        if (isEnabled && userState.mBoundServices.isEmpty()
+                && userState.mBindingServices.isEmpty()) {
             userState.mIsAccessibilityEnabled = false;
             Settings.Secure.putIntForUser(mContext.getContentResolver(),
                     Settings.Secure.ACCESSIBILITY_ENABLED, 0, userState.mUserId);
diff --git a/services/core/java/com/android/server/input/InputManagerService.java b/services/core/java/com/android/server/input/InputManagerService.java
index 7f89947..15dcd44 100644
--- a/services/core/java/com/android/server/input/InputManagerService.java
+++ b/services/core/java/com/android/server/input/InputManagerService.java
@@ -695,6 +695,8 @@
             final int numFullKeyboards = mTempFullKeyboards.size();
             boolean missingLayoutForExternalKeyboard = false;
             boolean missingLayoutForExternalKeyboardAdded = false;
+            boolean multipleMissingLayoutsForExternalKeyboardsAdded = false;
+            InputDevice keyboardMissingLayout = null;
             synchronized (mDataStore) {
                 for (int i = 0; i < numFullKeyboards; i++) {
                     final InputDevice inputDevice = mTempFullKeyboards.get(i);
@@ -704,13 +706,25 @@
                         missingLayoutForExternalKeyboard = true;
                         if (i < numFullKeyboardsAdded) {
                             missingLayoutForExternalKeyboardAdded = true;
+                            if (keyboardMissingLayout == null) {
+                                keyboardMissingLayout = inputDevice;
+                            } else {
+                                multipleMissingLayoutsForExternalKeyboardsAdded = true;
+                            }
                         }
                     }
                 }
             }
             if (missingLayoutForExternalKeyboard) {
                 if (missingLayoutForExternalKeyboardAdded) {
-                    showMissingKeyboardLayoutNotification();
+                    if (multipleMissingLayoutsForExternalKeyboardsAdded) {
+                        // We have more than one keyboard missing a layout, so drop the
+                        // user at the generic input methods page so they can pick which
+                        // one to set.
+                        showMissingKeyboardLayoutNotification(null);
+                    } else {
+                        showMissingKeyboardLayoutNotification(keyboardMissingLayout);
+                    }
                 }
             } else if (mKeyboardLayoutNotificationShown) {
                 hideMissingKeyboardLayoutNotification();
@@ -761,16 +775,17 @@
     }
 
     // Must be called on handler.
-    private void showMissingKeyboardLayoutNotification() {
+    private void showMissingKeyboardLayoutNotification(InputDevice device) {
         if (!mKeyboardLayoutNotificationShown) {
-            if (mKeyboardLayoutIntent == null) {
-                final Intent intent = new Intent("android.settings.INPUT_METHOD_SETTINGS");
-                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
-                        | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
-                        | Intent.FLAG_ACTIVITY_CLEAR_TOP);
-                mKeyboardLayoutIntent = PendingIntent.getActivityAsUser(mContext, 0,
-                        intent, 0, null, UserHandle.CURRENT);
+            final Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
+            if (device != null) {
+                intent.putExtra(Settings.EXTRA_INPUT_DEVICE_IDENTIFIER, device.getIdentifier());
             }
+            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
+                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
+                    | Intent.FLAG_ACTIVITY_CLEAR_TOP);
+            final PendingIntent keyboardLayoutIntent = PendingIntent.getActivityAsUser(mContext, 0,
+                    intent, 0, null, UserHandle.CURRENT);
 
             Resources r = mContext.getResources();
             Notification notification = new Notification.Builder(mContext)
@@ -778,7 +793,7 @@
                             R.string.select_keyboard_layout_notification_title))
                     .setContentText(r.getString(
                             R.string.select_keyboard_layout_notification_message))
-                    .setContentIntent(mKeyboardLayoutIntent)
+                    .setContentIntent(keyboardLayoutIntent)
                     .setSmallIcon(R.drawable.ic_settings_language)
                     .setPriority(Notification.PRIORITY_LOW)
                     .setColor(mContext.getResources().getColor(