1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * 15 * 16 */ 17 18 package com.android.car.developeroptions.fuelgauge; 19 20 import android.app.Activity; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.icu.text.NumberFormat; 25 import android.os.BatteryManager; 26 import android.os.PowerManager; 27 import android.text.TextUtils; 28 import android.widget.TextView; 29 30 import androidx.annotation.VisibleForTesting; 31 import androidx.preference.PreferenceFragmentCompat; 32 import androidx.preference.PreferenceScreen; 33 34 import com.android.car.developeroptions.R; 35 import com.android.car.developeroptions.core.BasePreferenceController; 36 import com.android.car.developeroptions.core.PreferenceControllerMixin; 37 import com.android.car.developeroptions.widget.EntityHeaderController; 38 import com.android.settingslib.Utils; 39 import com.android.settingslib.core.lifecycle.Lifecycle; 40 import com.android.settingslib.core.lifecycle.LifecycleObserver; 41 import com.android.settingslib.core.lifecycle.events.OnStart; 42 import com.android.settingslib.widget.LayoutPreference; 43 44 /** 45 * Controller that update the battery header view 46 */ 47 public class BatteryHeaderPreferenceController extends BasePreferenceController 48 implements PreferenceControllerMixin, LifecycleObserver, OnStart { 49 @VisibleForTesting 50 static final String KEY_BATTERY_HEADER = "battery_header"; 51 52 @VisibleForTesting 53 BatteryMeterView mBatteryMeterView; 54 @VisibleForTesting 55 TextView mBatteryPercentText; 56 @VisibleForTesting 57 TextView mSummary1; 58 @VisibleForTesting 59 TextView mSummary2; 60 61 private Activity mActivity; 62 private PreferenceFragmentCompat mHost; 63 private Lifecycle mLifecycle; 64 private final PowerManager mPowerManager; 65 66 private LayoutPreference mBatteryLayoutPref; 67 BatteryHeaderPreferenceController(Context context, String key)68 public BatteryHeaderPreferenceController(Context context, String key) { 69 super(context, key); 70 mPowerManager = context.getSystemService(PowerManager.class); 71 } 72 setActivity(Activity activity)73 public void setActivity(Activity activity) { 74 mActivity = activity; 75 } 76 setFragment(PreferenceFragmentCompat fragment)77 public void setFragment(PreferenceFragmentCompat fragment) { 78 mHost = fragment; 79 } 80 setLifecycle(Lifecycle lifecycle)81 public void setLifecycle(Lifecycle lifecycle) { 82 mLifecycle = lifecycle; 83 } 84 85 @Override displayPreference(PreferenceScreen screen)86 public void displayPreference(PreferenceScreen screen) { 87 super.displayPreference(screen); 88 mBatteryLayoutPref = screen.findPreference(getPreferenceKey()); 89 mBatteryMeterView = mBatteryLayoutPref 90 .findViewById(R.id.battery_header_icon); 91 mBatteryPercentText = mBatteryLayoutPref.findViewById(R.id.battery_percent); 92 mSummary1 = mBatteryLayoutPref.findViewById(R.id.summary1); 93 mSummary2 = mBatteryLayoutPref.findViewById(R.id.summary2); 94 95 quickUpdateHeaderPreference(); 96 } 97 98 @Override getAvailabilityStatus()99 public int getAvailabilityStatus() { 100 return AVAILABLE_UNSEARCHABLE; 101 } 102 103 @Override onStart()104 public void onStart() { 105 EntityHeaderController.newInstance(mActivity, mHost, 106 mBatteryLayoutPref.findViewById(R.id.battery_entity_header)) 107 .setRecyclerView(mHost.getListView(), mLifecycle) 108 .styleActionBar(mActivity); 109 } 110 updateHeaderPreference(BatteryInfo info)111 public void updateHeaderPreference(BatteryInfo info) { 112 mBatteryPercentText.setText(formatBatteryPercentageText(info.batteryLevel)); 113 if (info.remainingLabel == null) { 114 mSummary1.setText(info.statusLabel); 115 } else { 116 mSummary1.setText(info.remainingLabel); 117 } 118 // Clear this just to be sure we don't get UI jank on re-entering this view from another 119 // activity. 120 mSummary2.setText(""); 121 122 mBatteryMeterView.setBatteryLevel(info.batteryLevel); 123 mBatteryMeterView.setCharging(!info.discharging); 124 mBatteryMeterView.setPowerSave(mPowerManager.isPowerSaveMode()); 125 } 126 quickUpdateHeaderPreference()127 public void quickUpdateHeaderPreference() { 128 Intent batteryBroadcast = mContext.registerReceiver(null, 129 new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 130 final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast); 131 final boolean discharging = 132 batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0; 133 134 // Set battery level and charging status 135 mBatteryMeterView.setBatteryLevel(batteryLevel); 136 mBatteryMeterView.setCharging(!discharging); 137 mBatteryMeterView.setPowerSave(mPowerManager.isPowerSaveMode()); 138 mBatteryPercentText.setText(formatBatteryPercentageText(batteryLevel)); 139 } 140 formatBatteryPercentageText(int batteryLevel)141 private CharSequence formatBatteryPercentageText(int batteryLevel) { 142 return TextUtils.expandTemplate(mContext.getText(R.string.battery_header_title_alternate), 143 NumberFormat.getIntegerInstance().format(batteryLevel)); 144 } 145 } 146