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.settings.fuelgauge;
18 
19 import android.content.Context;
20 
21 import androidx.preference.Preference;
22 import androidx.preference.PreferenceScreen;
23 
24 import com.android.settings.R;
25 import com.android.settings.core.BasePreferenceController;
26 import com.android.settingslib.core.lifecycle.LifecycleObserver;
27 import com.android.settingslib.core.lifecycle.events.OnStart;
28 import com.android.settingslib.core.lifecycle.events.OnStop;
29 
30 public class TopLevelBatteryPreferenceController extends BasePreferenceController implements
31         LifecycleObserver, OnStart, OnStop {
32 
33     private final BatteryBroadcastReceiver mBatteryBroadcastReceiver;
34     private Preference mPreference;
35     private BatteryInfo mBatteryInfo;
36 
TopLevelBatteryPreferenceController(Context context, String preferenceKey)37     public TopLevelBatteryPreferenceController(Context context, String preferenceKey) {
38         super(context, preferenceKey);
39         mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(mContext);
40         mBatteryBroadcastReceiver.setBatteryChangedListener(type -> {
41             BatteryInfo.getBatteryInfo(mContext, info -> {
42                 mBatteryInfo = info;
43                 updateState(mPreference);
44             }, true /* shortString */);
45         });
46     }
47 
48     @Override
getAvailabilityStatus()49     public int getAvailabilityStatus() {
50         return mContext.getResources().getBoolean(R.bool.config_show_top_level_battery)
51                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
52     }
53 
54     @Override
displayPreference(PreferenceScreen screen)55     public void displayPreference(PreferenceScreen screen) {
56         super.displayPreference(screen);
57         mPreference = screen.findPreference(getPreferenceKey());
58     }
59 
60     @Override
onStart()61     public void onStart() {
62         mBatteryBroadcastReceiver.register();
63     }
64 
65     @Override
onStop()66     public void onStop() {
67         mBatteryBroadcastReceiver.unRegister();
68     }
69 
70     @Override
getSummary()71     public CharSequence getSummary() {
72         return getDashboardLabel(mContext, mBatteryInfo);
73     }
74 
getDashboardLabel(Context context, BatteryInfo info)75     static CharSequence getDashboardLabel(Context context, BatteryInfo info) {
76         if (info == null || context == null) {
77             return null;
78         }
79         CharSequence label;
80         if (!info.discharging && info.chargeLabel != null) {
81             label = info.chargeLabel;
82         } else if (info.remainingLabel == null) {
83             label = info.batteryPercentString;
84         } else {
85             label = context.getString(R.string.power_remaining_settings_home_page,
86                     info.batteryPercentString,
87                     info.remainingLabel);
88         }
89         return label;
90     }
91 }
92