Merge "Add string for output switcher transferring error" into rvc-dev
diff --git a/res/layout/accessibility_shortcut_secondary_action.xml b/res/layout/accessibility_shortcut_secondary_action.xml
index 84c8e6b..a114f05 100644
--- a/res/layout/accessibility_shortcut_secondary_action.xml
+++ b/res/layout/accessibility_shortcut_secondary_action.xml
@@ -29,28 +29,23 @@
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:gravity="start|center_vertical"
-        android:focusable="true"
-        android:clickable="true"
-        android:background="?android:attr/selectableItemBackground">
+        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
+        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">
 
         <FrameLayout
+            android:id="@+id/icon_frame"
             android:layout_width="wrap_content"
-            android:layout_height="match_parent"
-            android:layout_marginEnd="16dp"
+            android:layout_height="wrap_content"
             android:minWidth="56dp"
-            android:orientation="horizontal"
-            android:focusable="false"
-            android:clickable="false">
+            android:paddingEnd="12dp"
+            android:paddingTop="16dp"
+            android:paddingBottom="4dp">
 
-            <CheckBox
-                android:id="@+id/checkbox"
+            <ImageView
+                android:id="@android:id/icon"
                 android:layout_width="wrap_content"
-                android:layout_height="wrap_content"
-                android:layout_gravity="center"
-                android:focusable="false"
-                android:clickable="false"
-                android:background="@null"/>
-
+                android:layout_height="wrap_content" />
+            
         </FrameLayout>
 
         <RelativeLayout
@@ -94,9 +89,8 @@
         android:id="@android:id/widget_frame"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
-        android:contentDescription="@string/accessibility_shortcut_settings"
         android:gravity="end|center_vertical"
         android:minWidth="58dp"
         android:orientation="vertical" />
 
