Home | History | Annotate | Download | only in policy
      1 /*
      2  * Copyright (C) 2010 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.systemui.statusbar.policy;
     18 
     19 import java.util.ArrayList;
     20 
     21 import android.bluetooth.BluetoothAdapter.BluetoothStateChangeCallback;
     22 import android.content.BroadcastReceiver;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.content.IntentFilter;
     26 import android.os.BatteryManager;
     27 import android.util.Slog;
     28 import android.widget.ImageView;
     29 import android.widget.TextView;
     30 
     31 import com.android.systemui.R;
     32 
     33 public class BatteryController extends BroadcastReceiver {
     34     private static final String TAG = "StatusBar.BatteryController";
     35 
     36     private Context mContext;
     37     private ArrayList<ImageView> mIconViews = new ArrayList<ImageView>();
     38     private ArrayList<TextView> mLabelViews = new ArrayList<TextView>();
     39 
     40     private ArrayList<BatteryStateChangeCallback> mChangeCallbacks =
     41             new ArrayList<BatteryStateChangeCallback>();
     42 
     43     public interface BatteryStateChangeCallback {
     44         public void onBatteryLevelChanged(int level, boolean pluggedIn);
     45     }
     46 
     47     public BatteryController(Context context) {
     48         mContext = context;
     49 
     50         IntentFilter filter = new IntentFilter();
     51         filter.addAction(Intent.ACTION_BATTERY_CHANGED);
     52         context.registerReceiver(this, filter);
     53     }
     54 
     55     public void addIconView(ImageView v) {
     56         mIconViews.add(v);
     57     }
     58 
     59     public void addLabelView(TextView v) {
     60         mLabelViews.add(v);
     61     }
     62 
     63     public void addStateChangedCallback(BatteryStateChangeCallback cb) {
     64         mChangeCallbacks.add(cb);
     65     }
     66 
     67     public void onReceive(Context context, Intent intent) {
     68         final String action = intent.getAction();
     69         if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
     70             final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
     71             final int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
     72                     BatteryManager.BATTERY_STATUS_UNKNOWN);
     73 
     74             boolean plugged = false;
     75             switch (status) {
     76                 case BatteryManager.BATTERY_STATUS_CHARGING:
     77                 case BatteryManager.BATTERY_STATUS_FULL:
     78                     plugged = true;
     79                     break;
     80             }
     81 
     82             final int icon = plugged ? R.drawable.stat_sys_battery_charge
     83                                      : R.drawable.stat_sys_battery;
     84 
     85             int N = mIconViews.size();
     86             for (int i=0; i<N; i++) {
     87                 ImageView v = mIconViews.get(i);
     88                 v.setImageResource(icon);
     89                 v.setImageLevel(level);
     90                 v.setContentDescription(mContext.getString(R.string.accessibility_battery_level,
     91                         level));
     92             }
     93             N = mLabelViews.size();
     94             for (int i=0; i<N; i++) {
     95                 TextView v = mLabelViews.get(i);
     96                 v.setText(mContext.getString(R.string.status_bar_settings_battery_meter_format,
     97                         level));
     98             }
     99 
    100             for (BatteryStateChangeCallback cb : mChangeCallbacks) {
    101                 cb.onBatteryLevelChanged(level, plugged);
    102             }
    103         }
    104     }
    105 }
    106