PageRenderTime 40ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lcarswallpaper/src/com/example/android/maxpapers/lcars/BatteryReceiver.java

http://lcarswallpaper.googlecode.com/
Java | 83 lines | 56 code | 9 blank | 18 comment | 2 complexity | 53a21abdf95c1a10d8413c0c5d446524 MD5 | raw file
  1. package com.example.android.maxpapers.lcars;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.BatteryManager;
  6. /**
  7. * @author cedarrapidsboy
  8. * Provider for battery information.
  9. *
  10. */
  11. public class BatteryReceiver extends BroadcastReceiver {
  12. private static final String DEUTERIUM_STOPPED = "Deuterium refill stopped";
  13. private static final String DEUTERIUM_FULL = "Deuterium tanks full";
  14. private static final String DEUTERIUM_FLOW_NORMAL = "Deuterium flow normal";
  15. private static final String DEUTERIUM_REFILLING = "Refilling deuterium";
  16. private static final String DEUTERIUM_STATUS_UNKNOWN = "Deuterium status unknown";
  17. private int level = 0;
  18. private String status = DEUTERIUM_STATUS_UNKNOWN;
  19. private double eV = 0.0d;
  20. @Override
  21. public void onReceive(Context context, Intent intent) {
  22. level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
  23. status = DEUTERIUM_STATUS_UNKNOWN;
  24. eV = 0.0d;
  25. int s = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
  26. switch (s) {
  27. case BatteryManager.BATTERY_STATUS_CHARGING:
  28. status = DEUTERIUM_REFILLING;
  29. break;
  30. case BatteryManager.BATTERY_STATUS_DISCHARGING:
  31. status = DEUTERIUM_FLOW_NORMAL;
  32. break;
  33. case BatteryManager.BATTERY_STATUS_FULL:
  34. status = DEUTERIUM_FULL;
  35. break;
  36. case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
  37. status = DEUTERIUM_STOPPED;
  38. break;
  39. case BatteryManager.BATTERY_STATUS_UNKNOWN:
  40. status = DEUTERIUM_STATUS_UNKNOWN;
  41. break;
  42. }
  43. int i = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
  44. eV = DateCalc.roundToDecimals(i / 1000d, 2);
  45. }
  46. /**
  47. * @return battery percentage (0-100)
  48. */
  49. public int getBatteryLevel(){
  50. return level;
  51. }
  52. /**
  53. * @return battery voltage
  54. */
  55. public double geteV(){
  56. return eV;
  57. }
  58. /**
  59. * @return lcars-themed battery status message
  60. */
  61. public String getStatus(){
  62. return status;
  63. }
  64. /**
  65. * @param threshold percentage (<=) that will trigger a true
  66. * @return true if battery level is below or equal to threshold
  67. */
  68. public boolean isBatteryLow(int threshold){
  69. if (level <= threshold){
  70. return true;
  71. }
  72. return false;
  73. }
  74. }