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.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 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 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; 74 int N = mIconViews.size(); 75 for (int i=0; i<N; i++) { 76 ImageView v = mIconViews.get(i); 77 v.setImageResource(icon); 78 v.setImageLevel(level); 79 v.setContentDescription(mContext.getString(R.string.accessibility_battery_level, 80 level)); 81 } 82 N = mLabelViews.size(); 83 for (int i=0; i<N; i++) { 84 TextView v = mLabelViews.get(i); 85 v.setText(mContext.getString(R.string.status_bar_settings_battery_meter_format, 86 level)); 87 } 88 89 for (BatteryStateChangeCallback cb : mChangeCallbacks) { 90 cb.onBatteryLevelChanged(level, plugged); 91 } 92 } 93 } 94} 95