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 this app didn't declare this permission in their manifest, don't bother showing UI.
54         final Set<String> needAccessApps =
55                 ZenAccessController.getPackagesRequestingNotificationPolicyAccess();
56         if (needAccessApps.contains(mPackageName)) {
57             updatePreference(context, findPreference(SWITCH_PREF_KEY));
58         } else {
59             finish();
60         }
61         return true;
62     }
63 
64     @Override
createDialog(int id, int errorCode)65     protected AlertDialog createDialog(int id, int errorCode) {
66         return null;
67     }
68 
updatePreference(Context context, SwitchPreference preference)69     public void updatePreference(Context context, SwitchPreference preference) {
70         final CharSequence label = mPackageInfo.applicationInfo.loadLabel(mPm);
71         final Set<String> autoApproved = ZenAccessController.getAutoApprovedPackages(context);
72         if (autoApproved.contains(mPackageName)) {
73             //Auto approved, user cannot do anything. Hard code summary and disable preference.
74             preference.setEnabled(false);
75             preference.setSummary(getString(R.string.zen_access_disabled_package_warning));
76             return;
77         }
78         preference.setChecked(ZenAccessController.hasAccess(context, mPackageName));
79         preference.setOnPreferenceChangeListener((p, newValue) -> {
80             final boolean access = (Boolean) newValue;
81             if (access) {
82                 new ScaryWarningDialogFragment()
83                         .setPkgInfo(mPackageName, label, ZenAccessDetails.this)
84                         .show(getFragmentManager(), "dialog");
85             } else {
86                 new FriendlyWarningDialogFragment()
87                         .setPkgInfo(mPackageName, label, ZenAccessDetails.this)
88                         .show(getFragmentManager(), "dialog");
89             }
90             return false;
91         });
92     }
93 
94     @Override
onZenAccessPolicyChanged()95     public void onZenAccessPolicyChanged() {
96         refreshUi();
97     }
98 }
99