diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 56ec02f93..496ea4fde 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -124,7 +124,7 @@ public class GBApplication extends Application { //if preferences have to be migrated, increment the following and add the migration logic in migratePrefs below; see http://stackoverflow.com/questions/16397848/how-can-i-migrate-android-preferences-with-a-new-version private static final int CURRENT_PREFS_VERSION = 28; - private static LimitedQueue mIDSenderLookup = new LimitedQueue(16); + private static final LimitedQueue mIDSenderLookup = new LimitedQueue<>(16); private static Prefs prefs; private static GBPrefs gbPrefs; private static LockHandler lockHandler; @@ -1481,7 +1481,7 @@ public class GBApplication extends Application { LocalBroadcastManager.getInstance(context).sendBroadcast(intent); } - public static LimitedQueue getIDSenderLookup() { + public static LimitedQueue getIDSenderLookup() { return mIDSenderLookup; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractWeekChartFragment.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractWeekChartFragment.java index d8c22f75c..48fcff7ea 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractWeekChartFragment.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/AbstractWeekChartFragment.java @@ -371,14 +371,14 @@ public abstract class AbstractWeekChartFragment extends AbstractActivityChartFra private ActivityAmounts getActivityAmountsForDay(DBHandler db, Calendar day, GBDevice device) { - LimitedQueue activityAmountCache = null; + LimitedQueue activityAmountCache = null; ActivityAmounts amounts = null; Activity activity = getActivity(); int key = (int) (day.getTimeInMillis() / 1000) + (mOffsetHours * 3600); if (activity != null) { activityAmountCache = ((ActivityChartsActivity) activity).mActivityAmountCache; - amounts = (ActivityAmounts) (activityAmountCache.lookup(key)); + amounts = activityAmountCache.lookup(key); } if (amounts == null) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityChartsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityChartsActivity.java index d4b702e5d..d3d818bd7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityChartsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/charts/ActivityChartsActivity.java @@ -33,12 +33,13 @@ import nodomain.freeyourgadget.gadgetbridge.activities.AbstractFragmentPagerAdap import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; +import nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts; import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes; import nodomain.freeyourgadget.gadgetbridge.util.LimitedQueue; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class ActivityChartsActivity extends AbstractChartsActivity { - LimitedQueue mActivityAmountCache = new LimitedQueue(60); + LimitedQueue mActivityAmountCache = new LimitedQueue<>(60); @Override protected AbstractFragmentPagerAdapter createFragmentPagerAdapter(final FragmentManager fragmentManager) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java index 389ed4364..e9fbfb2f4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/NotificationListener.java @@ -83,17 +83,6 @@ import nodomain.freeyourgadget.gadgetbridge.util.MediaManager; import nodomain.freeyourgadget.gadgetbridge.util.NotificationUtils; import nodomain.freeyourgadget.gadgetbridge.util.PebbleUtils; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Objects; -import java.util.Set; -import java.util.concurrent.TimeUnit; import static nodomain.freeyourgadget.gadgetbridge.activities.NotificationFilterActivity.NOTIFICATION_FILTER_MODE_BLACKLIST; import static nodomain.freeyourgadget.gadgetbridge.activities.NotificationFilterActivity.NOTIFICATION_FILTER_MODE_WHITELIST; @@ -115,9 +104,9 @@ public class NotificationListener extends NotificationListenerService { public static final String ACTION_REPLY = "nodomain.freeyourgadget.gadgetbridge.notificationlistener.action.reply"; - private final LimitedQueue mActionLookup = new LimitedQueue(32); - private final LimitedQueue mPackageLookup = new LimitedQueue(64); - private final LimitedQueue mNotificationHandleLookup = new LimitedQueue(128); + private final LimitedQueue mActionLookup = new LimitedQueue<>(32); + private final LimitedQueue mPackageLookup = new LimitedQueue<>(64); + private final LimitedQueue mNotificationHandleLookup = new LimitedQueue<>(128); private final HashMap notificationBurstPrevention = new HashMap<>(); private final HashMap notificationOldRepeatPrevention = new HashMap<>(); @@ -158,7 +147,7 @@ public class NotificationListener extends NotificationListenerService { case ACTION_OPEN: { StatusBarNotification[] sbns = NotificationListener.this.getActiveNotifications(); - Long ts = (Long) mNotificationHandleLookup.lookup(handle); + Long ts = mNotificationHandleLookup.lookup(handle); if (ts == null) { LOG.info("could not lookup handle for open action"); break; @@ -179,7 +168,7 @@ public class NotificationListener extends NotificationListenerService { break; } case ACTION_MUTE: - String packageName = (String) mPackageLookup.lookup(handle); + String packageName = mPackageLookup.lookup(handle); if (packageName == null) { LOG.info("could not lookup handle for mute action"); break; @@ -193,7 +182,7 @@ public class NotificationListener extends NotificationListenerService { break; case ACTION_DISMISS: { StatusBarNotification[] sbns = NotificationListener.this.getActiveNotifications(); - Long ts = (Long) mNotificationHandleLookup.lookup(handle); + Long ts = mNotificationHandleLookup.lookup(handle); if (ts == null) { LOG.info("could not lookup handle for dismiss action"); break; @@ -210,7 +199,7 @@ public class NotificationListener extends NotificationListenerService { NotificationListener.this.cancelAllNotifications(); break; case ACTION_REPLY: - NotificationCompat.Action wearableAction = (NotificationCompat.Action) mActionLookup.lookup(handle); + NotificationCompat.Action wearableAction = mActionLookup.lookup(handle); String reply = intent.getStringExtra("reply"); if (wearableAction != null) { PendingIntent actionIntent = wearableAction.getActionIntent(); @@ -766,17 +755,16 @@ public class NotificationListener extends NotificationListenerService { if (shouldIgnoreNotification(sbn, true)) return; // Build list of all currently active notifications - ArrayList activeNotificationsIds = new ArrayList(); + ArrayList activeNotificationsIds = new ArrayList<>(); for (StatusBarNotification notification : getActiveNotifications()) { - Object o = mNotificationHandleLookup.lookupByValue(notification.getPostTime()); - if (o != null) { - int id = (int) o; + Integer id = mNotificationHandleLookup.lookupByValue(notification.getPostTime()); + if (id != null) { activeNotificationsIds.add(id); } } // Build list of notifications that aren't active anymore - ArrayList notificationsToRemove = new ArrayList(); + ArrayList notificationsToRemove = new ArrayList<>(); for (int notificationId : notificationsActive) { if (!activeNotificationsIds.contains(notificationId)) { notificationsToRemove.add(notificationId); 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 84e371f72..e9a89ac27 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/AbstractDeviceSupport.java @@ -461,7 +461,7 @@ public abstract class AbstractDeviceSupport implements DeviceSupport { break; case REPLY: if (deviceEvent.phoneNumber == null) { - deviceEvent.phoneNumber = (String) GBApplication.getIDSenderLookup().lookup((int) (deviceEvent.handle >> 4)); + deviceEvent.phoneNumber = GBApplication.getIDSenderLookup().lookup((int) (deviceEvent.handle >> 4)); } if (deviceEvent.phoneNumber != null) { LOG.info("Got notification reply for SMS from " + deviceEvent.phoneNumber + " : " + deviceEvent.reply); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java index 5c051bbe6..c0cee6b81 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/banglejs/BangleJSDeviceSupport.java @@ -159,7 +159,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { /// Last battery percentage reported (or -1) to help with smoothing reported battery levels private int lastBatteryPercent = -1; - private final LimitedQueue/*Long*/ mNotificationReplyAction = new LimitedQueue(16); + private final LimitedQueue mNotificationReplyAction = new LimitedQueue<>(16); private boolean gpsUpdateSetup = false; @@ -623,7 +623,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { /* REPLY responses don't use the ID from the event (MUTE/etc seem to), but instead * they use a handle that was provided in an action list on the onNotification.. event */ if (deviceEvtNotificationControl.event == GBDeviceEventNotificationControl.Event.REPLY) { - Long foundHandle = (Long)mNotificationReplyAction.lookup((int)deviceEvtNotificationControl.handle); + Long foundHandle = mNotificationReplyAction.lookup((int)deviceEvtNotificationControl.handle); if (foundHandle!=null) deviceEvtNotificationControl.handle = foundHandle; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/zeppos/services/ZeppOsNotificationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/zeppos/services/ZeppOsNotificationService.java index 715d7f1dd..f6c12c56c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/zeppos/services/ZeppOsNotificationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huami/zeppos/services/ZeppOsNotificationService.java @@ -70,7 +70,7 @@ public class ZeppOsNotificationService extends AbstractZeppOsService { // Keep track of Notification ID -> action handle, as BangleJSDeviceSupport. // This needs to be simplified. - private final LimitedQueue mNotificationReplyAction = new LimitedQueue(16); + private final LimitedQueue mNotificationReplyAction = new LimitedQueue<>(16); private final ZeppOsFileTransferService fileTransferService; @@ -98,7 +98,7 @@ public class ZeppOsNotificationService extends AbstractZeppOsService { case NOTIFICATION_CMD_REPLY: // TODO make this configurable? final int notificationId = BLETypeConversions.toUint32(subarray(payload, 1, 5)); - final Long replyHandle = (Long) mNotificationReplyAction.lookup(notificationId); + final Long replyHandle = mNotificationReplyAction.lookup(notificationId); if (replyHandle == null) { LOG.warn("Failed to find reply handle for notification ID {}", notificationId); return; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LimitedQueue.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LimitedQueue.java index 846f94e0d..df51b110d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LimitedQueue.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LimitedQueue.java @@ -22,41 +22,41 @@ import android.util.Pair; import java.util.Iterator; import java.util.LinkedList; -public class LimitedQueue { +public class LimitedQueue { private final int limit; - private LinkedList list = new LinkedList<>(); + private final LinkedList> list = new LinkedList<>(); - public LimitedQueue(int limit) { + public LimitedQueue(final int limit) { this.limit = limit; } - synchronized public void add(int id, Object obj) { + synchronized public void add(final K id, final V obj) { if (list.size() > limit - 1) { list.removeFirst(); } list.add(new Pair<>(id, obj)); } - synchronized public void remove(int id) { - for (Iterator iter = list.iterator(); iter.hasNext(); ) { - Pair pair = iter.next(); - if ((Integer) pair.first == id) { - iter.remove(); + synchronized public void remove(final K id) { + for (final Iterator> it = list.iterator(); it.hasNext(); ) { + Pair pair = it.next(); + if (id.equals(pair.first)) { + it.remove(); } } } - synchronized public Object lookup(int id) { - for (Pair entry : list) { - if (id == (Integer) entry.first) { + synchronized public V lookup(final K id) { + for (final Pair entry : list) { + if (id.equals(entry.first)) { return entry.second; } } return null; } - synchronized public Object lookupByValue(Object value){ - for (Pair entry : list) { + synchronized public K lookupByValue(final V value){ + for (final Pair entry : list) { if (value.equals(entry.second)) { return entry.first; } diff --git a/app/src/main/res/xml/devicesettings_autoremove_notifications.xml b/app/src/main/res/xml/devicesettings_autoremove_notifications.xml index 0ecb6d6aa..682b7156c 100644 --- a/app/src/main/res/xml/devicesettings_autoremove_notifications.xml +++ b/app/src/main/res/xml/devicesettings_autoremove_notifications.xml @@ -6,4 +6,4 @@ android:key="autoremove_notifications" android:summary="@string/pref_summary_autoremove_notifications" android:title="@string/pref_title_autoremove_notifications" /> - \ No newline at end of file +