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