Add back "Tapping Stop will stop x" dialog while turning off an accessibility service

Bug: 146539308
Test: make RunSettingsRoboTests2
Change-Id: Ic572f9245db016bd79a272030f97004a21f4b672
diff --git a/src/com/android/settings/accessibility/AccessibilityServiceWarning.java b/src/com/android/settings/accessibility/AccessibilityServiceWarning.java
index 63ccb6e..2206e81 100644
--- a/src/com/android/settings/accessibility/AccessibilityServiceWarning.java
+++ b/src/com/android/settings/accessibility/AccessibilityServiceWarning.java
@@ -21,6 +21,7 @@
 import android.accessibilityservice.AccessibilityServiceInfo;
 import android.app.Dialog;
 import android.content.Context;
+import android.content.DialogInterface;
 import android.graphics.drawable.Drawable;
 import android.os.storage.StorageManager;
 import android.text.BidiFormatter;
@@ -59,13 +60,7 @@
         return false;
     };
 
-    /**
-     * Gets a content View for a dialog to confirm that they want to enable a service.
-     *
-     * @param context A valid context
-     * @param info The info about a service
-     * @return A content view suitable for viewing
-     */
+    /** Returns a {@link Dialog} to be shown to confirm that they want to enable a service. */
     public static Dialog createCapabilitiesDialog(Context context,
             AccessibilityServiceInfo info, View.OnClickListener listener) {
         final AlertDialog ad = new AlertDialog.Builder(context)
@@ -137,6 +132,23 @@
         return content;
     }
 
+    /** Returns a {@link Dialog} to be shown to confirm that they want to disable a service. */
+    public static Dialog createDisableDialog(Context context,
+            AccessibilityServiceInfo info, DialogInterface.OnClickListener listener) {
+        final AlertDialog dialog = new AlertDialog.Builder(context)
+                .setTitle(context.getString(R.string.disable_service_title,
+                        info.getResolveInfo().loadLabel(context.getPackageManager())))
+                .setMessage(context.getString(R.string.disable_service_message,
+                        context.getString(R.string.accessibility_dialog_button_stop),
+                        getServiceName(context, info)))
+                .setCancelable(true)
+                .setPositiveButton(R.string.accessibility_dialog_button_stop, listener)
+                .setNegativeButton(R.string.accessibility_dialog_button_cancel, listener)
+                .create();
+
+        return dialog;
+    }
+
     // Get the service name and bidi wrap it to protect from bidi side effects.
     private static CharSequence getServiceName(Context context, AccessibilityServiceInfo info) {
         final Locale locale = context.getResources().getConfiguration().getLocales().get(0);
diff --git a/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java b/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java
index fcdd010..36b2da1 100644
--- a/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleAccessibilityServicePreferenceFragment.java
@@ -23,6 +23,7 @@
 import android.app.settings.SettingsEnums;
 import android.content.ComponentName;
 import android.content.ContentResolver;
+import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.pm.ResolveInfo;
 import android.content.pm.ServiceInfo;
@@ -129,7 +130,7 @@
                 }
                 mDialog = AccessibilityServiceWarning
                         .createCapabilitiesDialog(getPrefContext(), info,
-                                this::onDialogButtonFromToggleClicked);
+                                this::onDialogButtonFromEnableToggleClicked);
                 break;
             }
             case DialogEnums.ENABLE_WARNING_FROM_SHORTCUT: {
@@ -142,6 +143,16 @@
                                 this::onDialogButtonFromShortcutClicked);
                 break;
             }
+            case DialogEnums.DISABLE_WARNING_FROM_TOGGLE: {
+                final AccessibilityServiceInfo info = getAccessibilityServiceInfo();
+                if (info == null) {
+                    return null;
+                }
+                mDialog = AccessibilityServiceWarning
+                        .createDisableDialog(getPrefContext(), info,
+                                this::onDialogButtonFromDisableToggleClicked);
+                break;
+            }
             case DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL: {
                 if (AccessibilityUtil.isGestureNavigateEnabled(getPrefContext())) {
                     mDialog = AccessibilityGestureNavigationTutorial
@@ -165,6 +176,8 @@
             case DialogEnums.ENABLE_WARNING_FROM_TOGGLE:
             case DialogEnums.ENABLE_WARNING_FROM_SHORTCUT:
                 return SettingsEnums.DIALOG_ACCESSIBILITY_SERVICE_ENABLE;
+            case DialogEnums.DISABLE_WARNING_FROM_TOGGLE:
+                return SettingsEnums.DIALOG_ACCESSIBILITY_SERVICE_DISABLE;
             case DialogEnums.LAUNCH_ACCESSIBILITY_TUTORIAL:
                 return AccessibilityUtil.isGestureNavigateEnabled(getPrefContext())
                         ? SettingsEnums.DIALOG_TOGGLE_SCREEN_GESTURE_NAVIGATION
@@ -203,7 +216,7 @@
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (requestCode == ACTIVITY_REQUEST_CONFIRM_CREDENTIAL_FOR_WEAKER_ENCRYPTION) {
             if (resultCode == Activity.RESULT_OK) {
-                handleConfirmServiceEnabled(true);
+                handleConfirmServiceEnabled(/* confirmed= */ true);
                 // The user confirmed that they accept weaker encryption when
                 // enabling the accessibility service, so change encryption.
                 // Since we came here asynchronously, check encryption again.
@@ -213,7 +226,7 @@
                             Settings.Global.REQUIRE_PASSWORD_TO_DECRYPT, 0);
                 }
             } else {
-                handleConfirmServiceEnabled(false);
+                handleConfirmServiceEnabled(/* confirmed= */ false);
             }
         }
     }
