• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.settings.fuelgauge;
18 
19 import static com.android.settings.fuelgauge.BatteryBroadcastReceiver.BatteryUpdateType.BATTERY_NOT_PRESENT;
20 
21 import android.content.Context;
22 import android.content.Intent;
23 
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.annotation.VisibleForTesting;
27 import androidx.lifecycle.Lifecycle;
28 import androidx.lifecycle.LifecycleEventObserver;
29 import androidx.lifecycle.LifecycleOwner;
30 import androidx.preference.PreferenceScreen;
31 
32 import com.android.settings.core.BasePreferenceController;
33 import com.android.settings.core.PreferenceControllerMixin;
34 import com.android.settingslib.Utils;
35 import com.android.settingslib.widget.UsageProgressBarPreference;
36 
37 // LINT.IfChange
38 /** Controller that update the battery header view */
39 public class BatteryHeaderPreferenceController extends BasePreferenceController
40         implements PreferenceControllerMixin, LifecycleEventObserver {
41     private static final String TAG = "BatteryHeaderPreferenceController";
42     private static final int BATTERY_MAX_LEVEL = 100;
43 
44     @Nullable @VisibleForTesting BatteryBroadcastReceiver mBatteryBroadcastReceiver;
45     @Nullable @VisibleForTesting UsageProgressBarPreference mBatteryUsageProgressBarPreference;
46 
BatteryHeaderPreferenceController(Context context, String key)47     public BatteryHeaderPreferenceController(Context context, String key) {
48         super(context, key);
49     }
50 
51     @Override
onStateChanged(@onNull LifecycleOwner lifecycleOwner, @NonNull Lifecycle.Event event)52     public void onStateChanged(@NonNull LifecycleOwner lifecycleOwner,
53             @NonNull Lifecycle.Event event) {
54         switch (event) {
55             case ON_CREATE:
56                 mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(mContext);
57                 mBatteryBroadcastReceiver.setBatteryChangedListener(
58                         type -> {
59                             if (type != BATTERY_NOT_PRESENT) {
60                                 quickUpdateHeaderPreference();
61                             }
62                         });
63                 break;
64             case ON_START:
65                 if (mBatteryBroadcastReceiver != null) {
66                     mBatteryBroadcastReceiver.register();
67                 }
68                 break;
69             case ON_STOP:
70                 if (mBatteryBroadcastReceiver != null) {
71                     mBatteryBroadcastReceiver.unRegister();
72                 }
73                 break;
74             default:
75                 break;
76         }
77     }
78 
79     @Override
displayPreference(PreferenceScreen screen)80     public void displayPreference(PreferenceScreen screen) {
81         super.displayPreference(screen);
82         mBatteryUsageProgressBarPreference = screen.findPreference(getPreferenceKey());
83         // Hide the bottom summary from the progress bar.
84         mBatteryUsageProgressBarPreference.setBottomSummary("");
85 
86         if (com.android.settings.Utils.isBatteryPresent(mContext)) {
87             quickUpdateHeaderPreference();
88         } else {
89             mBatteryUsageProgressBarPreference.setVisible(false);
90         }
91     }
92 
93     @Override
getAvailabilityStatus()94     public int getAvailabilityStatus() {
95         return AVAILABLE_UNSEARCHABLE;
96     }
97 
98     /** Updates {@link UsageProgressBarPreference} information. */
quickUpdateHeaderPreference()99     public void quickUpdateHeaderPreference() {
100         if (mBatteryUsageProgressBarPreference == null) {
101             return;
102         }
103 
104         Intent batteryBroadcast =
105                 com.android.settingslib.fuelgauge.BatteryUtils.getBatteryIntent(mContext);
106         final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast);
107 
108         mBatteryUsageProgressBarPreference.setUsageSummary(
109                 formatBatteryPercentageText(batteryLevel));
110         mBatteryUsageProgressBarPreference.setPercent(batteryLevel, BATTERY_MAX_LEVEL);
111     }
112 
formatBatteryPercentageText(int batteryLevel)113     private CharSequence formatBatteryPercentageText(int batteryLevel) {
114         return com.android.settings.Utils.formatPercentage(batteryLevel);
115     }
116 }
117 // LINT.ThenChange(BatteryHeaderPreference.kt)
118