1 /* 2 * Copyright (C) 2022 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 package com.android.settings.applications.specialaccess.turnscreenon; 17 18 import static android.app.AppOpsManager.MODE_ALLOWED; 19 import static android.app.AppOpsManager.MODE_ERRORED; 20 import static android.app.AppOpsManager.OP_TURN_SCREEN_ON; 21 22 import android.app.AppOpsManager; 23 import android.app.settings.SettingsEnums; 24 import android.os.Bundle; 25 26 import androidx.appcompat.app.AlertDialog; 27 import androidx.preference.Preference; 28 import androidx.preference.Preference.OnPreferenceChangeListener; 29 import androidx.preference.SwitchPreference; 30 31 import com.android.settings.R; 32 import com.android.settings.applications.AppInfoWithHeader; 33 34 /** 35 * Detail page for turn screen on special app access. 36 */ 37 public class TurnScreenOnDetails extends AppInfoWithHeader 38 implements OnPreferenceChangeListener { 39 40 private static final String KEY_APP_OPS_SETTINGS_SWITCH = "app_ops_settings_switch"; 41 42 private SwitchPreference mSwitchPref; 43 private AppOpsManager mAppOpsManager; 44 45 @Override onCreate(Bundle savedInstanceState)46 public void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 49 mAppOpsManager = this.getSystemService(AppOpsManager.class); 50 51 // find preferences 52 addPreferencesFromResource(R.xml.turn_screen_on_permissions_details); 53 mSwitchPref = (SwitchPreference) findPreference(KEY_APP_OPS_SETTINGS_SWITCH); 54 55 // set title/summary for all of them 56 mSwitchPref.setTitle(R.string.allow_turn_screen_on); 57 58 // install event listeners 59 mSwitchPref.setOnPreferenceChangeListener(this); 60 } 61 62 @Override onPreferenceChange(Preference preference, Object newValue)63 public boolean onPreferenceChange(Preference preference, Object newValue) { 64 if (preference == mSwitchPref) { 65 setTurnScreenOnAppOp(mPackageInfo.applicationInfo.uid, mPackageName, 66 (Boolean) newValue); 67 return true; 68 } 69 return false; 70 } 71 72 @Override refreshUi()73 protected boolean refreshUi() { 74 boolean isAllowed = isTurnScreenOnAllowed(mAppOpsManager, 75 mPackageInfo.applicationInfo.uid, mPackageName); 76 mSwitchPref.setChecked(isAllowed); 77 return true; 78 } 79 80 @Override createDialog(int id, int errorCode)81 protected AlertDialog createDialog(int id, int errorCode) { 82 return null; 83 } 84 85 @Override getMetricsCategory()86 public int getMetricsCategory() { 87 return SettingsEnums.SETTINGS_MANAGE_TURN_SCREEN_ON; 88 } 89 90 /** 91 * Sets whether the app associated with the given {@code packageName} is allowed to turn the 92 * screen on. 93 */ setTurnScreenOnAppOp(int uid, String packageName, boolean value)94 void setTurnScreenOnAppOp(int uid, String packageName, boolean value) { 95 final int newMode = value ? MODE_ALLOWED : MODE_ERRORED; 96 mAppOpsManager.setMode(OP_TURN_SCREEN_ON, uid, packageName, newMode); 97 } 98 99 /** 100 * @return whether the app associated with the given {@code packageName} is allowed to turn the 101 * screen on. 102 */ isTurnScreenOnAllowed(AppOpsManager appOpsManager, int uid, String packageName)103 static boolean isTurnScreenOnAllowed(AppOpsManager appOpsManager, int uid, String packageName) { 104 return appOpsManager.checkOpNoThrow(OP_TURN_SCREEN_ON, uid, packageName) == MODE_ALLOWED; 105 } 106 107 /** 108 * @return the summary for the current state of whether the app associated with the given 109 * packageName is allowed to turn the screen on. 110 */ getPreferenceSummary(AppOpsManager appOpsManager, int uid, String packageName)111 public static int getPreferenceSummary(AppOpsManager appOpsManager, int uid, 112 String packageName) { 113 final boolean enabled = TurnScreenOnDetails.isTurnScreenOnAllowed(appOpsManager, uid, 114 packageName); 115 return enabled ? R.string.app_permission_summary_allowed 116 : R.string.app_permission_summary_not_allowed; 117 } 118 } 119