mirror of
https://github.com/oxen-io/session-android.git
synced 2025-01-12 01:13:38 +00:00
Improve key presence checking in SafeData.
This isn't a perfect check either, but it should be safer and more consistent than using static "invalid values".
This commit is contained in:
parent
d25ebdc818
commit
444e01deae
@ -11,9 +11,6 @@ import androidx.work.Data;
|
||||
*/
|
||||
public class SafeData {
|
||||
|
||||
private final static int INVALID_INT = Integer.MIN_VALUE;
|
||||
private final static long INVALID_LONG = Long.MIN_VALUE;
|
||||
|
||||
private final Data data;
|
||||
|
||||
public SafeData(@NonNull Data data) {
|
||||
@ -21,60 +18,38 @@ public class SafeData {
|
||||
}
|
||||
|
||||
public int getInt(@NonNull String key) {
|
||||
int value = data.getInt(key, INVALID_INT);
|
||||
|
||||
if (value == INVALID_INT) {
|
||||
throw new IllegalStateException("Missing key: " + key);
|
||||
}
|
||||
|
||||
return value;
|
||||
assertKeyPresence(key);
|
||||
return data.getInt(key, -1);
|
||||
}
|
||||
|
||||
public long getLong(@NonNull String key) {
|
||||
long value = data.getLong(key, INVALID_LONG);
|
||||
|
||||
if (value == INVALID_LONG) {
|
||||
throw new IllegalStateException("Missing key: " + key);
|
||||
}
|
||||
|
||||
return value;
|
||||
assertKeyPresence(key);
|
||||
return data.getLong(key, -1);
|
||||
}
|
||||
|
||||
public @NonNull String getString(@NonNull String key) {
|
||||
String value = data.getString(key);
|
||||
|
||||
if (value == null) {
|
||||
throw new IllegalStateException("Missing key: " + key);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public @Nullable String getNullableString(@NonNull String key) {
|
||||
public String getString(@NonNull String key) {
|
||||
assertKeyPresence(key);
|
||||
return data.getString(key);
|
||||
}
|
||||
|
||||
public @NonNull String[] getStringArray(@NonNull String key) {
|
||||
String[] value = data.getStringArray(key);
|
||||
|
||||
if (value == null) {
|
||||
throw new IllegalStateException("Missing key: " + key);
|
||||
}
|
||||
|
||||
return value;
|
||||
public String[] getStringArray(@NonNull String key) {
|
||||
assertKeyPresence(key);
|
||||
return data.getStringArray(key);
|
||||
}
|
||||
|
||||
public @NonNull long[] getLongArray(@NonNull String key) {
|
||||
long[] value = data.getLongArray(key);
|
||||
|
||||
if (value == null) {
|
||||
throw new IllegalStateException("Missing key: " + key);
|
||||
}
|
||||
|
||||
return value;
|
||||
public long[] getLongArray(@NonNull String key) {
|
||||
assertKeyPresence(key);
|
||||
return data.getLongArray(key);
|
||||
}
|
||||
|
||||
public boolean getBoolean(@NonNull String key, boolean defaultValue) {
|
||||
return data.getBoolean(key, defaultValue);
|
||||
public boolean getBoolean(@NonNull String key) {
|
||||
assertKeyPresence(key);
|
||||
return data.getBoolean(key, false);
|
||||
}
|
||||
|
||||
private void assertKeyPresence(@NonNull String key) {
|
||||
if (!data.getKeyValueMap().containsKey(key)) {
|
||||
throw new IllegalStateException("Missing key: " + key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ public class AttachmentDownloadJob extends MasterSecretJob implements Injectable
|
||||
messageId = data.getLong(KEY_MESSAGE_ID);
|
||||
partRowId = data.getLong(KEY_PART_ROW_ID);
|
||||
partUniqueId = data.getLong(KEY_PAR_UNIQUE_ID);
|
||||
manual = data.getBoolean(KEY_MANUAL, false);
|
||||
manual = data.getBoolean(KEY_MANUAL);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -50,11 +50,11 @@ public class DirectoryRefreshJob extends ContextJob {
|
||||
|
||||
@Override
|
||||
protected void initialize(@NonNull SafeData data) {
|
||||
String serializedAddress = data.getNullableString(KEY_ADDRESS);
|
||||
String serializedAddress = data.getString(KEY_ADDRESS);
|
||||
Address address = serializedAddress != null ? Address.fromSerialized(serializedAddress) : null;
|
||||
|
||||
recipient = address != null ? Recipient.from(context, address, true) : null;
|
||||
notifyOfNewUsers = data.getBoolean(KEY_NOTIFY_OF_NEW_USERS, false);
|
||||
notifyOfNewUsers = data.getBoolean(KEY_NOTIFY_OF_NEW_USERS);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -83,7 +83,7 @@ public class MmsDownloadJob extends MasterSecretJob {
|
||||
protected void initialize(@NonNull SafeData data) {
|
||||
messageId = data.getLong(KEY_MESSAGE_ID);
|
||||
threadId = data.getLong(KEY_THREAD_ID);
|
||||
automatic = data.getBoolean(KEY_AUTOMATIC, false);
|
||||
automatic = data.getBoolean(KEY_AUTOMATIC);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -99,8 +99,8 @@ public class MultiDeviceContactUpdateJob extends MasterSecretJob implements Inje
|
||||
|
||||
@Override
|
||||
protected void initialize(@NonNull SafeData data) {
|
||||
address = data.getNullableString(KEY_ADDRESS);
|
||||
forceSync = data.getBoolean(KEY_FORCE_SYNC, false);
|
||||
address = data.getString(KEY_ADDRESS);
|
||||
forceSync = data.getBoolean(KEY_FORCE_SYNC);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -48,7 +48,7 @@ public class MultiDeviceReadReceiptUpdateJob extends ContextJob implements Injec
|
||||
|
||||
@Override
|
||||
protected void initialize(@NonNull SafeData data) {
|
||||
enabled = data.getBoolean(KEY_ENABLED, false);
|
||||
enabled = data.getBoolean(KEY_ENABLED);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -40,7 +40,7 @@ public class PushContentReceiveJob extends PushReceivedJob {
|
||||
|
||||
@Override
|
||||
protected void initialize(@NonNull SafeData data) {
|
||||
this.data = data.getNullableString(KEY_DATA);
|
||||
this.data = data.getString(KEY_DATA);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -84,7 +84,7 @@ public class PushGroupSendJob extends PushSendJob implements InjectableType {
|
||||
@Override
|
||||
protected void initialize(@NonNull SafeData data) {
|
||||
messageId = data.getLong(KEY_MESSAGE_ID);
|
||||
filterAddress = data.getNullableString(KEY_FILTER_ADDRESS);
|
||||
filterAddress = data.getString(KEY_FILTER_ADDRESS);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -59,7 +59,7 @@ public class RetrieveProfileAvatarJob extends ContextJob implements InjectableTy
|
||||
|
||||
@Override
|
||||
protected void initialize(@NonNull SafeData data) {
|
||||
profileAvatar = data.getNullableString(KEY_PROFILE_AVATAR);
|
||||
profileAvatar = data.getString(KEY_PROFILE_AVATAR);
|
||||
recipient = Recipient.from(context, Address.fromSerialized(data.getString(KEY_ADDRESS)), true);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user