blob: 575b44ebce6f96608f3dc36b4f51f4a268a7c88a [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
Joe Onoratofd52b182010-11-10 18:00:52 -080019import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.os.BatteryManager;
Joe Onoratofd52b182010-11-10 18:00:52 -080024import android.widget.ImageView;
25import android.widget.TextView;
26
27import com.android.systemui.R;
28
John Spurlockde84f0e2013-06-12 12:41:00 -040029import java.util.ArrayList;
30
Joe Onoratofd52b182010-11-10 18:00:52 -080031public class BatteryController extends BroadcastReceiver {
32 private static final String TAG = "StatusBar.BatteryController";
33
34 private Context mContext;
35 private ArrayList<ImageView> mIconViews = new ArrayList<ImageView>();
36 private ArrayList<TextView> mLabelViews = new ArrayList<TextView>();
37
Winson Chungd63c59782012-09-05 17:34:41 -070038 private ArrayList<BatteryStateChangeCallback> mChangeCallbacks =
39 new ArrayList<BatteryStateChangeCallback>();
40
41 public interface BatteryStateChangeCallback {
42 public void onBatteryLevelChanged(int level, boolean pluggedIn);
43 }
44
Joe Onoratofd52b182010-11-10 18:00:52 -080045 public BatteryController(Context context) {
46 mContext = context;
47
48 IntentFilter filter = new IntentFilter();
49 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
50 context.registerReceiver(this, filter);
51 }
52
53 public void addIconView(ImageView v) {
54 mIconViews.add(v);
55 }
56
57 public void addLabelView(TextView v) {
58 mLabelViews.add(v);
59 }
60
Winson Chungd63c59782012-09-05 17:34:41 -070061 public void addStateChangedCallback(BatteryStateChangeCallback cb) {
62 mChangeCallbacks.add(cb);
63 }
64
Joe Onoratofd52b182010-11-10 18:00:52 -080065 public void onReceive(Context context, Intent intent) {
66 final String action = intent.getAction();
67 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
68 final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
Daniel Sandlerfe0e1e42012-11-29 15:11:50 -050069 final int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
70 BatteryManager.BATTERY_STATUS_UNKNOWN);
71
72 boolean plugged = false;
73 switch (status) {
John Spurlock209bede2013-07-17 12:23:27 -040074 case BatteryManager.BATTERY_STATUS_CHARGING:
Daniel Sandlerfe0e1e42012-11-29 15:11:50 -050075 case BatteryManager.BATTERY_STATUS_FULL:
76 plugged = true;
77 break;
78 }
79
80 final int icon = plugged ? R.drawable.stat_sys_battery_charge
Daniel Sandler2b697352011-07-22 16:23:09 -040081 : R.drawable.stat_sys_battery;
Daniel Sandlerfe0e1e42012-11-29 15:11:50 -050082
Joe Onoratofd52b182010-11-10 18:00:52 -080083 int N = mIconViews.size();
84 for (int i=0; i<N; i++) {
Joe Onoratofd52b182010-11-10 18:00:52 -080085 ImageView v = mIconViews.get(i);
86 v.setImageResource(icon);
87 v.setImageLevel(level);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070088 v.setContentDescription(mContext.getString(R.string.accessibility_battery_level,
89 level));
Joe Onoratofd52b182010-11-10 18:00:52 -080090 }
91 N = mLabelViews.size();
92 for (int i=0; i<N; i++) {
Joe Onoratofd52b182010-11-10 18:00:52 -080093 TextView v = mLabelViews.get(i);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -070094 v.setText(mContext.getString(R.string.status_bar_settings_battery_meter_format,
95 level));
Joe Onoratofd52b182010-11-10 18:00:52 -080096 }
Winson Chungd63c59782012-09-05 17:34:41 -070097
98 for (BatteryStateChangeCallback cb : mChangeCallbacks) {
99 cb.onBatteryLevelChanged(level, plugged);
100 }
Joe Onoratofd52b182010-11-10 18:00:52 -0800101 }
102 }
103}