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 android.hardware.devicestate.cts; 18 19 import android.app.Activity; 20 import android.hardware.devicestate.DeviceStateManager; 21 import android.hardware.devicestate.DeviceStateRequest; 22 import android.os.Bundle; 23 24 /** 25 * This is an activity that can request device state changes via 26 * {@link DeviceStateManager#requestState} as well as cancel the active 27 * override request with {@link DeviceStateManager#cancelStateRequest}. 28 * 29 * @see {@link DeviceStateManagerTests#testRequestStateFailsAsBackgroundApp} 30 * @see {@link DeviceStateManagerTests#testRequestStateSucceedsAsTopApp} 31 * @see {@link DeviceStateManagerTests#testCancelOverrideRequestFromNewActivity} 32 */ 33 public class DeviceStateTestActivity extends Activity { 34 35 public boolean requestStateFailed = false; 36 public boolean cancelStateRequestFailed = false; 37 private DeviceStateManager mDeviceStateManager; 38 39 @Override onCreate(Bundle savedInstanceState)40 protected void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 mDeviceStateManager = getSystemService(DeviceStateManager.class); 43 } 44 requestDeviceStateChange(int state)45 public void requestDeviceStateChange(int state) { 46 DeviceStateRequest request = DeviceStateRequest.newBuilder(state).build(); 47 try { 48 requestStateFailed = false; 49 mDeviceStateManager.requestState(request, null, null); 50 } catch (SecurityException e) { 51 requestStateFailed = true; 52 } 53 } 54 cancelOverriddenState()55 public void cancelOverriddenState() { 56 try { 57 cancelStateRequestFailed = false; 58 mDeviceStateManager.cancelStateRequest(); 59 } catch (SecurityException e) { 60 cancelStateRequestFailed = true; 61 } 62 } 63 } 64