Skip to content

Commit 814fe14

Browse files
authored
Allow changing of Device ID more than once (#498)
* Allow changing of Device ID * add unit tests
1 parent 4732ff0 commit 814fe14

File tree

4 files changed

+133
-34
lines changed

4 files changed

+133
-34
lines changed

AndroidSDKCore/src/main/java/com/leanplum/Leanplum.java

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.leanplum.callbacks.StartCallback;
3737
import com.leanplum.callbacks.VariablesChangedCallback;
3838
import com.leanplum.internal.APIConfig;
39-
import com.leanplum.internal.ActionManager;
4039
import com.leanplum.internal.ApiConfigLoader;
4140
import com.leanplum.internal.Constants;
4241
import com.leanplum.internal.CountAggregator;
@@ -56,7 +55,6 @@
5655
import com.leanplum.internal.RequestSender;
5756
import com.leanplum.internal.RequestSenderTimer;
5857
import com.leanplum.internal.RequestUtil;
59-
import com.leanplum.internal.Socket;
6058
import com.leanplum.internal.Util;
6159
import com.leanplum.internal.Util.DeviceIdInfo;
6260
import com.leanplum.internal.VarCache;
@@ -331,6 +329,40 @@ public static void setDeviceId(String deviceId) {
331329
userSpecifiedDeviceId = true;
332330
}
333331

332+
/**
333+
* (Advanced) Sets new device ID. Must be called after Leanplum finished starting.
334+
* This method allows multiple changes of device ID in opposite of
335+
* {@link Leanplum#setDeviceId(String)}, which allows only one.
336+
*/
337+
public static void forceNewDeviceId(String deviceId) {
338+
if (TextUtils.isEmpty(deviceId)) {
339+
Log.i("forceNewDeviceId - Empty deviceId parameter provided.");
340+
return;
341+
}
342+
343+
if (deviceId.equals(APIConfig.getInstance().deviceId())) {
344+
// same device ID, nothing to change
345+
return;
346+
}
347+
348+
if (hasStarted()) {
349+
APIConfig.getInstance().setDeviceId(deviceId);
350+
APIConfig.getInstance().save();
351+
VarCache.saveDiffs(); // device ID is saved there
352+
353+
Map<String, Object> params = new HashMap<>();
354+
attachDeviceParams(params);
355+
356+
Request request = RequestBuilder
357+
.withSetDeviceAttributesAction()
358+
.andParams(params)
359+
.andType(RequestType.IMMEDIATE)
360+
.create();
361+
362+
RequestSender.getInstance().send(request);
363+
}
364+
}
365+
334366
/**
335367
* Sets a custom locale. You should call this before {@link Leanplum#start}.
336368
*/
@@ -640,6 +672,38 @@ private static void checkAndStartNotificationsModules() {
640672
}
641673
}
642674

