From bf6abe967264a838c4151809a5b361c758a9ccdd Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Thu, 22 Jan 2015 22:49:50 +0100 Subject: [PATCH] Quick and dirty support for incoming calls notification the Pebble way Incoming calls are no longer send as simple notifications but properly as incoming calls. The Pebble will vibrate until the call is taken or dismissed. It is not yet possible to dismiss the call using the Pebble button. --- app/src/main/AndroidManifest.xml | 10 ++++--- .../BluetoothCommunicationService.java | 14 +++++++-- .../gadgetbridge/NotificationListener.java | 6 +++- .../gadgetbridge/PebbleProtocol.java | 5 ++-- .../gadgetbridge/PhoneCallReceiver.java | 29 +++++++++++++++++++ 5 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/PhoneCallReceiver.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c135f1631..843efd562 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,10 +3,8 @@ package="nodomain.freeyourgadget.gadgetbridge"> - + + + + + + - diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java index 75ad9dc07..a4616ae9d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/BluetoothCommunicationService.java @@ -30,7 +30,9 @@ public class BluetoothCommunicationService extends Service { public static final String ACTION_STOP = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.stop"; public static final String ACTION_SENDMESSAGE - = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.sendbluetoothmessage"; + = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.sendmessage"; + public static final String ACTION_INCOMINGCALL + = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.incomingcall"; public static final String ACTION_SETTIME = "nodomain.freeyourgadget.gadgetbride.bluetoothcommunicationservice.action.settime"; @@ -121,6 +123,14 @@ public class BluetoothCommunicationService extends Service { byte[] msg = PebbleProtocol.encodeSMS(title, content); mBtSocketIoThread.write(msg); } + } else if (intent.getAction().equals(ACTION_INCOMINGCALL)) { + String phoneNumber = intent.getStringExtra("incomingcall_phonenumber"); + byte phoneState = intent.getByteExtra("incomingcall_state", (byte) 0); + + if (mBtSocketIoThread != null) { + byte[] msg = PebbleProtocol.encodeIncomingCall(phoneNumber, phoneNumber, phoneState); + mBtSocketIoThread.write(msg); + } } else if (intent.getAction().equals(ACTION_SETTIME)) { if (mBtSocketIoThread != null) { byte[] msg = PebbleProtocol.encodeSetTime(-1); @@ -220,7 +230,7 @@ public class BluetoothCommunicationService extends Service { if (length == 1 && endpoint == PebbleProtocol.ENDPOINT_PHONEVERSION) { Log.i(TAG, "Pebble asked for Phone/App Version - repLYING!"); write(PebbleProtocol.encodePhoneVersion(PebbleProtocol.PHONEVERSION_REMOTE_OS_ANDROID)); - } else { + } else if (endpoint != PebbleProtocol.ENDPOINT_DATALOG) { Log.i(TAG, "unhandled message to endpoint " + endpoint + " (" + bytes + " bytes)"); } try { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/NotificationListener.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/NotificationListener.java index 8d52d671d..82632e62c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/NotificationListener.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/NotificationListener.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.os.Bundle; import android.service.notification.NotificationListenerService; import android.service.notification.StatusBarNotification; +import android.util.Log; public class NotificationListener extends NotificationListenerService { @@ -28,9 +29,12 @@ public class NotificationListener extends NotificationListenerService { * This includes keyboard selection message, usb connection messages, etc * Hope it does not filter out too much, we will see... */ - if (sbn.getPackageName().equals("android")) + String source = sbn.getPackageName(); + if (source.equals("android") || source.equals("com.android.dialer")) return; + Log.i(TAG, sbn.getPackageName()); + Bundle extras = notification.extras; String title = extras.getString(Notification.EXTRA_TITLE); String content = ""; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/PebbleProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/PebbleProtocol.java index a5d4f1252..6c616e23c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/PebbleProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/PebbleProtocol.java @@ -25,6 +25,7 @@ public class PebbleProtocol { static final short ENDPOINT_SYSREG = 5000; static final short ENDPOINT_FCTREG = 5001; static final short ENDPOINT_APPMANAGER = 6000; + static final short ENDPOINT_DATALOG = 6778; static final short ENDPOINT_RUNKEEPER = 7000; static final short ENDPOINT_SCREENSHOT = 8000; static final short ENDPOINT_PUTBYTES = (short) 48879; @@ -130,10 +131,10 @@ public class PebbleProtocol { return buf.array(); } - public static byte[] encodeIncomingCall(String number, String name) { + public static byte[] encodeIncomingCall(String number, String name, byte state) { String cookie = "000"; // That's a dirty trick to make the cookie part 4 bytes long :P String[] parts = {cookie, number, name}; - return encodeMessage(ENDPOINT_PHONECONTROL, PHONECONTROL_INCOMINGCALL, parts); + return encodeMessage(ENDPOINT_PHONECONTROL, state, parts); } public static byte[] encodePhoneVersion(byte os) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/PhoneCallReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/PhoneCallReceiver.java new file mode 100644 index 000000000..769ae5ae7 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/PhoneCallReceiver.java @@ -0,0 +1,29 @@ +package nodomain.freeyourgadget.gadgetbridge; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.telephony.TelephonyManager; + +public class PhoneCallReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + String phoneState = intent.getStringExtra(TelephonyManager.EXTRA_STATE); + byte state = 0; + if (phoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) { + state = PebbleProtocol.PHONECONTROL_INCOMINGCALL; + } else if (phoneState.equals(TelephonyManager.EXTRA_STATE_IDLE) || phoneState.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) { + state = PebbleProtocol.PHONECONTROL_END; + } + + if (state != 0) { + String phoneNumber = intent.hasExtra(TelephonyManager.EXTRA_INCOMING_NUMBER) ? intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER) : ""; + Intent startIntent = new Intent(context, BluetoothCommunicationService.class); + startIntent.setAction(BluetoothCommunicationService.ACTION_INCOMINGCALL); + startIntent.putExtra("incomingcall_phonenumber", phoneNumber); + startIntent.putExtra("incomingcall_state", state); + context.startService(startIntent); + } + } +}