• 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.settings.applications.specialaccess.zenaccess;
18 
19 import android.app.ActivityManager;
20 import android.app.AppGlobals;
21 import android.app.NotificationManager;
22 import android.app.settings.SettingsEnums;
23 import android.content.Context;
24 import android.content.pm.PackageInfo;
25 import android.content.pm.ParceledListSlice;
26 import android.os.AsyncTask;
27 import android.os.RemoteException;
28 import android.util.ArraySet;
29 import android.util.Log;
30 
31 import androidx.annotation.VisibleForTesting;
32 
33 import com.android.settings.core.BasePreferenceController;
34 import com.android.settings.overlay.FeatureFactory;
35 
36 import java.util.List;
37 import java.util.Set;
38 
39 public class ZenAccessController extends BasePreferenceController {
40 
41     private static final String TAG = "ZenAccessController";
42 
ZenAccessController(Context context, String preferenceKey)43     public ZenAccessController(Context context, String preferenceKey) {
44         super(context, preferenceKey);
45     }
46 
47     @Override
getAvailabilityStatus()48     public int getAvailabilityStatus() {
49         return AVAILABLE;
50     }
51 
getPackagesRequestingNotificationPolicyAccess()52     public static Set<String> getPackagesRequestingNotificationPolicyAccess() {
53         final String[] PERM = {
54                 android.Manifest.permission.ACCESS_NOTIFICATION_POLICY
55         };
56         return getPackagesWithPermissions(PERM);
57     }
58 
getPackagesWithManageNotifications()59     public static Set<String> getPackagesWithManageNotifications() {
60         final String[] PERM = {
61                 android.Manifest.permission.MANAGE_NOTIFICATIONS
62         };
63         return getPackagesWithPermissions(PERM);
64     }
65 
getPackagesWithPermissions(String[] permList)66     public static Set<String> getPackagesWithPermissions(String[] permList) {
67         final ArraySet<String> requestingPackages = new ArraySet<>();
68         try {
69             final ParceledListSlice list = AppGlobals.getPackageManager()
70                     .getPackagesHoldingPermissions(permList, 0 /*flags*/,
71                             ActivityManager.getCurrentUser());
72             final List<PackageInfo> pkgs = list.getList();
73             if (pkgs != null) {
74                 for (PackageInfo info : pkgs) {
75                     if (info.applicationInfo.enabled) {
76                         requestingPackages.add(info.packageName);
77                     }
78                 }
79             }
80         } catch (RemoteException e) {
81             Log.e(TAG, "Cannot reach packagemanager", e);
82         }
83         return requestingPackages;
84     }
85 
getAutoApprovedPackages(Context context)86     public static Set<String> getAutoApprovedPackages(Context context) {
87         final Set<String> autoApproved = new ArraySet<>();
88         autoApproved.addAll(context.getSystemService(NotificationManager.class)
89                 .getEnabledNotificationListenerPackages());
90         return autoApproved;
91     }
92 
hasAccess(Context context, String pkg)93     public static boolean hasAccess(Context context, String pkg) {
94         return context.getSystemService(
95                 NotificationManager.class).isNotificationPolicyAccessGrantedForPackage(pkg);
96     }
97 
setAccess(final Context context, final String pkg, final boolean access)98     public static void setAccess(final Context context, final String pkg, final boolean access) {
99         logSpecialPermissionChange(access, pkg, context);
100         AsyncTask.execute(() -> {
101             final NotificationManager mgr = context.getSystemService(NotificationManager.class);
102             mgr.setNotificationPolicyAccessGranted(pkg, access);
103         });
104     }
105 
deleteRules(final Context context, final String pkg)106     public static void deleteRules(final Context context, final String pkg) {
107         AsyncTask.execute(() -> {
108             final NotificationManager mgr = context.getSystemService(NotificationManager.class);
109             mgr.removeAutomaticZenRules(pkg);
110         });
111     }
112 
113     @VisibleForTesting
logSpecialPermissionChange(boolean enable, String packageName, Context context)114     static void logSpecialPermissionChange(boolean enable, String packageName, Context context) {
115         int logCategory = enable ? SettingsEnums.APP_SPECIAL_PERMISSION_DND_ALLOW
116                 : SettingsEnums.APP_SPECIAL_PERMISSION_DND_DENY;
117         FeatureFactory.getFactory(context).getMetricsFeatureProvider().action(context,
118                 logCategory, packageName);
119     }
120 }
121