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