Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shared pref storage #986

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<>();
}
};
Expand Down
8 changes: 7 additions & 1 deletion android/src/main/java/io/ably/lib/push/LocalDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void reset() {
this.clientId = null;
this.clearRegistrationToken();

storage.clear(SharedPrefKeys.class.getDeclaredFields());
storage.clear(SharedPrefKeys.getAllKeys());
}

boolean isRegistered() {
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.lang.reflect.Field;

public class SharedPreferenceStorage implements Storage{

Expand Down Expand Up @@ -38,14 +37,10 @@ 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) {
try {
editor.remove((String) f.get(null));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
for (String key : keys) {
editor.remove(key);
}
editor.commit();
}
Expand Down
8 changes: 3 additions & 5 deletions lib/src/main/java/io/ably/lib/push/Storage.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.ably.lib.push;

import java.lang.reflect.Field;

/**
* Interface for an entity that supplies key value store
*/
Expand Down Expand Up @@ -38,8 +36,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);
}
4 changes: 2 additions & 2 deletions lib/src/test/java/io/ably/lib/test/common/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
}

/**
Expand All @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Loading