BatteryController.java revision 2b69735e014872ca2183b4a39a8381daa337e146
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.content.BroadcastReceiver; 22import android.content.Context; 23import android.content.Intent; 24import android.content.IntentFilter; 25import android.os.BatteryManager; 26import android.util.Slog; 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 public BatteryController(Context context) { 40 mContext = context; 41 42 IntentFilter filter = new IntentFilter(); 43 filter.addAction(Intent.ACTION_BATTERY_CHANGED); 44 context.registerReceiver(this, filter); 45 } 46 47 public void addIconView(ImageView v) { 48 mIconViews.add(v); 49 } 50 51 public void addLabelView(TextView v) { 52 mLabelViews.add(v); 53 } 54 55 public void onReceive(Context context, Intent intent) { 56 final String action = intent.getAction(); 57 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) { 58 final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); 59 final boolean plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0; 60 final int icon = plugged ? R.drawable.stat_sys_battery_charge 61 : R.drawable.stat_sys_battery; 62 int N = mIconViews.size(); 63 for (int i=0; i<N; i++) { 64 ImageView v = mIconViews.get(i); 65 v.setImageResource(icon); 66 v.setImageLevel(level); 67 v.setContentDescription(mContext.getString(R.string.accessibility_battery_level, 68 level)); 69 } 70 N = mLabelViews.size(); 71 for (int i=0; i<N; i++) { 72 TextView v = mLabelViews.get(i); 73 v.setText(mContext.getString(R.string.status_bar_settings_battery_meter_format, 74 level)); 75 } 76 } 77 } 78} 79