From 46bbab7df05845d37809040c0869a22138f5900e Mon Sep 17 00:00:00 2001 From: Andreas Shimokawa Date: Sat, 9 Jan 2016 16:07:22 +0100 Subject: [PATCH] rename IDSenderLookup to LimitedQueue and store Object instead of String --- .../gadgetbridge/GBApplication.java | 23 +++---------------- .../service/AbstractDeviceSupport.java | 2 +- .../service/DeviceCommunicationService.java | 1 - .../devices/pebble/PebbleProtocol.java | 2 +- ...{IDSenderLookup.java => LimitedQueue.java} | 16 ++++++++----- 5 files changed, 15 insertions(+), 29 deletions(-) rename app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/{IDSenderLookup.java => LimitedQueue.java} (57%) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 3c854781a..05c1ac7a9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -29,7 +29,7 @@ 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; +import nodomain.freeyourgadget.gadgetbridge.util.LimitedQueue; //import nodomain.freeyourgadget.gadgetbridge.externalevents.BluetoothConnectReceiver; @@ -45,7 +45,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(); + private static LimitedQueue mIDSenderLookup = new LimitedQueue(16); public static final String ACTION_QUIT = "nodomain.freeyourgadget.gadgetbridge.gbapplication.action.quit"; @@ -61,10 +61,7 @@ public class GBApplication extends Application { } }; - //private BluetoothConnectReceiver systemBTReceiver = new BluetoothConnectReceiver(); - private void quit() { - //unregisterSystemBTReceiver(); GB.removeAllNotifications(this); } @@ -103,25 +100,11 @@ public class GBApplication extends Application { filterLocal.addAction(ACTION_QUIT); LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal); - //registerSystemBTReceiver(); // for testing DB stuff // SQLiteDatabase db = mActivityDatabaseHandler.getWritableDatabase(); // db.close(); } - /* - private void registerSystemBTReceiver() { - IntentFilter filter = new IntentFilter(); - filter.addAction("android.bluetooth.device.action.ACL_CONNECTED"); - filter.addAction("android.bluetooth.device.action.ACL_CONNECTED"); - registerReceiver(systemBTReceiver, filter); - } - - private void unregisterSystemBTReceiver() { - unregisterReceiver(systemBTReceiver); - } - */ - private void setupExceptionHandler() { LoggingExceptionHandler handler = new LoggingExceptionHandler(Thread.getDefaultUncaughtExceptionHandler()); Thread.setDefaultUncaughtExceptionHandler(handler); @@ -259,7 +242,7 @@ public class GBApplication extends Application { return result; } - public static IDSenderLookup getIDSenderLookup() { + public static LimitedQueue getIDSenderLookup() { return mIDSenderLookup; } } 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 f9d3b8144..09a0ddaab 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java @@ -229,7 +229,7 @@ public abstract class AbstractDeviceSupport implements DeviceSupport { action = NotificationListener.ACTION_MUTE; break; case REPLY: - String phoneNumber = GBApplication.getIDSenderLookup().lookup(deviceEvent.handle); + String phoneNumber = (String) GBApplication.getIDSenderLookup().lookup(deviceEvent.handle); if (phoneNumber != null) { LOG.info("got notfication reply for " + phoneNumber + " : " + deviceEvent.reply); SmsManager.getDefault().sendTextMessage(phoneNumber, null, deviceEvent.reply, null, 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 f84014e9b..20bff1839 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -39,7 +39,6 @@ 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; 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 896be5124..bb606b092 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 @@ -1590,7 +1590,7 @@ public class PebbleProtocol extends GBDeviceProtocol { byte[] reply = new byte[length]; buf.get(reply); // FIXME: this does not belong here, but we want at least check if there is no chance at all to send out the SMS later before we report success - String phoneNumber = GBApplication.getIDSenderLookup().lookup(id); + String phoneNumber = (String) GBApplication.getIDSenderLookup().lookup(id); if (phoneNumber != null) { devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.REPLY; devEvtNotificationControl.reply = new String(reply); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/IDSenderLookup.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LimitedQueue.java similarity index 57% rename from app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/IDSenderLookup.java rename to app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LimitedQueue.java index 4c9143a8b..4058762bc 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/IDSenderLookup.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LimitedQueue.java @@ -4,21 +4,25 @@ import android.util.Pair; import java.util.LinkedList; -public class IDSenderLookup { - private static final int LIMIT = 16; +public class LimitedQueue { + private final int limit; private LinkedList list = new LinkedList<>(); - public void add(int id, String sender) { - if (list.size() > LIMIT - 1) { + public LimitedQueue(int limit) { + this.limit = limit; + } + + public void add(int id, Object sender) { + if (list.size() > limit - 1) { list.removeFirst(); } list.add(new Pair<>(id, sender)); } - public String lookup(int id) { + public Object lookup(int id) { for (Pair entry : list) { if (id == (Integer) entry.first) { - return (String) entry.second; + return entry.second; } } return null;