1 /* 2 * Copyright (C) 2014 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.cts.verifier.sensors.helpers; 18 19 import com.android.cts.verifier.R; 20 import com.android.cts.verifier.sensors.base.ISensorTestStateContainer; 21 22 import android.content.ContentResolver; 23 import android.content.pm.PackageManager; 24 import android.os.Build; 25 import android.provider.Settings; 26 27 import java.lang.reflect.Field; 28 29 /** 30 * A helper class that provides a mechanism to: 31 * - prompt users to activate/deactivate features that are known to register for sensor data. 32 * - turn on/off certain components of the device on behalf of the test (described as 'runtime 33 * features') 34 * - keep track of the initial state for each sensor feature, so it can be restored at will 35 */ 36 public class SensorFeaturesDeactivator { 37 38 private final ISensorTestStateContainer mStateContainer; 39 40 private final SensorSettingContainer mAirplaneMode = new AirplaneModeSettingContainer(); 41 private final SensorSettingContainer mScreenBrightnessMode = 42 new ScreenBrightnessModeSettingContainer(); 43 private final SensorSettingContainer mAmbientDisplayMode = new AmbientDisplaySettingContainer(); 44 private final SensorSettingContainer mAutoRotateScreenMode = 45 new AutoRotateScreenModeSettingContainer(); 46 private final SensorSettingContainer mKeepScreenOnMode = new KeepScreenOnModeSettingContainer(); 47 private final SensorSettingContainer mLocationMode = new LocationModeSettingContainer(); 48 SensorFeaturesDeactivator(ISensorTestStateContainer stateContainer)49 public SensorFeaturesDeactivator(ISensorTestStateContainer stateContainer) { 50 mStateContainer = stateContainer; 51 } 52 requestDeactivationOfFeatures()53 public synchronized void requestDeactivationOfFeatures() throws InterruptedException { 54 captureInitialState(); 55 56 mAirplaneMode.requestToSetMode(mStateContainer, true); 57 mScreenBrightnessMode.requestToSetMode(mStateContainer, false); 58 mAmbientDisplayMode.requestToSetMode(mStateContainer, false); 59 mAutoRotateScreenMode.requestToSetMode(mStateContainer, false); 60 mKeepScreenOnMode.requestToSetMode(mStateContainer, false); 61 mLocationMode.requestToSetMode(mStateContainer, false); 62 63 // TODO: find a way to find out if there are clients still registered at this time 64 mStateContainer.getTestLogger() 65 .logInstructions(R.string.snsr_sensor_feature_deactivation); 66 mStateContainer.waitForUserToContinue(); 67 } 68 requestToSetLocationMode(boolean state)69 public synchronized void requestToSetLocationMode(boolean state) throws InterruptedException { 70 mLocationMode.captureInitialState(); 71 mLocationMode.requestToSetMode(mStateContainer, state); 72 } 73 requestToRestoreFeatures()74 public synchronized void requestToRestoreFeatures() throws InterruptedException { 75 if (Thread.currentThread().isInterrupted()) { 76 // TODO: in the future, if the thread is interrupted, we might need to serialize the 77 // intermediate state we acquired so we can restore when we have a chance 78 return; 79 } 80 81 mAirplaneMode.requestToResetMode(mStateContainer); 82 mScreenBrightnessMode.requestToResetMode(mStateContainer); 83 mAmbientDisplayMode.requestToResetMode(mStateContainer); 84 mAutoRotateScreenMode.requestToResetMode(mStateContainer); 85 mKeepScreenOnMode.requestToResetMode(mStateContainer); 86 mLocationMode.requestToResetMode(mStateContainer); 87 } 88 captureInitialState()89 private void captureInitialState() { 90 mAirplaneMode.captureInitialState(); 91 mScreenBrightnessMode.captureInitialState(); 92 mAmbientDisplayMode.captureInitialState(); 93 mAutoRotateScreenMode.captureInitialState(); 94 mLocationMode.captureInitialState(); 95 mKeepScreenOnMode.captureInitialState(); 96 } 97 98 private class AirplaneModeSettingContainer extends SensorSettingContainer { AirplaneModeSettingContainer()99 public AirplaneModeSettingContainer() { 100 super(Settings.ACTION_AIRPLANE_MODE_SETTINGS, R.string.snsr_setting_airplane_mode); 101 } 102 103 @Override getSettingMode(int defaultValue)104 protected int getSettingMode(int defaultValue) { 105 ContentResolver contentResolver = mStateContainer.getContentResolver(); 106 // Settings.System.AIRPLANE_MODE_ON is deprecated in API 17 107 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { 108 return Settings.System 109 .getInt(contentResolver, Settings.System.AIRPLANE_MODE_ON, defaultValue); 110 } else { 111 return Settings.Global 112 .getInt(contentResolver, Settings.Global.AIRPLANE_MODE_ON, defaultValue); 113 } 114 } 115 116 @Override isSettingAvailable()117 protected boolean isSettingAvailable() { 118 // call base first, lean back UI device does not have airplane mode 119 return super.isSettingAvailable() 120 && !mStateContainer.hasSystemFeature(PackageManager.FEATURE_LEANBACK) 121 && !mStateContainer.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); 122 } 123 } 124 125 private class ScreenBrightnessModeSettingContainer extends SensorSettingContainer { ScreenBrightnessModeSettingContainer()126 public ScreenBrightnessModeSettingContainer() { 127 super(Settings.ACTION_DISPLAY_SETTINGS, R.string.snsr_setting_screen_brightness_mode); 128 } 129 130 @Override getSettingMode(int defaultValue)131 public int getSettingMode(int defaultValue) { 132 return Settings.System.getInt( 133 mStateContainer.getContentResolver(), 134 Settings.System.SCREEN_BRIGHTNESS_MODE, 135 defaultValue); 136 } 137 } 138 139 private class AmbientDisplaySettingContainer extends SensorSettingContainer { AmbientDisplaySettingContainer()140 public AmbientDisplaySettingContainer() { 141 super(Settings.ACTION_DISPLAY_SETTINGS, R.string.snsr_setting_ambient_display); 142 } 143 144 @Override getSettingMode(int defaultValue)145 protected int getSettingMode(int defaultValue) { 146 return Settings.Secure.getInt( 147 mStateContainer.getContentResolver(), 148 Settings.Secure.DOZE_ENABLED, 149 defaultValue); 150 } 151 } 152 153 private class AutoRotateScreenModeSettingContainer extends SensorSettingContainer { AutoRotateScreenModeSettingContainer()154 public AutoRotateScreenModeSettingContainer() { 155 super(Settings.ACTION_ACCESSIBILITY_SETTINGS, 156 R.string.snsr_setting_auto_rotate_screen_mode); 157 } 158 159 @Override getSettingMode(int defaultValue)160 protected int getSettingMode(int defaultValue) { 161 return Settings.System.getInt( 162 mStateContainer.getContentResolver(), 163 Settings.System.ACCELEROMETER_ROTATION, 164 defaultValue); 165 } 166 } 167 168 private class KeepScreenOnModeSettingContainer extends SensorSettingContainer { KeepScreenOnModeSettingContainer()169 public KeepScreenOnModeSettingContainer() { 170 super(Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS, 171 R.string.snsr_setting_keep_screen_on); 172 } 173 174 @Override getSettingMode(int defaultValue)175 protected int getSettingMode(int defaultValue) { 176 return Settings.Global.getInt( 177 mStateContainer.getContentResolver(), 178 Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 179 defaultValue); 180 } 181 182 @Override isSettingAvailable()183 protected boolean isSettingAvailable() { 184 // STAY_ON_WHILE_PLUGGED_In is not supported in Automotive Settings. 185 return super.isSettingAvailable() 186 && !mStateContainer.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); 187 } 188 } 189 190 private class LocationModeSettingContainer extends SensorSettingContainer { LocationModeSettingContainer()191 public LocationModeSettingContainer() { 192 super(Settings.ACTION_LOCATION_SOURCE_SETTINGS, R.string.snsr_setting_location_mode); 193 } 194 195 @Override getSettingMode(int defaultValue)196 protected int getSettingMode(int defaultValue) { 197 return Settings.Secure.getInt( 198 mStateContainer.getContentResolver(), 199 Settings.Secure.LOCATION_MODE, 200 defaultValue); 201 } 202 } 203 } 204