Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 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 | |
Winson Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 21 | import android.bluetooth.BluetoothAdapter.BluetoothStateChangeCallback; |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 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 | |
Winson Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 40 | private ArrayList<BatteryStateChangeCallback> mChangeCallbacks = |
| 41 | new ArrayList<BatteryStateChangeCallback>(); |
| 42 | |
| 43 | public interface BatteryStateChangeCallback { |
| 44 | public void onBatteryLevelChanged(int level, boolean pluggedIn); |
| 45 | } |
| 46 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 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 | |
Winson Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 63 | public void addStateChangedCallback(BatteryStateChangeCallback cb) { |
| 64 | mChangeCallbacks.add(cb); |
| 65 | } |
| 66 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 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); |
Daniel Sandler | fe0e1e4 | 2012-11-29 15:11:50 -0500 | [diff] [blame] | 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 |
Daniel Sandler | 2b69735 | 2011-07-22 16:23:09 -0400 | [diff] [blame] | 83 | : R.drawable.stat_sys_battery; |
Daniel Sandler | fe0e1e4 | 2012-11-29 15:11:50 -0500 | [diff] [blame] | 84 | |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 85 | int N = mIconViews.size(); |
| 86 | for (int i=0; i<N; i++) { |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 87 | ImageView v = mIconViews.get(i); |
| 88 | v.setImageResource(icon); |
| 89 | v.setImageLevel(level); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 90 | v.setContentDescription(mContext.getString(R.string.accessibility_battery_level, |
| 91 | level)); |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 92 | } |
| 93 | N = mLabelViews.size(); |
| 94 | for (int i=0; i<N; i++) { |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 95 | TextView v = mLabelViews.get(i); |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 96 | v.setText(mContext.getString(R.string.status_bar_settings_battery_meter_format, |
| 97 | level)); |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 98 | } |
Winson Chung | d63c5978 | 2012-09-05 17:34:41 -0700 | [diff] [blame] | 99 | |
| 100 | for (BatteryStateChangeCallback cb : mChangeCallbacks) { |
| 101 | cb.onBatteryLevelChanged(level, plugged); |
| 102 | } |
Joe Onorato | fd52b18 | 2010-11-10 18:00:52 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |