3636import com .leanplum .callbacks .StartCallback ;
3737import com .leanplum .callbacks .VariablesChangedCallback ;
3838import com .leanplum .internal .APIConfig ;
39- import com .leanplum .internal .ActionManager ;
4039import com .leanplum .internal .ApiConfigLoader ;
4140import com .leanplum .internal .Constants ;
4241import com .leanplum .internal .CountAggregator ;
5655import com .leanplum .internal .RequestSender ;
5756import com .leanplum .internal .RequestSenderTimer ;
5857import com .leanplum .internal .RequestUtil ;
59- import com .leanplum .internal .Socket ;
6058import com .leanplum .internal .Util ;
6159import com .leanplum .internal .Util .DeviceIdInfo ;
6260import 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 );
0 commit comments