675+
private static void attachDeviceParams(@NonNull Map<String, Object> params) {
676+
String versionName = Util.getVersionName();
677+
if (customAppVersion != null) {
678+
versionName = customAppVersion;
679+
}
680+
if (versionName == null) {
681+
versionName = "";
682+
}
683+
684+
String fcmRegistrationId = SharedPreferencesUtil.getString(context,
685+
Constants.Defaults.LEANPLUM_PUSH, Constants.Defaults.PROPERTY_FCM_TOKEN_ID);
686+
String miPushRegistrationId = SharedPreferencesUtil.getString(context,
687+
Constants.Defaults.LEANPLUM_PUSH, Constants.Defaults.PROPERTY_MIPUSH_TOKEN_ID);
688+
String hmsRegistrationId = SharedPreferencesUtil.getString(context,
689+
Constants.Defaults.LEANPLUM_PUSH, Constants.Defaults.PROPERTY_HMS_TOKEN_ID);
690+
691+
params.put(Constants.Params.VERSION_NAME, versionName);
692+
params.put(Constants.Params.DEVICE_NAME, Util.getDeviceName());
693+
params.put(Constants.Params.DEVICE_MODEL, Util.getDeviceModel());
694+
params.put(Constants.Params.DEVICE_SYSTEM_NAME, Util.getSystemName());
695+
params.put(Constants.Params.DEVICE_SYSTEM_VERSION, Util.getSystemVersion());
696+
if (!TextUtils.isEmpty(fcmRegistrationId)) {
697+
params.put(Constants.Params.DEVICE_FCM_PUSH_TOKEN, fcmRegistrationId);
698+
}
699+
if (!TextUtils.isEmpty(miPushRegistrationId)) {
700+
params.put(Constants.Params.DEVICE_MIPUSH_TOKEN, miPushRegistrationId);
701+
}
702+
if (!TextUtils.isEmpty(hmsRegistrationId)) {
703+
params.put(Constants.Params.DEVICE_HMS_TOKEN, hmsRegistrationId);
704+
}
705+
}
706+
643707
private static void startHelper(
644708
String userId, final Map<String, ?> attributes, final boolean isBackground) {
645709
LeanplumEventDataManager.sharedInstance();
@@ -671,15 +735,6 @@ private static void startHelper(
671735
}
672736
APIConfig.getInstance().setUserId(userId);
673737

674-
// Setup parameters.
675-
String versionName = Util.getVersionName();
676-
if (customAppVersion != null) {
677-
versionName = customAppVersion;
678-
}
679-
if (versionName == null) {
680-
versionName = "";
681-
}
682-
683738
String locale = Util.getLocale();
684739
if (!TextUtils.isEmpty(customLocale)) {
685740
locale = customLocale;
@@ -689,32 +744,14 @@ private static void startHelper(
689744
Date now = new Date();
690745
int timezoneOffsetSeconds = localTimeZone.getOffset(now.getTime()) / 1000;
691746

692-
String fcmRegistrationId = SharedPreferencesUtil.getString(context,
693-
Constants.Defaults.LEANPLUM_PUSH, Constants.Defaults.PROPERTY_FCM_TOKEN_ID);
694-
String miPushRegistrationId = SharedPreferencesUtil.getString(context,
695-
Constants.Defaults.LEANPLUM_PUSH, Constants.Defaults.PROPERTY_MIPUSH_TOKEN_ID);
696-
String hmsRegistrationId = SharedPreferencesUtil.getString(context,
697-
Constants.Defaults.LEANPLUM_PUSH, Constants.Defaults.PROPERTY_HMS_TOKEN_ID);
698-
699747
HashMap<String, Object> params = new HashMap<>();
748+
749+
attachDeviceParams(params);
750+
700751
params.put(Constants.Params.INCLUDE_DEFAULTS, Boolean.toString(false));
701752
if (isBackground) {
702753
params.put(Constants.Params.BACKGROUND, Boolean.toString(true));
703754
}
704-
params.put(Constants.Params.VERSION_NAME, versionName);
705-
params.put(Constants.Params.DEVICE_NAME, Util.getDeviceName());
706-
params.put(Constants.Params.DEVICE_MODEL, Util.getDeviceModel());
707-
params.put(Constants.Params.DEVICE_SYSTEM_NAME, Util.getSystemName());
708-
params.put(Constants.Params.DEVICE_SYSTEM_VERSION, Util.getSystemVersion());
709-
if (!TextUtils.isEmpty(fcmRegistrationId)) {
710-
params.put(Constants.Params.DEVICE_FCM_PUSH_TOKEN, fcmRegistrationId);
711-
}
712-
if (!TextUtils.isEmpty(miPushRegistrationId)) {
713-
params.put(Constants.Params.DEVICE_MIPUSH_TOKEN, miPushRegistrationId);
714-
}
715-
if (!TextUtils.isEmpty(hmsRegistrationId)) {
716-
params.put(Constants.Params.DEVICE_HMS_TOKEN, hmsRegistrationId);
717-
}
718755
params.put(Constants.Keys.TIMEZONE, localTimeZone.getID());
719756
params.put(Constants.Keys.TIMEZONE_OFFSET_SECONDS, Integer.toString(timezoneOffsetSeconds));
720757
params.put(Constants.Keys.LOCALE, locale);

AndroidSDKCore/src/main/java/com/leanplum/internal/APIConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private void load() {
129129
}
130130
}
131131

132-
private void save() {
132+
public void save() {
133133
Context context = Leanplum.getContext();
134134
SharedPreferences defaults = context.getSharedPreferences(
135135
Constants.Defaults.LEANPLUM, Context.MODE_PRIVATE);

AndroidSDKTests/src/test/java/com/leanplum/LeanplumTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.leanplum.__setup.AbstractTest;
3030
import com.leanplum.__setup.TestClassUtil;
3131
import com.leanplum._whitebox.utilities.RequestHelper;
32+
import com.leanplum._whitebox.utilities.RequestHelper.RequestHandler;
3233
import com.leanplum._whitebox.utilities.ResponseHelper;
3334
import com.leanplum._whitebox.utilities.VariablesTestClass;
3435
import com.leanplum.annotations.Parser;
@@ -84,9 +85,11 @@
8485
import static org.junit.Assert.assertArrayEquals;
8586
import static org.junit.Assert.assertEquals;
8687
import static org.junit.Assert.assertFalse;
88+
import static org.junit.Assert.assertNotEquals;
8789
import static org.junit.Assert.assertNotNull;
8890
import static org.junit.Assert.assertNull;
8991
import static org.junit.Assert.assertTrue;
92+
import static org.junit.Assert.fail;
9093
import static org.mockito.Matchers.any;
9194
import static org.mockito.Matchers.anyBoolean;
9295
import static org.mockito.Matchers.anyDouble;
@@ -1325,6 +1328,65 @@ public void testUserId() {
13251328
assertEquals("test_id", APIConfig.getInstance().userId());
13261329
}
13271330

1331+
/**
1332+
* Tests setting the device ID before Leanplum start.
1333+
*/
1334+
@Test
1335+
public void testDeviceIdBeforeStart() {
1336+
RequestHelper.addRequestHandler((httpMethod, apiMethod, params) -> {
1337+
assertNotEquals(
1338+
"Not allowed to set the device ID before start",
1339+
"setDeviceAttributes",
1340+
apiMethod);
1341+
});
1342+
1343+
String newDeviceId = "device123";
1344+
Leanplum.forceNewDeviceId(newDeviceId);
1345+
1346+
setupSDK(mContext, "/responses/simple_start_response.json");
1347+
1348+
assertNotEquals(newDeviceId, APIConfig.getInstance().deviceId());
1349+
}
1350+
1351+
/**
1352+
* Tests that setting the same device ID is not initiating a network request.
1353+
*/
1354+
@Test
1355+
public void testSameDeviceId() {
1356+
setupSDK(mContext, "/responses/simple_start_response.json");
1357+
1358+
RequestHelper.addRequestHandler((httpMethod, apiMethod, params) -> {
1359+
fail("Setting the same device ID should not initiate a network request!");
1360+
});
1361+
1362+
String deviceId = APIConfig.getInstance().deviceId();
1363+
Leanplum.forceNewDeviceId(deviceId);
1364+
1365+
assertEquals(deviceId, APIConfig.getInstance().deviceId());
1366+
}
1367+
1368+
/**
1369+
* Tests setting the device ID after Leanplum has started.
1370+
*/
1371+
@Test
1372+
public void testDeviceIdAfterStart() {
1373+
setupSDK(mContext, "/responses/simple_start_response.json");
1374+
1375+
RequestHelper.addRequestHandler((httpMethod, apiMethod, params) -> {
1376+
assertEquals("setDeviceAttributes", apiMethod);
1377+
assertNotNull(params.get("versionName"));
1378+
assertNotNull(params.get("deviceName"));
1379+
assertNotNull(params.get("deviceModel"));
1380+
assertNotNull(params.get("systemName"));
1381+
assertNotNull(params.get("systemVersion"));
1382+
});
1383+
1384+
String deviceId = "device123";
1385+
Leanplum.forceNewDeviceId(deviceId);
1386+
1387+
assertEquals(deviceId, APIConfig.getInstance().deviceId());
1388+
}
1389+
13281390
/**
13291391
* Test traffic source params
13301392
*/

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ allprojects {
2020
}
2121
jcenter()
2222
maven {
23-
url 'http://developer.huawei.com/repo/'
23+
url 'https://developer.huawei.com/repo/'
2424
}
2525
}
2626
}

0 commit comments

Comments
 (0)