Add ResetAppPreferences to AllAppListPage

Fix: 259520506
Test: Unit test
Test: Manually with Settings
Change-Id: I3cbd1171df7e6bfcac6b9ebf4901e36fc2d7b14f
This commit is contained in:
Chaohui Wang
2022-11-23 15:50:30 +08:00
parent 3b05ba6da8
commit 2cc51c1685
4 changed files with 197 additions and 46 deletions

View File

@@ -110,58 +110,57 @@ public class ResetAppsHelper implements DialogInterface.OnClickListener,
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
if (mResetDialog != dialog) { if (mResetDialog == dialog) {
return; resetApps();
} }
AsyncTask.execute(new Runnable() { }
@Override
public void run() { /** Resets the app preferences. */
final List<String> allowList = Arrays.asList( public void resetApps() {
mContext.getResources().getStringArray( AsyncTask.execute(() -> {
R.array.config_skip_reset_apps_package_name)); final List<String> allowList = Arrays.asList(
for (UserHandle userHandle : mUm.getEnabledProfiles()) { mContext.getResources().getStringArray(
final int userId = userHandle.getIdentifier(); R.array.config_skip_reset_apps_package_name));
final List<ApplicationInfo> apps = mPm.getInstalledApplicationsAsUser( for (UserHandle userHandle : mUm.getEnabledProfiles()) {
PackageManager.GET_DISABLED_COMPONENTS, userId); final int userId = userHandle.getIdentifier();
for (int i = 0; i < apps.size(); i++) { final List<ApplicationInfo> apps = mPm.getInstalledApplicationsAsUser(
ApplicationInfo app = apps.get(i); PackageManager.GET_DISABLED_COMPONENTS, userId);
if (allowList.contains(app.packageName)) { for (ApplicationInfo app : apps) {
continue; if (allowList.contains(app.packageName)) {
} continue;
}
try {
mNm.clearData(app.packageName, app.uid, false);
} catch (RemoteException ex) {
}
if (!app.enabled) {
try { try {
mNm.clearData(app.packageName, app.uid, false); if (mIPm.getApplicationEnabledSetting(app.packageName, userId)
} catch (android.os.RemoteException ex) { == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) {
} mIPm.setApplicationEnabledSetting(app.packageName,
if (!app.enabled) { PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
try { PackageManager.DONT_KILL_APP,
if (mIPm.getApplicationEnabledSetting(app.packageName, userId) userId,
== PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) { mContext.getPackageName());
mIPm.setApplicationEnabledSetting(app.packageName,
PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
PackageManager.DONT_KILL_APP,
userId,
mContext.getPackageName());
}
} catch (RemoteException e) {
Log.e(TAG, "Error during reset disabled apps.", e);
} }
} catch (RemoteException e) {
Log.e(TAG, "Error during reset disabled apps.", e);
} }
} }
} }
try { }
mIPm.resetApplicationPreferences(UserHandle.myUserId()); try {
} catch (RemoteException e) { mIPm.resetApplicationPreferences(UserHandle.myUserId());
} } catch (RemoteException e) {
mAom.resetAllModes(); }
BatteryOptimizeUtils.resetAppOptimizationMode(mContext, mIPm, mAom); mAom.resetAllModes();
final int[] restrictedUids = mNpm.getUidsWithPolicy( BatteryOptimizeUtils.resetAppOptimizationMode(mContext, mIPm, mAom);
POLICY_REJECT_METERED_BACKGROUND); final int[] restrictedUids = mNpm.getUidsWithPolicy(POLICY_REJECT_METERED_BACKGROUND);
final int currentUserId = ActivityManager.getCurrentUser(); final int currentUserId = ActivityManager.getCurrentUser();
for (int uid : restrictedUids) { for (int uid : restrictedUids) {
// Only reset for current user // Only reset for current user
if (UserHandle.getUserId(uid) == currentUserId) { if (UserHandle.getUserId(uid) == currentUserId) {
mNpm.setUidPolicy(uid, POLICY_NONE); mNpm.setUidPolicy(uid, POLICY_NONE);
}
} }
} }
}); });

