Merge "Use MobileDataEnabledFlow in BillingCyclePreference" into main
diff --git a/aconfig/settings_flag_declarations.aconfig b/aconfig/settings_flag_declarations.aconfig
new file mode 100644
index 0000000..c4c33b0
--- /dev/null
+++ b/aconfig/settings_flag_declarations.aconfig
@@ -0,0 +1,8 @@
+package: "com.android.settings.flags"
+
+flag {
+    name: "show_factory_reset_cancel_button"
+    namespace: "android_settings"
+    description: "This flag controls whether to show a Cancel button when factory reset"
+    bug: "300634367"
+}
diff --git a/src/com/android/settings/MainClear.java b/src/com/android/settings/MainClear.java
index 58fc0d5..0aba5ca 100644
--- a/src/com/android/settings/MainClear.java
+++ b/src/com/android/settings/MainClear.java
@@ -63,6 +63,7 @@
 
 import com.android.settings.core.InstrumentedFragment;
 import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
+import com.android.settings.flags.Flags;
 import com.android.settings.network.SubscriptionUtil;
 import com.android.settings.password.ChooseLockSettingsHelper;
 import com.android.settings.password.ConfirmLockPattern;
@@ -431,14 +432,24 @@
 
         final GlifLayout layout = mContentView.findViewById(R.id.setup_wizard_layout);
         final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class);
+        final Activity activity = getActivity();
         mixin.setPrimaryButton(
-                new FooterButton.Builder(getActivity())
+                new FooterButton.Builder(activity)
                         .setText(R.string.main_clear_button_text)
                         .setListener(mInitiateListener)
                         .setButtonType(ButtonType.OTHER)
                         .setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Primary)
-                        .build()
-        );
+                        .build());
+        if (Flags.showFactoryResetCancelButton()) {
+            mixin.setSecondaryButton(
+                    new FooterButton.Builder(activity)
+                            .setText(android.R.string.cancel)
+                            .setListener(view -> activity.onBackPressed())
+                            .setButtonType(ButtonType.CANCEL)
+                            .setTheme(
+                                    com.google.android.setupdesign.R.style.SudGlifButton_Secondary)
+                            .build());
+        }
         mInitiateButton = mixin.getPrimaryButton();
     }
 
diff --git a/src/com/android/settings/system/ClientInitiatedActionRepository.kt b/src/com/android/settings/system/ClientInitiatedActionRepository.kt
new file mode 100644
index 0000000..24c04b4
--- /dev/null
+++ b/src/com/android/settings/system/ClientInitiatedActionRepository.kt
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2023 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.
+ */
+
+package com.android.settings.system
+
+import android.content.Context
+import android.content.Intent
+import android.telephony.CarrierConfigManager
+import android.util.Log
+
+class ClientInitiatedActionRepository(private val context: Context) {
+    private val configManager = context.getSystemService(CarrierConfigManager::class.java)!!
+
+    /**
+     * Trigger client initiated action (send intent) on system update
+     */
+    fun onSystemUpdate() {
+        val bundle =
+            configManager.getConfig(
+                CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL,
+                CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING,
+                CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_STRING,
+                CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_VAL_STRING,
+            )
+
+        if (!bundle.getBoolean(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL)) return
+
+        val action =
+            bundle.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING)
+        if (action.isNullOrEmpty()) return
+        val extra = bundle.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_STRING)
+        val extraValue =
+            bundle.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_VAL_STRING)
+        Log.d(TAG, "onSystemUpdate: broadcasting intent $action with extra $extra, $extraValue")
+        val intent = Intent(action).apply {
+            if (!extra.isNullOrEmpty()) putExtra(extra, extraValue)
+            addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND)
+        }
+        context.applicationContext.sendBroadcast(intent)
+    }
+
+    companion object {
+        private const val TAG = "ClientInitiatedAction"
+    }
+}
diff --git a/src/com/android/settings/system/SystemUpdatePreferenceController.kt b/src/com/android/settings/system/SystemUpdatePreferenceController.kt
index 01df065..fa135aa 100644
--- a/src/com/android/settings/system/SystemUpdatePreferenceController.kt
+++ b/src/com/android/settings/system/SystemUpdatePreferenceController.kt
@@ -17,12 +17,9 @@
 package com.android.settings.system
 
 import android.content.Context
