• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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.os.UserHandle;
29 import android.os.UserManager;
30 import android.util.ArraySet;
31 import android.view.View;
32 import android.util.Log;
33 
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.settingslib.search.SearchIndexable;
44 import com.android.settingslib.widget.AppPreference;
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(R.string.zen_access_empty_text);
80     }
81 
82     @Override
getPreferenceScreenResId()83     protected int getPreferenceScreenResId() {
84         return R.xml.zen_access_settings;
85     }
86 
87     @Override
onResume()88     public void onResume() {
89         super.onResume();
90         reloadList();
91     }
92 
93     @Override
onZenAccessPolicyChanged()94     public void onZenAccessPolicyChanged() {
95         reloadList();
96     }
97 
reloadList()98     private void reloadList() {
99         if (mContext.getSystemService(UserManager.class)
100                 .isManagedProfile(UserHandle.myUserId())) {
101             Log.w(TAG, "DND access cannot be enabled in a work profile");
102             return;
103         }
104         final PreferenceScreen screen = getPreferenceScreen();
105         screen.removeAll();
106         final ArrayList<ApplicationInfo> apps = new ArrayList<>();
107         final Set<String> requesting =
108                 ZenAccessController.getPackagesRequestingNotificationPolicyAccess();
109         if (!requesting.isEmpty()) {
110             final List<ApplicationInfo> installed = mPkgMan.getInstalledApplications(0);
111             if (installed != null) {
112                 for (ApplicationInfo app : installed) {
113                     if (requesting.contains(app.packageName)) {
114                         apps.add(app);
115                     }
116                 }
117             }
118         }
119         ArraySet<String> autoApproved = new ArraySet<>();
120         autoApproved.addAll(mNoMan.getEnabledNotificationListenerPackages());
121         autoApproved.addAll(ZenAccessController.getPackagesWithManageNotifications());
122         Collections.sort(apps, new PackageItemInfo.DisplayNameComparator(mPkgMan));
123         for (ApplicationInfo app : apps) {
124             final String pkg = app.packageName;
125             final CharSequence label = app.loadLabel(mPkgMan);
126             final AppPreference pref = new AppPreference(getPrefContext());
127             pref.setKey(pkg);
128             pref.setIcon(app.loadIcon(mPkgMan));
129             pref.setTitle(label);
130             if (autoApproved.contains(pkg)) {
131                 //Auto approved, user cannot do anything. Hard code summary and disable preference.
132                 pref.setEnabled(false);
133                 pref.setSummary(getString(R.string.zen_access_disabled_package_warning));
134             } else {
135                 // Not auto approved, update summary according to notification backend.
136                 pref.setSummary(getPreferenceSummary(pkg));
137             }
138             pref.setOnPreferenceClickListener(preference -> {
139                 AppInfoBase.startAppInfoFragment(
140                         ZenAccessDetails.class  /* fragment */,
141                         R.string.manage_zen_access_title /* titleRes */,
142                         pkg,
143                         app.uid,
144                         this /* source */,
145                         -1 /* requestCode */,
146                         getMetricsCategory() /* sourceMetricsCategory */);
147                 return true;
148             });
149 
150             screen.addPreference(pref);
151         }
152     }
153 
154     /**
155      * @return the summary for the current state of whether the app associated with the given
156      * {@param packageName} is allowed to enter picture-in-picture.
157      */
getPreferenceSummary(String packageName)158     private int getPreferenceSummary(String packageName) {
159         final boolean enabled = ZenAccessController.hasAccess(getContext(), packageName);
160         return enabled ? R.string.app_permission_summary_allowed
161                 : R.string.app_permission_summary_not_allowed;
162     }
163 
164     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
165             new BaseSearchIndexProvider(R.xml.zen_access_settings);
166 }
167