blob: 7f9bcac97761c5775f6f91ffcfa9e019673763cd [file] [log] [blame]
Joe Onoratofd52b182010-11-10 18:00:52 -08001/*
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
Winson Chungd63c59782012-09-05 17:34:41 -070021import android.bluetooth.BluetoothAdapter.BluetoothStateChangeCallback;
Joe Onoratofd52b182010-11-10 18:00:52 -080022import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.os.BatteryManager;
27import android.util.Slog;
28import android.widget.ImageView;
29import android.widget.TextView;
30
31import com.android.systemui.R;
32
33public 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
Winson Chungd63c59782012-09-05 17:34:41 -070040 private ArrayList<BatteryStateChangeCallback> mChangeCallbacks =
41 new ArrayList<BatteryStateChangeCallback>();
42
43 public interface BatteryStateChangeCallback {
44 public void onBatteryLevelChanged(int level, boolean pluggedIn);
45 }
46
Joe Onoratofd52b182010-11-10 18:00:52 -080047 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
Winson Chungd63c59782012-09-05 17:34:41 -070063 public void addStateChangedCallback(BatteryStateChangeCallback cb) {
64 mChangeCallbacks.add(cb);
65 }
66
Joe Onoratofd52b182010-11-10 18:00:52 -080067 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);
Daniel Sandler2b697352011-07-22 16:23:09 -040071 final boolean plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
72 final int icon = plugged ? R.drawable.stat_sys_battery_charge
73 : R.drawable.stat_sys_battery;
Joe Onoratofd52b182010-11-10 18:00:52 -080074 int N = mIconViews.size();
75 for (int i=0; i<N; i++) {
Joe Onoratofd52b182010-11-10 18:00:52 -080076 ImageView v = mIconViews.get(i);
77 v.setImageResource(icon);
78 v.setImageLevel(level);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070079 v.setContentDescription(mContext.getString(R.string.accessibility_battery_level,
80 level));
Joe Onoratofd52b182010-11-10 18:00:52 -080081 }
82 N = mLabelViews.size();
83 for (int i=0; i<N; i++) {
Joe Onoratofd52b182010-11-10 18:00:52 -080084 TextView v = mLabelViews.get(i);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070085 v.setText(mContext.getString(R.string.status_bar_settings_battery_meter_format,
86 level));
Joe Onoratofd52b182010-11-10 18:00:52 -080087 }
Winson Chungd63c59782012-09-05 17:34:41 -070088
89 for (BatteryStateChangeCallback cb : mChangeCallbacks) {
90 cb.onBatteryLevelChanged(level, plugged);
91 }
Joe Onoratofd52b182010-11-10 18:00:52 -080092 }
93 }
94}