Merge "Add new Special App Access screen for Backup Tasks." into main
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 57c577d..cbc0d8e 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -949,6 +949,39 @@
                        android:value="@string/menu_key_apps"/>
         </activity>
 
+        <activity-alias
+            android:name="BackupTasksActivity"
+            android:knownActivityEmbeddingCerts="@array/config_known_host_certs"
+            android:exported="true"
+            android:targetActivity=".spa.SpaBridgeActivity"
+            android:label="@string/run_backup_tasks_title">
+            <intent-filter android:priority="1">
+                <action android:name="android.settings.REQUEST_RUN_BACKUP_JOBS" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+            <meta-data android:name="com.android.settings.spa.DESTINATION"
+                       android:value="TogglePermissionAppList/BackupTasksApps"/>
+            <meta-data android:name="com.android.settings.HIGHLIGHT_MENU_KEY"
+                       android:value="@string/menu_key_apps"/>
+        </activity-alias>
+
+        <activity-alias
+            android:name="AppBackupTasksActivity"
+            android:knownActivityEmbeddingCerts="@array/config_known_host_certs"
+            android:exported="true"
+            android:targetActivity=".spa.SpaAppBridgeActivity"
+            android:label="@string/run_backup_tasks_title">
+            <intent-filter android:priority="1">
+                <action android:name="android.settings.REQUEST_RUN_BACKUP_JOBS" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <data android:scheme="package" />
+            </intent-filter>
+            <meta-data android:name="com.android.settings.spa.DESTINATION"
+                       android:value="TogglePermissionAppInfoPage/BackupTasksApps"/>
+            <meta-data android:name="com.android.settings.HIGHLIGHT_MENU_KEY"
+                       android:value="@string/menu_key_apps"/>
+        </activity-alias>
+
         <activity
             android:name="Settings$DateTimeSettingsActivity"
             android:label="@string/date_and_time"
diff --git a/aconfig/settings_perform_backup_tasks_flag_declarations.aconfig b/aconfig/settings_perform_backup_tasks_flag_declarations.aconfig
new file mode 100644
index 0000000..d060e24
--- /dev/null
+++ b/aconfig/settings_perform_backup_tasks_flag_declarations.aconfig
@@ -0,0 +1,9 @@
+package: "com.android.settings.flags"
+container: "system"
+
+flag {
+  name: "enable_perform_backup_tasks_in_settings"
+  namespace: "backstage_power"
+  description: "Enable the Perform Backup Tasks screen in Settings"
+  bug: "320563660"
+}
\ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
index ad35e26..ad33264 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -10232,6 +10232,21 @@
     <!-- Keywords for settings screen for controlling apps that can run long background tasks [CHAR LIMIT=NONE] -->
     <string name="keywords_long_background_tasks">long jobs, data transfer, background tasks</string>
 
+    <!-- Title for the settings screen for controlling apps that hold the run backup jobs permission [CHAR LIMIT=60] -->
+    <string name="run_backup_tasks_title">Perform backup tasks in background</string>
+    <!-- Label for the switch to toggle the run backup jobs permission [CHAR LIMIT=100] -->
+    <string name="run_backup_tasks_switch_title">Allow app to run backup-related background tasks</string>
+    <!-- Description that appears below the run_backup_tasks switch [CHAR LIMIT=NONE] -->
+    <string name="run_backup_tasks_footer_title">
+        Indicates that this app has a major use-case where it needs to backup or sync content.
+        Granting this permission allows the app to run in the background for a slightly longer time
+        in order to complete the backup-related work.
+        \n\nIf this permission is denied, the system will not give any special exemption to this
+        app to complete backup-related work in the background.
+    </string>
+    <!-- Keywords for settings screen for controlling apps that hold the run backup tasks permission [CHAR LIMIT=NONE] -->
+    <string name="keywords_run_backup_tasks">backup tasks, backup jobs</string>
+
     <!-- Reset rate-limiting in the system service ShortcutManager.  "ShortcutManager" is the name of a system service and not translatable.
     If the word "rate-limit" is hard to translate, use "Reset ShortcutManager API call limit" as the source text, which means
     the same thing in this context.
diff --git a/res/xml/special_access.xml b/res/xml/special_access.xml
index 743a122..d522ef6 100644
--- a/res/xml/special_access.xml
+++ b/res/xml/special_access.xml
@@ -20,6 +20,14 @@
     android:title="@string/special_access">
 
     <Preference
