mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-25 11:26:47 +01:00
Make LimitedQueue generic
This commit is contained in:
parent
4d12ac93e7
commit
ef73ea1bd5
@ -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
|
//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 final int CURRENT_PREFS_VERSION = 28;
|
||||||
|
|
||||||
private static LimitedQueue mIDSenderLookup = new LimitedQueue(16);
|
private static final LimitedQueue<Integer, String> mIDSenderLookup = new LimitedQueue<>(16);
|
||||||
private static Prefs prefs;
|
private static Prefs prefs;
|
||||||
private static GBPrefs gbPrefs;
|
private static GBPrefs gbPrefs;
|
||||||
private static LockHandler lockHandler;
|
private static LockHandler lockHandler;
|
||||||
@ -1481,7 +1481,7 @@ public class GBApplication extends Application {
|
|||||||
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static LimitedQueue getIDSenderLookup() {
|
public static LimitedQueue<Integer, String> getIDSenderLookup() {
|
||||||
return mIDSenderLookup;
|
return mIDSenderLookup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -371,14 +371,14 @@ public abstract class AbstractWeekChartFragment extends AbstractActivityChartFra
|
|||||||
|
|
||||||
private ActivityAmounts getActivityAmountsForDay(DBHandler db, Calendar day, GBDevice device) {
|
private ActivityAmounts getActivityAmountsForDay(DBHandler db, Calendar day, GBDevice device) {
|
||||||
|
|
||||||
LimitedQueue activityAmountCache = null;
|
LimitedQueue<Integer, ActivityAmounts> activityAmountCache = null;
|
||||||
ActivityAmounts amounts = null;
|
ActivityAmounts amounts = null;
|
||||||
|
|
||||||
Activity activity = getActivity();
|
Activity activity = getActivity();
|
||||||
int key = (int) (day.getTimeInMillis() / 1000) + (mOffsetHours * 3600);
|
int key = (int) (day.getTimeInMillis() / 1000) + (mOffsetHours * 3600);
|
||||||
if (activity != null) {
|
if (activity != null) {
|
||||||
activityAmountCache = ((ActivityChartsActivity) activity).mActivityAmountCache;
|
activityAmountCache = ((ActivityChartsActivity) activity).mActivityAmountCache;
|
||||||
amounts = (ActivityAmounts) (activityAmountCache.lookup(key));
|
amounts = activityAmountCache.lookup(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (amounts == null) {
|
if (amounts == null) {
|
||||||
|
@ -33,12 +33,13 @@ import nodomain.freeyourgadget.gadgetbridge.activities.AbstractFragmentPagerAdap
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
|
import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes;
|
import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.LimitedQueue;
|
import nodomain.freeyourgadget.gadgetbridge.util.LimitedQueue;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
||||||
|
|
||||||
public class ActivityChartsActivity extends AbstractChartsActivity {
|
public class ActivityChartsActivity extends AbstractChartsActivity {
|
||||||
LimitedQueue mActivityAmountCache = new LimitedQueue(60);
|
LimitedQueue<Integer, ActivityAmounts> mActivityAmountCache = new LimitedQueue<>(60);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected AbstractFragmentPagerAdapter createFragmentPagerAdapter(final FragmentManager fragmentManager) {
|
protected AbstractFragmentPagerAdapter createFragmentPagerAdapter(final FragmentManager fragmentManager) {
|
||||||
|
@ -83,17 +83,6 @@ import nodomain.freeyourgadget.gadgetbridge.util.MediaManager;
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.util.NotificationUtils;
|
import nodomain.freeyourgadget.gadgetbridge.util.NotificationUtils;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.PebbleUtils;
|
import nodomain.freeyourgadget.gadgetbridge.util.PebbleUtils;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
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_BLACKLIST;
|
||||||
import static nodomain.freeyourgadget.gadgetbridge.activities.NotificationFilterActivity.NOTIFICATION_FILTER_MODE_WHITELIST;
|
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
|
public static final String ACTION_REPLY
|
||||||
= "nodomain.freeyourgadget.gadgetbridge.notificationlistener.action.reply";
|
= "nodomain.freeyourgadget.gadgetbridge.notificationlistener.action.reply";
|
||||||
|
|
||||||
private final LimitedQueue mActionLookup = new LimitedQueue(32);
|
private final LimitedQueue<Integer, NotificationCompat.Action> mActionLookup = new LimitedQueue<>(32);
|
||||||
private final LimitedQueue mPackageLookup = new LimitedQueue(64);
|
private final LimitedQueue<Integer, String> mPackageLookup = new LimitedQueue<>(64);
|
||||||
private final LimitedQueue mNotificationHandleLookup = new LimitedQueue(128);
|
private final LimitedQueue<Integer, Long> mNotificationHandleLookup = new LimitedQueue<>(128);
|
||||||
|
|
||||||
private final HashMap<String, Long> notificationBurstPrevention = new HashMap<>();
|
private final HashMap<String, Long> notificationBurstPrevention = new HashMap<>();
|
||||||
private final HashMap<String, Long> notificationOldRepeatPrevention = new HashMap<>();
|
private final HashMap<String, Long> notificationOldRepeatPrevention = new HashMap<>();
|
||||||
@ -158,7 +147,7 @@ public class NotificationListener extends NotificationListenerService {
|
|||||||
|
|
||||||
case ACTION_OPEN: {
|
case ACTION_OPEN: {
|
||||||
StatusBarNotification[] sbns = NotificationListener.this.getActiveNotifications();
|
StatusBarNotification[] sbns = NotificationListener.this.getActiveNotifications();
|
||||||
Long ts = (Long) mNotificationHandleLookup.lookup(handle);
|
Long ts = mNotificationHandleLookup.lookup(handle);
|
||||||
if (ts == null) {
|
if (ts == null) {
|
||||||
LOG.info("could not lookup handle for open action");
|
LOG.info("could not lookup handle for open action");
|
||||||
break;
|
break;
|
||||||
@ -179,7 +168,7 @@ public class NotificationListener extends NotificationListenerService {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ACTION_MUTE:
|
case ACTION_MUTE:
|
||||||
String packageName = (String) mPackageLookup.lookup(handle);
|
String packageName = mPackageLookup.lookup(handle);
|
||||||
if (packageName == null) {
|
if (packageName == null) {
|
||||||
LOG.info("could not lookup handle for mute action");
|
LOG.info("could not lookup handle for mute action");
|
||||||
break;
|
break;
|
||||||
@ -193,7 +182,7 @@ public class NotificationListener extends NotificationListenerService {
|
|||||||
break;
|
break;
|
||||||
case ACTION_DISMISS: {
|
case ACTION_DISMISS: {
|
||||||
StatusBarNotification[] sbns = NotificationListener.this.getActiveNotifications();
|
StatusBarNotification[] sbns = NotificationListener.this.getActiveNotifications();
|
||||||
Long ts = (Long) mNotificationHandleLookup.lookup(handle);
|
Long ts = mNotificationHandleLookup.lookup(handle);
|
||||||
if (ts == null) {
|
if (ts == null) {
|
||||||
LOG.info("could not lookup handle for dismiss action");
|
LOG.info("could not lookup handle for dismiss action");
|
||||||
break;
|
break;
|
||||||
@ -210,7 +199,7 @@ public class NotificationListener extends NotificationListenerService {
|
|||||||
NotificationListener.this.cancelAllNotifications();
|
NotificationListener.this.cancelAllNotifications();
|
||||||
break;
|
break;
|
||||||
case ACTION_REPLY:
|
case ACTION_REPLY:
|
||||||
NotificationCompat.Action wearableAction = (NotificationCompat.Action) mActionLookup.lookup(handle);
|
NotificationCompat.Action wearableAction = mActionLookup.lookup(handle);
|
||||||
String reply = intent.getStringExtra("reply");
|
String reply = intent.getStringExtra("reply");
|
||||||
if (wearableAction != null) {
|
if (wearableAction != null) {
|
||||||
PendingIntent actionIntent = wearableAction.getActionIntent();
|
PendingIntent actionIntent = wearableAction.getActionIntent();
|
||||||
@ -766,17 +755,16 @@ public class NotificationListener extends NotificationListenerService {
|
|||||||
if (shouldIgnoreNotification(sbn, true)) return;
|
if (shouldIgnoreNotification(sbn, true)) return;
|
||||||
|
|
||||||
// Build list of all currently active notifications
|
// Build list of all currently active notifications
|
||||||
ArrayList<Integer> activeNotificationsIds = new ArrayList<Integer>();
|
ArrayList<Integer> activeNotificationsIds = new ArrayList<>();
|
||||||
for (StatusBarNotification notification : getActiveNotifications()) {
|
for (StatusBarNotification notification : getActiveNotifications()) {
|
||||||
Object o = mNotificationHandleLookup.lookupByValue(notification.getPostTime());
|
Integer id = mNotificationHandleLookup.lookupByValue(notification.getPostTime());
|
||||||
if (o != null) {
|
if (id != null) {
|
||||||
int id = (int) o;
|
|
||||||
activeNotificationsIds.add(id);
|
activeNotificationsIds.add(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build list of notifications that aren't active anymore
|
// Build list of notifications that aren't active anymore
|
||||||
ArrayList<Integer> notificationsToRemove = new ArrayList<Integer>();
|
ArrayList<Integer> notificationsToRemove = new ArrayList<>();
|
||||||
for (int notificationId : notificationsActive) {
|
for (int notificationId : notificationsActive) {
|
||||||
if (!activeNotificationsIds.contains(notificationId)) {
|
if (!activeNotificationsIds.contains(notificationId)) {
|
||||||
notificationsToRemove.add(notificationId);
|
notificationsToRemove.add(notificationId);
|
||||||
|
@ -461,7 +461,7 @@ public abstract class AbstractDeviceSupport implements DeviceSupport {
|
|||||||
break;
|
break;
|
||||||
case REPLY:
|
case REPLY:
|
||||||
if (deviceEvent.phoneNumber == null) {
|
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) {
|
if (deviceEvent.phoneNumber != null) {
|
||||||
LOG.info("Got notification reply for SMS from " + deviceEvent.phoneNumber + " : " + deviceEvent.reply);
|
LOG.info("Got notification reply for SMS from " + deviceEvent.phoneNumber + " : " + deviceEvent.reply);
|
||||||
|
@ -159,7 +159,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
|
|||||||
/// Last battery percentage reported (or -1) to help with smoothing reported battery levels
|
/// Last battery percentage reported (or -1) to help with smoothing reported battery levels
|
||||||
private int lastBatteryPercent = -1;
|
private int lastBatteryPercent = -1;
|
||||||
|
|
||||||
private final LimitedQueue/*Long*/ mNotificationReplyAction = new LimitedQueue(16);
|
private final LimitedQueue<Integer, Long> mNotificationReplyAction = new LimitedQueue<>(16);
|
||||||
|
|
||||||
private boolean gpsUpdateSetup = false;
|
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
|
/* 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 */
|
* they use a handle that was provided in an action list on the onNotification.. event */
|
||||||
if (deviceEvtNotificationControl.event == GBDeviceEventNotificationControl.Event.REPLY) {
|
if (deviceEvtNotificationControl.event == GBDeviceEventNotificationControl.Event.REPLY) {
|
||||||
Long foundHandle = (Long)mNotificationReplyAction.lookup((int)deviceEvtNotificationControl.handle);
|
Long foundHandle = mNotificationReplyAction.lookup((int)deviceEvtNotificationControl.handle);
|
||||||
if (foundHandle!=null)
|
if (foundHandle!=null)
|
||||||
deviceEvtNotificationControl.handle = foundHandle;
|
deviceEvtNotificationControl.handle = foundHandle;
|
||||||
}
|
}
|
||||||
|
@ -70,7 +70,7 @@ public class ZeppOsNotificationService extends AbstractZeppOsService {
|
|||||||
|
|
||||||
// Keep track of Notification ID -> action handle, as BangleJSDeviceSupport.
|
// Keep track of Notification ID -> action handle, as BangleJSDeviceSupport.
|
||||||
// This needs to be simplified.
|
// This needs to be simplified.
|
||||||
private final LimitedQueue mNotificationReplyAction = new LimitedQueue(16);
|
private final LimitedQueue<Integer, Long> mNotificationReplyAction = new LimitedQueue<>(16);
|
||||||
|
|
||||||
private final ZeppOsFileTransferService fileTransferService;
|
private final ZeppOsFileTransferService fileTransferService;
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ public class ZeppOsNotificationService extends AbstractZeppOsService {
|
|||||||
case NOTIFICATION_CMD_REPLY:
|
case NOTIFICATION_CMD_REPLY:
|
||||||
// TODO make this configurable?
|
// TODO make this configurable?
|
||||||
final int notificationId = BLETypeConversions.toUint32(subarray(payload, 1, 5));
|
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) {
|
if (replyHandle == null) {
|
||||||
LOG.warn("Failed to find reply handle for notification ID {}", notificationId);
|
LOG.warn("Failed to find reply handle for notification ID {}", notificationId);
|
||||||
return;
|
return;
|
||||||
|
@ -22,41 +22,41 @@ import android.util.Pair;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
public class LimitedQueue {
|
public class LimitedQueue<K, V> {
|
||||||
private final int limit;
|
private final int limit;
|
||||||
private LinkedList<Pair> list = new LinkedList<>();
|
private final LinkedList<Pair<K, V>> list = new LinkedList<>();
|
||||||
|
|
||||||
public LimitedQueue(int limit) {
|
public LimitedQueue(final int limit) {
|
||||||
this.limit = 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) {
|
if (list.size() > limit - 1) {
|
||||||
list.removeFirst();
|
list.removeFirst();
|
||||||
}
|
}
|
||||||
list.add(new Pair<>(id, obj));
|
list.add(new Pair<>(id, obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized public void remove(int id) {
|
synchronized public void remove(final K id) {
|
||||||
for (Iterator<Pair> iter = list.iterator(); iter.hasNext(); ) {
|
for (final Iterator<Pair<K, V>> it = list.iterator(); it.hasNext(); ) {
|
||||||
Pair pair = iter.next();
|
Pair<K, V> pair = it.next();
|
||||||
if ((Integer) pair.first == id) {
|
if (id.equals(pair.first)) {
|
||||||
iter.remove();
|
it.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized public Object lookup(int id) {
|
synchronized public V lookup(final K id) {
|
||||||
for (Pair entry : list) {
|
for (final Pair<K, V> entry : list) {
|
||||||
if (id == (Integer) entry.first) {
|
if (id.equals(entry.first)) {
|
||||||
return entry.second;
|
return entry.second;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized public Object lookupByValue(Object value){
|
synchronized public K lookupByValue(final V value){
|
||||||
for (Pair entry : list) {
|
for (final Pair<K, V> entry : list) {
|
||||||
if (value.equals(entry.second)) {
|
if (value.equals(entry.second)) {
|
||||||
return entry.first;
|
return entry.first;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user