1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-25 11:26:47 +01:00

Make LimitedQueue generic

This commit is contained in:
José Rebelo 2023-12-10 10:30:27 +00:00
parent 4d12ac93e7
commit ef73ea1bd5
9 changed files with 37 additions and 48 deletions

View File

@ -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<Integer, String> 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<Integer, String> getIDSenderLookup() {
return mIDSenderLookup;
}

View File

@ -371,14 +371,14 @@ public abstract class AbstractWeekChartFragment extends AbstractActivityChartFra
private ActivityAmounts getActivityAmountsForDay(DBHandler db, Calendar day, GBDevice device) {
LimitedQueue activityAmountCache = null;
LimitedQueue<Integer, ActivityAmounts> 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) {

View File

@ -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<Integer, ActivityAmounts> mActivityAmountCache = new LimitedQueue<>(60);
@Override
protected AbstractFragmentPagerAdapter createFragmentPagerAdapter(final FragmentManager fragmentManager) {

View File

@ -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<Integer, NotificationCompat.Action> mActionLookup = new LimitedQueue<>(32);
private final LimitedQueue<Integer, String> mPackageLookup = new LimitedQueue<>(64);
private final LimitedQueue<Integer, Long> mNotificationHandleLookup = new LimitedQueue<>(128);
private final HashMap<String, Long> notificationBurstPrevention = new HashMap<>();
private final HashMap<String, Long> 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<Integer> activeNotificationsIds = new ArrayList<Integer>();
ArrayList<Integer> 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<Integer> notificationsToRemove = new ArrayList<Integer>();
ArrayList<Integer> notificationsToRemove = new ArrayList<>();
for (int notificationId : notificationsActive) {
if (!activeNotificationsIds.contains(notificationId)) {
notificationsToRemove.add(notificationId);

View File

@ -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);

View File

@ -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<Integer, Long> 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;
}

View File

@ -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<Integer, Long> 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;

View File

@ -22,41 +22,41 @@ import android.util.Pair;
import java.util.Iterator;
import java.util.LinkedList;
public class LimitedQueue {
public class LimitedQueue<K, V> {
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;
}
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<Pair> 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<Pair<K, V>> it = list.iterator(); it.hasNext(); ) {
Pair<K, V> 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<K, V> 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<K, V> entry : list) {
if (value.equals(entry.second)) {
return entry.first;
}

View File

@ -6,4 +6,4 @@
android:key="autoremove_notifications"
android:summary="@string/pref_summary_autoremove_notifications"
android:title="@string/pref_title_autoremove_notifications" />
</androidx.preference.PreferenceScreen>
</androidx.preference.PreferenceScreen>