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