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 43 private final ActivityManager mActivityManager; 44 ZenAccessController(Context context, String preferenceKey)45 public ZenAccessController(Context context, String preferenceKey) { 46 super(context, preferenceKey); 47 mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); 48 } 49 50 @Override getAvailabilityStatus()51 public int getAvailabilityStatus() { 52 return isSupported(mActivityManager) 53 ? AVAILABLE_UNSEARCHABLE 54 : UNSUPPORTED_ON_DEVICE; 55 } 56 isSupported(ActivityManager activityManager)57 public static boolean isSupported(ActivityManager activityManager) { 58 return !activityManager.isLowRamDevice(); 59 } 60 getPackagesRequestingNotificationPolicyAccess()61 public static Set<String> getPackagesRequestingNotificationPolicyAccess() { 62 final ArraySet<String> requestingPackages = new ArraySet<>(); 63 try { 64 final String[] PERM = { 65 android.Manifest.permission.ACCESS_NOTIFICATION_POLICY 66 }; 67 final ParceledListSlice list = AppGlobals.getPackageManager() 68 .getPackagesHoldingPermissions(PERM, 0 /*flags*/, 69 ActivityManager.getCurrentUser()); 70 final List<PackageInfo> pkgs = list.getList(); 71 if (pkgs != null) { 72 for (PackageInfo info : pkgs) { 73 requestingPackages.add(info.packageName); 74 } 75 } 76 } catch (RemoteException e) { 77 Log.e(TAG, "Cannot reach packagemanager", e); 78 } 79 return requestingPackages; 80 } 81 getAutoApprovedPackages(Context context)82 public static Set<String> getAutoApprovedPackages(Context context) { 83 final Set<String> autoApproved = new ArraySet<>(); 84 autoApproved.addAll(context.getSystemService(NotificationManager.class) 85 .getEnabledNotificationListenerPackages()); 86 return autoApproved; 87 } 88 hasAccess(Context context, String pkg)89 public static boolean hasAccess(Context context, String pkg) { 90 return context.getSystemService( 91 NotificationManager.class).isNotificationPolicyAccessGrantedForPackage(pkg); 92 } 93 setAccess(final Context context, final String pkg, final boolean access)94 public static void setAccess(final Context context, final String pkg, final boolean access) { 95 logSpecialPermissionChange(access, pkg, context); 96 AsyncTask.execute(() -> { 97 final NotificationManager mgr = context.getSystemService(NotificationManager.class); 98 mgr.setNotificationPolicyAccessGranted(pkg, access); 99 }); 100 } 101 deleteRules(final Context context, final String pkg)102 public static void deleteRules(final Context context, final String pkg) { 103 AsyncTask.execute(() -> { 104 final NotificationManager mgr = context.getSystemService(NotificationManager.class); 105 mgr.removeAutomaticZenRules(pkg); 106 }); 107 } 108 109 @VisibleForTesting logSpecialPermissionChange(boolean enable, String packageName, Context context)110 static void logSpecialPermissionChange(boolean enable, String packageName, Context context) { 111 int logCategory = enable ? SettingsEnums.APP_SPECIAL_PERMISSION_DND_ALLOW 112 : SettingsEnums.APP_SPECIAL_PERMISSION_DND_DENY; 113 FeatureFactory.getFactory(context).getMetricsFeatureProvider().action(context, 114 logCategory, packageName); 115 } 116 } 117