1 /* 2 * Copyright (C) 2017 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.settings.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.os.BatteryManager; 25 import android.support.annotation.VisibleForTesting; 26 import android.support.v14.preference.PreferenceFragment; 27 import android.support.v7.preference.PreferenceScreen; 28 import android.widget.TextView; 29 30 import com.android.settings.R; 31 import com.android.settings.applications.LayoutPreference; 32 import com.android.settings.core.PreferenceControllerMixin; 33 import com.android.settings.widget.EntityHeaderController; 34 import com.android.settingslib.Utils; 35 import com.android.settingslib.core.AbstractPreferenceController; 36 import com.android.settingslib.core.lifecycle.Lifecycle; 37 import com.android.settingslib.core.lifecycle.LifecycleObserver; 38 import com.android.settingslib.core.lifecycle.events.OnStart; 39 40 /** 41 * Controller that update the battery header view 42 */ 43 public class BatteryHeaderPreferenceController extends AbstractPreferenceController 44 implements PreferenceControllerMixin, LifecycleObserver, OnStart { 45 @VisibleForTesting 46 static final String KEY_BATTERY_HEADER = "battery_header"; 47 48 @VisibleForTesting 49 BatteryMeterView mBatteryMeterView; 50 @VisibleForTesting 51 TextView mBatteryPercentText; 52 @VisibleForTesting 53 TextView mSummary1; 54 @VisibleForTesting 55 TextView mSummary2; 56 57 private final Activity mActivity; 58 private final PreferenceFragment mHost; 59 private final Lifecycle mLifecycle; 60 61 private LayoutPreference mBatteryLayoutPref; 62 BatteryHeaderPreferenceController(Context context, Activity activity, PreferenceFragment host, Lifecycle lifecycle)63 public BatteryHeaderPreferenceController(Context context, Activity activity, 64 PreferenceFragment host, Lifecycle lifecycle) { 65 super(context); 66 mActivity = activity; 67 mHost = host; 68 mLifecycle = lifecycle; 69 if (mLifecycle != null) { 70 mLifecycle.addObserver(this); 71 } 72 } 73 74 @Override displayPreference(PreferenceScreen screen)75 public void displayPreference(PreferenceScreen screen) { 76 super.displayPreference(screen); 77 mBatteryLayoutPref = (LayoutPreference) screen.findPreference(KEY_BATTERY_HEADER); 78 mBatteryMeterView = (BatteryMeterView) mBatteryLayoutPref 79 .findViewById(R.id.battery_header_icon); 80 mBatteryPercentText = mBatteryLayoutPref.findViewById(R.id.battery_percent); 81 mSummary1 = mBatteryLayoutPref.findViewById(R.id.summary1); 82 mSummary2 = mBatteryLayoutPref.findViewById(R.id.summary2); 83 84 quickUpdateHeaderPreference(); 85 } 86 87 @Override isAvailable()88 public boolean isAvailable() { 89 return true; 90 } 91 92 @Override getPreferenceKey()93 public String getPreferenceKey() { 94 return KEY_BATTERY_HEADER; 95 } 96 97 @Override onStart()98 public void onStart() { 99 EntityHeaderController.newInstance(mActivity, mHost, 100 mBatteryLayoutPref.findViewById(R.id.battery_entity_header)) 101 .setRecyclerView(mHost.getListView(), mLifecycle) 102 .styleActionBar(mActivity); 103 } 104 updateHeaderPreference(BatteryInfo info)105 public void updateHeaderPreference(BatteryInfo info) { 106 mBatteryPercentText.setText(Utils.formatPercentage(info.batteryLevel)); 107 if (info.remainingLabel == null) { 108 mSummary1.setText(info.statusLabel); 109 } else { 110 mSummary1.setText(info.remainingLabel); 111 } 112 // Clear this just to be sure we don't get UI jank on re-entering this view from another 113 // activity. 114 mSummary2.setText(""); 115 116 mBatteryMeterView.setBatteryLevel(info.batteryLevel); 117 mBatteryMeterView.setCharging(!info.discharging); 118 } 119 quickUpdateHeaderPreference()120 public void quickUpdateHeaderPreference() { 121 Intent batteryBroadcast = mContext.registerReceiver(null, 122 new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 123 final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast); 124 final boolean discharging = 125 batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0; 126 127 // Set battery level and charging status 128 mBatteryMeterView.setBatteryLevel(batteryLevel); 129 mBatteryMeterView.setCharging(!discharging); 130 mBatteryPercentText.setText(Utils.formatPercentage(batteryLevel)); 131 132 // clear all the summaries 133 mSummary1.setText(""); 134 mSummary2.setText(""); 135 } 136 } 137