BatteryController.java revision 6179ea3196e9306d3f14361fe9ef14191b1edba6
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            int N = mIconViews.size();
60            for (int i=0; i<N; i++) {
61                final int icon = intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, 0);
62                ImageView v = mIconViews.get(i);
63                v.setImageResource(icon);
64                v.setImageLevel(level);
65                v.setContentDescription(mContext.getString(R.string.accessibility_battery_level,
66                        level));
67            }
68            N = mLabelViews.size();
69            for (int i=0; i<N; i++) {
70                //final boolean plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
71                TextView v = mLabelViews.get(i);
72                v.setText(mContext.getString(R.string.status_bar_settings_battery_meter_format,
73                        level));
74            }
75        }
76    }
77}
78