1 /*
2  * Copyright 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.hdmicec.cts;
18 
19 import com.android.tradefed.device.ITestDevice;
20 
21 /**
22  * Helper class to help standby test acquire and release wakelock to avoid device going into deep
23  * sleep.
24  */
25 public final class WakeLockHelper {
26     private static final String TAG = "HdmiCecWakeLock";
27     private static final String ACQUIRE_LOCK = "android.hdmicec.app.ACQUIRE_LOCK";
28     private static final String RELEASE_LOCK = "android.hdmicec.app.RELEASE_LOCK";
29 
30     /** The package name of the APK. */
31     private static final String PACKAGE = "android.hdmicec.app";
32 
33     /** The class name of the wake lock activity in the APK. */
34     private static final String CLASS = "HdmiCecWakeLock";
35 
36     /** The command to launch the wake lock activity. */
37     private static final String START_COMMAND =
38             String.format("am start -n %s/%s.%s -a ", PACKAGE, PACKAGE, CLASS);
39 
40     /** The command to clear the wake lock activity. */
41     private static final String CLEAR_COMMAND = String.format("pm clear %s", PACKAGE);
42 
43     private static boolean mWakelockAcquired = false;
44 
acquirePartialWakeLock(ITestDevice device)45     public static void acquirePartialWakeLock(ITestDevice device) throws Exception {
46         if (mWakelockAcquired == false) {
47             // Clear activity if any.
48             device.executeShellCommand(CLEAR_COMMAND);
49             // Start the APK to acquire the wake lock and wait for it to complete.
50             device.executeShellCommand(START_COMMAND + ACQUIRE_LOCK);
51             LogHelper.assumeLog(device, TAG, "Acquired wake lock.");
52             mWakelockAcquired = true;
53         }
54     }
55 
releasePartialWakeLock(ITestDevice device)56     public static void releasePartialWakeLock(ITestDevice device) throws Exception {
57         if (mWakelockAcquired == true) {
58             // Release the acquired wake lock.
59             device.executeShellCommand(START_COMMAND + RELEASE_LOCK);
60             // Clear activity
61             device.executeShellCommand(CLEAR_COMMAND);
62             mWakelockAcquired = false;
63         }
64     }
65 }
66