Skip to content

Commit 242720b

Browse files
committed
Additional internal device ID refactoring. Adding null checks.
1 parent d516365 commit 242720b

File tree

10 files changed

+135
-156
lines changed

10 files changed

+135
-156
lines changed

sdk/src/androidTest/java/ly/count/android/sdk/DeviceIdTests.java

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -167,48 +167,22 @@ public void getId() {
167167
* Validating 'changeToId' with developer supplied values
168168
*/
169169
@Test
170-
public void changeToIdDevSupplied() {
170+
public void changeToCustomIdAndEnterTempIDMode() {
171171
DeviceId did = new DeviceId(DeviceIdType.DEVELOPER_SUPPLIED, "abc", store, mock(ModuleLog.class), null);
172172
assertEquals("abc", did.getCurrentId());
173-
174-
did.changeToId(DeviceIdType.DEVELOPER_SUPPLIED, "123");
175-
assertEquals("123", did.getCurrentId());
176173
assertEquals(DeviceIdType.DEVELOPER_SUPPLIED, did.getType());
177174

178-
did.changeToId(DeviceIdType.DEVELOPER_SUPPLIED, "456");
179-
assertEquals("456", did.getCurrentId());
175+
did.changeToCustomId("123");
176+
assertEquals("123", did.getCurrentId());
180177
assertEquals(DeviceIdType.DEVELOPER_SUPPLIED, did.getType());
181-
}
182-
183-
/**
184-
* Validating 'changeToId' around openUDID
185-
*/
186-
@Test
187-
public void changeToIdOpenUDID() {
188-
DeviceId did = new DeviceId(DeviceIdType.DEVELOPER_SUPPLIED, "abc", store, mock(ModuleLog.class), openUDIDProvider);
189-
assertEquals("abc", did.getCurrentId());
190178

191-
//set first value without running init, should use the provided value
192-
did.changeToId(DeviceIdType.OPEN_UDID, "123");
193-
assertEquals("123", did.getCurrentId());
194-
assertEquals(DeviceIdType.OPEN_UDID, did.getType());
179+
did.enterTempIDMode();
180+
assertEquals(DeviceId.temporaryCountlyDeviceId, did.getCurrentId());
181+
assertEquals(DeviceIdType.TEMPORARY_ID, did.getType());
195182

196-
//do a reset
197-
did.changeToId(DeviceIdType.DEVELOPER_SUPPLIED, "aaa");
183+
did.changeToCustomId("aaa");
198184
assertEquals("aaa", did.getCurrentId());
199185
assertEquals(DeviceIdType.DEVELOPER_SUPPLIED, did.getType());
200-
201-
//change with init, since a value is specified, it should take precedence
202-
currentOpenUDIDValue = "uio|";
203-
did.changeToId(DeviceIdType.OPEN_UDID, "456");
204-
assertEquals("456", did.getCurrentId());
205-
assertEquals(DeviceIdType.OPEN_UDID, did.getType());
206-
207-
//change with init, it should use it's own value because a null device ID is provided
208-
currentOpenUDIDValue = "sdfh";
209-
did.changeToId(DeviceIdType.OPEN_UDID, null);
210-
assertEquals(currentOpenUDIDValue, did.getCurrentId());
211-
assertEquals(DeviceIdType.OPEN_UDID, did.getType());
212186
}
213187

214188
/**

sdk/src/main/java/ly/count/android/sdk/ConnectionProcessor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,11 @@ public void run() {
362362
storageProvider_.removeRequest(storedEvents[0]);
363363

364364
if (deviceIdChange) {
365-
deviceIdProvider_.getDeviceIdInstance().changeToId(DeviceIdType.DEVELOPER_SUPPLIED, newId);//todo needs to be refactored
365+
if(newId != null && !newId.isEmpty()) {
366+
deviceIdProvider_.getDeviceIdInstance().changeToCustomId(newId);//todo needs to be refactored
367+
} else {
368+
L.e("[Connection Processor] Failed to change device ID with merging because the new ID was empty or null. [" + newId + "]");
369+
}
366370
}
367371

368372
if (deviceIdChange || deviceIdOverride) {

sdk/src/main/java/ly/count/android/sdk/Countly.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,11 @@ public synchronized Countly init(CountlyConfig config) {
482482
L.d("[Init] Parameter tampering protection salt set");
483483
}
484484

485+
if(connectionQueue_ == null) {
486+
L.e("[Init] SDK failed to initialize because the connection queue failed to be created");
487+
return this;
488+
}
489+
485490
//check legacy access methods
486491
if (locationFallback != null && config.locationCountyCode == null && config.locationCity == null && config.locationLocation == null && config.locationIpAddress == null) {
487492
//if the fallback was set and config did not contain any location, use the fallback info
@@ -557,9 +562,8 @@ public synchronized Countly init(CountlyConfig config) {
557562
if (config.customNetworkRequestHeaders != null) {
558563
L.i("[Countly] Calling addCustomNetworkRequestHeaders");
559564
requestHeaderCustomValues = config.customNetworkRequestHeaders;
560-
if (connectionQueue_ != null) {
561-
connectionQueue_.setRequestHeaderCustomValues(requestHeaderCustomValues);
562-
}
565+
566+
connectionQueue_.setRequestHeaderCustomValues(requestHeaderCustomValues);
563567
}
564568

565569
if (config.httpPostForced) {
@@ -932,7 +936,17 @@ public void changeDeviceIdWithoutMerge(DeviceId.Type type, String deviceId) {
932936
return;
933937
}
934938

935-
moduleDeviceId.changeDeviceIdWithoutMergeInternal(ModuleDeviceId.fromOldDeviceIdToNew(type), deviceId);
939+
if(deviceId == null || deviceId.isEmpty()) {
940+
L.e("changeDeviceIdWithoutMerge, can't change device ID to and empty or null value");
941+
return;
942+
}
943+
944+
if(type != DeviceId.Type.DEVELOPER_SUPPLIED) {
945+
L.e("changeDeviceIdWithoutMerge, provided device ID type mus be 'DEVELOPER_SUPPLIED'");
946+
return;
947+
}
948+
949+
moduleDeviceId.changeDeviceIdWithoutMergeInternal(deviceId);
936950
}
937951

938952
/**

sdk/src/main/java/ly/count/android/sdk/CountlyStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static SharedPreferences createPreferencesPush(Context context) {
113113
return context.getSharedPreferences(PREFERENCES_PUSH, Context.MODE_PRIVATE);
114114
}
115115

116-
private @Nullable String storageReadRequestQueue() {
116+
private @NonNull String storageReadRequestQueue() {
117117
if(explicitStorageModeEnabled) {
118118
//L.v("[CountlyStore] Returning RQ from cache");
119119
if(esRequestQueueCache == null) {
@@ -145,7 +145,7 @@ private void storageWriteRequestQueue(@Nullable String requestQueue, boolean wri
145145
}
146146
}
147147

148-
private @Nullable String storageReadEventQueue() {
148+
private @NonNull String storageReadEventQueue() {
149149
if(explicitStorageModeEnabled) {
150150
//L.v("[CountlyStore] Returning EQ from cache");
151151
if(esEventQueueCache == null) {

sdk/src/main/java/ly/count/android/sdk/CrashDetails.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ private static long getTotalRAM() {
7474
value = m.group(1);
7575
}
7676
try {
77-
totalMemory = Long.parseLong(value) / 1024;
77+
if(value != null) {
78+
totalMemory = Long.parseLong(value) / 1024;
79+
} else {
80+
totalMemory = 0;
81+
}
7882
} catch (NumberFormatException ex) {
7983
totalMemory = 0;
8084
}

sdk/src/main/java/ly/count/android/sdk/DeviceId.java

Lines changed: 54 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
public class DeviceId {
77
/**
88
* Enum used throughout Countly which controls what kind of ID Countly should use.
9+
*
910
* @deprecated Replace this type with "DeviceIdType"
1011
*/
1112
public enum Type {
@@ -34,92 +35,69 @@ public enum Type {
3435
@NonNull
3536
OpenUDIDProvider openUDIDProvider;
3637

37-
protected DeviceId(@NonNull DeviceIdType type, @Nullable String developerSuppliedId, @NonNull StorageProvider givenStorageProvider, @NonNull ModuleLog moduleLog, @NonNull OpenUDIDProvider openUDIDProvider) {
38-
if(type == DeviceIdType.DEVELOPER_SUPPLIED) {
39-
if (developerSuppliedId == null || "".equals(developerSuppliedId)) {
40-
throw new IllegalStateException("If using developer supplied type, a valid device ID should be provided, [" + developerSuppliedId + "]");
38+
protected DeviceId(@NonNull DeviceIdType providedType, @Nullable String providedId, @NonNull StorageProvider givenStorageProvider, @NonNull ModuleLog moduleLog, @NonNull OpenUDIDProvider openUDIDProvider) {
39+
if (providedType == DeviceIdType.DEVELOPER_SUPPLIED) {
40+
if (providedId == null || "".equals(providedId)) {
41+
throw new IllegalStateException("If using developer supplied type, a valid device ID should be provided, [" + providedId + "]");
4142
}
42-
} else if(type == DeviceIdType.TEMPORARY_ID) {
43-
if (developerSuppliedId == null || "".equals(developerSuppliedId)) {
44-
throw new IllegalStateException("If using temporary ID type, a valid device ID should be provided, [" + developerSuppliedId + "]");
43+
} else if (providedType == DeviceIdType.TEMPORARY_ID) {
44+
if (providedId == null || "".equals(providedId)) {
45+
throw new IllegalStateException("If using temporary ID type, a valid device ID should be provided, [" + providedId + "]");
4546
}
4647

47-
if(!developerSuppliedId.equals(temporaryCountlyDeviceId)) {
48-
throw new IllegalStateException("If using temporary ID type, the device ID value should be the required one, [" + developerSuppliedId + "]");
48+
if (!providedId.equals(temporaryCountlyDeviceId)) {
49+
throw new IllegalStateException("If using temporary ID type, the device ID value should be the required one, [" + providedId + "]");
4950
}
50-
51-
} else if(type == DeviceIdType.OPEN_UDID || type == DeviceIdType.ADVERTISING_ID){
52-
51+
} else if (providedType == DeviceIdType.OPEN_UDID || providedType == DeviceIdType.ADVERTISING_ID) {
52+
//just adding this check for completeness
5353
} else {
5454
throw new IllegalStateException("Null device ID type is not allowed");
5555
}
5656

5757
storageProvider = givenStorageProvider;
5858
this.openUDIDProvider = openUDIDProvider;
59-
60-
//setup the preferred device ID type
61-
this.type = type;
62-
this.id = developerSuppliedId;
6359
L = moduleLog;
6460

65-
66-
L.d("[DeviceId-int] initialising with values, device ID:[" + this.id + "] type:[" + this.type + "]");
61+
L.d("[DeviceId-int] initialising with values, device ID:[" + providedId + "] type:[" + providedType + "]");
6762

6863
//check if there wasn't a value set before. Read if from storage
6964
String storedId = storageProvider.getDeviceID();
70-
if (storedId != null) {
71-
//if there was a value saved previously, set it and it's type. Overwrite the in constructor set ones
72-
this.id = storedId;
73-
this.type = retrieveStoredType();
74-
75-
L.d("[DeviceId-int] retrieveId, Retrieving a previously set device ID:[" + this.id + "] type:[" + this.type + "]");
76-
} else {
77-
L.d("[DeviceId-int] retrieveId, no previous ID stored");
78-
}
79-
80-
//call init implicitly
81-
init();
82-
}
83-
84-
/**
85-
* Initialize device ID generation
86-
*/
87-
private void init() {
8865
DeviceIdType storedType = retrieveStoredType();
89-
L.d("[DeviceId-int] init, current type:[" + type + "] overriddenType:[" + storedType + "]");
9066

91-
// Some time ago some ID generation strategy was not available and SDK fell back to
92-
// some other strategy. We still have to use that strategy.
93-
if (storedType != null && storedType != type) {
94-
L.i("[DeviceId-int] init, Overridden device ID generation strategy detected: " + storedType + ", using it instead of " + this.type);
95-
type = storedType;
96-
}
67+
L.d("[DeviceId-int] The following values were stored, device ID:[" + storedId + "] type:[" + storedType + "]");
9768

98-
if (type == null) {
99-
L.e("[DeviceId-int] init, device id type currently is null, falling back to OPEN_UDID");
100-
type = DeviceIdType.OPEN_UDID;
101-
}
69+
if (storedId != null && storedType != null) {
70+
//values are set, just use them and ignore the provided ones
71+
id = storedId;
72+
type = storedType;
73+
} else {
74+
if (storedType == null && storedId != null) {
75+
L.e("[DeviceId-int] init, device id type currently is null, falling back to OPEN_UDID");
76+
setAndStoreId(DeviceIdType.OPEN_UDID, storedId);
77+
}
10278

103-
String storedID = storageProvider.getDeviceID();
104-
105-
if (storedID == null) {
106-
//id value will be regenerated only if the values isn't already set
107-
//this is to prevent the device id to change in case the underlying mechanism for openUDID or advertising ID changes
108-
switch (type) {
109-
case DEVELOPER_SUPPLIED:
110-
// no initialization for developer id
111-
// we just store the provided value so that it's
112-
setAndStoreId(DeviceIdType.DEVELOPER_SUPPLIED, id);
113-
break;
114-
case OPEN_UDID:
115-
L.i("[DeviceId-int] Using OpenUDID");
116-
fallbackToOpenUDID();
117-
break;
118-
case ADVERTISING_ID:
119-
// Fall back to OpenUDID on devices without google play services set up
120-
L.i("[DeviceId-int] Use of Advertising ID is deprecated, falling back to OpenUDID");
121-
fallbackToOpenUDID();
122-
break;
79+
if (storedId == null) {
80+
//id value will be regenerated only if the values isn't already set
81+
//this is to prevent the device id to change in case the underlying mechanism for openUDID or advertising ID changes
82+
switch (providedType) {
83+
case TEMPORARY_ID:
84+
setAndStoreId(DeviceIdType.TEMPORARY_ID, providedId);
85+
break;
86+
case DEVELOPER_SUPPLIED:
87+
// no initialization for developer id
88+
// we just store the provided value so that it's
89+
setAndStoreId(DeviceIdType.DEVELOPER_SUPPLIED, providedId);
90+
break;
91+
case OPEN_UDID:
92+
L.i("[DeviceId-int] Using OpenUDID");
93+
setAndStoreId(DeviceIdType.OPEN_UDID, openUDIDProvider.getOpenUDID());
94+
break;
95+
case ADVERTISING_ID:
96+
// Fall back to OpenUDID on devices without google play services set up
97+
L.i("[DeviceId-int] Use of Advertising ID is deprecated, falling back to OpenUDID");
98+
setAndStoreId(DeviceIdType.OPEN_UDID, openUDIDProvider.getOpenUDID());
99+
break;
100+
}
123101
}
124102
}
125103
}
@@ -158,6 +136,7 @@ protected String getCurrentId() {
158136

159137
/**
160138
* Used only for tests
139+
*
161140
* @param type
162141
* @param id
163142
*/
@@ -168,19 +147,19 @@ protected void setId(DeviceIdType type, String id) {
168147
this.id = id;
169148
}
170149

171-
protected void fallbackToOpenUDID() {
172-
setAndStoreId(DeviceIdType.OPEN_UDID, openUDIDProvider.getOpenUDID());
173-
}
174-
175150
/**
176151
* If a value is provided, it will take precedence and will not matter what the type is
177152
*
178-
* @param type
179153
* @param deviceId
180154
*/
181-
protected void changeToId(@NonNull DeviceIdType type, @Nullable String deviceId) {
182-
L.v("[DeviceId-int] changeToId, Device ID is " + id + " (type " + type + ")");
183-
setAndStoreId(type, deviceId);
155+
protected void changeToCustomId(@NonNull String deviceId) {
156+
L.v("[DeviceId-int] changeToCustomId, Device ID is " + id);
157+
setAndStoreId(DeviceIdType.DEVELOPER_SUPPLIED, deviceId);
158+
}
159+
160+
protected void enterTempIDMode() {
161+
L.v("[DeviceId-int] enterTempIDMode");
162+
setAndStoreId(DeviceIdType.DEVELOPER_SUPPLIED, ly.count.android.sdk.DeviceId.temporaryCountlyDeviceId);
184163
}
185164

186165
void setAndStoreId(DeviceIdType type, String deviceId) {

sdk/src/main/java/ly/count/android/sdk/ModuleCrash.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ void enableCrashReporting() {
163163
Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
164164

165165
@Override
166-
public void uncaughtException(@NonNull Thread t, Throwable e) {
166+
public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
167167
L.d("[ModuleCrash] Uncaught crash handler triggered");
168168
if (consentProvider.getConsent(Countly.CountlyFeatureNames.crashes)) {
169169

0 commit comments

Comments
 (0)