1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.settings.applications.manageapplications; 17 18 import static android.net.NetworkPolicyManager.POLICY_NONE; 19 import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; 20 21 import android.app.ActivityManager; 22 import android.app.AppOpsManager; 23 import android.app.INotificationManager; 24 import android.content.Context; 25 import android.content.DialogInterface; 26 import android.content.pm.ApplicationInfo; 27 import android.content.pm.IPackageManager; 28 import android.content.pm.PackageManager; 29 import android.net.NetworkPolicyManager; 30 import android.os.AsyncTask; 31 import android.os.Bundle; 32 import android.os.RemoteException; 33 import android.os.ServiceManager; 34 import android.os.UserHandle; 35 36 import androidx.appcompat.app.AlertDialog; 37 38 import com.android.settings.R; 39 40 import java.util.Arrays; 41 import java.util.List; 42 43 public class ResetAppsHelper implements DialogInterface.OnClickListener, 44 DialogInterface.OnDismissListener { 45 46 private static final String EXTRA_RESET_DIALOG = "resetDialog"; 47 48 private final PackageManager mPm; 49 private final IPackageManager mIPm; 50 private final INotificationManager mNm; 51 private final NetworkPolicyManager mNpm; 52 private final AppOpsManager mAom; 53 private final Context mContext; 54 55 private AlertDialog mResetDialog; 56 ResetAppsHelper(Context context)57 public ResetAppsHelper(Context context) { 58 mContext = context; 59 mPm = context.getPackageManager(); 60 mIPm = IPackageManager.Stub.asInterface(ServiceManager.getService("package")); 61 mNm = INotificationManager.Stub.asInterface( 62 ServiceManager.getService(Context.NOTIFICATION_SERVICE)); 63 mNpm = NetworkPolicyManager.from(context); 64 mAom = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); 65 } 66 onRestoreInstanceState(Bundle savedInstanceState)67 public void onRestoreInstanceState(Bundle savedInstanceState) { 68 if (savedInstanceState != null && savedInstanceState.getBoolean(EXTRA_RESET_DIALOG)) { 69 buildResetDialog(); 70 } 71 } 72 onSaveInstanceState(Bundle outState)73 public void onSaveInstanceState(Bundle outState) { 74 if (mResetDialog != null) { 75 outState.putBoolean(EXTRA_RESET_DIALOG, true); 76 } 77 } 78 stop()79 public void stop() { 80 if (mResetDialog != null) { 81 mResetDialog.dismiss(); 82 mResetDialog = null; 83 } 84 } 85 buildResetDialog()86 void buildResetDialog() { 87 if (mResetDialog == null) { 88 mResetDialog = new AlertDialog.Builder(mContext) 89 .setTitle(R.string.reset_app_preferences_title) 90 .setMessage(R.string.reset_app_preferences_desc) 91 .setPositiveButton(R.string.reset_app_preferences_button, this) 92 .setNegativeButton(R.string.cancel, null) 93 .setOnDismissListener(this) 94 .show(); 95 } 96 } 97 98 @Override onDismiss(DialogInterface dialog)99 public void onDismiss(DialogInterface dialog) { 100 if (mResetDialog == dialog) { 101 mResetDialog = null; 102 } 103 } 104 105 @Override onClick(DialogInterface dialog, int which)106 public void onClick(DialogInterface dialog, int which) { 107 if (mResetDialog != dialog) { 108 return; 109 } 110 AsyncTask.execute(new Runnable() { 111 @Override 112 public void run() { 113 final List<ApplicationInfo> apps = mPm.getInstalledApplications( 114 PackageManager.GET_DISABLED_COMPONENTS); 115 final List<String> whiteList = Arrays.asList( 116 mContext.getResources().getStringArray( 117 R.array.config_skip_reset_apps_package_name)); 118 119 for (int i = 0; i < apps.size(); i++) { 120 ApplicationInfo app = apps.get(i); 121 if (whiteList.contains(app.packageName)) { 122 continue; 123 } 124 try { 125 mNm.clearData(app.packageName, app.uid, false); 126 } catch (android.os.RemoteException ex) { 127 } 128 if (!app.enabled) { 129 if (mPm.getApplicationEnabledSetting(app.packageName) 130 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) { 131 mPm.setApplicationEnabledSetting(app.packageName, 132 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 133 PackageManager.DONT_KILL_APP); 134 } 135 } 136 } 137 try { 138 mIPm.resetApplicationPreferences(UserHandle.myUserId()); 139 } catch (RemoteException e) { 140 } 141 mAom.resetAllModes(); 142 final int[] restrictedUids = mNpm.getUidsWithPolicy( 143 POLICY_REJECT_METERED_BACKGROUND); 144 final int currentUserId = ActivityManager.getCurrentUser(); 145 for (int uid : restrictedUids) { 146 // Only reset for current user 147 if (UserHandle.getUserId(uid) == currentUserId) { 148 mNpm.setUidPolicy(uid, POLICY_NONE); 149 } 150 } 151 } 152 }); 153 } 154 } 155