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