-import android.content.Intent
 import android.os.Build
-import android.os.PersistableBundle
 import android.os.SystemUpdateManager
 import android.os.UserManager
-import android.telephony.CarrierConfigManager
 import android.util.Log
 import androidx.lifecycle.Lifecycle
 import androidx.lifecycle.LifecycleOwner
@@ -39,6 +36,7 @@
 open class SystemUpdatePreferenceController(context: Context, preferenceKey: String) :
     BasePreferenceController(context, preferenceKey) {
     private val userManager: UserManager = context.userManager
+    private val clientInitiatedActionRepository = ClientInitiatedActionRepository(context)
     private lateinit var preference: Preference
 
     override fun getAvailabilityStatus() =
@@ -61,12 +59,7 @@
 
     override fun handlePreferenceTreeClick(preference: Preference): Boolean {
         if (preferenceKey == preference.key) {
-            val configManager = mContext.getSystemService(CarrierConfigManager::class.java)!!
-            configManager.getConfig(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL)?.let {
-                if (it.getBoolean(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL)) {
-                    ciActionOnSysUpdate(it)
-                }
-            }
+            clientInitiatedActionRepository.onSystemUpdate()
         }
         // always return false here because this handler does not want to block other handlers.
         return false
@@ -111,26 +104,6 @@
         Build.VERSION.RELEASE_OR_PREVIEW_DISPLAY,
     )
 
-    /**
-     * Trigger client initiated action (send intent) on system update
-     */
-    private fun ciActionOnSysUpdate(b: PersistableBundle) {
-        val intentStr = b.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING)
-        if (intentStr.isNullOrEmpty()) return
-        val extra = b.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_STRING)
-        val extraVal =
-            b.getString(CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_EXTRA_VAL_STRING)
-        Log.d(
-            TAG,
-            "ciActionOnSysUpdate: broadcasting intent $intentStr with extra $extra, $extraVal"
-        )
-        val intent = Intent(intentStr).apply {
-            if (!extra.isNullOrEmpty()) putExtra(extra, extraVal)
-            addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND)
-        }
-        mContext.applicationContext.sendBroadcast(intent)
-    }
-
     companion object {
         private const val TAG = "SysUpdatePrefContr"
     }
diff --git a/tests/spa_unit/src/com/android/settings/system/ClientInitiatedActionRepositoryTest.kt b/tests/spa_unit/src/com/android/settings/system/ClientInitiatedActionRepositoryTest.kt
new file mode 100644
index 0000000..f202668
--- /dev/null
+++ b/tests/spa_unit/src/com/android/settings/system/ClientInitiatedActionRepositoryTest.kt
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2023 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.
+ */
+
+package com.android.settings.system
+
+import android.content.Context
+import android.content.Intent
+import android.telephony.CarrierConfigManager
+import androidx.core.os.persistableBundleOf
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import com.google.common.truth.Truth.assertThat
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.kotlin.any
+import org.mockito.kotlin.anyVararg
+import org.mockito.kotlin.argumentCaptor
+import org.mockito.kotlin.doReturn
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.never
+import org.mockito.kotlin.stub
+import org.mockito.kotlin.verify
+
+@RunWith(AndroidJUnit4::class)
+class ClientInitiatedActionRepositoryTest {
+    private val mockCarrierConfigManager = mock<CarrierConfigManager>()
+
+    private val context = mock<Context> {
+        on { applicationContext } doReturn mock
+        on { getSystemService(CarrierConfigManager::class.java) } doReturn mockCarrierConfigManager
+    }
+
+    private val repository = ClientInitiatedActionRepository(context)
+
+    @Test
+    fun onSystemUpdate_notEnabled() {
+        mockCarrierConfigManager.stub {
+            on { getConfig(anyVararg()) } doReturn persistableBundleOf()
+        }
+
+        repository.onSystemUpdate()
+
+        verify(context, never()).sendBroadcast(any())
+    }
+
+    @Test
+    fun onSystemUpdate_enabled() {
+        mockCarrierConfigManager.stub {
+            on { getConfig(anyVararg()) } doReturn persistableBundleOf(
+                CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_BOOL to true,
+                CarrierConfigManager.KEY_CI_ACTION_ON_SYS_UPDATE_INTENT_STRING to ACTION,
+            )
+        }
+
+        repository.onSystemUpdate()
+
+        val intent = argumentCaptor<Intent> {
+            verify(context).sendBroadcast(capture())
+        }.firstValue
+        assertThat(intent.action).isEqualTo(ACTION)
+    }
+
+    private companion object {
+        const val ACTION = "ACTION"
+    }
+}
diff --git a/tests/unit/Android.bp b/tests/unit/Android.bp
index 0f045a8..327b6aa 100644
--- a/tests/unit/Android.bp
+++ b/tests/unit/Android.bp
@@ -21,6 +21,7 @@
         "aconfig_settings_flags_lib",
         "androidx.arch.core_core-testing",
         "androidx.test.core",
