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