From de5f30ae97466638523ca899dd5eb1688d43dddd Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Mon, 14 Dec 2015 23:31:31 +0100 Subject: [PATCH] WIP: Work towards SMS replies / canned replies, round 3 - put random id/phone number pair into limited lookup list (last 16 sms messages) when sms arrives - lookup the phone number when replying from the a device THIS STILL DOES NOT DO ANYTHING USEFUL --- .../gadgetbridge/GBApplication.java | 10 +++++-- .../activities/DebugActivity.java | 2 +- .../service/AbstractDeviceSupport.java | 6 ++++- .../service/DeviceCommunicationService.java | 8 ++++++ .../devices/pebble/PebbleProtocol.java | 2 +- .../gadgetbridge/util/IDSenderLookup.java | 26 +++++++++++++++++++ 6 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/IDSenderLookup.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 11390e350..40acb8727 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -1,7 +1,6 @@ package nodomain.freeyourgadget.gadgetbridge; import android.app.Application; -import android.bluetooth.BluetoothDevice; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -26,11 +25,11 @@ import java.util.concurrent.locks.ReentrantLock; import nodomain.freeyourgadget.gadgetbridge.database.ActivityDatabaseHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBConstants; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; -import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService; import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; +import nodomain.freeyourgadget.gadgetbridge.util.IDSenderLookup; /** * Main Application class that initializes and provides access to certain things like @@ -44,6 +43,7 @@ public class GBApplication extends Application { private static final Lock dbLock = new ReentrantLock(); private static DeviceService deviceService; private static SharedPreferences sharedPrefs; + private static IDSenderLookup mIDSenderLookup = new IDSenderLookup(); public static final String ACTION_QUIT = "nodomain.freeyourgadget.gadgetbridge.gbapplication.action.quit"; @@ -189,6 +189,7 @@ public class GBApplication extends Application { public static boolean isRunningOnKitkatOrLater() { return VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; } + public static boolean isRunningLollipopOrLater() { return VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; } @@ -226,6 +227,7 @@ public class GBApplication extends Application { /** * Deletes the entire Activity database and recreates it with empty tables. + * * @return true on successful deletion */ public static synchronized boolean deleteActivityDatabase() { @@ -237,4 +239,8 @@ public class GBApplication extends Application { mActivityDatabaseHandler = new ActivityDatabaseHandler(getContext()); return result; } + + public static IDSenderLookup getIDSenderLookup() { + return mIDSenderLookup; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java index c260735a7..1e7cdc00d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java @@ -76,7 +76,7 @@ public class DebugActivity extends Activity { @Override public void onClick(View v) { NotificationSpec notificationSpec = new NotificationSpec(); - notificationSpec.phoneNumber = getResources().getText(R.string.app_name).toString(); + notificationSpec.phoneNumber = editContent.getText().toString(); notificationSpec.body = editContent.getText().toString(); notificationSpec.type = NotificationType.SMS; notificationSpec.id = -1; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java index 40988d6e8..2886d9358 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java @@ -20,6 +20,7 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.AppManagerActivity; import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsHost; @@ -227,7 +228,10 @@ public abstract class AbstractDeviceSupport implements DeviceSupport { action = NotificationListener.ACTION_MUTE; break; case REPLY: - GB.toast(context, "got notfication reply: " + deviceEvent.reply, 2, GB.INFO); + String phoneNumber = GBApplication.getIDSenderLookup().lookup(deviceEvent.handle); + if (phoneNumber != null) { + GB.toast(context, "got notfication reply for " + phoneNumber + " : " + deviceEvent.reply, 2, GB.INFO); + } break; } if (action != null) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index 847a035db..55c7d9eea 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -21,8 +21,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; +import java.util.Random; import java.util.UUID; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.externalevents.K9Receiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.MusicPlaybackReceiver; @@ -37,6 +39,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; import nodomain.freeyourgadget.gadgetbridge.model.ServiceCommand; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.GB; +import nodomain.freeyourgadget.gadgetbridge.util.IDSenderLookup; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CALLSTATE; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_CONNECT; @@ -94,6 +97,8 @@ public class DeviceCommunicationService extends Service { private MusicPlaybackReceiver mMusicPlaybackReceiver = null; private TimeChangeReceiver mTimeChangeReceiver = null; + private Random mRandom = new Random(); + private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -218,6 +223,9 @@ public class DeviceCommunicationService extends Service { if (notificationSpec.type == NotificationType.SMS && notificationSpec.phoneNumber != null) { notificationSpec.sender = getContactDisplayNameByNumber(notificationSpec.phoneNumber); + notificationSpec.id = mRandom.nextInt(); // FIXME: add this in external SMS Receiver? + GBApplication.getIDSenderLookup().add(notificationSpec.id, notificationSpec.phoneNumber); + // NOTE: maybe not where it belongs SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); if (sharedPrefs.getBoolean("pebble_force_untested", false)) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java index 0b913c9e7..a0be93339 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/pebble/PebbleProtocol.java @@ -398,7 +398,7 @@ public class PebbleProtocol extends GBDeviceProtocol { @Override public byte[] encodeNotification(NotificationSpec notificationSpec) { - boolean hasHandle = notificationSpec.id != -1; + boolean hasHandle = notificationSpec.id != -1 && notificationSpec.phoneNumber == null; int id = notificationSpec.id != -1 ? notificationSpec.id : mRandom.nextInt(); String title; String subtitle = null; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/IDSenderLookup.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/IDSenderLookup.java new file mode 100644 index 000000000..4c9143a8b --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/IDSenderLookup.java @@ -0,0 +1,26 @@ +package nodomain.freeyourgadget.gadgetbridge.util; + +import android.util.Pair; + +import java.util.LinkedList; + +public class IDSenderLookup { + private static final int LIMIT = 16; + private LinkedList list = new LinkedList<>(); + + public void add(int id, String sender) { + if (list.size() > LIMIT - 1) { + list.removeFirst(); + } + list.add(new Pair<>(id, sender)); + } + + public String lookup(int id) { + for (Pair entry : list) { + if (id == (Integer) entry.first) { + return (String) entry.second; + } + } + return null; + } +}