Home | History | Annotate | Download | only in fuelgauge
      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.content.Context;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.support.annotation.VisibleForTesting;
     24 import android.support.v7.preference.PreferenceScreen;
     25 import android.widget.TextView;
     26 
     27 import com.android.settings.R;
     28 import com.android.settings.applications.LayoutPreference;
     29 import com.android.settings.core.PreferenceController;
     30 import com.android.settingslib.BatteryInfo;
     31 import com.android.settingslib.Utils;
     32 
     33 /**
     34  * Controller that update the battery header view
     35  */
     36 public class BatteryHeaderPreferenceController extends PreferenceController {
     37     @VisibleForTesting
     38     static final String KEY_BATTERY_HEADER = "battery_header";
     39     @VisibleForTesting
     40     BatteryMeterView mBatteryMeterView;
     41     @VisibleForTesting
     42     TextView mTimeText;
     43     @VisibleForTesting
     44     TextView mSummary;
     45 
     46     private LayoutPreference mBatteryLayoutPref;
     47 
     48     public BatteryHeaderPreferenceController(Context context) {
     49         super(context);
     50     }
     51 
     52     @Override
     53     public void displayPreference(PreferenceScreen screen) {
     54         super.displayPreference(screen);
     55 
     56         mBatteryLayoutPref = (LayoutPreference) screen.findPreference(KEY_BATTERY_HEADER);
     57         mBatteryMeterView = (BatteryMeterView) mBatteryLayoutPref
     58                 .findViewById(R.id.battery_header_icon);
     59         mTimeText = (TextView) mBatteryLayoutPref.findViewById(R.id.battery_percent);
     60         mSummary = (TextView) mBatteryLayoutPref.findViewById(R.id.summary1);
     61 
     62         Intent batteryBroadcast = mContext.registerReceiver(null,
     63                 new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
     64         final int batteryLevel = Utils.getBatteryLevel(batteryBroadcast);
     65 
     66         mBatteryMeterView.setBatteryLevel(batteryLevel);
     67         mTimeText.setText(Utils.formatPercentage(batteryLevel));
     68     }
     69 
     70     @Override
     71     public boolean isAvailable() {
     72         return true;
     73     }
     74 
     75     @Override
     76     public String getPreferenceKey() {
     77         return KEY_BATTERY_HEADER;
     78     }
     79 
     80     public void updateHeaderPreference(BatteryInfo info) {
     81         mTimeText.setText(Utils.formatPercentage(info.batteryLevel));
     82         if (info.remainingLabel == null) {
     83             mSummary.setText(info.statusLabel);
     84         } else {
     85             mSummary.setText(info.remainingLabel);
     86         }
     87 
     88         mBatteryMeterView.setBatteryLevel(info.batteryLevel);
     89         mBatteryMeterView.setCharging(!info.discharging);
     90     }
     91 }
     92