Skip to content

Commit

Permalink
Merge pull request #623 from ably/experimental/push-verbose-logs
Browse files Browse the repository at this point in the history
Add verbose logs in push notification related code
  • Loading branch information
QuintinWillison authored Nov 20, 2020
2 parents 0e4da0f + ae09d05 commit 19649e2
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 7 deletions.
8 changes: 8 additions & 0 deletions android/src/main/java/io/ably/lib/platform/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.ably.lib.transport.NetworkConnectivity.DelegatedNetworkConnectivity;
import io.ably.lib.types.AblyException;
import io.ably.lib.types.ErrorInfo;
import io.ably.lib.util.Log;

import java.util.WeakHashMap;

Expand All @@ -25,12 +26,17 @@ public Context getApplicationContext() {
* Set the Android Context for this instance
*/
public void setAndroidContext(Context context) throws AblyException {
Log.v(TAG, "setAndroidContext: context=" + context);
context = context.getApplicationContext();
if(applicationContext != null) {
Log.v(TAG, "setAndroidContext(): applicationContext has already been set");
if(context == applicationContext) {
Log.v(TAG, "setAndroidContext(): existing applicationContext is compatible with that being set");
return;
}
throw AblyException.fromErrorInfo(new ErrorInfo("Incompatible application context set", 40000, 400));
} else {
Log.v(TAG, "setAndroidContext(): there was no existing applicationContext");
}
applicationContext = context;
AndroidNetworkConnectivity.getNetworkConnectivity(context).addListener(this.networkConnectivity);
Expand All @@ -50,4 +56,6 @@ public NetworkConnectivity getNetworkConnectivity() {

private Context applicationContext;
private final DelegatedNetworkConnectivity networkConnectivity = new DelegatedNetworkConnectivity();

private static final String TAG = Platform.class.getName();
}
24 changes: 24 additions & 0 deletions android/src/main/java/io/ably/lib/push/ActivationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,25 @@ Context getContext() {

public synchronized LocalDevice getLocalDevice() {
if(localDevice == null) {
Log.v(TAG, "getLocalDevice(): creating new instance and returning that");
localDevice = new LocalDevice(this);
} else {
Log.v(TAG, "getLocalDevice(): returning existing instance");
}
return localDevice;
}

public synchronized void setActivationStateMachine(ActivationStateMachine activationStateMachine) {
Log.v(TAG, "setActivationStateMachine(): activationStateMachine=" + activationStateMachine);
this.activationStateMachine = activationStateMachine;
}

public synchronized ActivationStateMachine getActivationStateMachine() {
if(activationStateMachine == null) {
Log.v(TAG, "getActivationStateMachine(): creating new instance and returning that");
activationStateMachine = new ActivationStateMachine(this);
} else {
Log.v(TAG, "getActivationStateMachine(): returning existing instance");
}
return activationStateMachine;
}
Expand All @@ -55,6 +62,8 @@ AblyRest getAbly() throws AblyException {
if(ably != null) {
Log.v(TAG, "getAbly(): returning existing Ably instance");
return ably;
} else {
Log.v(TAG, "getAbly(): creating new Ably instance");
}

String deviceIdentityToken = getLocalDevice().deviceIdentityToken;
Expand All @@ -67,22 +76,27 @@ AblyRest getAbly() throws AblyException {
}

public boolean setClientId(String clientId, boolean propagateGotPushDeviceDetails) {
Log.v(TAG, "setClientId(): clientId=" + clientId + ", propagateGotPushDeviceDetails=" + propagateGotPushDeviceDetails);
boolean updated = !clientId.equals(this.clientId);
if(updated) {
this.clientId = clientId;
if(localDevice != null) {
Log.v(TAG, "setClientId(): local device exists");
/* Spec: RSH8d */
localDevice.setClientId(clientId);
if(localDevice.isRegistered() && activationStateMachine != null && propagateGotPushDeviceDetails) {
/* Spec: RSH8e */
activationStateMachine.handleEvent(new ActivationStateMachine.GotPushDeviceDetails());
}
} else {
Log.v(TAG, "setClientId(): local device doest not exist");
}
}
return updated;
}

public void onNewRegistrationToken(RegistrationToken.Type type, String token) {
Log.v(TAG, "onNewRegistrationToken(): type=" + type + ", token=" + token);
LocalDevice localDevice = getLocalDevice();
RegistrationToken previous = localDevice.getRegistrationToken();
if (previous != null) {
Expand All @@ -100,6 +114,8 @@ public void onNewRegistrationToken(RegistrationToken.Type type, String token) {
}

public void reset() {
Log.v(TAG, "reset()");

ably = null;

getActivationStateMachine().reset();
Expand All @@ -120,19 +136,26 @@ public static ActivationContext getActivationContext(Context applicationContext,
if(activationContext == null) {
Log.v(TAG, "getActivationContext(): creating new ActivationContext for this application");
activationContexts.put(applicationContext, (activationContext = new ActivationContext(applicationContext)));
} else {
Log.v(TAG, "getActivationContext(): returning existing ActivationContext for this application");
}
if(ably != null) {
Log.v(TAG, "Setting Ably instance on the activation context");
activationContext.setAbly(ably);
} else {
Log.v(TAG, "Not setting Ably instance on the activation context");
}
}
return activationContext;
}

protected void getRegistrationToken(final Callback<String> callback) {
Log.v(TAG, "getRegistrationToken(): callback=" + callback);
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(Task<InstanceIdResult> task) {
Log.v(TAG, "getRegistrationToken(): firebase called onComplete(): task=" + task);
if(task.isSuccessful()) {
/* Get new Instance ID token */
String token = task.getResult().getToken();
Expand All @@ -145,6 +168,7 @@ public void onComplete(Task<InstanceIdResult> task) {
}

public static void setActivationContext(Context applicationContext, ActivationContext activationContext) {
Log.v(TAG, "setActivationContext(): applicationContext=" + applicationContext + ", activationContext=" + activationContext);
activationContexts.put(applicationContext, activationContext);
}

Expand Down
13 changes: 11 additions & 2 deletions android/src/main/java/io/ably/lib/push/LocalDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ private void loadPersisted() {
if(id != null) {
Log.v(TAG, "loadPersisted(): existing deviceId found; id: " + id);
deviceSecret = prefs.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);
Expand All @@ -65,8 +67,8 @@ private void loadPersisted() {
if(type != null) {
RegistrationToken token = null;
String tokenString = prefs.getString(SharedPrefKeys.TOKEN, null);
Log.d(TAG, "loadPersisted(): token string = " + tokenString);
if(tokenString != null) {
Log.d(TAG, "loadPersisted(): token string = " + tokenString);
token = new RegistrationToken(type, tokenString);
setRegistrationToken(token);
}
Expand All @@ -76,26 +78,30 @@ private void loadPersisted() {
RegistrationToken getRegistrationToken() {
JsonObject recipient = push.recipient;
if(recipient == null) {
Log.v(TAG, "getRegistrationToken(): returning null because push.recipient is null");
return null;
}
Log.v(TAG, "getRegistrationToken(): returning a new registration token because push.recipient is set");
return new RegistrationToken(
RegistrationToken.Type.fromName(recipient.get("transportType").getAsString()),
recipient.get("registrationToken").getAsString()
);
}

private void setRegistrationToken(RegistrationToken token) {
Log.v(TAG, "setRegistrationToken(): token=" + token);
push.recipient = new JsonObject();
push.recipient.addProperty("transportType", token.type.toName());
push.recipient.addProperty("registrationToken", token.token);
}

private void clearRegistrationToken() {
Log.v(TAG, "clearRegistrationToken()");
push.recipient = null;
}

void setAndPersistRegistrationToken(RegistrationToken token) {
Log.v(TAG, "setAndPersistRegistrationToken(): token: " + token.token);
Log.v(TAG, "setAndPersistRegistrationToken(): token=" + token);
setRegistrationToken(token);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext());
prefs.edit()
Expand All @@ -105,12 +111,14 @@ void setAndPersistRegistrationToken(RegistrationToken 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();
}

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();
Expand Down Expand Up @@ -178,6 +186,7 @@ private static class SharedPrefKeys {
}

private static String generateSecret() {
Log.v(TAG, "generateSecret()");
byte[] entropy = new byte[64];
(new SecureRandom()).nextBytes(entropy);
MessageDigest digest = null;
Expand Down
25 changes: 20 additions & 5 deletions android/src/main/java/io/ably/lib/push/Push.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import io.ably.lib.types.Param;
import io.ably.lib.util.Log;

import java.util.Arrays;

public class Push extends PushBase {

public Push(AblyBase rest) {
Expand All @@ -22,7 +24,7 @@ public void activate() throws AblyException {
}

public void activate(boolean useCustomRegistrar) throws AblyException {
Log.v(TAG, "activate(): useCustomRegistrar " + useCustomRegistrar);
Log.v(TAG, "activate(): useCustomRegistrar=" + useCustomRegistrar);
Context context = getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
getStateMachine().handleEvent(ActivationStateMachine.CalledActivate.useCustomRegistrar(useCustomRegistrar, prefs));
Expand All @@ -33,7 +35,7 @@ public void deactivate() throws AblyException {
}

public void deactivate(boolean useCustomRegistrar) throws AblyException {
Log.v(TAG, "deactivate(): useCustomRegistrar " + useCustomRegistrar);
Log.v(TAG, "deactivate(): useCustomRegistrar=" + useCustomRegistrar);
Context context = getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
getStateMachine().handleEvent(ActivationStateMachine.CalledDeactivate.useCustomRegistrar(useCustomRegistrar, prefs));
Expand All @@ -46,7 +48,10 @@ synchronized ActivationStateMachine getStateMachine() throws AblyException {
public void tryRequestRegistrationToken() {
try {
if (getLocalDevice().isRegistered()) {
Log.v(TAG, "Local device is registered.");
getStateMachine().getRegistrationToken();
} else {
Log.v(TAG, "Local device is not registered.");
}
} catch (AblyException e) {
Log.e(TAG, "couldn't validate existing push recipient device details", e);
Expand All @@ -66,8 +71,11 @@ Context getApplicationContext() throws AblyException {

public ActivationContext getActivationContext() throws AblyException {
if (activationContext == null) {
Log.v(TAG, "getActivationContext(): creating a new context and returning that");
Context applicationContext = getApplicationContext();
activationContext = ActivationContext.getActivationContext(applicationContext, (AblyRest)rest);
} else {
Log.v(TAG, "getActivationContext(): returning existing content");
}
return activationContext;
}
Expand All @@ -81,11 +89,16 @@ Param[] pushRequestHeaders(boolean forLocalDevice) {
Param[] headers = super.pushRequestHeaders(forLocalDevice);
if(forLocalDevice) {
try {
Param[] deviceIdentityHeaders = getLocalDevice().deviceIdentityHeaders();
final Param[] deviceIdentityHeaders = getLocalDevice().deviceIdentityHeaders();
if(deviceIdentityHeaders != null) {
Log.v(TAG, "pushRequestHeaders(): deviceIdentityHeaders=" + Arrays.toString(deviceIdentityHeaders));
headers = HttpUtils.mergeHeaders(headers, deviceIdentityHeaders);
} else {
Log.w(TAG, "pushRequestHeaders(): Local device returned null device identity headers!");
}
} catch (AblyException e) {}
} catch (AblyException e) {
Log.w(TAG, "pushRequestHeaders(): Failed to get device identity headers. forLocalDevice=" + forLocalDevice, e);
}
}
return headers;
}
Expand All @@ -94,7 +107,9 @@ Param[] pushRequestHeaders(String deviceId) {
boolean forLocalDevice = false;
try {
forLocalDevice = deviceId != null && deviceId.equals(getLocalDevice().id);
} catch (AblyException e) {}
} catch (AblyException e) {
Log.w(TAG, "pushRequestHeaders(): deviceId=" + deviceId, e);
}
return pushRequestHeaders(forLocalDevice);
}

Expand Down
2 changes: 2 additions & 0 deletions android/src/main/java/io/ably/lib/rest/AblyRest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public LocalDevice device() throws AblyException {
* Set the Android Context for this instance
*/
public void setAndroidContext(Context context) throws AblyException {
Log.v(TAG, "setAndroidContext(): context=" + context);
this.platform.setAndroidContext(context);
this.push.tryRequestRegistrationToken();
}
Expand All @@ -49,6 +50,7 @@ public void setAndroidContext(Context context) throws AblyException {
* clientId set by late initialisation
*/
protected void onClientIdSet(String clientId) {
Log.v(TAG, "onClientIdSet(): clientId=" + clientId);
/* we only need to propagate any update to clientId if this is a late init */
if(push != null && platform.hasApplicationContext()) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ public String toName() {
return name().toLowerCase();
}
}

@Override
public String toString() {
return "RegistrationToken{" +
"type=" + type +
", token='" + token + '\'' +
'}';
}
}
Loading

0 comments on commit 19649e2

Please sign in to comment.