1 /* 2 * Copyright (C) 2018 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 17 package com.android.car.settings.system; 18 19 import android.app.ActivityManager; 20 import android.app.AppOpsManager; 21 import android.app.INotificationManager; 22 import android.content.Context; 23 import android.content.pm.ApplicationInfo; 24 import android.content.pm.IPackageManager; 25 import android.content.pm.PackageManager; 26 import android.os.AsyncTask; 27 import android.os.Bundle; 28 import android.os.IBinder; 29 import android.os.RemoteException; 30 import android.os.ServiceManager; 31 import android.widget.Toast; 32 33 import androidx.annotation.VisibleForTesting; 34 import androidx.annotation.XmlRes; 35 36 import com.android.car.settings.R; 37 import com.android.car.settings.common.Logger; 38 import com.android.car.settings.common.SettingsFragment; 39 import com.android.car.ui.toolbar.MenuItem; 40 41 import java.lang.ref.WeakReference; 42 import java.util.Collections; 43 import java.util.List; 44 45 /** 46 * Presents the user with information about resetting app preferences. 47 */ 48 public class ResetAppPrefFragment extends SettingsFragment { 49 50 private static final Logger LOG = new Logger(ResetAppPrefFragment.class); 51 52 private MenuItem mResetButton; 53 @VisibleForTesting 54 AsyncTask<Void, Void, Void> mResetTask; 55 56 @Override 57 @XmlRes getPreferenceScreenResId()58 protected int getPreferenceScreenResId() { 59 return R.xml.reset_app_pref_fragment; 60 } 61 62 @Override getToolbarMenuItems()63 public List<MenuItem> getToolbarMenuItems() { 64 return Collections.singletonList(mResetButton); 65 } 66 67 @Override onCreate(Bundle savedInstanceState)68 public void onCreate(Bundle savedInstanceState) { 69 super.onCreate(savedInstanceState); 70 71 mResetButton = new MenuItem.Builder(getContext()) 72 .setTitle(R.string.reset_app_pref_button_text) 73 .setOnClickListener(i -> resetAppPreferences()) 74 .build(); 75 } 76 resetAppPreferences()77 private void resetAppPreferences() { 78 mResetTask = new ResetTask(requireContext().getApplicationContext()); 79 mResetTask.execute(); 80 } 81 82 private static class ResetTask extends AsyncTask<Void, Void, Void> { 83 84 private final WeakReference<Context> mContext; 85 ResetTask(Context context)86 ResetTask(Context context) { 87 mContext = new WeakReference<>(context); 88 } 89 90 @Override doInBackground(Void... unused)91 protected Void doInBackground(Void... unused) { 92 Context context = mContext.get(); 93 if (context == null) { 94 LOG.w("Unable to reset app preferences. Null context"); 95 return null; 96 } 97 PackageManager packageManager = context.getPackageManager(); 98 IBinder notificationManagerServiceBinder = ServiceManager.getService( 99 Context.NOTIFICATION_SERVICE); 100 if (notificationManagerServiceBinder == null) { 101 LOG.w("Unable to reset app preferences. Null notification manager service"); 102 return null; 103 } 104 INotificationManager notificationManagerService = 105 INotificationManager.Stub.asInterface(notificationManagerServiceBinder); 106 107 // Reset app notifications. 108 // Reset disabled apps. 109 List<ApplicationInfo> apps = packageManager.getInstalledApplications( 110 PackageManager.MATCH_DISABLED_COMPONENTS); 111 for (ApplicationInfo app : apps) { 112 try { 113 notificationManagerService.setNotificationsEnabledForPackage( 114 app.packageName, 115 app.uid, true); 116 } catch (RemoteException e) { 117 LOG.w("Unable to reset notification preferences for app: " + app.name, e); 118 } 119 if (!app.enabled) { 120 if (packageManager.getApplicationEnabledSetting(app.packageName) 121 == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER) { 122 packageManager.setApplicationEnabledSetting(app.packageName, 123 PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, 124 PackageManager.DONT_KILL_APP); 125 } 126 } 127 } 128 129 // Reset default applications for actions. 130 // Reset background data restrictions for apps. 131 // Reset permission restrictions. 132 try { 133 IBinder packageManagerServiceBinder = ServiceManager.getService("package"); 134 if (packageManagerServiceBinder == null) { 135 LOG.w("Unable to reset app preferences. Null package manager service"); 136 return null; 137 } 138 IPackageManager.Stub.asInterface( 139 packageManagerServiceBinder).resetApplicationPreferences( 140 ActivityManager.getCurrentUser()); 141 } catch (RemoteException e) { 142 LOG.w("Unable to reset app preferences", e); 143 } 144 145 // Cleanup. 146 context.getSystemService(AppOpsManager.class).resetAllModes(); 147 148 return null; 149 } 150 151 @Override onPostExecute(Void unused)152 protected void onPostExecute(Void unused) { 153 super.onPostExecute(unused); 154 Context context = mContext.get(); 155 if (context != null) { 156 Toast.makeText(context, R.string.reset_app_pref_complete_toast, 157 Toast.LENGTH_SHORT).show(); 158 } 159 } 160 } 161 } 162