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