From 4176d9b15eb963998454976d0ad6caa695cedba7 Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Fri, 26 Jul 2013 16:23:26 +0200 Subject: [PATCH] Added battery reporting interface --- KdeConnect/src/main/AndroidManifest.xml | 1 + .../org/kde/connect/BackgroundService.java | 6 ++ .../ComputerLinks/TcpComputerLink.java | 2 +- .../src/main/java/org/kde/connect/Device.java | 2 +- .../java/org/kde/connect/NetworkPackage.java | 4 ++ .../BasePackageInterface.java | 4 +- .../BatteryMonitorPackageInterface.java | 55 +++++++++++++++++++ .../CallPackageInterface.java | 3 +- .../ClipboardPackageInterface.java | 4 +- .../PingPackageInterface.java | 5 +- Android.iml => kdeconnect-android.iml | 2 +- 11 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/BatteryMonitorPackageInterface.java rename Android.iml => kdeconnect-android.iml (77%) diff --git a/KdeConnect/src/main/AndroidManifest.xml b/KdeConnect/src/main/AndroidManifest.xml index ef52d462..f6056a2c 100644 --- a/KdeConnect/src/main/AndroidManifest.xml +++ b/KdeConnect/src/main/AndroidManifest.xml @@ -55,6 +55,7 @@ + diff --git a/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java b/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java index 621aa88d..f5cc1b3d 100644 --- a/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java +++ b/KdeConnect/src/main/java/org/kde/connect/BackgroundService.java @@ -12,6 +12,7 @@ import org.kde.connect.ComputerLinks.BaseComputerLink; import org.kde.connect.LinkProviders.AvahiTcpLinkProvider; import org.kde.connect.LinkProviders.BaseLinkProvider; import org.kde.connect.PackageInterfaces.BasePackageInterface; +import org.kde.connect.PackageInterfaces.BatteryMonitorPackageInterface; import org.kde.connect.PackageInterfaces.CallPackageInterface; import org.kde.connect.PackageInterfaces.ClipboardPackageInterface; import org.kde.connect.PackageInterfaces.PingPackageInterface; @@ -43,6 +44,11 @@ public class BackgroundService extends Service { if (settings.getBoolean("clipboard_interface", true)) { emitters.add(new ClipboardPackageInterface(getApplicationContext())); } + + if (settings.getBoolean("battery_interface", true)) { + emitters.add(new BatteryMonitorPackageInterface(getApplicationContext())); + } + } public void registerLinkProviders() { diff --git a/KdeConnect/src/main/java/org/kde/connect/ComputerLinks/TcpComputerLink.java b/KdeConnect/src/main/java/org/kde/connect/ComputerLinks/TcpComputerLink.java index d37d3582..73539c9d 100644 --- a/KdeConnect/src/main/java/org/kde/connect/ComputerLinks/TcpComputerLink.java +++ b/KdeConnect/src/main/java/org/kde/connect/ComputerLinks/TcpComputerLink.java @@ -63,7 +63,7 @@ public class TcpComputerLink extends BaseComputerLink { //Wait unit it is connected (this call makes it blocking, but we are on a thread anyway) future.awaitUninterruptibly(); if (!future.isConnected()) Log.e("TcpComputerLink","Could not connect"); - Log.e("TcpComputerLink","connected"); + else Log.e("TcpComputerLink","connected"); session = future.getSession(); } }).run(); diff --git a/KdeConnect/src/main/java/org/kde/connect/Device.java b/KdeConnect/src/main/java/org/kde/connect/Device.java index d5cf02a4..a1a8420e 100644 --- a/KdeConnect/src/main/java/org/kde/connect/Device.java +++ b/KdeConnect/src/main/java/org/kde/connect/Device.java @@ -85,7 +85,7 @@ public class Device { return null; } }.execute(); - Log.e("Device","/sendPackage"); + return true; //FIXME } diff --git a/KdeConnect/src/main/java/org/kde/connect/NetworkPackage.java b/KdeConnect/src/main/java/org/kde/connect/NetworkPackage.java index 5d999b92..3d098946 100644 --- a/KdeConnect/src/main/java/org/kde/connect/NetworkPackage.java +++ b/KdeConnect/src/main/java/org/kde/connect/NetworkPackage.java @@ -15,6 +15,7 @@ public class NetworkPackage { public final static String PACKAGE_TYPE_IDENTITY = "kdeconnect.identity"; public final static String PACKAGE_TYPE_PING = "kdeconnect.ping"; public final static String PACKAGE_TYPE_NOTIFICATION = "kdeconnect.notification"; + public final static String PACKAGE_TYPE_BATTERY = "kdeconnect.battery"; public final static String PACKAGE_TYPE_CALL = "kdeconnect.call"; public final static String PACKAGE_TYPE_CLIPBOARD = "kdeconnect.clipboard"; @@ -51,6 +52,9 @@ public class NetworkPackage { public boolean getBoolean(String key) { return mBody.optBoolean(key,false); } public boolean getBoolean(String key, boolean defaultValue) { return mBody.optBoolean(key,defaultValue); } public void set(String key, boolean value) { try { mBody.put(key,value); } catch(Exception e) { } } + public double getDouble(String key) { return mBody.optDouble(key,Double.NaN); } + public double getDouble(String key, double defaultValue) { return mBody.optDouble(key,defaultValue); } + public void set(String key, double value) { try { mBody.put(key,value); } catch(Exception e) { } } public String serialize() { JSONObject jo = new JSONObject(); diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/BasePackageInterface.java b/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/BasePackageInterface.java index 18add36d..09b6b1d0 100644 --- a/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/BasePackageInterface.java +++ b/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/BasePackageInterface.java @@ -34,6 +34,6 @@ public abstract class BasePackageInterface { return true; } - //To override - public abstract void onPackageReceived(Device d, NetworkPackage np); + //To override, returns true if package was handled + public abstract boolean onPackageReceived(Device d, NetworkPackage np); } diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/BatteryMonitorPackageInterface.java b/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/BatteryMonitorPackageInterface.java new file mode 100644 index 00000000..87300986 --- /dev/null +++ b/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/BatteryMonitorPackageInterface.java @@ -0,0 +1,55 @@ +package org.kde.connect.PackageInterfaces; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.BatteryManager; +import android.util.Log; + +import org.kde.connect.Device; +import org.kde.connect.NetworkPackage; + +public class BatteryMonitorPackageInterface extends BasePackageInterface { + + public BatteryMonitorPackageInterface(final Context context) { + final IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); + context.registerReceiver(new BroadcastReceiver() { + public int oldCurrentCharge; + public boolean oldIsCharging; + @Override + public void onReceive(Context context, Intent intent) { + + Log.e("BatteryMonitorPackageInterface", "Battery event"); + + boolean isCharging = (0 != intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0)); + + int currentCharge = 100; + int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); + if (status != BatteryManager.BATTERY_STATUS_FULL) { + Intent batteryStatus = context.registerReceiver(null, ifilter); + int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); + int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); + currentCharge = level*100 / scale; + } + + //Only notify if change is meaningful enough + if (isCharging != oldIsCharging || currentCharge != oldCurrentCharge) { + NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_BATTERY); + np.set("isCharging", isCharging); + np.set("currentCharge", currentCharge); + sendPackage(np); + oldIsCharging = isCharging; + oldCurrentCharge = currentCharge; + } + } + }, ifilter); + } + + @Override + public boolean onPackageReceived(Device d, NetworkPackage np) { + //Do nothing + return false; + } + +} diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/CallPackageInterface.java b/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/CallPackageInterface.java index 33a962f5..a6b1ba6c 100644 --- a/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/CallPackageInterface.java +++ b/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/CallPackageInterface.java @@ -101,7 +101,8 @@ public class CallPackageInterface extends BasePackageInterface { } @Override - public void onPackageReceived(Device d, NetworkPackage np) { + public boolean onPackageReceived(Device d, NetworkPackage np) { //Do nothing + return false; } } diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/ClipboardPackageInterface.java b/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/ClipboardPackageInterface.java index 1e6017fa..431d0d2c 100644 --- a/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/ClipboardPackageInterface.java +++ b/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/ClipboardPackageInterface.java @@ -37,10 +37,12 @@ public class ClipboardPackageInterface extends BasePackageInterface { } @Override - public void onPackageReceived(Device d, NetworkPackage np) { + public boolean onPackageReceived(Device d, NetworkPackage np) { if (np.getType().equals(NetworkPackage.PACKAGE_TYPE_CLIPBOARD)) { ignore_next_clipboard_change = true; cm.setText(np.getString("content")); + return true; } + return false; } } diff --git a/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/PingPackageInterface.java b/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/PingPackageInterface.java index 8abcc35c..88f33dd4 100644 --- a/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/PingPackageInterface.java +++ b/KdeConnect/src/main/java/org/kde/connect/PackageInterfaces/PingPackageInterface.java @@ -26,7 +26,7 @@ public class PingPackageInterface extends BasePackageInterface { } @Override - public void onPackageReceived(Device d, NetworkPackage np) { + public boolean onPackageReceived(Device d, NetworkPackage np) { Log.e("PingPackageReceiver", "onPackageReceived"); if (np.getType().equals(NetworkPackage.PACKAGE_TYPE_PING)) { Log.e("PingPackageReceiver", "was a ping!"); @@ -42,8 +42,9 @@ public class PingPackageInterface extends BasePackageInterface { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, noti); - + return true; } + return false; } } diff --git a/Android.iml b/kdeconnect-android.iml similarity index 77% rename from Android.iml rename to kdeconnect-android.iml index b996df8c..c2318111 100644 --- a/Android.iml +++ b/kdeconnect-android.iml @@ -1,5 +1,5 @@ - +