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