1 /* 2 * Copyright (C) 2019 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.car.developeroptions.notification; 18 19 import android.annotation.Nullable; 20 import android.app.ActivityManager; 21 import android.app.NotificationManager; 22 import android.app.settings.SettingsEnums; 23 import android.content.Context; 24 import android.content.pm.ApplicationInfo; 25 import android.content.pm.PackageItemInfo; 26 import android.content.pm.PackageManager; 27 import android.os.Bundle; 28 import android.provider.SearchIndexableResource; 29 import android.util.ArraySet; 30 import android.view.View; 31 32 import androidx.preference.PreferenceScreen; 33 34 import com.android.car.developeroptions.R; 35 import com.android.car.developeroptions.applications.AppInfoBase; 36 import com.android.car.developeroptions.applications.specialaccess.zenaccess.ZenAccessController; 37 import com.android.car.developeroptions.applications.specialaccess.zenaccess.ZenAccessDetails; 38 import com.android.car.developeroptions.applications.specialaccess.zenaccess.ZenAccessSettingObserverMixin; 39 import com.android.car.developeroptions.search.BaseSearchIndexProvider; 40 import com.android.car.developeroptions.widget.EmptyTextSettings; 41 import com.android.settingslib.search.Indexable; 42 import com.android.settingslib.search.SearchIndexable; 43 import com.android.settingslib.widget.apppreference.AppPreference; 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 final PreferenceScreen screen = getPreferenceScreen(); 99 screen.removeAll(); 100 final ArrayList<ApplicationInfo> apps = new ArrayList<>(); 101 final Set<String> requesting = 102 ZenAccessController.getPackagesRequestingNotificationPolicyAccess(); 103 if (!requesting.isEmpty()) { 104 final List<ApplicationInfo> installed = mPkgMan.getInstalledApplications(0); 105 if (installed != null) { 106 for (ApplicationInfo app : installed) { 107 if (requesting.contains(app.packageName)) { 108 apps.add(app); 109 } 110 } 111 } 112 } 113 ArraySet<String> autoApproved = new ArraySet<>(); 114 autoApproved.addAll(mNoMan.getEnabledNotificationListenerPackages()); 115 requesting.addAll(autoApproved); 116 Collections.sort(apps, new PackageItemInfo.DisplayNameComparator(mPkgMan)); 117 for (ApplicationInfo app : apps) { 118 final String pkg = app.packageName; 119 final CharSequence label = app.loadLabel(mPkgMan); 120 final AppPreference pref = new AppPreference(getPrefContext()); 121 pref.setKey(pkg); 122 pref.setIcon(app.loadIcon(mPkgMan)); 123 pref.setTitle(label); 124 if (autoApproved.contains(pkg)) { 125 //Auto approved, user cannot do anything. Hard code summary and disable preference. 126 pref.setEnabled(false); 127 pref.setSummary(getString(R.string.zen_access_disabled_package_warning)); 128 } else { 129 // Not auto approved, update summary according to notification backend. 130 pref.setSummary(getPreferenceSummary(pkg)); 131 } 132 pref.setOnPreferenceClickListener(preference -> { 133 AppInfoBase.startAppInfoFragment( 134 ZenAccessDetails.class /* fragment */, 135 R.string.manage_zen_access_title /* titleRes */, 136 pkg, 137 app.uid, 138 this /* source */, 139 -1 /* requestCode */, 140 getMetricsCategory() /* sourceMetricsCategory */); 141 return true; 142 }); 143 144 screen.addPreference(pref); 145 } 146 } 147 148 /** 149 * @return the summary for the current state of whether the app associated with the given 150 * {@param packageName} is allowed to enter picture-in-picture. 151 */ getPreferenceSummary(String packageName)152 private int getPreferenceSummary(String packageName) { 153 final boolean enabled = ZenAccessController.hasAccess(getContext(), packageName); 154 return enabled ? R.string.app_permission_summary_allowed 155 : R.string.app_permission_summary_not_allowed; 156 } 157 158 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 159 new BaseSearchIndexProvider() { 160 @Override 161 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context, 162 boolean enabled) { 163 final ArrayList<SearchIndexableResource> result = new ArrayList<>(); 164 165 final SearchIndexableResource sir = new SearchIndexableResource(context); 166 sir.xmlResId = R.xml.zen_access_settings; 167 result.add(sir); 168 return result; 169 } 170 }; 171 } 172