PageRenderTime 29ms CodeModel.GetById 20ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/packages/SystemUI/src/com/android/systemui/statusbar/policy/BatteryController.java

https://github.com/aizuzi/platform_frameworks_base
Java | 68 lines | 41 code | 12 blank | 15 comment | 3 complexity | 0b1fce06644e80123ff1ae528d537737 MD5 | raw file
 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 android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.os.BatteryManager;
24
25import java.util.ArrayList;
26
27public class BatteryController extends BroadcastReceiver {
28    private static final String TAG = "StatusBar.BatteryController";
29
30
31    private ArrayList<BatteryStateChangeCallback> mChangeCallbacks =
32            new ArrayList<BatteryStateChangeCallback>();
33
34    public interface BatteryStateChangeCallback {
35        public void onBatteryLevelChanged(int level, boolean pluggedIn);
36    }
37
38    public BatteryController(Context context) {
39        IntentFilter filter = new IntentFilter();
40        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
41        context.registerReceiver(this, filter);
42    }
43
44    public void addStateChangedCallback(BatteryStateChangeCallback cb) {
45        mChangeCallbacks.add(cb);
46    }
47
48    public void onReceive(Context context, Intent intent) {
49        final String action = intent.getAction();
50        if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
51            final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
52            final int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
53                    BatteryManager.BATTERY_STATUS_UNKNOWN);
54
55            boolean plugged = false;
56            switch (status) {
57                case BatteryManager.BATTERY_STATUS_CHARGING:
58                case BatteryManager.BATTERY_STATUS_FULL:
59                    plugged = true;
60                    break;
61            }
62
63            for (BatteryStateChangeCallback cb : mChangeCallbacks) {
64                cb.onBatteryLevelChanged(level, plugged);
65            }
66        }
67    }
68}