Skip to content

Commit

Permalink
Merge pull request #972 from ably/deviceid-and-token-consistency
Browse files Browse the repository at this point in the history
fix: deviceId and deviceToken consistence
  • Loading branch information
ttypic authored Nov 20, 2023
2 parents 442e82e + ab42bc6 commit 78a7137
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void events_subclasses_correctly_constructed_by_name() throws ClassNotFou

@Test
public void events_with_constructor_parameter_do_not_have_persisted_name() {
assertNull(new GotDeviceRegistration(null).getPersistedName());
assertNull(new GotDeviceRegistration(null, null).getPersistedName());
assertNull(new GettingDeviceRegistrationFailed(null).getPersistedName());
assertNull(new GettingPushDeviceDetailsFailed(null).getPersistedName());
assertNull(new SyncRegistrationFailed(null).getPersistedName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,6 @@ public static void setActivationContext(Context applicationContext, ActivationCo
protected final SharedPreferences prefs;
protected final Context context;

private static WeakHashMap<Context, ActivationContext> activationContexts = new WeakHashMap<Context, ActivationContext>();
private static final WeakHashMap<Context, ActivationContext> activationContexts = new WeakHashMap<>();
private static final String TAG = ActivationContext.class.getName();
}
24 changes: 18 additions & 6 deletions android/src/main/java/io/ably/lib/push/ActivationStateMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,13 @@ public String toString() {
}

public static class GotDeviceRegistration extends ActivationStateMachine.Event {
final String deviceId;
final String deviceIdentityToken;
public GotDeviceRegistration(String token) { this.deviceIdentityToken = token; }

public GotDeviceRegistration(String deviceId, String token) {
this.deviceId = deviceId;
this.deviceIdentityToken = token;
}

@Override
public String toString() {
Expand Down Expand Up @@ -317,7 +322,7 @@ public void onSuccess(JsonObject response) {
activationContext.setClientId(responseClientId, false);
}
}
machine.handleEvent(new ActivationStateMachine.GotDeviceRegistration(deviceIdentityTokenJson.getAsJsonPrimitive("token").getAsString()));
machine.handleEvent(new ActivationStateMachine.GotDeviceRegistration(device.id, deviceIdentityTokenJson.getAsJsonPrimitive("token").getAsString()));
}
@Override
public void onError(ErrorInfo reason) {
Expand Down Expand Up @@ -346,7 +351,13 @@ public ActivationStateMachine.State transition(ActivationStateMachine.Event even
return this;
} else if (event instanceof ActivationStateMachine.GotDeviceRegistration) {
LocalDevice device = machine.getDevice();
device.setDeviceIdentityToken(((ActivationStateMachine.GotDeviceRegistration) event).deviceIdentityToken);
ActivationStateMachine.GotDeviceRegistration gotDeviceRegistration = (ActivationStateMachine.GotDeviceRegistration) event;
if (device.id.equals(gotDeviceRegistration.deviceId)) {
device.setDeviceIdentityToken(gotDeviceRegistration.deviceIdentityToken);
} else {
Log.e(TAG, "error registering " + device.id + ": " + "deviceId has been changed during registration, it was " + gotDeviceRegistration.deviceId);
throw new IllegalStateException("DeviceId has been changed during registration");
}
machine.callActivatedCallback(null);
return new ActivationStateMachine.WaitingForNewPushDeviceDetails(machine);
} else if (event instanceof ActivationStateMachine.GettingDeviceRegistrationFailed) {
Expand Down Expand Up @@ -553,19 +564,20 @@ private void sendErrorIntent(String name, ErrorInfo error) {
}

private void invokeCustomRegistration(final DeviceDetails device, final boolean isNew) {
final String deviceId = device.id;
registerOnceReceiver("PUSH_DEVICE_REGISTERED", new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ErrorInfo error = IntentUtils.getErrorInfo(intent);
if (error == null) {
Log.i(TAG, "custom registration for " + device.id);
Log.i(TAG, "custom registration for " + deviceId);
if (isNew) {
handleEvent(new ActivationStateMachine.GotDeviceRegistration(intent.getStringExtra("deviceIdentityToken")));
handleEvent(new ActivationStateMachine.GotDeviceRegistration(deviceId, intent.getStringExtra("deviceIdentityToken")));
} else {
handleEvent(new RegistrationSynced());
}
} else {
Log.e(TAG, "error from custom registration for " + device.id + ": " + error.toString());
Log.e(TAG, "error from custom registration for " + deviceId + ": " + error.toString());
if (isNew) {
handleEvent(new ActivationStateMachine.GettingDeviceRegistrationFailed(error));
} else {
Expand Down
1 change: 1 addition & 0 deletions android/src/main/java/io/ably/lib/push/LocalDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ void create() {
storage.put(SharedPrefKeys.DEVICE_ID, (id = UUID.randomUUID().toString()));
storage.put(SharedPrefKeys.CLIENT_ID, (clientId = activationContext.clientId));
storage.put(SharedPrefKeys.DEVICE_SECRET, (deviceSecret = generateSecret()));
storage.put(SharedPrefKeys.DEVICE_TOKEN, (deviceIdentityToken = null));
}

public void reset() {
Expand Down

0 comments on commit 78a7137

Please sign in to comment.