From 9a6a7f486f070d30cb0c8ee1d73a97339703b423 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Tue, 30 Jan 2024 00:17:06 +0530 Subject: [PATCH 1/4] Refactored realtime delta decoder helpers --- lib/src/test/java/io/ably/lib/test/common/Helpers.java | 4 ++-- .../io/ably/lib/test/realtime/RealtimeDeltaDecoderTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/src/test/java/io/ably/lib/test/common/Helpers.java b/lib/src/test/java/io/ably/lib/test/common/Helpers.java index 8a9a7e38b..61db9db86 100644 --- a/lib/src/test/java/io/ably/lib/test/common/Helpers.java +++ b/lib/src/test/java/io/ably/lib/test/common/Helpers.java @@ -263,7 +263,7 @@ public MessageWaiter(Channel channel, String event) { */ public synchronized void waitFor(int count) { while(receivedMessages.size() < count) - try { wait(); } catch(InterruptedException e) {} + try { wait(); } catch(InterruptedException ignored) {} } /** @@ -274,7 +274,7 @@ public synchronized void waitFor(int count, long time) { long targetTime = System.currentTimeMillis() + time; long remaining = time; while(receivedMessages.size() < count && remaining > 0) { - try { wait(remaining); } catch(InterruptedException e) {} + try { wait(remaining); } catch(InterruptedException ignored) {} remaining = targetTime - System.currentTimeMillis(); } } diff --git a/lib/src/test/java/io/ably/lib/test/realtime/RealtimeDeltaDecoderTest.java b/lib/src/test/java/io/ably/lib/test/realtime/RealtimeDeltaDecoderTest.java index d0fe03531..78d2ea026 100644 --- a/lib/src/test/java/io/ably/lib/test/realtime/RealtimeDeltaDecoderTest.java +++ b/lib/src/test/java/io/ably/lib/test/realtime/RealtimeDeltaDecoderTest.java @@ -62,7 +62,7 @@ public void simple_delta_codec() { Message message = messageWaiter.receivedMessages.get(i); int messageIndex = Integer.parseInt(message.name); assertEquals("Verify message order", i, messageIndex); - assertEquals("Verify message data", true, testData[messageIndex].equals(message.data)); + assertEquals("Verify message data", testData[messageIndex], message.data); } } catch(Exception e) { fail(testName + ": Unexpected exception " + e.getMessage()); From d2f3efa24da17665788dd37831eeb50de589bde5 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Mon, 5 Feb 2024 13:37:37 +0530 Subject: [PATCH 2/4] Fixed shared pref storage clear method, removed use of reflection --- android/src/main/java/io/ably/lib/push/LocalDevice.java | 8 +++++++- .../java/io/ably/lib/push/SharedPreferenceStorage.java | 6 +++--- lib/src/main/java/io/ably/lib/push/Storage.java | 6 +++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/android/src/main/java/io/ably/lib/push/LocalDevice.java b/android/src/main/java/io/ably/lib/push/LocalDevice.java index 66fd3709c..bed9d4554 100644 --- a/android/src/main/java/io/ably/lib/push/LocalDevice.java +++ b/android/src/main/java/io/ably/lib/push/LocalDevice.java @@ -144,7 +144,7 @@ public void reset() { this.clientId = null; this.clearRegistrationToken(); - storage.clear(SharedPrefKeys.class.getDeclaredFields()); + storage.clear(SharedPrefKeys.getAllKeys()); } boolean isRegistered() { @@ -170,6 +170,12 @@ private static class SharedPrefKeys { static final String DEVICE_TOKEN = "ABLY_DEVICE_IDENTITY_TOKEN"; static final String TOKEN_TYPE = "ABLY_REGISTRATION_TOKEN_TYPE"; static final String TOKEN = "ABLY_REGISTRATION_TOKEN"; + + static String[] getAllKeys() { + return new String[]{ + DEVICE_ID, CLIENT_ID, DEVICE_SECRET, DEVICE_TOKEN, TOKEN_TYPE, TOKEN + }; + } } private static String generateSecret() { diff --git a/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java b/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java index bcead470d..4eaba38be 100644 --- a/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java +++ b/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java @@ -38,11 +38,11 @@ public int get(String key, int defaultValue) { } @Override - public void clear(Field[] fields) { + public void clear(String[] keys) { SharedPreferences.Editor editor = activationContext.getPreferences().edit(); - for (Field f : fields) { + for (String key : keys) { try { - editor.remove((String) f.get(null)); + editor.remove(key); } catch (IllegalAccessException e) { throw new RuntimeException(e); } diff --git a/lib/src/main/java/io/ably/lib/push/Storage.java b/lib/src/main/java/io/ably/lib/push/Storage.java index 3ad1060b3..57ce90420 100644 --- a/lib/src/main/java/io/ably/lib/push/Storage.java +++ b/lib/src/main/java/io/ably/lib/push/Storage.java @@ -38,8 +38,8 @@ public interface Storage { int get(String key, int defaultValue); /** - * Removes fields from storage - * @param fields array of keys which values should be removed from storage + * Removes keys from storage + * @param keys array of keys which values should be removed from storage */ - void clear(Field[] fields); + void clear(String[] keys); } From 08ddfd51749cf4a7a331e78244d15f6950bc7f1c Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Mon, 5 Feb 2024 13:42:50 +0530 Subject: [PATCH 3/4] Fixed linting issues for android storage classes --- .../src/main/java/io/ably/lib/push/SharedPreferenceStorage.java | 1 - lib/src/main/java/io/ably/lib/push/Storage.java | 2 -- 2 files changed, 3 deletions(-) diff --git a/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java b/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java index 4eaba38be..9906ca1e4 100644 --- a/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java +++ b/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java @@ -3,7 +3,6 @@ import android.content.SharedPreferences; import android.preference.PreferenceManager; -import java.lang.reflect.Field; public class SharedPreferenceStorage implements Storage{ diff --git a/lib/src/main/java/io/ably/lib/push/Storage.java b/lib/src/main/java/io/ably/lib/push/Storage.java index 57ce90420..cb30dbd55 100644 --- a/lib/src/main/java/io/ably/lib/push/Storage.java +++ b/lib/src/main/java/io/ably/lib/push/Storage.java @@ -1,7 +1,5 @@ package io.ably.lib.push; -import java.lang.reflect.Field; - /** * Interface for an entity that supplies key value store */ From 379081c33c7cefd4629529d9120412ab62feba68 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Mon, 5 Feb 2024 16:45:25 +0530 Subject: [PATCH 4/4] Refactored code for sharedPref, removed unnecessary handled exception --- .../java/io/ably/lib/push/LocalDeviceStorageTest.java | 2 +- .../main/java/io/ably/lib/push/SharedPreferenceStorage.java | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/android/src/androidTest/java/io/ably/lib/push/LocalDeviceStorageTest.java b/android/src/androidTest/java/io/ably/lib/push/LocalDeviceStorageTest.java index 27f646b6b..8f1982ec4 100644 --- a/android/src/androidTest/java/io/ably/lib/push/LocalDeviceStorageTest.java +++ b/android/src/androidTest/java/io/ably/lib/push/LocalDeviceStorageTest.java @@ -49,7 +49,7 @@ public int get(String key, int defaultValue) { } @Override - public void clear(Field[] fields) { + public void clear(String[] keys) { hashMap = new HashMap<>(); } }; diff --git a/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java b/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java index 9906ca1e4..6f2315ed8 100644 --- a/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java +++ b/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java @@ -40,11 +40,7 @@ public int get(String key, int defaultValue) { public void clear(String[] keys) { SharedPreferences.Editor editor = activationContext.getPreferences().edit(); for (String key : keys) { - try { - editor.remove(key); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } + editor.remove(key); } editor.commit(); }