1 /*
2  * Copyright (C) 2023 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.settingslib.fuelgauge;
18 
19 import android.accessibilityservice.AccessibilityServiceInfo;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.os.SystemProperties;
25 import android.os.UserManager;
26 import android.provider.Settings;
27 import android.util.ArraySet;
28 import android.view.accessibility.AccessibilityManager;
29 
30 import androidx.annotation.Nullable;
31 import androidx.annotation.VisibleForTesting;
32 
33 import java.util.List;
34 
35 public final class BatteryUtils {
36 
37     /** The key to get the time to full from Settings.Global */
38     public static final String GLOBAL_TIME_TO_FULL_MILLIS = "time_to_full_millis";
39 
40     /** The system property key to check whether the charging string v2 is enabled or not. */
41     public static final String PROPERTY_CHARGING_STRING_V2_KEY = "charging_string.apply_v2";
42 
43     /** Gets the latest sticky battery intent from the Android system. */
getBatteryIntent(Context context)44     public static Intent getBatteryIntent(Context context) {
45         return context.registerReceiver(
46                 /*receiver=*/ null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
47     }
48 
49     /** Gets the current active accessibility related packages. */
getA11yPackageNames(Context context)50     public static ArraySet<String> getA11yPackageNames(Context context) {
51         context = context.getApplicationContext();
52         final ArraySet<String> packageNames = new ArraySet<>();
53         final String defaultTtsPackageName = Settings.Secure.getString(
54                 context.getContentResolver(), Settings.Secure.TTS_DEFAULT_SYNTH);
55         if (defaultTtsPackageName != null) {
56             packageNames.add(defaultTtsPackageName);
57         }
58         // Checks the current active packages.
59         final AccessibilityManager accessibilityManager =
60                 context.getSystemService(AccessibilityManager.class);
61         if (!accessibilityManager.isEnabled()) {
62             return packageNames;
63         }
64         final List<AccessibilityServiceInfo> serviceInfoList =
65                 accessibilityManager.getEnabledAccessibilityServiceList(
66                         AccessibilityServiceInfo.FEEDBACK_ALL_MASK);
67         if (serviceInfoList == null || serviceInfoList.isEmpty()) {
68             return packageNames;
69         }
70         for (AccessibilityServiceInfo serviceInfo : serviceInfoList) {
71             final ComponentName serviceComponent = ComponentName.unflattenFromString(
72                     serviceInfo.getId());
73             if (serviceComponent != null) {
74                 packageNames.add(serviceComponent.getPackageName());
75             }
76         }
77         return packageNames;
78     }
79 
80     /** Returns true if current user is a work profile user. */
isWorkProfile(Context context)81     public static boolean isWorkProfile(Context context) {
82         final UserManager userManager = context.getSystemService(UserManager.class);
83         return userManager.isManagedProfile() && !userManager.isSystemUser();
84     }
85 
86     private static Boolean sChargingStringV2Enabled = null;
87 
88     /** Returns {@code true} if the charging string v2 is enabled. */
isChargingStringV2Enabled()89     public static boolean isChargingStringV2Enabled() {
90         if (sChargingStringV2Enabled == null) {
91             sChargingStringV2Enabled =
92                     SystemProperties.getBoolean(PROPERTY_CHARGING_STRING_V2_KEY, false);
93         }
94         return sChargingStringV2Enabled;
95     }
96 
97 
98     /** Used to override the system property to enable or reset for charging string V2. */
99     @VisibleForTesting
setChargingStringV2Enabled(Boolean enabled)100     public static void setChargingStringV2Enabled(Boolean enabled) {
101         setChargingStringV2Enabled(enabled, true /* updateProperty */);
102     }
103 
104     /** Used to override the system property to enable or reset for charging string V2. */
105     @VisibleForTesting
setChargingStringV2Enabled( @ullable Boolean enabled, boolean updateProperty)106     public static void setChargingStringV2Enabled(
107             @Nullable Boolean enabled, boolean updateProperty) {
108         if (updateProperty) {
109             SystemProperties.set(
110                     BatteryUtils.PROPERTY_CHARGING_STRING_V2_KEY,
111                     enabled == null ? "" : String.valueOf(enabled));
112         }
113         BatteryUtils.sChargingStringV2Enabled = enabled;
114     }
115 }
116