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 return false; 58 } 59 updatePreference(context, findPreference(SWITCH_PREF_KEY)); 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, SwitchPreference preference)68 public void updatePreference(Context context, SwitchPreference 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