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.util.Log;
20 
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
23 
24 public class AppStandbyUtils {
25     private static final String TAG = "CtsAppStandbyUtils";
26 
27     /**
28      * Returns if app standby is enabled.
29      *
30      * @return true if enabled; or false if disabled.
31      */
isAppStandbyEnabled()32     public static boolean isAppStandbyEnabled() {
33         final String result = SystemUtil.runShellCommand(
34                 "dumpsys usagestats is-app-standby-enabled").trim();
35         return Boolean.parseBoolean(result);
36     }
37 
38     /**
39      * Sets enabled state for app standby feature for runtime switch.
40      *
41      * App standby feature has 2 switches. This one affects the switch at runtime. If the build
42      * switch is off, enabling the runtime switch will not enable App standby.
43      *
44      * @param enabled if App standby is enabled.
45      */
setAppStandbyEnabledAtRuntime(boolean enabled)46     public static void setAppStandbyEnabledAtRuntime(boolean enabled) {
47         final String value = enabled ? "1" : "0";
48         Log.d(TAG, "Setting AppStandby " + (enabled ? "enabled" : "disabled") + " at runtime.");
49         SettingsUtils.putGlobalSetting("app_standby_enabled", value);
50     }
51 
52     /**
53      * Returns if app standby is enabled at runtime. Note {@link #isAppStandbyEnabled()} may still
54      * return {@code false} if this method returns {@code true}, because app standby can be disabled
55      * at build time as well.
56      *
57      * @return true if enabled at runtime; or false if disabled at runtime.
58      */
isAppStandbyEnabledAtRuntime()59     public static boolean isAppStandbyEnabledAtRuntime() {
60         final String result =
61                 SystemUtil.runShellCommand("settings get global app_standby_enabled").trim();
62         final boolean boolResult = result.equals("1");
63         Log.d(TAG, "AppStandby is " + (boolResult ? "enabled" : "disabled") + " at runtime.");
64         return boolResult;
65     }
66 }
67