View File

@@ -58,10 +58,12 @@ object AllAppListPageProvider : SettingsPageProvider {
@Composable @Composable
private fun AllAppListPage() { private fun AllAppListPage() {
val resetAppDialogPresenter = rememberResetAppDialogPresenter()
AppListPage( AppListPage(
title = stringResource(R.string.all_apps), title = stringResource(R.string.all_apps),
listModel = remember { AllAppListModel() }, listModel = remember { AllAppListModel() },
showInstantApps = true, showInstantApps = true,
moreOptions = { ResetAppPreferences(resetAppDialogPresenter::open) }
) { itemModel -> ) { itemModel ->
AppListItem( AppListItem(
itemModel = itemModel, itemModel = itemModel,

View File

@@ -0,0 +1,60 @@
/*
* Copyright (C) 2022 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
import android.os.UserHandle
import android.os.UserManager
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import com.android.settings.R
import com.android.settings.applications.manageapplications.ResetAppsHelper
import com.android.settingslib.spa.widget.dialog.AlertDialogButton
import com.android.settingslib.spa.widget.dialog.AlertDialogPresenter
import com.android.settingslib.spa.widget.dialog.rememberAlertDialogPresenter
import com.android.settingslib.spa.widget.scaffold.MoreOptionsScope
import com.android.settingslib.spaprivileged.model.enterprise.Restrictions
import com.android.settingslib.spaprivileged.template.scaffold.RestrictedMenuItem
@Composable
fun MoreOptionsScope.ResetAppPreferences(onClick: () -> Unit) {
RestrictedMenuItem(
text = stringResource(R.string.reset_app_preferences),
restrictions = remember {
Restrictions(
userId = UserHandle.myUserId(),
keys = listOf(UserManager.DISALLOW_APPS_CONTROL),
)
},
onClick = onClick,
)
}
@Composable
fun rememberResetAppDialogPresenter(): AlertDialogPresenter {
val context = LocalContext.current
return rememberAlertDialogPresenter(
confirmButton = AlertDialogButton(stringResource(R.string.reset_app_preferences_button)) {
ResetAppsHelper(context).resetApps()
},
dismissButton = AlertDialogButton(stringResource(R.string.cancel)),
title = stringResource(R.string.reset_app_preferences_title),
text = { Text(stringResource(R.string.reset_app_preferences_desc)) },
)
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright (C) 2022 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
import android.content.Context
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settings.R
import com.android.settingslib.spa.widget.scaffold.MoreOptionsScope
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Spy
@RunWith(AndroidJUnit4::class)
class ResetAppPreferencesTest {
@get:Rule
val composeTestRule = createComposeRule()
@Spy
private val context: Context = ApplicationProvider.getApplicationContext()
@Test
fun resetAppPreferences_titleIsDisplayed() {
setResetAppPreferences()
composeTestRule.onNodeWithText(context.getString(R.string.reset_app_preferences))
.assertIsDisplayed()
}
private fun setResetAppPreferences() {
val fakeMoreOptionsScope = object : MoreOptionsScope {
override fun dismiss() {}
}
composeTestRule.setContent {
fakeMoreOptionsScope.ResetAppPreferences {}
}
}
@Test
fun resetAppDialogPresenter_confirmButtonDisplayed() {
setAndOpenDialog()
composeTestRule.onNodeWithText(context.getString(R.string.reset_app_preferences_button))
.assertIsDisplayed()
}
@Test
fun resetAppDialogPresenter_titleDisplayed() {
setAndOpenDialog()
composeTestRule.onNodeWithText(context.getString(R.string.reset_app_preferences_title))
.assertIsDisplayed()
}
@Test
fun resetAppDialogPresenter_textDisplayed() {
setAndOpenDialog()
composeTestRule.onNodeWithText(context.getString(R.string.reset_app_preferences_desc))
.assertIsDisplayed()
}
private fun setAndOpenDialog() {
composeTestRule.setContent {
val dialogPresenter = rememberResetAppDialogPresenter()
LaunchedEffect(Unit) {
dialogPresenter.open()
}
}
}
}