1 /* 2 * Copyright (C) 2015 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.notification.zen; 18 19 import android.app.NotificationManager; 20 import android.app.settings.SettingsEnums; 21 import android.content.Context; 22 import android.content.pm.ApplicationInfo; 23 import android.content.pm.PackageItemInfo; 24 import android.content.pm.PackageManager; 25 import android.os.Bundle; 26 import android.os.UserHandle; 27 import android.os.UserManager; 28 import android.util.ArraySet; 29 import android.util.Log; 30 import android.view.View; 31 32 import androidx.annotation.Nullable; 33 import androidx.preference.PreferenceScreen; 34 35 import com.android.settings.R; 36 import com.android.settings.applications.AppInfoBase; 37 import com.android.settings.applications.specialaccess.zenaccess.ZenAccessController; 38 import com.android.settings.applications.specialaccess.zenaccess.ZenAccessDetails; 39 import com.android.settings.applications.specialaccess.zenaccess.ZenAccessSettingObserverMixin; 40 import com.android.settings.search.BaseSearchIndexProvider; 41 import com.android.settings.widget.EmptyTextSettings; 42 import com.android.settings.widget.RestrictedAppPreference; 43 import com.android.settingslib.search.SearchIndexable; 44 45 import java.util.ArrayList; 46 import java.util.Collections; 47 import java.util.List; 48 import java.util.Set; 49 50 @SearchIndexable 51 public class ZenAccessSettings extends EmptyTextSettings implements 52 ZenAccessSettingObserverMixin.Listener { 53 private final String TAG = "ZenAccessSettings"; 54 55 private Context mContext; 56 private PackageManager mPkgMan; 57 private NotificationManager mNoMan; 58 59 @Override getMetricsCategory()60 public int getMetricsCategory() { 61 return SettingsEnums.NOTIFICATION_ZEN_MODE_ACCESS; 62 } 63 64 @Override onCreate(Bundle icicle)65 public void onCreate(Bundle icicle) { 66 super.onCreate(icicle); 67 68 mContext = getActivity(); 69 mPkgMan = mContext.getPackageManager(); 70 mNoMan = mContext.getSystemService(NotificationManager.class); 71 getSettingsLifecycle().addObserver( 72 new ZenAccessSettingObserverMixin(getContext(), this /* listener */)); 73 } 74 75 @Override onViewCreated(View view, @Nullable Bundle savedInstanceState)76 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 77 super.onViewCreated(view, savedInstanceState); 78 setEmptyText(R.string.zen_access_empty_text); 79 } 80 81 @Override getPreferenceScreenResId()82 protected int getPreferenceScreenResId() { 83 return R.xml.zen_access_settings; 84 } 85 86 @Override onResume()87 public void onResume() { 88 super.onResume(); 89 reloadList(); 90 } 91 92 @Override onZenAccessPolicyChanged()93 public void onZenAccessPolicyChanged() { 94 reloadList(); 95 } 96 reloadList()97 private void reloadList() { 98 if (mContext.getSystemService(UserManager.class) 99 .isManagedProfile(UserHandle.myUserId())) { 100 Log.w(TAG, "DND access cannot be enabled in a work profile"); 101 return; 102 } 103 final PreferenceScreen screen = getPreferenceScreen(); 104 screen.removeAll(); 105 final ArrayList<ApplicationInfo> apps = new ArrayList<>(); 106 final Set<String> requesting = 107 ZenAccessController.getPackagesRequestingNotificationPolicyAccess(); 108 if (!requesting.isEmpty()) { 109 final List<ApplicationInfo> installed = mPkgMan.getInstalledApplications(0); 110 if (installed != null) { 111 for (ApplicationInfo app : installed) { 112 if (requesting.contains(app.packageName)) { 113 apps.add(app); 114 } 115 } 116 } 117 } 118 ArraySet<String> autoApproved = new ArraySet<>(); 119 autoApproved.addAll(mNoMan.getEnabledNotificationListenerPackages()); 120 autoApproved.addAll(ZenAccessController.getPackagesWithManageNotifications()); 121 Collections.sort(apps, new PackageItemInfo.DisplayNameComparator(mPkgMan)); 122 for (ApplicationInfo app : apps) { 123 final String pkg = app.packageName; 124 final CharSequence label = app.loadLabel(mPkgMan); 125 final RestrictedAppPreference pref = new RestrictedAppPreference(getPrefContext()); 126 pref.setKey(pkg); 127 pref.setIcon(app.loadIcon(mPkgMan)); 128 pref.setTitle(label); 129 if (autoApproved.contains(pkg)) { 130 //Auto approved, user cannot do anything. Hard code summary and disable preference. 131 pref.setEnabled(false); 132 pref.setSummary(getString(R.string.zen_access_disabled_package_warning)); 133 } else { 134 // Not auto approved, update summary according to notification backend. 135 pref.setSummary(getPreferenceSummary(pkg)); 136 pref.checkEcmRestrictionAndSetDisabled( 137 android.Manifest.permission.MANAGE_NOTIFICATIONS, app.packageName); 138 } 139 pref.setOnPreferenceClickListener(preference -> { 140 AppInfoBase.startAppInfoFragment( 141 ZenAccessDetails.class /* fragment */, 142 getString(R.string.manage_zen_access_title) /* titleRes */, 143 pkg, 144 app.uid, 145 this /* source */, 146 -1 /* requestCode */, 147 getMetricsCategory() /* sourceMetricsCategory */); 148 return true; 149 }); 150 151 screen.addPreference(pref); 152 } 153 } 154 155 /** 156 * @return the summary for the current state of whether the app associated with the given 157 * {@param packageName} is allowed to enter picture-in-picture. 158 */ getPreferenceSummary(String packageName)159 private int getPreferenceSummary(String packageName) { 160 final boolean enabled = ZenAccessController.hasAccess(getContext(), packageName); 161 return enabled ? R.string.app_permission_summary_allowed 162 : R.string.app_permission_summary_not_allowed; 163 } 164 165 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 166 new BaseSearchIndexProvider(R.xml.zen_access_settings); 167 } 168