/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. package com.android.systemui.statusbar.policy;
  17. import android.content.BroadcastReceiver;
  18. import android.content.Context;
  19. import android.content.Intent;
  20. import android.content.IntentFilter;
  21. import android.os.BatteryManager;
  22. import java.util.ArrayList;
  23. public class BatteryController extends BroadcastReceiver {
  24. private static final String TAG = "StatusBar.BatteryController";
  25. private ArrayList<BatteryStateChangeCallback> mChangeCallbacks =
  26. new ArrayList<BatteryStateChangeCallback>();
  27. public interface BatteryStateChangeCallback {
  28. public void onBatteryLevelChanged(int level, boolean pluggedIn);
  29. }
  30. public BatteryController(Context context) {
  31. IntentFilter filter = new IntentFilter();
  32. filter.addAction(Intent.ACTION_BATTERY_CHANGED);
  33. context.registerReceiver(this, filter);
  34. }
  35. public void addStateChangedCallback(BatteryStateChangeCallback cb) {
  36. mChangeCallbacks.add(cb);
  37. }
  38. public void onReceive(Context context, Intent intent) {
  39. final String action = intent.getAction();
  40. if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
  41. final int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
  42. final int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
  43. BatteryManager.BATTERY_STATUS_UNKNOWN);
  44. boolean plugged = false;
  45. switch (status) {
  46. case BatteryManager.BATTERY_STATUS_CHARGING:
  47. case BatteryManager.BATTERY_STATUS_FULL:
  48. plugged = true;
  49. break;
  50. }
  51. for (BatteryStateChangeCallback cb : mChangeCallbacks) {
  52. cb.onBatteryLevelChanged(level, plugged);
  53. }
  54. }
  55. }
  56. }