+        "androidx.test.espresso.core",
         "androidx.test.rules",
         "androidx.test.ext.junit",
         "androidx.preference_preference",
diff --git a/tests/unit/src/com/android/settings/MainClearTest.kt b/tests/unit/src/com/android/settings/MainClearTest.kt
new file mode 100644
index 0000000..05f06df
--- /dev/null
+++ b/tests/unit/src/com/android/settings/MainClearTest.kt
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2023 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.
+ */
+
+package com.android.settings
+
+import android.platform.test.flag.junit.SetFlagsRule
+import androidx.test.core.app.ActivityScenario
+import androidx.test.espresso.Espresso.onView
+import androidx.test.espresso.action.ViewActions.click
+import androidx.test.espresso.assertion.ViewAssertions.doesNotExist
+import androidx.test.espresso.assertion.ViewAssertions.matches
+import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
+import androidx.test.espresso.matcher.ViewMatchers.withText
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import com.android.settings.Settings.FactoryResetActivity
+import com.android.settings.flags.Flags
+import com.google.common.truth.Truth.assertThat
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+
+/** Test [MainClear]. */
+@RunWith(AndroidJUnit4::class)
+class MainClearTest {
+    @get:Rule
+    val mSetFlagsRule = SetFlagsRule()
+
+    @Test
+    fun factoryResetCancelButton_flagDisabled_noCancelButton() {
+        mSetFlagsRule.disableFlags(Flags.FLAG_SHOW_FACTORY_RESET_CANCEL_BUTTON)
+        ActivityScenario.launch(FactoryResetActivity::class.java).use {
+            ensurePrimaryButton()
+            onView(withText(android.R.string.cancel)).check(doesNotExist())
+            it.onActivity { activity -> assertThat(activity.isFinishing).isFalse() }
+        }
+    }
+
+    @Test
+    fun factoryResetCancelButton_flagEnabled_showCancelButton() {
+        mSetFlagsRule.enableFlags(Flags.FLAG_SHOW_FACTORY_RESET_CANCEL_BUTTON)
+        ActivityScenario.launch(FactoryResetActivity::class.java).use {
+            ensurePrimaryButton()
+            it.onActivity { activity -> assertThat(activity.isFinishing).isFalse() }
+
+            // Note: onView CANNOT be called within onActivity block, which runs in the main thread
+            onView(withText(android.R.string.cancel)).check(matches(isDisplayed())).perform(click())
+
+            it.onActivity { activity -> assertThat(activity.isFinishing).isTrue() }
+        }
+    }
+
+    private fun ensurePrimaryButton() {
+        onView(withText(R.string.main_clear_button_text)).check(matches(isDisplayed()))
+    }
+}
\ No newline at end of file