From c4e3edb9321f81f1c10a293e69f591779f798264 Mon Sep 17 00:00:00 2001 From: Martin Morek Date: Fri, 2 Jul 2021 16:35:01 +0200 Subject: [PATCH 1/6] Added posibility to provide custom storage implementation instead of default SharedPreferences --- .../io/ably/lib/push/ActivationContext.java | 5 +- .../java/io/ably/lib/push/LocalDevice.java | 70 ++++++------------- .../lib/push/SharedPreferenceStorage.java | 52 ++++++++++++++ .../main/java/io/ably/lib/push/Storage.java | 16 +++++ .../java/io/ably/lib/types/ClientOptions.java | 7 ++ 5 files changed, 101 insertions(+), 49 deletions(-) create mode 100644 android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java create mode 100644 lib/src/main/java/io/ably/lib/push/Storage.java diff --git a/android/src/main/java/io/ably/lib/push/ActivationContext.java b/android/src/main/java/io/ably/lib/push/ActivationContext.java index c89e2f287..78a29c30d 100644 --- a/android/src/main/java/io/ably/lib/push/ActivationContext.java +++ b/android/src/main/java/io/ably/lib/push/ActivationContext.java @@ -5,7 +5,6 @@ import android.preference.PreferenceManager; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; -import com.google.firebase.FirebaseApp; import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.InstanceIdResult; import io.ably.lib.rest.AblyRest; @@ -31,7 +30,9 @@ Context getContext() { public synchronized LocalDevice getLocalDevice() { if(localDevice == null) { Log.v(TAG, "getLocalDevice(): creating new instance and returning that"); - localDevice = new LocalDevice(this); + Storage storage = ably != null ? ably.options.storage : null; + + localDevice = new LocalDevice(this, storage); } else { Log.v(TAG, "getLocalDevice(): returning existing instance"); } 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 8f82fa334..a6c4288f6 100644 --- a/android/src/main/java/io/ably/lib/push/LocalDevice.java +++ b/android/src/main/java/io/ably/lib/push/LocalDevice.java @@ -1,38 +1,34 @@ package io.ably.lib.push; import android.content.Context; -import android.content.SharedPreferences; import android.content.res.Configuration; -import android.preference.PreferenceManager; - import com.google.gson.JsonObject; - -import java.lang.reflect.Field; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; - import io.ably.lib.rest.DeviceDetails; -import io.ably.lib.types.AblyException; +import io.ably.lib.types.Param; import io.ably.lib.types.RegistrationToken; import io.ably.lib.util.Base64Coder; import io.ably.lib.util.Log; -import io.ably.lib.types.Param; import io.azam.ulidj.ULID; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + public class LocalDevice extends DeviceDetails { public String deviceSecret; public String deviceIdentityToken; + private final Storage storage; private final ActivationContext activationContext; - public LocalDevice(ActivationContext activationContext) { + public LocalDevice(ActivationContext activationContext, Storage storage) { super(); Log.v(TAG, "LocalDevice(): initialising"); this.platform = "android"; this.formFactor = isTablet(activationContext.getContext()) ? "tablet" : "phone"; this.activationContext = activationContext; this.push = new DeviceDetails.Push(); + this.storage = storage != null ? storage : new SharedPreferenceStorage(activationContext); loadPersisted(); } @@ -47,26 +43,24 @@ public JsonObject toJsonObject() { private void loadPersisted() { /* Spec: RSH8a */ - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); - - String id = prefs.getString(SharedPrefKeys.DEVICE_ID, null); + String id = storage.getString(SharedPrefKeys.DEVICE_ID, null); this.id = id; if(id != null) { Log.v(TAG, "loadPersisted(): existing deviceId found; id: " + id); - deviceSecret = prefs.getString(SharedPrefKeys.DEVICE_SECRET, null); + deviceSecret = storage.getString(SharedPrefKeys.DEVICE_SECRET, null); } else { Log.v(TAG, "loadPersisted(): existing deviceId not found."); } - this.clientId = prefs.getString(SharedPrefKeys.CLIENT_ID, null); - this.deviceIdentityToken = prefs.getString(SharedPrefKeys.DEVICE_TOKEN, null); + this.clientId = storage.getString(SharedPrefKeys.CLIENT_ID, null); + this.deviceIdentityToken = storage.getString(SharedPrefKeys.DEVICE_TOKEN, null); RegistrationToken.Type type = RegistrationToken.Type.fromOrdinal( - prefs.getInt(SharedPrefKeys.TOKEN_TYPE, -1)); + storage.getInt(SharedPrefKeys.TOKEN_TYPE, -1)); Log.d(TAG, "loadPersisted(): token type = " + type); if(type != null) { RegistrationToken token = null; - String tokenString = prefs.getString(SharedPrefKeys.TOKEN, null); + String tokenString = storage.getString(SharedPrefKeys.TOKEN, null); Log.d(TAG, "loadPersisted(): token string = " + tokenString); if(tokenString != null) { token = new RegistrationToken(type, tokenString); @@ -103,42 +97,32 @@ private void clearRegistrationToken() { void setAndPersistRegistrationToken(RegistrationToken token) { Log.v(TAG, "setAndPersistRegistrationToken(): token=" + token); setRegistrationToken(token); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); - prefs.edit() - .putInt(SharedPrefKeys.TOKEN_TYPE, token.type.ordinal()) - .putString(SharedPrefKeys.TOKEN, token.token) - .apply(); + storage.putInt(SharedPrefKeys.TOKEN_TYPE, token.type.ordinal()); + storage.putString(SharedPrefKeys.TOKEN, token.token); } void setClientId(String clientId) { Log.v(TAG, "setClientId(): clientId=" + clientId); this.clientId = clientId; - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); - prefs.edit().putString(SharedPrefKeys.CLIENT_ID, clientId).apply(); + storage.putString(SharedPrefKeys.CLIENT_ID, clientId); } public void setDeviceIdentityToken(String token) { Log.v(TAG, "setDeviceIdentityToken(): token=" + token); this.deviceIdentityToken = token; - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); - prefs.edit().putString(SharedPrefKeys.DEVICE_TOKEN, token).apply(); + storage.putString(SharedPrefKeys.DEVICE_TOKEN, token); } boolean isCreated() { return id != null; } - boolean create() { + void create() { /* Spec: RSH8b */ Log.v(TAG, "create()"); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); - SharedPreferences.Editor editor = prefs.edit(); - - editor.putString(SharedPrefKeys.DEVICE_ID, (id = ULID.random())); - editor.putString(SharedPrefKeys.CLIENT_ID, (clientId = activationContext.clientId)); - editor.putString(SharedPrefKeys.DEVICE_SECRET, (deviceSecret = generateSecret())); - - return editor.commit(); + storage.putString(SharedPrefKeys.DEVICE_ID, (id = ULID.random())); + storage.putString(SharedPrefKeys.CLIENT_ID, (clientId = activationContext.clientId)); + storage.putString(SharedPrefKeys.DEVICE_SECRET, (deviceSecret = generateSecret())); } public void reset() { @@ -149,15 +133,7 @@ public void reset() { this.clientId = null; this.clearRegistrationToken(); - SharedPreferences.Editor editor = activationContext.getPreferences().edit(); - for (Field f : SharedPrefKeys.class.getDeclaredFields()) { - try { - editor.remove((String) f.get(null)); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - } - editor.commit(); + storage.reset(SharedPrefKeys.class.getDeclaredFields()); } boolean isRegistered() { diff --git a/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java b/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java new file mode 100644 index 000000000..3df775569 --- /dev/null +++ b/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java @@ -0,0 +1,52 @@ +package io.ably.lib.push; + +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +import java.lang.reflect.Field; + +public class SharedPreferenceStorage implements Storage{ + + private final ActivationContext activationContext; + + public SharedPreferenceStorage(ActivationContext activationContext) { + this.activationContext = activationContext; + } + + @Override + public void putString(String key, String value) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); + prefs.edit().putString(key, value).apply(); + } + + @Override + public void putInt(String key, int value) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); + prefs.edit().putInt(key, value).apply(); + } + + @Override + public String getString(String key, String defaultValue) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); + return prefs.getString(key, defaultValue); + } + + @Override + public int getInt(String key, int defValue) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); + return prefs.getInt(key, defValue); + } + + @Override + public void reset(Field[] fields) { + SharedPreferences.Editor editor = activationContext.getPreferences().edit(); + for (Field f : fields) { + try { + editor.remove((String) f.get(null)); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + editor.commit(); + } +} diff --git a/lib/src/main/java/io/ably/lib/push/Storage.java b/lib/src/main/java/io/ably/lib/push/Storage.java new file mode 100644 index 000000000..eb7b363f1 --- /dev/null +++ b/lib/src/main/java/io/ably/lib/push/Storage.java @@ -0,0 +1,16 @@ +package io.ably.lib.push; + +import java.lang.reflect.Field; + +public interface Storage { + + void putString(String key, String value); + + void putInt(String key, int value); + + String getString(String key, String defaultValue); + + int getInt(String key, int defValue); + + void reset(Field[] fields); +} diff --git a/lib/src/main/java/io/ably/lib/types/ClientOptions.java b/lib/src/main/java/io/ably/lib/types/ClientOptions.java index 8444964b3..6340bd013 100644 --- a/lib/src/main/java/io/ably/lib/types/ClientOptions.java +++ b/lib/src/main/java/io/ably/lib/types/ClientOptions.java @@ -1,5 +1,6 @@ package io.ably.lib.types; +import io.ably.lib.push.Storage; import io.ably.lib.rest.Auth.AuthOptions; import io.ably.lib.rest.Auth.TokenParams; import io.ably.lib.transport.Defaults; @@ -195,4 +196,10 @@ public ClientOptions(String key) throws AblyException { * before responding. */ public boolean pushFullWait = false; + + /** + * Allows provide custom Local Device storage. In a case nothing is provided default implementation + * using SharedPreferences is used. + */ + public Storage storage = null; } From 8c5bd79a200f71a71148fffdfd71481ac523b5af Mon Sep 17 00:00:00 2001 From: Martin Morek Date: Tue, 6 Jul 2021 14:34:03 +0200 Subject: [PATCH 2/6] Test correct storage is used --- .../ably/lib/push/LocalDeviceStorageTest.java | 149 ++++++++++++++++++ .../lib/push/SharedPreferenceStorage.java | 4 +- .../main/java/io/ably/lib/push/Storage.java | 2 +- 3 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 android/src/androidTest/java/io/ably/lib/push/LocalDeviceStorageTest.java diff --git a/android/src/androidTest/java/io/ably/lib/push/LocalDeviceStorageTest.java b/android/src/androidTest/java/io/ably/lib/push/LocalDeviceStorageTest.java new file mode 100644 index 000000000..7297a485a --- /dev/null +++ b/android/src/androidTest/java/io/ably/lib/push/LocalDeviceStorageTest.java @@ -0,0 +1,149 @@ +package io.ably.lib.push; + +import android.content.Context; +import android.test.AndroidTestCase; +import io.ably.lib.types.RegistrationToken; +import junit.extensions.TestSetup; +import junit.framework.TestSuite; +import org.junit.BeforeClass; + +import java.lang.reflect.Field; +import java.util.HashMap; + +public class LocalDeviceStorageTest extends AndroidTestCase { + private Context context; + private ActivationContext activationContext; + + + private HashMap hashMap = new HashMap<>(); + + private Storage inMemoryStorage = new Storage() { + @Override + public void putString(String key, String value) { + hashMap.put(key, value); + } + + @Override + public void putInt(String key, int value) { + hashMap.put(key, value); + } + + @Override + public String getString(String key, String defaultValue) { + Object value = hashMap.get(key); + return value != null ? (String) value : defaultValue; + } + + @Override + public int getInt(String key, int defaultValue) { + Object value = hashMap.get(key); + return value != null ? (int) value : defaultValue; + } + + @Override + public void reset(Field[] fields) { + hashMap = new HashMap<>(); + } + }; + + @BeforeClass + public void setUp() { + context = getContext(); + activationContext = new ActivationContext(context.getApplicationContext()); + } + + public static junit.framework.Test suite() { + TestSuite suite = new TestSuite(); + suite.addTest(new TestSetup(new TestSuite(LocalDeviceStorageTest.class)) {}); + return suite; + } + + public void test_shared_preferences_storage_used_by_default() { + LocalDevice localDevice = new LocalDevice(activationContext, null); + /* initialize properties in storage */ + localDevice.create(); + + /* verify custom storage is not used */ + assertTrue(hashMap.isEmpty()); + + /* load properties */ + assertNotNull(localDevice.id); + assertNotNull(localDevice.deviceSecret); + } + + public void test_shared_preferences_storage_works_correctly() { + LocalDevice localDevice = new LocalDevice(activationContext, null); + + RegistrationToken registrationToken= new RegistrationToken(RegistrationToken.Type.FCM, "ABLY"); + /* initialize properties in storage */ + localDevice.create(); + localDevice.setAndPersistRegistrationToken(registrationToken); + + /* verify custom storage is not used */ + assertTrue(hashMap.isEmpty()); + + /* load properties */ + assertNotNull(localDevice.id); + assertNotNull(localDevice.deviceSecret); + assertTrue(localDevice.isCreated()); + assertEquals("FCM", localDevice.getRegistrationToken().type.name()); + assertEquals("ABLY", localDevice.getRegistrationToken().token); + + /* reset all properties */ + localDevice.reset(); + + /* properties were cleared */ + assertNull(localDevice.id); + assertNull(localDevice.deviceSecret); + assertNull(localDevice.getRegistrationToken()); + } + + public void test_custom_storage_used_if_provided() { + LocalDevice localDevice = new LocalDevice(activationContext, inMemoryStorage); + /* initialize properties in storage */ + localDevice.create(); + + /* verify in memory storage is used */ + assertFalse(hashMap.isEmpty()); + + /* load properties */ + assertNotNull(localDevice.id); + assertNotNull(localDevice.deviceSecret); + + String deviceId = localDevice.id; + String deviceSecret = localDevice.deviceSecret; + + /* values are the same */ + assertEquals(deviceId, hashMap.get("ABLY_DEVICE_ID")); + assertEquals(deviceSecret, hashMap.get("ABLY_DEVICE_SECRET")); + } + + public void test_custom_storage_works_correctly() { + LocalDevice localDevice = new LocalDevice(activationContext, inMemoryStorage); + + RegistrationToken registrationToken= new RegistrationToken(RegistrationToken.Type.FCM, "ABLY"); + /* initialize properties in storage */ + localDevice.create(); + localDevice.setAndPersistRegistrationToken(registrationToken); + + /* verify custom storage is used */ + assertFalse(hashMap.isEmpty()); + + /* load properties */ + assertNotNull(localDevice.id); + assertNotNull(localDevice.deviceSecret); + assertTrue(localDevice.isCreated()); + assertEquals("FCM", localDevice.getRegistrationToken().type.name()); + assertEquals("ABLY", localDevice.getRegistrationToken().token); + + /* reset all properties */ + localDevice.reset(); + /* verify custom storage was cleared out */ + assertTrue(hashMap.isEmpty()); + + /* properties were cleared */ + assertNull(localDevice.id); + assertNull(localDevice.deviceSecret); + assertNull(localDevice.getRegistrationToken()); + } +} 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 3df775569..52146fbdb 100644 --- a/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java +++ b/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java @@ -32,9 +32,9 @@ public String getString(String key, String defaultValue) { } @Override - public int getInt(String key, int defValue) { + public int getInt(String key, int defaultValue) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); - return prefs.getInt(key, defValue); + return prefs.getInt(key, defaultValue); } @Override 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 eb7b363f1..e26ccaec7 100644 --- a/lib/src/main/java/io/ably/lib/push/Storage.java +++ b/lib/src/main/java/io/ably/lib/push/Storage.java @@ -10,7 +10,7 @@ public interface Storage { String getString(String key, String defaultValue); - int getInt(String key, int defValue); + int getInt(String key, int defaultValue); void reset(Field[] fields); } From 6dcd8a850c7c1658eb50bd9278ccaa4c8ff420a7 Mon Sep 17 00:00:00 2001 From: Martin Morek Date: Wed, 7 Jul 2021 18:23:51 +0200 Subject: [PATCH 3/6] Renamed method reset to clear, added documentation comment --- .../java/io/ably/lib/push/LocalDeviceStorageTest.java | 2 +- android/src/main/java/io/ably/lib/push/LocalDevice.java | 2 +- .../main/java/io/ably/lib/push/SharedPreferenceStorage.java | 2 +- lib/src/main/java/io/ably/lib/push/Storage.java | 6 +++++- 4 files changed, 8 insertions(+), 4 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 7297a485a..3ae9e56ab 100644 --- a/android/src/androidTest/java/io/ably/lib/push/LocalDeviceStorageTest.java +++ b/android/src/androidTest/java/io/ably/lib/push/LocalDeviceStorageTest.java @@ -41,7 +41,7 @@ public int getInt(String key, int defaultValue) { } @Override - public void reset(Field[] fields) { + public void clear(Field[] fields) { hashMap = new HashMap<>(); } }; 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 a6c4288f6..41ebe3f1c 100644 --- a/android/src/main/java/io/ably/lib/push/LocalDevice.java +++ b/android/src/main/java/io/ably/lib/push/LocalDevice.java @@ -133,7 +133,7 @@ public void reset() { this.clientId = null; this.clearRegistrationToken(); - storage.reset(SharedPrefKeys.class.getDeclaredFields()); + storage.clear(SharedPrefKeys.class.getDeclaredFields()); } boolean isRegistered() { 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 52146fbdb..54a5a99d3 100644 --- a/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java +++ b/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java @@ -38,7 +38,7 @@ public int getInt(String key, int defaultValue) { } @Override - public void reset(Field[] fields) { + public void clear(Field[] fields) { SharedPreferences.Editor editor = activationContext.getPreferences().edit(); for (Field f : fields) { try { 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 e26ccaec7..dfc2bd4ca 100644 --- a/lib/src/main/java/io/ably/lib/push/Storage.java +++ b/lib/src/main/java/io/ably/lib/push/Storage.java @@ -2,6 +2,10 @@ import java.lang.reflect.Field; +/** + * Interface for an entity that supplies key value store + * - methods getString and getInt have to return default value if requested key is not found + */ public interface Storage { void putString(String key, String value); @@ -12,5 +16,5 @@ public interface Storage { int getInt(String key, int defaultValue); - void reset(Field[] fields); + void clear(Field[] fields); } From 74ff1469383a111b58cfdbb605fc6e196c3f87d7 Mon Sep 17 00:00:00 2001 From: Martin Morek Date: Thu, 8 Jul 2021 15:38:25 +0200 Subject: [PATCH 4/6] Extracted code repetition into separate method --- .../ably/lib/push/SharedPreferenceStorage.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 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 54a5a99d3..2cbe0710d 100644 --- a/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java +++ b/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java @@ -13,28 +13,28 @@ public SharedPreferenceStorage(ActivationContext activationContext) { this.activationContext = activationContext; } + private SharedPreferences sharedPreferences() { + return PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); + } + @Override public void putString(String key, String value) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); - prefs.edit().putString(key, value).apply(); + sharedPreferences().edit().putString(key, value).apply(); } @Override public void putInt(String key, int value) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); - prefs.edit().putInt(key, value).apply(); + sharedPreferences().edit().putInt(key, value).apply(); } @Override public String getString(String key, String defaultValue) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); - return prefs.getString(key, defaultValue); + return sharedPreferences().getString(key, defaultValue); } @Override public int getInt(String key, int defaultValue) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext()); - return prefs.getInt(key, defaultValue); + return sharedPreferences().getInt(key, defaultValue); } @Override From 872859ff99ab1526bd47bac1df3919593ee479c4 Mon Sep 17 00:00:00 2001 From: Martin Morek Date: Mon, 12 Jul 2021 17:15:07 +0200 Subject: [PATCH 5/6] Added missing documentation --- .../main/java/io/ably/lib/push/Storage.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) 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 dfc2bd4ca..5fcbafc92 100644 --- a/lib/src/main/java/io/ably/lib/push/Storage.java +++ b/lib/src/main/java/io/ably/lib/push/Storage.java @@ -4,17 +4,42 @@ /** * Interface for an entity that supplies key value store - * - methods getString and getInt have to return default value if requested key is not found */ public interface Storage { + /** + * Insert string value in to storage + * @param key name under which value is stored + * @param value stored string value + */ void putString(String key, String value); + /** + * Insert integer value in to storage + * @param key name after which value is stored + * @param value stored integer value + */ void putInt(String key, int value); + /** + * Returns string value based on key from storage + * @param key name under value is stored + * @param defaultValue value which is returned if key is not found + * @return value stored under key or default value if key is not found + */ String getString(String key, String defaultValue); + /** + * Returns integer value based on key from storage + * @param key name under value is stored + * @param defaultValue value which is returned if key is not found + * @return value stored under key or default value if key is not found + */ int getInt(String key, int defaultValue); + /** + * Removes fields from storage + * @param fields array of keys which values should be removed from storage + */ void clear(Field[] fields); } From 16bf5233f352b5817f5afae1af1e95ab7e283c7f Mon Sep 17 00:00:00 2001 From: Martin Morek Date: Tue, 27 Jul 2021 15:59:26 +0200 Subject: [PATCH 6/6] Names refactoring --- .../ably/lib/push/LocalDeviceStorageTest.java | 8 +++--- .../io/ably/lib/push/ActivationContext.java | 2 +- .../java/io/ably/lib/push/LocalDevice.java | 26 +++++++++---------- .../lib/push/SharedPreferenceStorage.java | 8 +++--- .../main/java/io/ably/lib/push/Storage.java | 12 ++++----- .../java/io/ably/lib/types/ClientOptions.java | 4 +-- 6 files changed, 30 insertions(+), 30 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 3ae9e56ab..eeea84e0a 100644 --- a/android/src/androidTest/java/io/ably/lib/push/LocalDeviceStorageTest.java +++ b/android/src/androidTest/java/io/ably/lib/push/LocalDeviceStorageTest.java @@ -19,23 +19,23 @@ public class LocalDeviceStorageTest extends AndroidTestCase { private Storage inMemoryStorage = new Storage() { @Override - public void putString(String key, String value) { + public void put(String key, String value) { hashMap.put(key, value); } @Override - public void putInt(String key, int value) { + public void put(String key, int value) { hashMap.put(key, value); } @Override - public String getString(String key, String defaultValue) { + public String get(String key, String defaultValue) { Object value = hashMap.get(key); return value != null ? (String) value : defaultValue; } @Override - public int getInt(String key, int defaultValue) { + public int get(String key, int defaultValue) { Object value = hashMap.get(key); return value != null ? (int) value : defaultValue; } diff --git a/android/src/main/java/io/ably/lib/push/ActivationContext.java b/android/src/main/java/io/ably/lib/push/ActivationContext.java index 78a29c30d..37439d77a 100644 --- a/android/src/main/java/io/ably/lib/push/ActivationContext.java +++ b/android/src/main/java/io/ably/lib/push/ActivationContext.java @@ -30,7 +30,7 @@ Context getContext() { public synchronized LocalDevice getLocalDevice() { if(localDevice == null) { Log.v(TAG, "getLocalDevice(): creating new instance and returning that"); - Storage storage = ably != null ? ably.options.storage : null; + Storage storage = ably != null ? ably.options.localStorage : null; localDevice = new LocalDevice(this, storage); } else { 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 41ebe3f1c..b5fc58c30 100644 --- a/android/src/main/java/io/ably/lib/push/LocalDevice.java +++ b/android/src/main/java/io/ably/lib/push/LocalDevice.java @@ -43,24 +43,24 @@ public JsonObject toJsonObject() { private void loadPersisted() { /* Spec: RSH8a */ - String id = storage.getString(SharedPrefKeys.DEVICE_ID, null); + String id = storage.get(SharedPrefKeys.DEVICE_ID, null); this.id = id; if(id != null) { Log.v(TAG, "loadPersisted(): existing deviceId found; id: " + id); - deviceSecret = storage.getString(SharedPrefKeys.DEVICE_SECRET, null); + deviceSecret = storage.get(SharedPrefKeys.DEVICE_SECRET, null); } else { Log.v(TAG, "loadPersisted(): existing deviceId not found."); } - this.clientId = storage.getString(SharedPrefKeys.CLIENT_ID, null); - this.deviceIdentityToken = storage.getString(SharedPrefKeys.DEVICE_TOKEN, null); + this.clientId = storage.get(SharedPrefKeys.CLIENT_ID, null); + this.deviceIdentityToken = storage.get(SharedPrefKeys.DEVICE_TOKEN, null); RegistrationToken.Type type = RegistrationToken.Type.fromOrdinal( - storage.getInt(SharedPrefKeys.TOKEN_TYPE, -1)); + storage.get(SharedPrefKeys.TOKEN_TYPE, -1)); Log.d(TAG, "loadPersisted(): token type = " + type); if(type != null) { RegistrationToken token = null; - String tokenString = storage.getString(SharedPrefKeys.TOKEN, null); + String tokenString = storage.get(SharedPrefKeys.TOKEN, null); Log.d(TAG, "loadPersisted(): token string = " + tokenString); if(tokenString != null) { token = new RegistrationToken(type, tokenString); @@ -97,20 +97,20 @@ private void clearRegistrationToken() { void setAndPersistRegistrationToken(RegistrationToken token) { Log.v(TAG, "setAndPersistRegistrationToken(): token=" + token); setRegistrationToken(token); - storage.putInt(SharedPrefKeys.TOKEN_TYPE, token.type.ordinal()); - storage.putString(SharedPrefKeys.TOKEN, token.token); + storage.put(SharedPrefKeys.TOKEN_TYPE, token.type.ordinal()); + storage.put(SharedPrefKeys.TOKEN, token.token); } void setClientId(String clientId) { Log.v(TAG, "setClientId(): clientId=" + clientId); this.clientId = clientId; - storage.putString(SharedPrefKeys.CLIENT_ID, clientId); + storage.put(SharedPrefKeys.CLIENT_ID, clientId); } public void setDeviceIdentityToken(String token) { Log.v(TAG, "setDeviceIdentityToken(): token=" + token); this.deviceIdentityToken = token; - storage.putString(SharedPrefKeys.DEVICE_TOKEN, token); + storage.put(SharedPrefKeys.DEVICE_TOKEN, token); } boolean isCreated() { @@ -120,9 +120,9 @@ boolean isCreated() { void create() { /* Spec: RSH8b */ Log.v(TAG, "create()"); - storage.putString(SharedPrefKeys.DEVICE_ID, (id = ULID.random())); - storage.putString(SharedPrefKeys.CLIENT_ID, (clientId = activationContext.clientId)); - storage.putString(SharedPrefKeys.DEVICE_SECRET, (deviceSecret = generateSecret())); + storage.put(SharedPrefKeys.DEVICE_ID, (id = ULID.random())); + storage.put(SharedPrefKeys.CLIENT_ID, (clientId = activationContext.clientId)); + storage.put(SharedPrefKeys.DEVICE_SECRET, (deviceSecret = generateSecret())); } public void reset() { 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 2cbe0710d..bcead470d 100644 --- a/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java +++ b/android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java @@ -18,22 +18,22 @@ private SharedPreferences sharedPreferences() { } @Override - public void putString(String key, String value) { + public void put(String key, String value) { sharedPreferences().edit().putString(key, value).apply(); } @Override - public void putInt(String key, int value) { + public void put(String key, int value) { sharedPreferences().edit().putInt(key, value).apply(); } @Override - public String getString(String key, String defaultValue) { + public String get(String key, String defaultValue) { return sharedPreferences().getString(key, defaultValue); } @Override - public int getInt(String key, int defaultValue) { + public int get(String key, int defaultValue) { return sharedPreferences().getInt(key, defaultValue); } 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 5fcbafc92..3ad1060b3 100644 --- a/lib/src/main/java/io/ably/lib/push/Storage.java +++ b/lib/src/main/java/io/ably/lib/push/Storage.java @@ -8,18 +8,18 @@ public interface Storage { /** - * Insert string value in to storage + * Put string value in to storage * @param key name under which value is stored * @param value stored string value */ - void putString(String key, String value); + void put(String key, String value); /** - * Insert integer value in to storage + * Put integer value in to storage * @param key name after which value is stored * @param value stored integer value */ - void putInt(String key, int value); + void put(String key, int value); /** * Returns string value based on key from storage @@ -27,7 +27,7 @@ public interface Storage { * @param defaultValue value which is returned if key is not found * @return value stored under key or default value if key is not found */ - String getString(String key, String defaultValue); + String get(String key, String defaultValue); /** * Returns integer value based on key from storage @@ -35,7 +35,7 @@ public interface Storage { * @param defaultValue value which is returned if key is not found * @return value stored under key or default value if key is not found */ - int getInt(String key, int defaultValue); + int get(String key, int defaultValue); /** * Removes fields from storage diff --git a/lib/src/main/java/io/ably/lib/types/ClientOptions.java b/lib/src/main/java/io/ably/lib/types/ClientOptions.java index 6340bd013..7cabffe2b 100644 --- a/lib/src/main/java/io/ably/lib/types/ClientOptions.java +++ b/lib/src/main/java/io/ably/lib/types/ClientOptions.java @@ -198,8 +198,8 @@ public ClientOptions(String key) throws AblyException { public boolean pushFullWait = false; /** - * Allows provide custom Local Device storage. In a case nothing is provided default implementation + * Custom Local Device storage. In the case nothing is provided then a default implementation * using SharedPreferences is used. */ - public Storage storage = null; + public Storage localStorage = null; }