Advertisement
Guest User

Battery App Source Code

a guest
Mar 1st, 2014
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. ---------------------------
  2. File: BatteryView.java
  3. ---------------------------
  4.  
  5. package com.skovmandsit.batteryapp;
  6.  
  7. import android.app.Activity;
  8. import android.content.BroadcastReceiver;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.content.IntentFilter;
  12. import android.os.BatteryManager;
  13. import android.os.Bundle;
  14. import android.os.Handler;
  15. import android.support.v4.content.LocalBroadcastManager;
  16. import android.view.Menu;
  17. import android.view.MenuItem;
  18. import android.widget.TextView;
  19.  
  20. public class BatteryView extends Activity {
  21.  
  22. private BatteryReceiver batteryReceiver;
  23. private IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  24. public TextView txChargeState, txInformationAge, txPercentCharged;
  25.  
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_battery_view);
  30.  
  31. txChargeState = (TextView) findViewById(R.id.chargeState);
  32. txInformationAge = (TextView) findViewById(R.id.informationAge);
  33. txPercentCharged = (TextView) findViewById(R.id.percentCharged);
  34.  
  35. // Create the battery receiver
  36. batteryReceiver = new BatteryReceiver();
  37. batteryReceiver.setupTextViews(txChargeState,txInformationAge,txPercentCharged);
  38.  
  39. }
  40.  
  41. @Override
  42. protected void onResume() {
  43. super.onResume();
  44.  
  45. // Register the broadcastreceiver for the battery status:
  46. if (batteryReceiver != null) {
  47. registerReceiver(batteryReceiver, intentFilter);
  48. }
  49.  
  50.  
  51. }
  52.  
  53. @Override
  54. protected void onPause() {
  55. super.onPause();
  56.  
  57. // Register the broadcastreceiver here:
  58. if (batteryReceiver != null) {
  59. unregisterReceiver(batteryReceiver);
  60. }
  61.  
  62. }
  63.  
  64. @Override
  65. public boolean onCreateOptionsMenu(Menu menu) {
  66.  
  67. // Inflate the menu; this adds items to the action bar if it is present.
  68. getMenuInflater().inflate(R.menu.battery_view, menu);
  69. return true;
  70. }
  71.  
  72. public void updateTextFields() {
  73. txChargeState.setText( batteryReceiver.getChargeStateString() );
  74. txPercentCharged.setText( batteryReceiver.getCurrentCharge() + "%" );
  75. txInformationAge.setText( batteryReceiver.getInformationFreshnessString() );
  76. }
  77. }
  78.  
  79.  
  80.  
  81.  
  82. ---------------------------
  83. File: BatteryReceiver.java
  84. ---------------------------
  85.  
  86.  
  87. package com.skovmandsit.batteryapp;
  88.  
  89. import android.content.BroadcastReceiver;
  90. import android.content.Context;
  91. import android.content.Intent;
  92. import android.os.BatteryManager;
  93. import android.support.v4.content.LocalBroadcastManager;
  94. import android.widget.TextView;
  95.  
  96.  
  97. public class BatteryReceiver extends BroadcastReceiver {
  98.  
  99. protected enum batteryStates { CHARGING, DISCHARGING, FULL, UNKNOWN }
  100. protected enum powerSources { WIRELESS, AC, USB, UNKNOWN }
  101.  
  102. protected batteryStates currentBatteryState = batteryStates.UNKNOWN;
  103. protected powerSources currentPowerSource = powerSources.UNKNOWN;
  104.  
  105. protected boolean informationIsFresh = false;
  106.  
  107. protected int currentCharge = 0;
  108.  
  109. public TextView txChargeState, txInformationAge, txPercentCharged;
  110.  
  111. public void setupTextViews(TextView mTxChargeState, TextView mTxInformationAge, TextView mTxPercentCharged) {
  112. txChargeState = mTxChargeState;
  113. txInformationAge = mTxInformationAge;
  114. txPercentCharged = mTxPercentCharged;
  115. }
  116.  
  117.  
  118. @Override
  119. public void onReceive(Context context, Intent intent) {
  120.  
  121. // Get battery level and power source ...
  122. currentBatteryState = returnBatteryState(intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1));
  123. currentPowerSource = returnPowerSource(intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1));
  124.  
  125. // Get information age ...
  126. if (isInitialStickyBroadcast()) {
  127. informationIsFresh = false;
  128. } else {
  129. informationIsFresh = true;
  130. }
  131.  
  132. // Get current charge ...
  133. currentCharge = returnCurrentCharge(intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1), intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1));
  134.  
  135.  
  136. txChargeState.setText( getChargeStateString() );
  137. txPercentCharged.setText( getCurrentCharge() + "%" );
  138. txInformationAge.setText( getInformationFreshnessString() );
  139.  
  140. }
  141.  
  142. // Private methods:
  143.  
  144. private int returnCurrentCharge(int level, int scale) {
  145.  
  146. return (int) ((level / (float)scale) * 100);
  147. }
  148.  
  149. private batteryStates returnBatteryState(int batteryState) {
  150.  
  151. switch (batteryState) {
  152. case BatteryManager.BATTERY_STATUS_CHARGING:
  153. return batteryStates.CHARGING;
  154. case BatteryManager.BATTERY_STATUS_DISCHARGING:
  155. return batteryStates.DISCHARGING;
  156. case BatteryManager.BATTERY_STATUS_FULL:
  157. return batteryStates.FULL;
  158. default:
  159. return batteryStates.UNKNOWN;
  160. }
  161.  
  162. }
  163.  
  164. private powerSources returnPowerSource(int powerSource) {
  165.  
  166. switch (powerSource) {
  167. case BatteryManager.BATTERY_PLUGGED_USB:
  168. return powerSources.USB;
  169. case BatteryManager.BATTERY_PLUGGED_AC:
  170. return powerSources.AC;
  171. case BatteryManager.BATTERY_PLUGGED_WIRELESS:
  172. return powerSources.WIRELESS;
  173. default:
  174. return powerSources.UNKNOWN;
  175. }
  176.  
  177. }
  178.  
  179.  
  180. // Public methods / Interface:
  181.  
  182. public String getChargingMethod() {
  183. // Should all be of a kind that says "Charging using: ...."
  184.  
  185. switch (currentPowerSource) {
  186. case USB:
  187. return "USB";
  188. case AC:
  189. return "AC outlet";
  190. case WIRELESS:
  191. return "wireless charger";
  192. default:
  193. return "unknown method";
  194. }
  195.  
  196. }
  197.  
  198. public String getBatteryState() {
  199. // Should all be of a kind that says "Battery is: ...."
  200.  
  201. return currentBatteryState.toString().toLowerCase();
  202. }
  203.  
  204. public String getChargeStateString() {
  205.  
  206. switch (currentBatteryState) {
  207. case CHARGING:
  208. return "Battery is charging using " + getChargingMethod();
  209. case DISCHARGING:
  210. return "Battery is discharging";
  211. case FULL:
  212. return "Battery is full";
  213. default:
  214. return "Battery and charge state unknown";
  215. }
  216.  
  217. }
  218.  
  219. public String getInformationFreshnessString() {
  220. if (informationIsFresh)
  221. return "Information is fresh.";
  222. else
  223. return "Information is from before the app started";
  224. }
  225.  
  226. public int getCurrentCharge() {
  227. return currentCharge;
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement