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.annotation.VisibleForTesting; 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.settings.overlay.FeatureFactory; 28 import com.android.settingslib.core.lifecycle.LifecycleObserver; 29 import com.android.settingslib.core.lifecycle.events.OnStart; 30 import com.android.settingslib.core.lifecycle.events.OnStop; 31 32 public class TopLevelBatteryPreferenceController extends BasePreferenceController implements 33 LifecycleObserver, OnStart, OnStop, BatteryPreferenceController { 34 35 @VisibleForTesting 36 protected boolean mIsBatteryPresent = true; 37 private final BatteryBroadcastReceiver mBatteryBroadcastReceiver; 38 private Preference mPreference; 39 private BatteryInfo mBatteryInfo; 40 private BatteryStatusFeatureProvider mBatteryStatusFeatureProvider; 41 private String mBatteryStatusLabel; 42 TopLevelBatteryPreferenceController(Context context, String preferenceKey)43 public TopLevelBatteryPreferenceController(Context context, String preferenceKey) { 44 super(context, preferenceKey); 45 mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(mContext); 46 mBatteryBroadcastReceiver.setBatteryChangedListener(type -> { 47 if (type == BatteryBroadcastReceiver.BatteryUpdateType.BATTERY_NOT_PRESENT) { 48 mIsBatteryPresent = false; 49 } 50 BatteryInfo.getBatteryInfo(mContext, info -> { 51 mBatteryInfo = info; 52 updateState(mPreference); 53 }, true /* shortString */); 54 }); 55 56 mBatteryStatusFeatureProvider = FeatureFactory.getFactory(context) 57 .getBatteryStatusFeatureProvider(context); 58 } 59 60 @Override getAvailabilityStatus()61 public int getAvailabilityStatus() { 62 return mContext.getResources().getBoolean(R.bool.config_show_top_level_battery) 63 ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 64 } 65 66 @Override displayPreference(PreferenceScreen screen)67 public void displayPreference(PreferenceScreen screen) { 68 super.displayPreference(screen); 69 mPreference = screen.findPreference(getPreferenceKey()); 70 } 71 72 @Override onStart()73 public void onStart() { 74 mBatteryBroadcastReceiver.register(); 75 } 76 77 @Override onStop()78 public void onStop() { 79 mBatteryBroadcastReceiver.unRegister(); 80 } 81 82 @Override getSummary()83 public CharSequence getSummary() { 84 return getSummary(true /* batteryStatusUpdate */); 85 } 86 getSummary(boolean batteryStatusUpdate)87 private CharSequence getSummary(boolean batteryStatusUpdate) { 88 // Display help message if battery is not present. 89 if (!mIsBatteryPresent) { 90 return mContext.getText(R.string.battery_missing_message); 91 } 92 return getDashboardLabel(mContext, mBatteryInfo, batteryStatusUpdate); 93 } 94 getDashboardLabel(Context context, BatteryInfo info, boolean batteryStatusUpdate)95 protected CharSequence getDashboardLabel(Context context, BatteryInfo info, 96 boolean batteryStatusUpdate) { 97 if (info == null || context == null) { 98 return null; 99 } 100 101 if (batteryStatusUpdate) { 102 if (!mBatteryStatusFeatureProvider.triggerBatteryStatusUpdate(this, info)) { 103 mBatteryStatusLabel = null; // will generateLabel() 104 } 105 } 106 107 return (mBatteryStatusLabel == null) ? generateLabel(info) : mBatteryStatusLabel; 108 } 109 generateLabel(BatteryInfo info)110 private CharSequence generateLabel(BatteryInfo info) { 111 if (!info.discharging && info.chargeLabel != null) { 112 return info.chargeLabel; 113 } else if (info.remainingLabel == null) { 114 return info.batteryPercentString; 115 } else { 116 return mContext.getString(R.string.power_remaining_settings_home_page, 117 info.batteryPercentString, 118 info.remainingLabel); 119 } 120 } 121 122 /** 123 * Callback which receives text for the label. 124 */ updateBatteryStatus(String label, BatteryInfo info)125 public void updateBatteryStatus(String label, BatteryInfo info) { 126 mBatteryStatusLabel = label; // Null if adaptive charging is not active 127 128 if (mPreference != null) { 129 // Do not triggerBatteryStatusUpdate(), otherwise there will be an infinite loop 130 final CharSequence summary = getSummary(false /* batteryStatusUpdate */); 131 if (summary != null) { 132 mPreference.setSummary(summary); 133 } 134 } 135 } 136 } 137