+        android:key="run_backup_tasks"
+        android:title="@string/run_backup_tasks_title"
+        android:order="-2000"
+        settings:keywords="@string/keywords_run_backup_tasks"
+        settings:controller="com.android.settings.spa.app.specialaccess.BackupTasksAppsPreferenceController">
+    </Preference>
+
+    <Preference
         android:key="manage_external_storage"
         android:title="@string/manage_external_storage_title"
         android:order="-1900"
diff --git a/src/com/android/settings/SettingsActivityUtil.kt b/src/com/android/settings/SettingsActivityUtil.kt
index 4238ff8..b1927f1 100644
--- a/src/com/android/settings/SettingsActivityUtil.kt
+++ b/src/com/android/settings/SettingsActivityUtil.kt
@@ -31,6 +31,7 @@
 import com.android.settings.spa.SpaAppBridgeActivity.Companion.getDestinationForApp
 import com.android.settings.spa.app.specialaccess.AlarmsAndRemindersAppListProvider
 import com.android.settings.spa.app.specialaccess.AllFilesAccessAppListProvider
+import com.android.settings.spa.app.specialaccess.BackupTasksAppsListProvider
 import com.android.settings.spa.app.specialaccess.DisplayOverOtherAppsAppListProvider
 import com.android.settings.spa.app.specialaccess.InstallUnknownAppsListProvider
 import com.android.settings.spa.app.specialaccess.MediaManagementAppsAppListProvider
@@ -68,6 +69,8 @@
             NfcTagAppsSettingsProvider.getAppInfoRoutePrefix(),
         VoiceActivationAppsListProvider::class.qualifiedName to
             VoiceActivationAppsListProvider.getAppInfoRoutePrefix(),
+        BackupTasksAppsListProvider::class.qualifiedName to
+            BackupTasksAppsListProvider.getAppInfoRoutePrefix(),
     )
 
     @JvmStatic
diff --git a/src/com/android/settings/spa/SettingsSpaEnvironment.kt b/src/com/android/settings/spa/SettingsSpaEnvironment.kt
index 41852e5..7a1d915 100644
--- a/src/com/android/settings/spa/SettingsSpaEnvironment.kt
+++ b/src/com/android/settings/spa/SettingsSpaEnvironment.kt
@@ -29,6 +29,7 @@
 import com.android.settings.spa.app.backgroundinstall.BackgroundInstalledAppsPageProvider
 import com.android.settings.spa.app.specialaccess.AlarmsAndRemindersAppListProvider
 import com.android.settings.spa.app.specialaccess.AllFilesAccessAppListProvider
+import com.android.settings.spa.app.specialaccess.BackupTasksAppsListProvider
 import com.android.settings.spa.app.specialaccess.DisplayOverOtherAppsAppListProvider
 import com.android.settings.spa.app.specialaccess.InstallUnknownAppsListProvider
 import com.android.settings.spa.app.specialaccess.LongBackgroundTasksAppListProvider
@@ -79,6 +80,7 @@
             NfcTagAppsSettingsProvider,
             LongBackgroundTasksAppListProvider,
             TurnScreenOnAppsAppListProvider,
+            BackupTasksAppsListProvider,
         )
     }
 
diff --git a/src/com/android/settings/spa/app/appinfo/AppInfoSettings.kt b/src/com/android/settings/spa/app/appinfo/AppInfoSettings.kt
index 695e114..c12915c 100644
--- a/src/com/android/settings/spa/app/appinfo/AppInfoSettings.kt
+++ b/src/com/android/settings/spa/app/appinfo/AppInfoSettings.kt
@@ -38,6 +38,7 @@
 import com.android.settings.spa.SpaActivity.Companion.startSpaActivity
 import com.android.settings.spa.app.appcompat.UserAspectRatioAppPreference
 import com.android.settings.spa.app.specialaccess.AlarmsAndRemindersAppListProvider
+import com.android.settings.spa.app.specialaccess.BackupTasksAppsListProvider
 import com.android.settings.spa.app.specialaccess.DisplayOverOtherAppsAppListProvider
 import com.android.settings.spa.app.specialaccess.InstallUnknownAppsListProvider
 import com.android.settings.spa.app.specialaccess.ModifySystemSettingsAppListProvider
@@ -169,6 +170,9 @@
             if (Flags.enableVoiceActivationAppsInSettings()) {
                 VoiceActivationAppsListProvider.InfoPageEntryItem(app)
             }
