Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/BatteryPlugin/BatteryPlugin.java
Show All 31 Lines | |||||
32 | 32 | | |||
33 | 33 | | |||
34 | public class BatteryPlugin extends Plugin { | 34 | public class BatteryPlugin extends Plugin { | ||
35 | 35 | | |||
36 | public final static String PACKAGE_TYPE_BATTERY = "kdeconnect.battery"; | 36 | public final static String PACKAGE_TYPE_BATTERY = "kdeconnect.battery"; | ||
37 | public final static String PACKAGE_TYPE_BATTERY_REQUEST = "kdeconnect.battery.request"; | 37 | public final static String PACKAGE_TYPE_BATTERY_REQUEST = "kdeconnect.battery.request"; | ||
38 | 38 | | |||
39 | // keep these fields in sync with kdeconnect-kded:BatteryPlugin.h:ThresholdBatteryEvent | 39 | // keep these fields in sync with kdeconnect-kded:BatteryPlugin.h:ThresholdBatteryEvent | ||
40 | private static final int THRESHOLD_EVENT_NONE= 0; | 40 | private static final int THRESHOLD_EVENT_NONE = 0; | ||
41 | private static final int THRESHOLD_EVENT_BATTERY_LOW = 1; | 41 | private static final int THRESHOLD_EVENT_BATTERY_LOW = 1; | ||
42 | 42 | | |||
43 | private NetworkPackage batteryInfo = new NetworkPackage(PACKAGE_TYPE_BATTERY); | 43 | private NetworkPackage batteryInfo = new NetworkPackage(PACKAGE_TYPE_BATTERY); | ||
44 | 44 | | |||
45 | @Override | 45 | @Override | ||
46 | public String getDisplayName() { | 46 | public String getDisplayName() { | ||
47 | return context.getResources().getString(R.string.pref_plugin_battery); | 47 | return context.getResources().getString(R.string.pref_plugin_battery); | ||
48 | } | 48 | } | ||
49 | 49 | | |||
50 | @Override | 50 | @Override | ||
51 | public String getDescription() { | 51 | public String getDescription() { | ||
52 | return context.getResources().getString(R.string.pref_plugin_battery_desc); | 52 | return context.getResources().getString(R.string.pref_plugin_battery_desc); | ||
53 | } | 53 | } | ||
54 | 54 | | |||
55 | private final BroadcastReceiver receiver = new BroadcastReceiver() { | 55 | private final BroadcastReceiver receiver = new BroadcastReceiver() { | ||
56 | @Override | 56 | @Override | ||
57 | public void onReceive(Context context, Intent batteryIntent) { | 57 | public void onReceive(Context context, Intent batteryIntent) { | ||
58 | 58 | | |||
59 | int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); | 59 | int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); | ||
60 | int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 1); | 60 | int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 1); | ||
61 | int plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); | 61 | int plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); | ||
62 | 62 | | |||
63 | int currentCharge = (level == -1)? batteryInfo.getInt("currentCharge") : level*100 / scale; | 63 | int currentCharge = (level == -1) ? batteryInfo.getInt("currentCharge") : level * 100 / scale; | ||
64 | boolean isCharging = (plugged == -1)? batteryInfo.getBoolean("isCharging") : (0 != plugged); | 64 | boolean isCharging = (plugged == -1) ? batteryInfo.getBoolean("isCharging") : (0 != plugged); | ||
65 | boolean lowBattery = Intent.ACTION_BATTERY_LOW.equals(batteryIntent.getAction()); | 65 | boolean lowBattery = Intent.ACTION_BATTERY_LOW.equals(batteryIntent.getAction()); | ||
66 | int thresholdEvent = lowBattery? THRESHOLD_EVENT_BATTERY_LOW : THRESHOLD_EVENT_NONE; | 66 | int thresholdEvent = lowBattery ? THRESHOLD_EVENT_BATTERY_LOW : THRESHOLD_EVENT_NONE; | ||
67 | 67 | | |||
68 | if (isCharging == batteryInfo.getBoolean("isCharging") | 68 | if (isCharging == batteryInfo.getBoolean("isCharging") | ||
69 | && currentCharge == batteryInfo.getInt("currentCharge") | 69 | && currentCharge == batteryInfo.getInt("currentCharge") | ||
70 | && thresholdEvent == batteryInfo.getInt("thresholdEvent") | 70 | && thresholdEvent == batteryInfo.getInt("thresholdEvent") | ||
71 | ) { | 71 | ) { | ||
72 | 72 | | |||
73 | //Do not send again if nothing has changed | 73 | //Do not send again if nothing has changed | ||
74 | 74 | | |||
▲ Show 20 Lines • Show All 48 Lines • Show Last 20 Lines |