Skip to content

Commit accf93b

Browse files
authored
Merge pull request #986 from ably/fix/clear_shared_pref_storage
Fix shared pref storage
2 parents 72992c9 + 379081c commit accf93b

File tree

6 files changed

+17
-18
lines changed

6 files changed

+17
-18
lines changed

android/src/androidTest/java/io/ably/lib/push/LocalDeviceStorageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public int get(String key, int defaultValue) {
4949
}
5050

5151
@Override
52-
public void clear(Field[] fields) {
52+
public void clear(String[] keys) {
5353
hashMap = new HashMap<>();
5454
}
5555
};

android/src/main/java/io/ably/lib/push/LocalDevice.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public void reset() {
144144
this.clientId = null;
145145
this.clearRegistrationToken();
146146

147-
storage.clear(SharedPrefKeys.class.getDeclaredFields());
147+
storage.clear(SharedPrefKeys.getAllKeys());
148148
}
149149

150150
boolean isRegistered() {
@@ -170,6 +170,12 @@ private static class SharedPrefKeys {
170170
static final String DEVICE_TOKEN = "ABLY_DEVICE_IDENTITY_TOKEN";
171171
static final String TOKEN_TYPE = "ABLY_REGISTRATION_TOKEN_TYPE";
172172
static final String TOKEN = "ABLY_REGISTRATION_TOKEN";
173+
174+
static String[] getAllKeys() {
175+
return new String[]{
176+
DEVICE_ID, CLIENT_ID, DEVICE_SECRET, DEVICE_TOKEN, TOKEN_TYPE, TOKEN
177+
};
178+
}
173179
}
174180

175181
private static String generateSecret() {

android/src/main/java/io/ably/lib/push/SharedPreferenceStorage.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.content.SharedPreferences;
44
import android.preference.PreferenceManager;
55

6-
import java.lang.reflect.Field;
76

87
public class SharedPreferenceStorage implements Storage{
98

@@ -38,14 +37,10 @@ public int get(String key, int defaultValue) {
3837
}
3938

4039
@Override
41-
public void clear(Field[] fields) {
40+
public void clear(String[] keys) {
4241
SharedPreferences.Editor editor = activationContext.getPreferences().edit();
43-
for (Field f : fields) {
44-
try {
45-
editor.remove((String) f.get(null));
46-
} catch (IllegalAccessException e) {
47-
throw new RuntimeException(e);
48-
}
42+
for (String key : keys) {
43+
editor.remove(key);
4944
}
5045
editor.commit();
5146
}

lib/src/main/java/io/ably/lib/push/Storage.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.ably.lib.push;
22

3-
import java.lang.reflect.Field;
4-
53
/**
64
* Interface for an entity that supplies key value store
75
*/
@@ -38,8 +36,8 @@ public interface Storage {
3836
int get(String key, int defaultValue);
3937

4038
/**
41-
* Removes fields from storage
42-
* @param fields array of keys which values should be removed from storage
39+
* Removes keys from storage
40+
* @param keys array of keys which values should be removed from storage
4341
*/
44-
void clear(Field[] fields);
42+
void clear(String[] keys);
4543
}

lib/src/test/java/io/ably/lib/test/common/Helpers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public MessageWaiter(Channel channel, String event) {
263263
*/
264264
public synchronized void waitFor(int count) {
265265
while(receivedMessages.size() < count)
266-
try { wait(); } catch(InterruptedException e) {}
266+
try { wait(); } catch(InterruptedException ignored) {}
267267
}
268268

269269
/**
@@ -274,7 +274,7 @@ public synchronized void waitFor(int count, long time) {
274274
long targetTime = System.currentTimeMillis() + time;
275275
long remaining = time;
276276
while(receivedMessages.size() < count && remaining > 0) {
277-
try { wait(remaining); } catch(InterruptedException e) {}
277+
try { wait(remaining); } catch(InterruptedException ignored) {}
278278
remaining = targetTime - System.currentTimeMillis();
279279
}
280280
}

lib/src/test/java/io/ably/lib/test/realtime/RealtimeDeltaDecoderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void simple_delta_codec() {
6262
Message message = messageWaiter.receivedMessages.get(i);
6363
int messageIndex = Integer.parseInt(message.name);
6464
assertEquals("Verify message order", i, messageIndex);
65-
assertEquals("Verify message data", true, testData[messageIndex].equals(message.data));
65+
assertEquals("Verify message data", testData[messageIndex], message.data);
6666
}
6767
} catch(Exception e) {
6868
fail(testName + ": Unexpected exception " + e.getMessage());

0 commit comments

Comments
 (0)