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     public boolean mResumed = false;
38     private DeviceStateManager mDeviceStateManager;
39 
40     @Override
onCreate(Bundle savedInstanceState)41     protected void onCreate(Bundle savedInstanceState) {
42         super.onCreate(savedInstanceState);
43         mDeviceStateManager = getSystemService(DeviceStateManager.class);
44     }
45 
requestDeviceStateChange(int state)46     public void requestDeviceStateChange(int state) {
47         DeviceStateRequest request = DeviceStateRequest.newBuilder(state).build();
48         try {
49             requestStateFailed = false;
50             mDeviceStateManager.requestState(request, null, null);
51         } catch (SecurityException e) {
52             requestStateFailed = true;
53         }
54     }
55 
cancelOverriddenState()56     public void cancelOverriddenState() {
57         try {
58             cancelStateRequestFailed = false;
59             mDeviceStateManager.cancelStateRequest();
60         } catch (SecurityException e) {
61             cancelStateRequestFailed = true;
62         }
63     }
64 
65     @Override
onPause()66     protected void onPause() {
67         super.onPause();
68         mResumed = false;
69     }
70 
71     @Override
onResume()72     protected void onResume() {
73         super.onResume();
74         mResumed = true;
75     }
76 }
77