1 /* 2 * Copyright (C) 2018 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.compatibility.common.util; 17 18 import static com.android.compatibility.common.util.SettingsUtils.putGlobalSetting; 19 import static com.android.compatibility.common.util.TestUtils.waitUntil; 20 21 import android.os.BatteryManager; 22 import android.os.PowerManager; 23 import android.provider.Settings.Global; 24 import android.support.test.InstrumentationRegistry; 25 import android.util.Log; 26 27 public class BatteryUtils { 28 private static final String TAG = "CtsBatteryUtils"; 29 30 private BatteryUtils() { 31 } 32 33 public static BatteryManager getBatteryManager() { 34 return InstrumentationRegistry.getContext().getSystemService(BatteryManager.class); 35 } 36 37 public static PowerManager getPowerManager() { 38 return InstrumentationRegistry.getContext().getSystemService(PowerManager.class); 39 } 40 41 /** Make the target device think it's off charger. */ 42 public static void runDumpsysBatteryUnplug() throws Exception { 43 SystemUtil.runShellCommandForNoOutput("dumpsys battery unplug"); 44 45 Log.d(TAG, "Battery UNPLUGGED"); 46 } 47 48 /** Reset {@link #runDumpsysBatteryUnplug}. */ 49 public static void runDumpsysBatteryReset() throws Exception { 50 SystemUtil.runShellCommandForNoOutput(("dumpsys battery reset")); 51 52 Log.d(TAG, "Battery RESET"); 53 } 54 55 /** 56 * Enable / disable battery saver. Note {@link #runDumpsysBatteryUnplug} must have been 57 * executed before enabling BS. 58 */ 59 public static void enableBatterySaver(boolean enabled) throws Exception { 60 if (enabled) { 61 SystemUtil.runShellCommandForNoOutput("cmd power set-mode 1"); 62 putGlobalSetting(Global.LOW_POWER_MODE, "1"); 63 waitUntil("Battery saver still off", () -> getPowerManager().isPowerSaveMode()); 64 waitUntil("Location mode still " + getPowerManager().getLocationPowerSaveMode(), 65 () -> (PowerManager.LOCATION_MODE_NO_CHANGE 66 != getPowerManager().getLocationPowerSaveMode())); 67 68 Thread.sleep(500); 69 waitUntil("Force all apps standby still off", 70 () -> SystemUtil.runShellCommand("dumpsys alarm") 71 .contains(" Force all apps standby: true\n")); 72 73 } else { 74 SystemUtil.runShellCommandForNoOutput("cmd power set-mode 0"); 75 putGlobalSetting(Global.LOW_POWER_MODE_STICKY, "0"); 76 waitUntil("Battery saver still on", () -> !getPowerManager().isPowerSaveMode()); 77 waitUntil("Location mode still " + getPowerManager().getLocationPowerSaveMode(), 78 () -> (PowerManager.LOCATION_MODE_NO_CHANGE 79 == getPowerManager().getLocationPowerSaveMode())); 80 81 Thread.sleep(500); 82 waitUntil("Force all apps standby still on", 83 () -> SystemUtil.runShellCommand("dumpsys alarm") 84 .contains(" Force all apps standby: false\n")); 85 } 86 87 AmUtils.waitForBroadcastIdle(); 88 Log.d(TAG, "Battery saver turned " + (enabled ? "ON" : "OFF")); 89 } 90 91 /** 92 * Turn on/off screen. 93 */ 94 public static void turnOnScreen(boolean on) throws Exception { 95 if (on) { 96 SystemUtil.runShellCommandForNoOutput("input keyevent KEYCODE_WAKEUP"); 97 waitUntil("Device still not interactive", () -> getPowerManager().isInteractive()); 98 99 } else { 100 SystemUtil.runShellCommandForNoOutput("input keyevent KEYCODE_SLEEP"); 101 waitUntil("Device still interactive", () -> !getPowerManager().isInteractive()); 102 } 103 AmUtils.waitForBroadcastIdle(); 104 Log.d(TAG, "Screen turned " + (on ? "ON" : "OFF")); 105 } 106 } 107