@@ -323,17 +336,31 @@
                 getPackageManager());
     }
 
-    private void onDialogButtonFromToggleClicked(View view) {
-        if (view.getId() == R.id.permission_enable_allow_button) {
-            onAllowButtonFromToggleClicked();
-        } else if (view.getId() == R.id.permission_enable_deny_button) {
-            onDenyButtonFromToggleClicked();
+    private void onDialogButtonFromDisableToggleClicked(DialogInterface dialog, int which) {
+        switch (which) {
+            case DialogInterface.BUTTON_POSITIVE:
+                handleConfirmServiceEnabled(/* confirmed= */ false);
+                break;
+            case DialogInterface.BUTTON_NEGATIVE:
+                handleConfirmServiceEnabled(/* confirmed= */ true);
+                break;
+            default:
+                throw new IllegalArgumentException("Unexpected button identifier");
+        }
+    }
+
+    private void onDialogButtonFromEnableToggleClicked(View view) {
+        final int viewId = view.getId();
+        if (viewId == R.id.permission_enable_allow_button) {
+            onAllowButtonFromEnableToggleClicked();
+        } else if (viewId == R.id.permission_enable_deny_button) {
+            onDenyButtonFromEnableToggleClicked();
         } else {
             throw new IllegalArgumentException("Unexpected view id");
         }
     }
 
-    private void onAllowButtonFromToggleClicked() {
+    private void onAllowButtonFromEnableToggleClicked() {
         if (isFullDiskEncrypted()) {
             final String title = createConfirmCredentialReasonMessage();
             final Intent intent = ConfirmDeviceCredentialActivity.createIntent(title, /* details= */
@@ -351,15 +378,16 @@
         mDialog.dismiss();
     }
 
-    private void onDenyButtonFromToggleClicked() {
+    private void onDenyButtonFromEnableToggleClicked() {
         handleConfirmServiceEnabled(/* confirmed= */ false);
         mDialog.dismiss();
     }
 
     void onDialogButtonFromShortcutClicked(View view) {
-        if (view.getId() == R.id.permission_enable_allow_button) {
+        final int viewId = view.getId();
+        if (viewId == R.id.permission_enable_allow_button) {
             onAllowButtonFromShortcutClicked();
-        } else if (view.getId() == R.id.permission_enable_deny_button) {
+        } else if (viewId == R.id.permission_enable_deny_button) {
             onDenyButtonFromShortcutClicked();
         } else {
             throw new IllegalArgumentException("Unexpected view id");
@@ -384,7 +412,8 @@
     private boolean onBeforeCheckedChanged(ToggleSwitch toggleSwitch, boolean checked) {
         if (checked) {
             mSwitchBar.setCheckedInternal(false);
-            getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED, false);
+            getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED,
+                    /* disableService */ false);
             if (!mShortcutPreference.getChecked()) {
                 showPopupDialog(DialogEnums.ENABLE_WARNING_FROM_TOGGLE);
             } else {
@@ -394,7 +423,10 @@
                 }
             }
         } else {
-            handleConfirmServiceEnabled(/* confirmed= */ false);
+            mSwitchBar.setCheckedInternal(true);
+            getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED,
+                    /* enableService */ true);
+            showDialog(DialogEnums.DISABLE_WARNING_FROM_TOGGLE);
         }
         return true;
     }
diff --git a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
index 409b395..63d458a 100644
--- a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
+++ b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java
@@ -258,6 +258,7 @@
     /** Denotes the dialog emuns for show dialog */
     @Retention(RetentionPolicy.SOURCE)
     protected @interface DialogEnums {
+
         /** OPEN: Settings > Accessibility > Any toggle service > Shortcut > Settings. */
         int EDIT_SHORTCUT = 1;
 
@@ -265,28 +266,38 @@
         int MAGNIFICATION_EDIT_SHORTCUT = 1001;
 
         /**
-         * OPEN: Settings > Accessibility > Magnification > Toggle user service in gesture
-         * navigation.
+         * OPEN: Settings > Accessibility > Downloaded toggle service > Toggle use service to
+         * enable service.
          */
-        int GESTURE_NAVIGATION_TUTORIAL = 1002;
+        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 > Toggle use service to
+         * disable service.
+         */
+        int DISABLE_WARNING_FROM_TOGGLE = 1004;
 
         /**
          * OPEN: Settings > Accessibility > Magnification > Toggle user service in button
          * navigation.
          */
-        int ACCESSIBILITY_BUTTON_TUTORIAL = 1003;
+        int ACCESSIBILITY_BUTTON_TUTORIAL = 1005;
 
-        /** OPEN: Settings > Accessibility > Downloaded toggle service > Toggle user service. */
-        int ENABLE_WARNING_FROM_TOGGLE = 1004;
-
-        /** OPEN: Settings > Accessibility > Downloaded toggle service > Shortcut checkbox. */
-        int ENABLE_WARNING_FROM_SHORTCUT = 1005;
+        /**
+         * OPEN: Settings > Accessibility > Magnification > Toggle user service in gesture
+         * navigation.
+         */
+        int GESTURE_NAVIGATION_TUTORIAL = 1006;
 
         /**
          * OPEN: Settings > Accessibility > Downloaded toggle service > Toggle user service > Show
          * launch tutorial.
          */
-        int LAUNCH_ACCESSIBILITY_TUTORIAL = 1006;
+        int LAUNCH_ACCESSIBILITY_TUTORIAL = 1007;
     }
 
     @Override