-</LinearLayout>
\ No newline at end of file
+</LinearLayout>
diff --git a/res/layout/preference_widget_settings.xml b/res/layout/preference_widget_settings.xml
deleted file mode 100644
index 73c6875..0000000
--- a/res/layout/preference_widget_settings.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-  Copyright (C) 2019 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
-  -->
-
-<!-- Layout used by ShortcutPreference. This is inflated inside android.R.layout.preference. -->
-<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
-    android:id="@+id/settings_button"
-    android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:layout_gravity="center"
-    android:background="?android:attr/selectableItemBackground"
-    android:scaleType="center"
-    android:src="@drawable/ic_mode_edit"
-    android:contentDescription="@string/settings_button" />
diff --git a/src/com/android/settings/accessibility/InvisibleToggleAccessibilityServicePreferenceFragment.java b/src/com/android/settings/accessibility/InvisibleToggleAccessibilityServicePreferenceFragment.java
index 93064c5..beeabd5 100644
--- a/src/com/android/settings/accessibility/InvisibleToggleAccessibilityServicePreferenceFragment.java
+++ b/src/com/android/settings/accessibility/InvisibleToggleAccessibilityServicePreferenceFragment.java
@@ -32,7 +32,7 @@
  * {@link AccessibilityServiceInfo#FLAG_REQUEST_ACCESSIBILITY_BUTTON} is set.
  */
 public class InvisibleToggleAccessibilityServicePreferenceFragment extends
-        ToggleAccessibilityServicePreferenceFragment implements ShortcutPreference.OnClickListener{
+        ToggleAccessibilityServicePreferenceFragment implements ShortcutPreference.OnClickCallback {
 
     @Override
     protected void onInstallSwitchPreferenceToggleSwitch() {
@@ -47,10 +47,10 @@
      * accessibility service when shortcutPreference is unchecked.
      */
     @Override
-    public void onCheckboxClicked(ShortcutPreference preference) {
-        super.onCheckboxClicked(preference);
+    public void onToggleClicked(ShortcutPreference preference) {
+        super.onToggleClicked(preference);
         boolean enabled = getArguments().getBoolean(AccessibilitySettings.EXTRA_CHECKED)
-                && preference.getChecked();
+                && preference.isChecked();
 
         AccessibilityUtils.setAccessibilityServiceState(getContext(), mComponentName, enabled);
     }
@@ -61,8 +61,8 @@
      * Enables accessibility service when user clicks permission allow button.
      */
     @Override
-    void onDialogButtonFromShortcutClicked(View view) {
-        super.onDialogButtonFromShortcutClicked(view);
+    void onDialogButtonFromShortcutToggleClicked(View view) {
+        super.onDialogButtonFromShortcutToggleClicked(view);
         if (view.getId() == R.id.permission_enable_allow_button) {
             AccessibilityUtils.setAccessibilityServiceState(getContext(), mComponentName,
                     true);
diff --git a/src/com/android/settings/accessibility/LegacyAccessibilityServicePreferenceFragment.java b/src/com/android/settings/accessibility/LegacyAccessibilityServicePreferenceFragment.java
index 2739b6b..1803874 100644
--- a/src/com/android/settings/accessibility/LegacyAccessibilityServicePreferenceFragment.java
+++ b/src/com/android/settings/accessibility/LegacyAccessibilityServicePreferenceFragment.java
@@ -43,7 +43,7 @@
         final CharSequence hardwareTitle = getPrefContext().getText(
                 R.string.accessibility_shortcut_edit_dialog_title_hardware);
         mShortcutPreference.setSummary(hardwareTitle);
-        mShortcutPreference.setSettingsVisibility(View.GONE);
+        mShortcutPreference.setSettingsEditable(false);
 
         setAllowedPreferredShortcutType(UserShortcutType.HARDWARE);
     }
diff --git a/src/com/android/settings/accessibility/ShortcutPreference.java b/src/com/android/settings/accessibility/ShortcutPreference.java
index 00b63b6..82ab506 100644
--- a/src/com/android/settings/accessibility/ShortcutPreference.java
+++ b/src/com/android/settings/accessibility/ShortcutPreference.java
@@ -18,9 +18,11 @@
 
 import android.content.Context;
 import android.util.AttributeSet;
+import android.util.TypedValue;
+import android.view.MotionEvent;
 import android.view.View;
-import android.widget.CheckBox;
 import android.widget.LinearLayout;
+import android.widget.Switch;
 
 import androidx.preference.Preference;
 import androidx.preference.PreferenceViewHolder;
@@ -34,77 +36,84 @@
 public class ShortcutPreference extends Preference {
 
     /**
-     * Interface definition for a callback to be invoked when the checkbox or settings has been
+     * Interface definition for a callback to be invoked when the toggle or settings has been
      * clicked.
      */
-    public interface OnClickListener {
-        /**
-         * Called when the checkbox in ShortcutPreference has been clicked.
-         *
-         * @param preference The clicked preference
-         */
-        void onCheckboxClicked(ShortcutPreference preference);
+    public interface OnClickCallback {
         /**
          * Called when the settings view has been clicked.
          *
          * @param preference The clicked preference
          */
         void onSettingsClicked(ShortcutPreference preference);
-    }
-    private OnClickListener mListener = null;
 
-    private static final float DISABLED_ALPHA = 0.77f;
-    private static final float ENABLED_ALPHA = 1.0f;
-    private int mSettingsVisibility = View.VISIBLE;
-    private boolean mAutoEnabledSettings;
+        /**
+         * Called when the toggle in ShortcutPreference has been clicked.
+         *
+         * @param preference The clicked preference
+         */
+        void onToggleClicked(ShortcutPreference preference);
+    }
+
+    private OnClickCallback mClickCallback = null;
     private boolean mChecked = false;
+    private boolean mSettingsEditable = true;
 
     ShortcutPreference(Context context, AttributeSet attrs) {
         super(context, attrs);
-        init();
+        setLayoutResource(R.layout.accessibility_shortcut_secondary_action);
+        setWidgetLayoutResource(R.layout.preference_widget_master_switch);
+        setIconSpaceReserved(true);
+        setSelectable(false);
     }
 
     @Override
     public void onBindViewHolder(PreferenceViewHolder holder) {
         super.onBindViewHolder(holder);
 
+        final TypedValue outValue = new TypedValue();
+        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
+                outValue, true);
+
         final LinearLayout mainFrame = holder.itemView.findViewById(R.id.main_frame);
         if (mainFrame != null) {
-            mainFrame.setOnClickListener(view -> callOnCheckboxClicked());
+            mainFrame.setOnClickListener(view -> callOnSettingsClicked());
+            mainFrame.setClickable(mSettingsEditable);
+            mainFrame.setFocusable(mSettingsEditable);
+            mainFrame.setBackgroundResource(
+                    mSettingsEditable ? outValue.resourceId : /* Remove background */ 0);
         }
 
-        final CheckBox checkBox = holder.itemView.findViewById(R.id.checkbox);
-        if (checkBox != null) {
-            checkBox.setChecked(mChecked);
-        }
-
-
-        final View settings = holder.itemView.findViewById(android.R.id.widget_frame);
-        if (settings != null) {
-            settings.setOnClickListener(view -> callOnSettingsClicked());
-            settings.setEnabled(mAutoEnabledSettings ? mChecked : /* enabled */ true);
-
-            float alpha;
-            if (mAutoEnabledSettings) {
-                alpha = mChecked ? ENABLED_ALPHA : DISABLED_ALPHA;
-            } else {
-                alpha = ENABLED_ALPHA;
-            }
-
-            settings.setAlpha(alpha);
-            settings.setVisibility(mSettingsVisibility);
+        Switch switchWidget = holder.itemView.findViewById(R.id.switchWidget);
+        if (switchWidget != null) {
+            // Consumes move events to ignore drag actions.
+            switchWidget.setOnTouchListener((v, event) -> {
+                return event.getActionMasked() == MotionEvent.ACTION_MOVE;
+            });
+            switchWidget.setContentDescription(
+                    getContext().getText(R.string.accessibility_shortcut_settings));
+            switchWidget.setChecked(mChecked);
+            switchWidget.setOnClickListener(view -> callOnToggleClicked());
+            switchWidget.setClickable(mSettingsEditable);
+            switchWidget.setFocusable(mSettingsEditable);
+            switchWidget.setBackgroundResource(
+                    mSettingsEditable ? outValue.resourceId : /* Remove background */ 0);
         }
 
         final View divider = holder.itemView.findViewById(R.id.divider);
         if (divider != null) {
-            divider.setVisibility(mSettingsVisibility);
+            divider.setVisibility(mSettingsEditable ? View.VISIBLE : View.GONE);
         }
+
+        holder.itemView.setOnClickListener(view -> callOnToggleClicked());
+        holder.itemView.setClickable(!mSettingsEditable);
+        holder.itemView.setFocusable(!mSettingsEditable);
     }
 
     /**
-     * Sets the shortcut checkbox according to checked value.
+     * Sets the shortcut toggle according to checked value.
      *
-     * @param checked the state value of shortcut checkbox
+     * @param checked the state value of shortcut toggle
      */
     public void setChecked(boolean checked) {
         if (mChecked != checked) {
@@ -114,72 +123,50 @@
     }
 
     /**
-     * Gets the checked value of shortcut checkbox.
+     * Gets the checked value of shortcut toggle.
      *
-     * @return the checked value of shortcut checkbox
+     * @return the checked value of shortcut toggle
      */
-    public boolean getChecked() {
+    public boolean isChecked() {
         return mChecked;
     }
 
     /**
-     * Automatically/Manually enable settings according to checkbox click status.
-     *
-     * Automatically enable settings means settings view enabled when checkbox is clicked, and
-     * disabled when checkbox is not clicked.
-     * Manually enable settings means settings view always enabled.
-     *
-     * @param autoEnabled True will automatically enable settings, false will let settings view
-     *                    always enabled.
+     * Sets the editable state of Settings view. If the view cannot edited, it makes the settings
+     * and toggle be not touchable. The main ui handles touch event directly by {@link #onClick}.
      */
-    public void setAutoEnabledSettings(boolean autoEnabled) {
-        if (mAutoEnabledSettings != autoEnabled) {
-            mAutoEnabledSettings = autoEnabled;
+    public void setSettingsEditable(boolean enabled) {
+        if (mSettingsEditable != enabled) {
+            mSettingsEditable = enabled;
+            // Disable whole component to let each child component can be addressed.
+            setSelectable(!mSettingsEditable);
             notifyChanged();
         }
     }
 
-    /**
-     * Sets the visibility state of Settings view.
-     *
-     * @param visibility one of {@link View#VISIBLE}, {@link View#INVISIBLE}, or {@link View#GONE}.
-     */
-    public void setSettingsVisibility(@View.Visibility int visibility) {
-        if (mSettingsVisibility != visibility) {
-            mSettingsVisibility = visibility;
-            notifyChanged();
-        }
+    public boolean isSettingsEditable() {
+        return mSettingsEditable;
     }
 
     /**
      * Sets the callback to be invoked when this preference is clicked by the user.
      *
-     * @param listener the callback to be invoked
+     * @param callback the callback to be invoked
      */
-    public void setOnClickListener(OnClickListener listener) {
-        mListener = listener;
-    }
-
-    private void init() {
-        setLayoutResource(R.layout.accessibility_shortcut_secondary_action);
-        setWidgetLayoutResource(R.layout.preference_widget_settings);
-        setIconSpaceReserved(false);
-        mAutoEnabledSettings = true;
-
-        // Disable whole component to let each child component can be addressed.
-        setSelectable(false);
+    public void setOnClickCallback(OnClickCallback callback) {
+        mClickCallback = callback;
     }
 
     private void callOnSettingsClicked() {
-        if (mListener != null) {
-            mListener.onSettingsClicked(this);
+        if (mClickCallback != null) {
+            mClickCallback.onSettingsClicked(this);
         }
     }
 
-    private void callOnCheckboxClicked() {
+    private void callOnToggleClicked() {
         setChecked(!mChecked);
-        if (mListener != null) {
-            mListener.onCheckboxClicked(this);
+        if (mClickCallback != null) {
+            mClickCallback.onToggleClicked(this);
         }
     }
 }
diff --git a/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java b/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java
index df97607..d61be67 100644
--- a/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java
@@ -134,6 +134,16 @@
                                 this::onDialogButtonFromEnableToggleClicked);
                 break;
             }
+            case DialogEnums.ENABLE_WARNING_FROM_SHORTCUT_TOGGLE: {
+                final AccessibilityServiceInfo info = getAccessibilityServiceInfo();
+                if (info == null) {
+                    return null;
+                }
+                mDialog = AccessibilityServiceWarning
+                        .createCapabilitiesDialog(getPrefContext(), info,
+                                this::onDialogButtonFromShortcutToggleClicked);
+                break;
+            }
             case DialogEnums.ENABLE_WARNING_FROM_SHORTCUT: {
                 final AccessibilityServiceInfo info = getAccessibilityServiceInfo();
                 if (info == null) {
@@ -176,6 +186,7 @@
         switch (dialogId) {
             case DialogEnums.ENABLE_WARNING_FROM_TOGGLE:
             case DialogEnums.ENABLE_WARNING_FROM_SHORTCUT:
+            case DialogEnums.ENABLE_WARNING_FROM_SHORTCUT_TOGGLE:
                 return SettingsEnums.DIALOG_ACCESSIBILITY_SERVICE_ENABLE;
             case DialogEnums.DISABLE_WARNING_FROM_TOGGLE:
                 return SettingsEnums.DIALOG_ACCESSIBILITY_SERVICE_DISABLE;
@@ -283,12 +294,12 @@
     }
 
     @Override
-    public void onCheckboxClicked(ShortcutPreference preference) {
+    public void onToggleClicked(ShortcutPreference preference) {
         final int shortcutTypes = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
-        if (preference.getChecked()) {
-            if (!getArguments().getBoolean(AccessibilitySettings.EXTRA_CHECKED)) {
+        if (preference.isChecked()) {
+            if (!mToggleServiceDividerSwitchPreference.isChecked()) {
                 preference.setChecked(false);
-                showPopupDialog(DialogEnums.ENABLE_WARNING_FROM_SHORTCUT);
+                showPopupDialog(DialogEnums.ENABLE_WARNING_FROM_SHORTCUT_TOGGLE);
             } else {
                 AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), shortcutTypes,
                         mComponentName);
@@ -296,14 +307,17 @@
         } else {
             AccessibilityUtil.optOutAllValuesFromSettings(getPrefContext(), shortcutTypes,
                     mComponentName);
-            getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED, false);
         }
+        mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
     }
 
     @Override
     public void onSettingsClicked(ShortcutPreference preference) {
         super.onSettingsClicked(preference);
-        showPopupDialog(DialogEnums.EDIT_SHORTCUT);
+        final boolean isServiceOnOrShortcutAdded = mShortcutPreference.isChecked()
+                || mToggleServiceDividerSwitchPreference.isChecked();
+        showPopupDialog(isServiceOnOrShortcutAdded ? DialogEnums.EDIT_SHORTCUT
+                : DialogEnums.ENABLE_WARNING_FROM_SHORTCUT);
     }
 
     @Override
@@ -385,6 +399,32 @@
         mDialog.dismiss();
     }
 
+    void onDialogButtonFromShortcutToggleClicked(View view) {
+        final int viewId = view.getId();
+        if (viewId == R.id.permission_enable_allow_button) {
+            onAllowButtonFromShortcutToggleClicked();
+        } else if (viewId == R.id.permission_enable_deny_button) {
+            onDenyButtonFromShortcutToggleClicked();
+        } else {
+            throw new IllegalArgumentException("Unexpected view id");
+        }
+    }
+
+    private void onAllowButtonFromShortcutToggleClicked() {
+        mShortcutPreference.setChecked(true);
+
+        final int shortcutTypes = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
+        AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), shortcutTypes, mComponentName);
+
+        mDialog.dismiss();
+    }
+
+    private void onDenyButtonFromShortcutToggleClicked() {
+        mShortcutPreference.setChecked(false);
+
+        mDialog.dismiss();
+    }
+
     void onDialogButtonFromShortcutClicked(View view) {
         final int viewId = view.getId();
         if (viewId == R.id.permission_enable_allow_button) {
@@ -397,17 +437,13 @@
     }
 
     private void onAllowButtonFromShortcutClicked() {
-        mShortcutPreference.setChecked(true);
-
-        final int shortcutTypes = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
-        AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), shortcutTypes, mComponentName);
+        mIsDialogShown.set(false);
+        showPopupDialog(DialogEnums.EDIT_SHORTCUT);
 
         mDialog.dismiss();
     }
 
     private void onDenyButtonFromShortcutClicked() {
-        mShortcutPreference.setChecked(false);
-
         mDialog.dismiss();
     }
 
@@ -417,7 +453,7 @@
             mToggleServiceDividerSwitchPreference.setChecked(false);
             getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED,
                     /* disableService */ false);
-            if (!mShortcutPreference.getChecked()) {
+            if (!mShortcutPreference.isChecked()) {
                 showPopupDialog(DialogEnums.ENABLE_WARNING_FROM_TOGGLE);
             } else {
                 handleConfirmServiceEnabled(/* confirmed= */ true);
diff --git a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
index 838a758..0b8353e 100644
--- a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
@@ -69,7 +69,7 @@
  * and dialog management.
  */
 public abstract class ToggleFeaturePreferenceFragment extends SettingsPreferenceFragment
-        implements ShortcutPreference.OnClickListener {
+        implements ShortcutPreference.OnClickCallback {
 
     protected DividerSwitchPreference mToggleServiceDividerSwitchPreference;
     protected ShortcutPreference mShortcutPreference;
@@ -277,33 +277,38 @@
          */
         int ENABLE_WARNING_FROM_TOGGLE = 1002;
 
-
         /** OPEN: Settings > Accessibility > Downloaded toggle service > Shortcut checkbox. */
         int ENABLE_WARNING_FROM_SHORTCUT = 1003;
 
         /**
+         * OPEN: Settings > Accessibility > Downloaded toggle service > Shortcut checkbox
+         * toggle.
+         */
+        int ENABLE_WARNING_FROM_SHORTCUT_TOGGLE = 1004;
+
+        /**
          * OPEN: Settings > Accessibility > Downloaded toggle service > Toggle use service to
          * disable service.
          */
-        int DISABLE_WARNING_FROM_TOGGLE = 1004;
+        int DISABLE_WARNING_FROM_TOGGLE = 1005;
 
         /**
          * OPEN: Settings > Accessibility > Magnification > Toggle user service in button
          * navigation.
          */
-        int ACCESSIBILITY_BUTTON_TUTORIAL = 1005;
+        int ACCESSIBILITY_BUTTON_TUTORIAL = 1006;
 
         /**
          * OPEN: Settings > Accessibility > Magnification > Toggle user service in gesture
          * navigation.
          */
-        int GESTURE_NAVIGATION_TUTORIAL = 1006;
+        int GESTURE_NAVIGATION_TUTORIAL = 1007;
 
         /**
          * OPEN: Settings > Accessibility > Downloaded toggle service > Toggle user service > Show
          * launch tutorial.
          */
-        int LAUNCH_ACCESSIBILITY_TUTORIAL = 1007;
+        int LAUNCH_ACCESSIBILITY_TUTORIAL = 1008;
     }
 
     @Override
@@ -516,7 +521,15 @@
         SharedPreferenceUtils.setUserShortcutType(context, info);
     }
 
-    private String getShortcutTypeSummary(Context context) {
+    protected CharSequence getShortcutTypeSummary(Context context) {
+        if (!mShortcutPreference.isSettingsEditable()) {
+            return context.getText(R.string.accessibility_shortcut_edit_dialog_title_hardware);
+        }
+
+        if (!mShortcutPreference.isChecked()) {
+            return context.getText(R.string.switch_off_text);
+        }
+
         final int shortcutType = getUserShortcutType(context, UserShortcutType.SOFTWARE);
         int resId = R.string.accessibility_shortcut_edit_summary_software;
         if (AccessibilityUtil.isGestureNavigateEnabled(context)) {
@@ -569,12 +582,13 @@
         }
 
         updateUserShortcutType(/* saveChanges= */ true);
-        if (mShortcutPreference.getChecked()) {
+        if (mShortcutPreference.isChecked()) {
             AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), mUserShortcutType,
                     mComponentName);
             AccessibilityUtil.optOutAllValuesFromSettings(getPrefContext(), ~mUserShortcutType,
                     mComponentName);
         }
+        mShortcutPreference.setChecked(true);
         mShortcutPreference.setSummary(
                 getShortcutTypeSummary(getPrefContext()));
     }
@@ -606,7 +620,7 @@
         mShortcutPreference = new ShortcutPreference(getPrefContext(), null);
         mShortcutPreference.setPersistent(false);
         mShortcutPreference.setKey(getShortcutPreferenceKey());
-        mShortcutPreference.setOnClickListener(this);
+        mShortcutPreference.setOnClickCallback(this);
 
         final CharSequence title = getString(R.string.accessibility_shortcut_title, mPackageName);
         mShortcutPreference.setTitle(title);
@@ -629,19 +643,20 @@
     }
 
     @Override
-    public void onCheckboxClicked(ShortcutPreference preference) {
+    public void onToggleClicked(ShortcutPreference preference) {
         if (mComponentName == null) {
             return;
         }
 
         final int shortcutTypes = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
-        if (preference.getChecked()) {
+        if (preference.isChecked()) {
             AccessibilityUtil.optInAllValuesToSettings(getPrefContext(), shortcutTypes,
                     mComponentName);
         } else {
             AccessibilityUtil.optOutAllValuesFromSettings(getPrefContext(), shortcutTypes,
                     mComponentName);
         }
+        mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
     }
 
     @Override
diff --git a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java
index e31feaa..6716234 100644
--- a/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleScreenMagnificationPreferenceFragment.java
@@ -330,7 +330,12 @@
         SharedPreferenceUtils.setUserShortcutType(context, info);
     }
 
-    private String getShortcutTypeSummary(Context context) {
+    @Override
+    protected CharSequence getShortcutTypeSummary(Context context) {
+        if (!mShortcutPreference.isChecked()) {
+            return context.getText(R.string.switch_off_text);
+        }
+
         final int shortcutType = getUserShortcutType(context, UserShortcutType.DEFAULT);
         int resId = R.string.accessibility_shortcut_edit_summary_software;
         if (AccessibilityUtil.isGestureNavigateEnabled(context)) {
@@ -381,10 +386,11 @@
 
     private void callOnAlertDialogCheckboxClicked(DialogInterface dialog, int which) {
         updateUserShortcutType(/* saveChanges= */ true);
-        if (mShortcutPreference.getChecked()) {
+        if (mShortcutPreference.isChecked()) {
             optInAllMagnificationValuesToSettings(getPrefContext(), mUserShortcutType);
             optOutAllMagnificationValuesFromSettings(getPrefContext(), ~mUserShortcutType);
         }
+        mShortcutPreference.setChecked(true);
         mShortcutPreference.setSummary(
                 getShortcutTypeSummary(getPrefContext()));
     }
@@ -442,13 +448,14 @@
     }
 
     @Override
-    public void onCheckboxClicked(ShortcutPreference preference) {
+    public void onToggleClicked(ShortcutPreference preference) {
         final int shortcutTypes = getUserShortcutType(getPrefContext(), UserShortcutType.SOFTWARE);
-        if (preference.getChecked()) {
+        if (preference.isChecked()) {
             optInAllMagnificationValuesToSettings(getPrefContext(), shortcutTypes);
         } else {
             optOutAllMagnificationValuesFromSettings(getPrefContext(), shortcutTypes);
         }
+        mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
     }
 
     @Override
@@ -473,7 +480,7 @@
         mShortcutPreference.setPersistent(false);
         mShortcutPreference.setKey(KEY_SHORTCUT_PREFERENCE);
         mShortcutPreference.setSummary(getShortcutTypeSummary(getPrefContext()));
-        mShortcutPreference.setOnClickListener(this);
+        mShortcutPreference.setOnClickCallback(this);
 
         final CharSequence title = getString(R.string.accessibility_shortcut_title, mPackageName);
         mShortcutPreference.setTitle(title);
diff --git a/src/com/android/settings/accounts/AccountRestrictionHelper.java b/src/com/android/settings/accounts/AccountRestrictionHelper.java
index 05b27aa..c67f5d1 100644
--- a/src/com/android/settings/accounts/AccountRestrictionHelper.java
+++ b/src/com/android/settings/accounts/AccountRestrictionHelper.java
@@ -15,8 +15,17 @@
  */
 package com.android.settings.accounts;
 
+import static android.os.UserManager.DISALLOW_REMOVE_MANAGED_PROFILE;
+
+import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
+
 import android.annotation.UserIdInt;
+import android.app.admin.DevicePolicyManager;
+import android.content.ComponentName;
 import android.content.Context;
+import android.content.pm.UserInfo;
+import android.os.UserHandle;
+import android.os.UserManager;
 
 import com.android.settings.AccessiblePreferenceCategory;
 import com.android.settingslib.RestrictedLockUtilsInternal;
@@ -44,7 +53,12 @@
             return;
         }
         if (hasBaseUserRestriction(userRestriction, userId)) {
-            preference.setEnabled(false);
+            if (userRestriction.equals(DISALLOW_REMOVE_MANAGED_PROFILE)
+                    && isOrganizationOwnedDevice()) {
+                preference.setDisabledByAdmin(getEnforcedAdmin(userRestriction, userId));
+            } else {
+                preference.setEnabled(false);
+            }
         } else {
             preference.checkRestrictionAndSetDisabled(userRestriction, userId);
         }
@@ -55,6 +69,41 @@
                 userId);
     }
 
+    private boolean isOrganizationOwnedDevice() {
+        final DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
+                Context.DEVICE_POLICY_SERVICE);
+        if (dpm == null) {
+            return false;
+        }
+        return dpm.isOrganizationOwnedDeviceWithManagedProfile();
+    }
+
+    private EnforcedAdmin getEnforcedAdmin(String userRestriction, int userId) {
+        final DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(
+                Context.DEVICE_POLICY_SERVICE);
+        if (dpm == null) {
+            return null;
+        }
+        final int managedUsedId = getManagedUserId(userId);
+        ComponentName adminComponent = dpm.getProfileOwnerAsUser(managedUsedId);
+        if (adminComponent != null) {
+            return new EnforcedAdmin(adminComponent, userRestriction,
+                    UserHandle.of(managedUsedId));
+        }
+        return null;
+    }
+
+    private int getManagedUserId(int userId) {
+        final UserManager um = UserManager.get(mContext);
+        for (UserInfo ui : um.getProfiles(userId)) {
+            if (ui.id == userId || !ui.isManagedProfile()) {
+                continue;
+            }
+            return ui.id;
+        }
+        return -1;
+    }
+
     public AccessiblePreferenceCategory createAccessiblePreferenceCategory(Context context) {
         return new AccessiblePreferenceCategory(context);
     }
diff --git a/tests/robotests/src/com/android/settings/accessibility/ShortcutPreferenceTest.java b/tests/robotests/src/com/android/settings/accessibility/ShortcutPreferenceTest.java
index f3522a9..597e94e 100644
--- a/tests/robotests/src/com/android/settings/accessibility/ShortcutPreferenceTest.java
+++ b/tests/robotests/src/com/android/settings/accessibility/ShortcutPreferenceTest.java
@@ -44,10 +44,10 @@
     private PreferenceViewHolder mPreferenceViewHolder;
     private String mResult;
 
-    private ShortcutPreference.OnClickListener mListener =
-            new ShortcutPreference.OnClickListener() {
+    private ShortcutPreference.OnClickCallback mListener =
+            new ShortcutPreference.OnClickCallback() {
                 @Override
-                public void onCheckboxClicked(ShortcutPreference preference) {
+                public void onToggleClicked(ShortcutPreference preference) {
                     mResult = CHECKBOX_CLICKED;
                 }
 
@@ -71,19 +71,19 @@
     @Test
     public void clickLinearLayout_checkboxClicked() {
         mShortcutPreference.onBindViewHolder(mPreferenceViewHolder);
-        mShortcutPreference.setOnClickListener(mListener);
+        mShortcutPreference.setOnClickCallback(mListener);
 
         LinearLayout mainFrame = mPreferenceViewHolder.itemView.findViewById(R.id.main_frame);
         mainFrame.performClick();
 
         assertThat(mResult).isEqualTo(CHECKBOX_CLICKED);
-        assertThat(mShortcutPreference.getChecked()).isTrue();
+        assertThat(mShortcutPreference.isChecked()).isTrue();
     }
 
     @Test
     public void clickSettings_settingsClicked() {
         mShortcutPreference.onBindViewHolder(mPreferenceViewHolder);
-        mShortcutPreference.setOnClickListener(mListener);
+        mShortcutPreference.setOnClickCallback(mListener);
 
         View settings = mPreferenceViewHolder.itemView.findViewById(android.R.id.widget_frame);
         settings.performClick();
@@ -95,6 +95,6 @@
     public void setCheckedTrue_getCheckedIsTrue() {
         mShortcutPreference.setChecked(true);
 
-        assertThat(mShortcutPreference.getChecked()).isEqualTo(true);
+        assertThat(mShortcutPreference.isChecked()).isEqualTo(true);
     }
 }