+            if (Flags.enablePerformBackupTasksInSettings()) {
+                BackupTasksAppsListProvider.InfoPageEntryItem(app)
+            }
         }
 
         Category(title = stringResource(R.string.app_install_details_group_title)) {
diff --git a/src/com/android/settings/spa/app/specialaccess/BackupTasksApps.kt b/src/com/android/settings/spa/app/specialaccess/BackupTasksApps.kt
new file mode 100644
index 0000000..d6d8fd4
--- /dev/null
+++ b/src/com/android/settings/spa/app/specialaccess/BackupTasksApps.kt
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2024 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.spa.app.specialaccess
+
+import android.Manifest
+import android.app.AppOpsManager
+import android.app.settings.SettingsEnums
+import android.content.Context
+import com.android.settings.R
+import com.android.settings.overlay.FeatureFactory.Companion.featureFactory
+import com.android.settingslib.spaprivileged.template.app.AppOpPermissionListModel
+import com.android.settingslib.spaprivileged.template.app.AppOpPermissionRecord
+import com.android.settingslib.spaprivileged.template.app.TogglePermissionAppListProvider
+
+object BackupTasksAppsListProvider : TogglePermissionAppListProvider {
+    override val permissionType = "BackupTasksApps"
+    override fun createModel(context: Context) = BackupTasksAppsListModel(context)
+}
+
+class BackupTasksAppsListModel(context: Context) : AppOpPermissionListModel(context) {
+    override val pageTitleResId = R.string.run_backup_tasks_title
+    override val switchTitleResId = R.string.run_backup_tasks_switch_title
+    override val footerResId = R.string.run_backup_tasks_footer_title
+    override val appOp = AppOpsManager.OP_RUN_BACKUP_JOBS
+    override val permission = Manifest.permission.RUN_BACKUP_JOBS
+    override val setModeByUid = true
+
+    override fun setAllowed(record: AppOpPermissionRecord, newAllowed: Boolean) {
+        super.setAllowed(record, newAllowed)
+        logPermissionChange(newAllowed)
+    }
+
+    private fun logPermissionChange(newAllowed: Boolean) {
+        featureFactory.metricsFeatureProvider.action(
+            context,
+            SettingsEnums.ACTION_RUN_BACKUP_TASKS_TOGGLE,
+            if (newAllowed) 1 else 0
+        )
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/settings/spa/app/specialaccess/BackupTasksAppsPreferenceController.kt b/src/com/android/settings/spa/app/specialaccess/BackupTasksAppsPreferenceController.kt
new file mode 100644
index 0000000..8d6de4e
--- /dev/null
+++ b/src/com/android/settings/spa/app/specialaccess/BackupTasksAppsPreferenceController.kt
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2024 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.spa.app.specialaccess
+
+import android.content.Context
+import androidx.preference.Preference
+import com.android.settings.core.BasePreferenceController
+import com.android.settings.flags.Flags
+import com.android.settings.spa.SpaActivity.Companion.startSpaActivity
+
+class BackupTasksAppsPreferenceController(context: Context, preferenceKey: String) :
+        BasePreferenceController(context, preferenceKey) {
+    override fun getAvailabilityStatus() =
+        if (Flags.enablePerformBackupTasksInSettings()) AVAILABLE
+        else CONDITIONALLY_UNAVAILABLE
+
+    override fun handlePreferenceTreeClick(preference: Preference): Boolean {
+        if (preference.key == mPreferenceKey) {
+            mContext.startSpaActivity(BackupTasksAppsListProvider.getAppListRoute())
+            return true
+        }
+        return false
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/settings/spa/app/specialaccess/SpecialAppAccess.kt b/src/com/android/settings/spa/app/specialaccess/SpecialAppAccess.kt
index 0285b74..4f79173 100644
--- a/src/com/android/settings/spa/app/specialaccess/SpecialAppAccess.kt
+++ b/src/com/android/settings/spa/app/specialaccess/SpecialAppAccess.kt
@@ -71,6 +71,7 @@
                 WifiControlAppListProvider,
                 LongBackgroundTasksAppListProvider,
                 TurnScreenOnAppsAppListProvider,
+                BackupTasksAppsListProvider,
             )
             .map { it.buildAppListInjectEntry().setLink(fromPage = owner).build() }
     }
diff --git a/tests/spa_unit/src/com/android/settings/spa/app/specialaccess/BackupTasksAppsPreferenceControllerTest.kt b/tests/spa_unit/src/com/android/settings/spa/app/specialaccess/BackupTasksAppsPreferenceControllerTest.kt
new file mode 100644
index 0000000..38f81fe
--- /dev/null
+++ b/tests/spa_unit/src/com/android/settings/spa/app/specialaccess/BackupTasksAppsPreferenceControllerTest.kt
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2024 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.spa.app.specialaccess
+
+import android.content.Context
+import android.platform.test.annotations.RequiresFlagsDisabled
+import android.platform.test.annotations.RequiresFlagsEnabled
+import android.platform.test.flag.junit.CheckFlagsRule
+import android.platform.test.flag.junit.DeviceFlagsValueProvider
+import androidx.preference.Preference
+import androidx.test.core.app.ApplicationProvider
+import androidx.test.ext.junit.runners.AndroidJUnit4
+
+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
+import org.mockito.kotlin.any
+import org.mockito.kotlin.doNothing
+import org.mockito.kotlin.spy
+import org.mockito.kotlin.whenever
+
+@RunWith(AndroidJUnit4::class)
+class BackupTasksAppsPreferenceControllerTest {
+
+    @get:Rule
+    val checkFlagsRule: CheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule()
+
+    private val context: Context = spy(ApplicationProvider.getApplicationContext()) {
+        doNothing().whenever(mock).startActivity(any())
+    }
+
+    private val matchedPreference = Preference(context).apply { key = preferenceKey }
+
+    private val misMatchedPreference = Preference(context).apply { key = testPreferenceKey }
+
+    private val controller = BackupTasksAppsPreferenceController(context, preferenceKey)
+
+    @Test
+    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_PERFORM_BACKUP_TASKS_IN_SETTINGS)
+    fun getAvailabilityStatus_enableBackupTasksApps_returnAvailable() {
+        assertThat(controller.isAvailable).isTrue()
+    }
+
+    @Test
+    @RequiresFlagsDisabled(Flags.FLAG_ENABLE_PERFORM_BACKUP_TASKS_IN_SETTINGS)
+    fun getAvailableStatus_disableBackupTasksApps_returnConditionallyUnavailable() {
+        assertThat(controller.isAvailable).isFalse()
+    }
+
+    @Test
+    fun handlePreferenceTreeClick_keyMatched_returnTrue() {
+        assertThat(controller.handlePreferenceTreeClick(matchedPreference)).isTrue()
+    }
+
+    @Test
+    fun handlePreferenceTreeClick_keyMisMatched_returnFalse() {
+        assertThat(controller.handlePreferenceTreeClick(misMatchedPreference)).isFalse()
+    }
+
+    companion object {
+        private const val preferenceKey: String = "backup_tasks_apps"
+        private const val testPreferenceKey: String = "test_key"
+    }
+}
\ No newline at end of file
diff --git a/tests/spa_unit/src/com/android/settings/spa/app/specialaccess/BackupTasksAppsTest.kt b/tests/spa_unit/src/com/android/settings/spa/app/specialaccess/BackupTasksAppsTest.kt
new file mode 100644
index 0000000..d68f051
--- /dev/null
+++ b/tests/spa_unit/src/com/android/settings/spa/app/specialaccess/BackupTasksAppsTest.kt
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2024 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.spa.app.specialaccess
+
+import android.Manifest
+import android.app.AppOpsManager
+import android.content.Context
+import androidx.test.core.app.ApplicationProvider
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import com.google.common.truth.Truth.assertThat
+import com.android.settings.R
+import org.junit.Test
+import org.junit.runner.RunWith
+
+@RunWith(AndroidJUnit4::class)
+class BackupTasksAppsTest {
+    private val context: Context = ApplicationProvider.getApplicationContext()
+
+    private val listModel = BackupTasksAppsListModel(context)
+
+    @Test
+    fun modelResourceIdAndProperties() {
+        assertThat(listModel.pageTitleResId).isEqualTo(R.string.run_backup_tasks_title)
+        assertThat(listModel.switchTitleResId).isEqualTo(R.string.run_backup_tasks_switch_title)
+        assertThat(listModel.footerResId).isEqualTo(R.string.run_backup_tasks_footer_title)
+        assertThat(listModel.appOp).isEqualTo(AppOpsManager.OP_RUN_BACKUP_JOBS)
+        assertThat(listModel.permission).isEqualTo(Manifest.permission.RUN_BACKUP_JOBS)
+        assertThat(listModel.setModeByUid).isTrue()
+    }
+}
\ No newline at end of file