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