From ee0931f5bae4be4b1b2215cae910678a01e3bd1b Mon Sep 17 00:00:00 2001 From: Yanting Yang Date: Tue, 10 May 2022 07:06:14 +0800 Subject: [PATCH] Fix reset app preferences for work profile apps MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../manageapplications/ResetAppsHelper.java | 49 ++++++++++++------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/src/com/android/settings/applications/manageapplications/ResetAppsHelper.java b/src/com/android/settings/applications/manageapplications/ResetAppsHelper.java index 670b5a8ffd4..9cb5a3f851a 100644 --- a/src/com/android/settings/applications/manageapplications/ResetAppsHelper.java +++ b/src/com/android/settings/applications/manageapplications/ResetAppsHelper.java @@ -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 apps = mPm.getInstalledApplications( - PackageManager.GET_DISABLED_COMPONENTS); final List 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 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); + } } } }