1 /* 2 * Copyright (C) 2021 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.tv.settings.device.apps.specialaccess; 18 19 import android.Manifest; 20 import android.app.AppOpsManager; 21 import android.app.tvsettings.TvSettingsEnums; 22 import android.os.Bundle; 23 24 import androidx.annotation.Keep; 25 import androidx.annotation.NonNull; 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceGroup; 28 import androidx.preference.SwitchPreference; 29 import androidx.preference.TwoStatePreference; 30 31 import com.android.settingslib.applications.ApplicationsState; 32 import com.android.tv.settings.R; 33 34 /** 35 * Settings screen for managing "Alarms & Reminders" permission 36 */ 37 @Keep 38 public class AlarmsAndReminders extends ManageAppOp { 39 40 private AppOpsManager mAppOpsManager; 41 42 @Override onCreate(Bundle savedInstanceState)43 public void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 mAppOpsManager = getContext().getSystemService(AppOpsManager.class); 46 } 47 48 @Override getAppOpsOpCode()49 public int getAppOpsOpCode() { 50 return AppOpsManager.OP_SCHEDULE_EXACT_ALARM; 51 } 52 53 @Override getPermission()54 public String getPermission() { 55 return Manifest.permission.SCHEDULE_EXACT_ALARM; 56 } 57 58 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)59 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 60 setPreferencesFromResource(R.xml.alarms_and_reminders, null); 61 } 62 63 @NonNull 64 @Override bindPreference(@onNull Preference preference, ApplicationsState.AppEntry entry)65 public Preference bindPreference(@NonNull Preference preference, 66 ApplicationsState.AppEntry entry) { 67 final TwoStatePreference switchPref = (SwitchPreference) preference; 68 switchPref.setTitle(entry.label); 69 switchPref.setKey(entry.info.packageName); 70 switchPref.setIcon(entry.icon); 71 switchPref.setOnPreferenceChangeListener((pref, newValue) -> { 72 setAlarmsAndRemindersAccess(entry, (Boolean) newValue); 73 return true; 74 }); 75 76 switchPref.setSummary(getPreferenceSummary(entry)); 77 switchPref.setChecked(((PermissionState) entry.extraInfo).isAllowed()); 78 79 return switchPref; 80 } 81 82 @NonNull 83 @Override createAppPreference()84 public Preference createAppPreference() { 85 return new SwitchPreference(getPreferenceManager().getContext()); 86 } 87 88 @NonNull 89 @Override getAppPreferenceGroup()90 public PreferenceGroup getAppPreferenceGroup() { 91 return getPreferenceScreen(); 92 } 93 setAlarmsAndRemindersAccess(ApplicationsState.AppEntry entry, boolean grant)94 private void setAlarmsAndRemindersAccess(ApplicationsState.AppEntry entry, boolean grant) { 95 mAppOpsManager.setMode(getAppOpsOpCode(), 96 entry.info.uid, entry.info.packageName, 97 grant ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_ERRORED); 98 updateAppList(); 99 } 100 getPreferenceSummary(ApplicationsState.AppEntry entry)101 private CharSequence getPreferenceSummary(ApplicationsState.AppEntry entry) { 102 if (entry.extraInfo instanceof PermissionState) { 103 return getContext().getText(((PermissionState) entry.extraInfo).isAllowed() 104 ? R.string.app_permission_summary_allowed 105 : R.string.app_permission_summary_not_allowed); 106 } else { 107 return null; 108 } 109 } 110 111 @Override getPageId()112 protected int getPageId() { 113 return TvSettingsEnums.APPS_SPECIAL_APP_ACCESS_ALARMS_AND_REMINDERS; 114 } 115 } 116