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