Fix reset app preferences for work profile apps

In the past, Settings didn't support resetting the disable status of
work profile apps, it doesn’t follow the behavior that the reset dialog
mentioned.

To fix it, we are going to check apps by enabling user profiles and user
id. If the work profile is enabled, we will also reset them when users
select to reset app preferences.

Bug: 187387211
Test: manual
Change-Id: I25fd0129335539a23ed2ee0af57fdd9714eddf74
This commit is contained in:
Yanting Yang
2022-05-10 07:06:14 +08:00
parent 286682ec8a
commit ee0931f5ba

View File

@@ -32,6 +32,8 @@ import android.os.Bundle;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;
import androidx.appcompat.app.AlertDialog;
@@ -44,6 +46,7 @@ public class ResetAppsHelper implements DialogInterface.OnClickListener,
DialogInterface.OnDismissListener {
private static final String EXTRA_RESET_DIALOG = "resetDialog";
private static final String TAG = "ResetAppsHelper";
private final PackageManager mPm;
private final IPackageManager mIPm;
@@ -51,6 +54,7 @@ public class ResetAppsHelper implements DialogInterface.OnClickListener,
private final NetworkPolicyManager mNpm;
private final AppOpsManager mAom;
private final Context mContext;
private final UserManager mUm;
private AlertDialog mResetDialog;
@@ -62,6 +66,7 @@ public class ResetAppsHelper implements DialogInterface.OnClickListener,
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
mNpm = NetworkPolicyManager.from(context);
mAom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
mUm = (UserManager) context.getSystemService(Context.USER_SERVICE);
}
public void onRestoreInstanceState(Bundle savedInstanceState) {
@@ -110,27 +115,35 @@ public class ResetAppsHelper implements DialogInterface.OnClickListener,
AsyncTask.execute(new Runnable() {
@Override
public void run() {
final List<ApplicationInfo> apps = mPm.getInstalledApplications(
PackageManager.GET_DISABLED_COMPONENTS);
final List<String> allowList = Arrays.asList(
mContext.getResources().getStringArray(
R.array.config_skip_reset_apps_package_name));
for (int i = 0; i < apps.size(); i++) {
ApplicationInfo app = apps.get(i);
if (allowList.contains(app.packageName)) {
continue;
}
try {
mNm.clearData(app.packageName, app.uid, false);
} catch (android.os.RemoteException ex) {
}
if (!app.enabled) {
if (mPm.getApplicationEnabledSetting(app.packageName)
== PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) {
mPm.setApplicationEnabledSetting(app.packageName,
PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
PackageManager.DONT_KILL_APP);
for (UserHandle userHandle : mUm.getEnabledProfiles()) {
final int userId = userHandle.getIdentifier();
final List<ApplicationInfo> apps = mPm.getInstalledApplicationsAsUser(
PackageManager.GET_DISABLED_COMPONENTS, userId);
for (int i = 0; i < apps.size(); i++) {
ApplicationInfo app = apps.get(i);
if (allowList.contains(app.packageName)) {
continue;
}
try {
mNm.clearData(app.packageName, app.uid, false);
} catch (android.os.RemoteException ex) {
}
if (!app.enabled) {
try {
if (mIPm.getApplicationEnabledSetting(app.packageName, userId)
== PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) {
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);
}
}
}
}