AR/FR: Settings changes.

The basic AOSP settings infrastrucutre. Will add the Google specific
resources and tests to GoogleSettings in the next AR/FR change.

Test: make DEBUG_ROBOLECTRIC=1 RunSettingsRoboTests -j40
ROBOTEST_FILTER=com.android.settings.MasterClearTest.java

Change-Id: I7278b5c6d2a72e71d81c7fa5f937a2313d6c322c
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 317a2f5..9e113c3 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -9198,4 +9198,11 @@
     <!-- Keywords for Storage Access settings -->
     <string name="keywords_storage_access">storage access scoped directory</string>
 
+    <!-- Account type associated with the backup account. Empty for AOSP. [DO NOT TRANSLATE] -->
+    <string name="account_type"></string>
+    <!-- Package to target for Account credential confirmation. This will allow users to
+         remind/rediscover their backup account password prior to a reset. Empty for AOSP.
+         [DO NOT TRANSLATE] -->
+    <string name="account_confirmation_package"></string>
+
 </resources>
diff --git a/src/com/android/settings/MasterClear.java b/src/com/android/settings/MasterClear.java
index 7a6f966..3cc722b 100644
--- a/src/com/android/settings/MasterClear.java
+++ b/src/com/android/settings/MasterClear.java
@@ -28,6 +28,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
 import android.content.pm.UserInfo;
 import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
@@ -39,6 +40,7 @@
 import android.provider.Settings;
 import android.support.annotation.VisibleForTesting;
 import android.telephony.euicc.EuiccManager;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -74,6 +76,7 @@
     private static final String TAG = "MasterClear";
 
     private static final int KEYGUARD_REQUEST = 55;
+    private static final int CREDENTIAL_CONFIRM_REQUEST = 56;
 
     static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
     static final String ERASE_ESIMS_EXTRA = "erase_esim";
@@ -114,7 +117,7 @@
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
 
-        if (requestCode != KEYGUARD_REQUEST) {
+        if (requestCode != KEYGUARD_REQUEST || requestCode != CREDENTIAL_CONFIRM_REQUEST) {
             return;
         }
 
@@ -138,6 +141,33 @@
                 args, R.string.master_clear_confirm_title, null, null, 0);
     }
 
+    @VisibleForTesting
+    boolean tryShowAccountConfirmation() {
+        final Context context = getActivity();
+        final String accountType = context.getString(R.string.account_type);
+        final String packageName = context.getString(R.string.account_confirmation_package);
+        if (TextUtils.isEmpty(accountType) || TextUtils.isEmpty(packageName)) {
+            return false;
+        }
+        final AccountManager am = AccountManager.get(context);
+        Account[] accounts = am.getAccountsByType(accountType);
+        if (accounts != null && accounts.length > 0) {
+            final Intent requestAccountConfirmation = new Intent()
+                .setPackage(packageName)
+                .setAction("android.accounts.action.PRE_FACTORY_RESET");
+            // Check to make sure that the intent is supported.
+            final PackageManager pm = context.getPackageManager();
+            final List<ResolveInfo> resolutions =
+                pm.queryIntentActivities(requestAccountConfirmation, 0);
+            if (resolutions != null && resolutions.size() > 0) {
+                getActivity().startActivityForResult(
+                    requestAccountConfirmation, CREDENTIAL_CONFIRM_REQUEST);
+                return true;
+            }
+        }
+        return false;
+    }
+
     /**
      * If the user clicks to begin the reset sequence, we next require a
      * keyguard confirmation if the user has currently enabled one.  If there
@@ -158,7 +188,10 @@
                             .setAction(Intent.ACTION_FACTORY_RESET);
                     context.startActivity(requestFactoryReset);
                 }
-            } else if (!runKeyguardConfirmation(KEYGUARD_REQUEST)) {
+                return;
+            }
+
+            if (!tryShowAccountConfirmation() && !runKeyguardConfirmation(KEYGUARD_REQUEST)) {
                 showFinalConfirmation();
             }
         }
diff --git a/tests/robotests/src/com/android/settings/MasterClearTest.java b/tests/robotests/src/com/android/settings/MasterClearTest.java
index 838b1e8..361bc8f 100644
--- a/tests/robotests/src/com/android/settings/MasterClearTest.java
+++ b/tests/robotests/src/com/android/settings/MasterClearTest.java
@@ -160,6 +160,13 @@
         assertThat(componentName.getPackageName()).isEqualTo(intent.getPackage());
     }
 
+    @Test
+    public void testTryShowAccountConfirmation_unsupported() {
+      doReturn(mActivity).when(mMasterClear).getActivity();
+      /* Using the default resources, account confirmation shouldn't trigger */
+      assertThat(mMasterClear.tryShowAccountConfirmation()).isFalse();
+    }
+
     private void initScrollView(int height, int scrollY, int childBottom) {
         when(mScrollView.getHeight()).thenReturn(height);
         when(mScrollView.getScrollY()).thenReturn(scrollY);