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 
17 package com.android.compatibility.common.util;
18 
19 import android.app.usage.UsageStatsManager;
20 import android.util.Log;
21 
22 import androidx.test.InstrumentationRegistry;
23 
24 public class AppStandbyUtils {
25     private static final String TAG = "CtsAppStandbyUtils";
26     private static final UsageStatsManager sUsageStatsManager = InstrumentationRegistry
27             .getTargetContext().getSystemService(UsageStatsManager.class);
28 
29     /**
30      * Returns if app standby is enabled.
31      *
32      * @return true if enabled; or false if disabled.
33      */
isAppStandbyEnabled()34     public static boolean isAppStandbyEnabled() {
35         final String result = SystemUtil.runShellCommand(
36                 "dumpsys usagestats is-app-standby-enabled").trim();
37         return Boolean.parseBoolean(result);
38     }
39 
40     /**
41      * Sets enabled state for app standby feature for runtime switch.
42      *
43      * App standby feature has 2 switches. This one affects the switch at runtime. If the build
44      * switch is off, enabling the runtime switch will not enable App standby.
45      *
46      * @param enabled if App standby is enabled.
47      */
setAppStandbyEnabledAtRuntime(boolean enabled)48     public static void setAppStandbyEnabledAtRuntime(boolean enabled) {
49         final String value = enabled ? "1" : "0";
50         Log.d(TAG, "Setting AppStandby " + (enabled ? "enabled" : "disabled") + " at runtime.");
51         SettingsUtils.putGlobalSetting("app_standby_enabled", value);
52     }
53 
54     /**
55      * Returns if app standby is enabled at runtime. Note {@link #isAppStandbyEnabled()} may still
56      * return {@code false} if this method returns {@code true}, because app standby can be disabled
57      * at build time as well.
58      *
59      * @return true if enabled at runtime; or false if disabled at runtime.
60      */
isAppStandbyEnabledAtRuntime()61     public static boolean isAppStandbyEnabledAtRuntime() {
62         final String result =
63                 SystemUtil.runShellCommand("settings get global app_standby_enabled").trim();
64         final boolean boolResult = result.equals("1") || result.equals("null");
65         Log.d(TAG, "AppStandby is " + (boolResult ? "enabled" : "disabled") + " at runtime.");
66         return boolResult;
67     }
68 
69     /** Returns the current standby-bucket of the package on the device */
getAppStandbyBucket(String packageName)70     public static int getAppStandbyBucket(String packageName) {
71         try {
72             return SystemUtil.callWithShellPermissionIdentity(
73                     () -> sUsageStatsManager.getAppStandbyBucket(packageName));
74         } catch (Exception e) {
75             throw new RuntimeException("Could not get standby-bucket for " + packageName, e);
76         }
77     }
78 }
79