Skip to content

Commit 19649e2

Browse files
Merge pull request ably#623 from ably/experimental/push-verbose-logs
Add verbose logs in push notification related code
2 parents 0e4da0f + ae09d05 commit 19649e2

7 files changed

Lines changed: 92 additions & 7 deletions

File tree

android/src/main/java/io/ably/lib/platform/Platform.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.ably.lib.transport.NetworkConnectivity.DelegatedNetworkConnectivity;
1111
import io.ably.lib.types.AblyException;
1212
import io.ably.lib.types.ErrorInfo;
13+
import io.ably.lib.util.Log;
1314

1415
import java.util.WeakHashMap;
1516

@@ -25,12 +26,17 @@ public Context getApplicationContext() {
2526
* Set the Android Context for this instance
2627
*/
2728
public void setAndroidContext(Context context) throws AblyException {
29+
Log.v(TAG, "setAndroidContext: context=" + context);
2830
context = context.getApplicationContext();
2931
if(applicationContext != null) {
32+
Log.v(TAG, "setAndroidContext(): applicationContext has already been set");
3033
if(context == applicationContext) {
34+
Log.v(TAG, "setAndroidContext(): existing applicationContext is compatible with that being set");
3135
return;
3236
}
3337
throw AblyException.fromErrorInfo(new ErrorInfo("Incompatible application context set", 40000, 400));
38+
} else {
39+
Log.v(TAG, "setAndroidContext(): there was no existing applicationContext");
3440
}
3541
applicationContext = context;
3642
AndroidNetworkConnectivity.getNetworkConnectivity(context).addListener(this.networkConnectivity);
@@ -50,4 +56,6 @@ public NetworkConnectivity getNetworkConnectivity() {
5056

5157
private Context applicationContext;
5258
private final DelegatedNetworkConnectivity networkConnectivity = new DelegatedNetworkConnectivity();
59+
60+
private static final String TAG = Platform.class.getName();
5361
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,25 @@ Context getContext() {
3030

3131
public synchronized LocalDevice getLocalDevice() {
3232
if(localDevice == null) {
33+
Log.v(TAG, "getLocalDevice(): creating new instance and returning that");
3334
localDevice = new LocalDevice(this);
35+
} else {
36+
Log.v(TAG, "getLocalDevice(): returning existing instance");
3437
}
3538
return localDevice;
3639
}
3740

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

4246
public synchronized ActivationStateMachine getActivationStateMachine() {
4347
if(activationStateMachine == null) {
48+
Log.v(TAG, "getActivationStateMachine(): creating new instance and returning that");
4449
activationStateMachine = new ActivationStateMachine(this);
50+
} else {
51+
Log.v(TAG, "getActivationStateMachine(): returning existing instance");
4552
}
4653
return activationStateMachine;
4754
}
@@ -55,6 +62,8 @@ AblyRest getAbly() throws AblyException {
5562
if(ably != null) {
5663
Log.v(TAG, "getAbly(): returning existing Ably instance");
5764
return ably;
65+
} else {
66+
Log.v(TAG, "getAbly(): creating new Ably instance");
5867
}
5968

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

6978
public boolean setClientId(String clientId, boolean propagateGotPushDeviceDetails) {
79+
Log.v(TAG, "setClientId(): clientId=" + clientId + ", propagateGotPushDeviceDetails=" + propagateGotPushDeviceDetails);
7080
boolean updated = !clientId.equals(this.clientId);
7181
if(updated) {
7282
this.clientId = clientId;
7383
if(localDevice != null) {
84+
Log.v(TAG, "setClientId(): local device exists");
7485
/* Spec: RSH8d */
7586
localDevice.setClientId(clientId);
7687
if(localDevice.isRegistered() && activationStateMachine != null && propagateGotPushDeviceDetails) {
7788
/* Spec: RSH8e */
7889
activationStateMachine.handleEvent(new ActivationStateMachine.GotPushDeviceDetails());
7990
}
91+
} else {
92+
Log.v(TAG, "setClientId(): local device doest not exist");
8093
}
8194
}
8295
return updated;
8396
}
8497

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

102116
public void reset() {
117+
Log.v(TAG, "reset()");
118+
103119
ably = null;
104120

105121
getActivationStateMachine().reset();
@@ -120,19 +136,26 @@ public static ActivationContext getActivationContext(Context applicationContext,
120136
if(activationContext == null) {
121137
Log.v(TAG, "getActivationContext(): creating new ActivationContext for this application");
122138
activationContexts.put(applicationContext, (activationContext = new ActivationContext(applicationContext)));
139+
} else {
140+
Log.v(TAG, "getActivationContext(): returning existing ActivationContext for this application");
123141
}
124142
if(ably != null) {
143+
Log.v(TAG, "Setting Ably instance on the activation context");
125144
activationContext.setAbly(ably);
145+
} else {
146+
Log.v(TAG, "Not setting Ably instance on the activation context");
126147
}
127148
}
128149
return activationContext;
129150
}
130151

131152
protected void getRegistrationToken(final Callback<String> callback) {
153+
Log.v(TAG, "getRegistrationToken(): callback=" + callback);
132154
FirebaseInstanceId.getInstance().getInstanceId()
133155
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
134156
@Override
135157
public void onComplete(Task<InstanceIdResult> task) {
158+
Log.v(TAG, "getRegistrationToken(): firebase called onComplete(): task=" + task);
136159
if(task.isSuccessful()) {
137160
/* Get new Instance ID token */
138161
String token = task.getResult().getToken();
@@ -145,6 +168,7 @@ public void onComplete(Task<InstanceIdResult> task) {
145168
}
146169

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

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ private void loadPersisted() {
5454
if(id != null) {
5555
Log.v(TAG, "loadPersisted(): existing deviceId found; id: " + id);
5656
deviceSecret = prefs.getString(SharedPrefKeys.DEVICE_SECRET, null);
57+
} else {
58+
Log.v(TAG, "loadPersisted(): existing deviceId not found.");
5759
}
5860
this.clientId = prefs.getString(SharedPrefKeys.CLIENT_ID, null);
5961
this.deviceIdentityToken = prefs.getString(SharedPrefKeys.DEVICE_TOKEN, null);
@@ -65,8 +67,8 @@ private void loadPersisted() {
6567
if(type != null) {
6668
RegistrationToken token = null;
6769
String tokenString = prefs.getString(SharedPrefKeys.TOKEN, null);
70+
Log.d(TAG, "loadPersisted(): token string = " + tokenString);
6871
if(tokenString != null) {
69-
Log.d(TAG, "loadPersisted(): token string = " + tokenString);
7072
token = new RegistrationToken(type, tokenString);
7173
setRegistrationToken(token);
7274
}
@@ -76,26 +78,30 @@ private void loadPersisted() {
7678
RegistrationToken getRegistrationToken() {
7779
JsonObject recipient = push.recipient;
7880
if(recipient == null) {
81+
Log.v(TAG, "getRegistrationToken(): returning null because push.recipient is null");
7982
return null;
8083
}
84+
Log.v(TAG, "getRegistrationToken(): returning a new registration token because push.recipient is set");
8185
return new RegistrationToken(
8286
RegistrationToken.Type.fromName(recipient.get("transportType").getAsString()),
8387
recipient.get("registrationToken").getAsString()
8488
);
8589
}
8690

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

9398
private void clearRegistrationToken() {
99+
Log.v(TAG, "clearRegistrationToken()");
94100
push.recipient = null;
95101
}
96102

97103
void setAndPersistRegistrationToken(RegistrationToken token) {
98-
Log.v(TAG, "setAndPersistRegistrationToken(): token: " + token.token);
104+
Log.v(TAG, "setAndPersistRegistrationToken(): token=" + token);
99105
setRegistrationToken(token);
100106
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext());
101107
prefs.edit()
@@ -105,12 +111,14 @@ void setAndPersistRegistrationToken(RegistrationToken token) {
105111
}
106112

107113
void setClientId(String clientId) {
114+
Log.v(TAG, "setClientId(): clientId=" + clientId);
108115
this.clientId = clientId;
109116
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext());
110117
prefs.edit().putString(SharedPrefKeys.CLIENT_ID, clientId).apply();
111118
}
112119

113120
public void setDeviceIdentityToken(String token) {
121+
Log.v(TAG, "setDeviceIdentityToken(): token=" + token);
114122
this.deviceIdentityToken = token;
115123
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activationContext.getContext());
116124
prefs.edit().putString(SharedPrefKeys.DEVICE_TOKEN, token).apply();
@@ -178,6 +186,7 @@ private static class SharedPrefKeys {
178186
}
179187

180188
private static String generateSecret() {
189+
Log.v(TAG, "generateSecret()");
181190
byte[] entropy = new byte[64];
182191
(new SecureRandom()).nextBytes(entropy);
183192
MessageDigest digest = null;

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import io.ably.lib.types.Param;
1212
import io.ably.lib.util.Log;
1313

14+
import java.util.Arrays;
15+
1416
public class Push extends PushBase {
1517

1618
public Push(AblyBase rest) {
@@ -22,7 +24,7 @@ public void activate() throws AblyException {
2224
}
2325

2426
public void activate(boolean useCustomRegistrar) throws AblyException {
25-
Log.v(TAG, "activate(): useCustomRegistrar " + useCustomRegistrar);
27+
Log.v(TAG, "activate(): useCustomRegistrar=" + useCustomRegistrar);
2628
Context context = getApplicationContext();
2729
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
2830
getStateMachine().handleEvent(ActivationStateMachine.CalledActivate.useCustomRegistrar(useCustomRegistrar, prefs));
@@ -33,7 +35,7 @@ public void deactivate() throws AblyException {
3335
}
3436

3537
public void deactivate(boolean useCustomRegistrar) throws AblyException {
36-
Log.v(TAG, "deactivate(): useCustomRegistrar " + useCustomRegistrar);
38+
Log.v(TAG, "deactivate(): useCustomRegistrar=" + useCustomRegistrar);
3739
Context context = getApplicationContext();
3840
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
3941
getStateMachine().handleEvent(ActivationStateMachine.CalledDeactivate.useCustomRegistrar(useCustomRegistrar, prefs));
@@ -46,7 +48,10 @@ synchronized ActivationStateMachine getStateMachine() throws AblyException {
4648
public void tryRequestRegistrationToken() {
4749
try {
4850
if (getLocalDevice().isRegistered()) {
51+
Log.v(TAG, "Local device is registered.");
4952
getStateMachine().getRegistrationToken();
53+
} else {
54+
Log.v(TAG, "Local device is not registered.");
5055
}
5156
} catch (AblyException e) {
5257
Log.e(TAG, "couldn't validate existing push recipient device details", e);
@@ -66,8 +71,11 @@ Context getApplicationContext() throws AblyException {
6671

6772
public ActivationContext getActivationContext() throws AblyException {
6873
if (activationContext == null) {
74+
Log.v(TAG, "getActivationContext(): creating a new context and returning that");
6975
Context applicationContext = getApplicationContext();
7076
activationContext = ActivationContext.getActivationContext(applicationContext, (AblyRest)rest);
77+
} else {
78+
Log.v(TAG, "getActivationContext(): returning existing content");
7179
}
7280
return activationContext;
7381
}
@@ -81,11 +89,16 @@ Param[] pushRequestHeaders(boolean forLocalDevice) {
8189
Param[] headers = super.pushRequestHeaders(forLocalDevice);
8290
if(forLocalDevice) {
8391
try {
84-
Param[] deviceIdentityHeaders = getLocalDevice().deviceIdentityHeaders();
92+
final Param[] deviceIdentityHeaders = getLocalDevice().deviceIdentityHeaders();
8593
if(deviceIdentityHeaders != null) {
94+
Log.v(TAG, "pushRequestHeaders(): deviceIdentityHeaders=" + Arrays.toString(deviceIdentityHeaders));
8695
headers = HttpUtils.mergeHeaders(headers, deviceIdentityHeaders);
96+
} else {
97+
Log.w(TAG, "pushRequestHeaders(): Local device returned null device identity headers!");
8798
}
88-
} catch (AblyException e) {}
99+
} catch (AblyException e) {
100+
Log.w(TAG, "pushRequestHeaders(): Failed to get device identity headers. forLocalDevice=" + forLocalDevice, e);
101+
}
89102
}
90103
return headers;
91104
}
@@ -94,7 +107,9 @@ Param[] pushRequestHeaders(String deviceId) {
94107
boolean forLocalDevice = false;
95108
try {
96109
forLocalDevice = deviceId != null && deviceId.equals(getLocalDevice().id);
97-
} catch (AblyException e) {}
110+
} catch (AblyException e) {
111+
Log.w(TAG, "pushRequestHeaders(): deviceId=" + deviceId, e);
112+
}
98113
return pushRequestHeaders(forLocalDevice);
99114
}
100115

android/src/main/java/io/ably/lib/rest/AblyRest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public LocalDevice device() throws AblyException {
4141
* Set the Android Context for this instance
4242
*/
4343
public void setAndroidContext(Context context) throws AblyException {
44+
Log.v(TAG, "setAndroidContext(): context=" + context);
4445
this.platform.setAndroidContext(context);
4546
this.push.tryRequestRegistrationToken();
4647
}
@@ -49,6 +50,7 @@ public void setAndroidContext(Context context) throws AblyException {
4950
* clientId set by late initialisation
5051
*/
5152
protected void onClientIdSet(String clientId) {
53+
Log.v(TAG, "onClientIdSet(): clientId=" + clientId);
5254
/* we only need to propagate any update to clientId if this is a late init */
5355
if(push != null && platform.hasApplicationContext()) {
5456
try {

android/src/main/java/io/ably/lib/types/RegistrationToken.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,12 @@ public String toName() {
3333
return name().toLowerCase();
3434
}
3535
}
36+
37+
@Override
38+
public String toString() {
39+
return "RegistrationToken{" +
40+
"type=" + type +
41+
", token='" + token + '\'' +
42+
'}';
43+
}
3644
}

0 commit comments

Comments
 (0)