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