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