• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.applications.specialaccess.zenaccess;
18 
19 import android.app.ActivityManager;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 import android.os.Bundle;
23 
24 import androidx.appcompat.app.AlertDialog;
25 import androidx.preference.SwitchPreference;
26 
27 import com.android.settings.R;
28 import com.android.settings.applications.AppInfoWithHeader;
29 
30 import java.util.Set;
31 
32 public class ZenAccessDetails extends AppInfoWithHeader implements
33         ZenAccessSettingObserverMixin.Listener {
34 
35     private static final String SWITCH_PREF_KEY = "zen_access_switch";
36 
37     @Override
getMetricsCategory()38     public int getMetricsCategory() {
39         return SettingsEnums.ZEN_ACCESS_DETAIL;
40     }
41 
42     @Override
onCreate(Bundle savedInstanceState)43     public void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45         addPreferencesFromResource(R.xml.zen_access_permission_details);
46         getSettingsLifecycle().addObserver(
47                 new ZenAccessSettingObserverMixin(getContext(), this /* listener */));
48     }
49 
50     @Override
refreshUi()51     protected boolean refreshUi() {
52         final Context context = getContext();
53         if (!ZenAccessController.isSupported(context.getSystemService(ActivityManager.class))) {
54             return false;
55         }
56         // If this app didn't declare this permission in their manifest, don't bother showing UI.
57         final Set<String> needAccessApps =
58                 ZenAccessController.getPackagesRequestingNotificationPolicyAccess();
59         if (!needAccessApps.contains(mPackageName)) {
60             return false;
61         }
62         updatePreference(context, findPreference(SWITCH_PREF_KEY));
63         return true;
64     }
65 
66     @Override
createDialog(int id, int errorCode)67     protected AlertDialog createDialog(int id, int errorCode) {
68         return null;
69     }
70 
updatePreference(Context context, SwitchPreference preference)71     public void updatePreference(Context context, SwitchPreference preference) {
72         final CharSequence label = mPackageInfo.applicationInfo.loadLabel(mPm);
73         final Set<String> autoApproved = ZenAccessController.getAutoApprovedPackages(context);
74         if (autoApproved.contains(mPackageName)) {
75             //Auto approved, user cannot do anything. Hard code summary and disable preference.
76             preference.setEnabled(false);
77             preference.setSummary(getString(R.string.zen_access_disabled_package_warning));
78             return;
79         }
80         preference.setChecked(ZenAccessController.hasAccess(context, mPackageName));
81         preference.setOnPreferenceChangeListener((p, newValue) -> {
82             final boolean access = (Boolean) newValue;
83             if (access) {
84                 new ScaryWarningDialogFragment()
85                         .setPkgInfo(mPackageName, label)
86                         .show(getFragmentManager(), "dialog");
87             } else {
88                 new FriendlyWarningDialogFragment()
89                         .setPkgInfo(mPackageName, label)
90                         .show(getFragmentManager(), "dialog");
91             }
92             return false;
93         });
94     }
95 
96     @Override
onZenAccessPolicyChanged()97     public void onZenAccessPolicyChanged() {
98         refreshUi();
99     }
100 }
101