@@ -72,39 +72,39 @@ public void initFinished(final InternalConfig config) {
7272 L .i ("[ModuleDeviceIdCore] initFinished, Starting device ID acquisition" );
7373 if (config .getDeviceId () == null ) {
7474 // either fresh install, or migration from legacy SDK
75-
7675 L .i ("[ModuleDeviceIdCore] initFinished, Acquiring device id" );
7776
77+ Config .DID did ;
7878 if (Utils .isNotEmpty (config .getCustomDeviceId ())) {
7979 // developer specified id on SDK init
80- Config . DID did = new Config .DID (Config .DID .STRATEGY_CUSTOM , config .getCustomDeviceId ());
80+ did = new Config .DID (Config .DID .STRATEGY_CUSTOM , config .getCustomDeviceId ());
8181 L .d ("[ModuleDeviceIdCore] initFinished, Got developer id [" + did + "]" );
82- SDKCore .instance .onDeviceId (config , did , null );
8382 } else {
8483 // regular flow - acquire id using specified strategy
85- Config .DID did = new Config .DID (config .getDeviceIdStrategy (), null );
86- acquireId (config , did );
84+ did = acquireId (config );
8785 }
86+
87+ config .setDeviceId (did );
88+ config .storageProvider .setDeviceID (did .id );
89+ config .storageProvider .setDeviceIdType (DeviceIdType .fromInt (did .strategy , L ).name ());
8890 } else {
89- // second or next app launch, notify id is available
9091 Config .DID loadedDid = config .getDeviceId ();
9192 L .d ("[ModuleDeviceIdCore] initFinished, Loading previously saved device id:[" + loadedDid .id + "] strategy:[" + loadedDid .strategy + "]" );
92- SDKCore .instance .onDeviceId (config , loadedDid , loadedDid );
9393 }
9494 }
9595
9696 @ Override
97- public void deviceIdChanged (Config . DID oldDeviceId , boolean withMerge ) {
97+ public void deviceIdChanged (String oldDeviceId , boolean withMerge ) {
9898 L .d ("[ModuleDeviceIdCore] deviceIdChanged, oldDeviceId [" + oldDeviceId + "] withMerge [" + withMerge + "]" );
9999 Config .DID deviceId = internalConfig .getDeviceId ();
100100 SessionImpl session = SDKCore .instance .getSession ();
101101
102- if (deviceId != null && oldDeviceId != null && !deviceId .equals (oldDeviceId )) {
102+ if (deviceId != null && oldDeviceId != null && !deviceId .id . equals (oldDeviceId )) {
103103 // device id changed
104104 if (session != null && session .isActive ()) {
105105 // end previous session
106- L .d ("[ModuleDeviceIdCore] Ending session because device id was changed from [" + oldDeviceId . id + "]" );
107- session .end (null , null , oldDeviceId . id );
106+ L .d ("[ModuleDeviceIdCore] Ending session because device id was changed from [" + oldDeviceId + "]" );
107+ session .end (null , null , oldDeviceId );
108108 }
109109
110110 // add device id change request
@@ -115,56 +115,10 @@ public void deviceIdChanged(Config.DID oldDeviceId, boolean withMerge) {
115115 request .params .add (Params .PARAM_DEVICE_ID , deviceId .id );
116116 }
117117 //add the old device ID every time
118- request .params .add (Params .PARAM_OLD_DEVICE_ID , oldDeviceId . id );
118+ request .params .add (Params .PARAM_OLD_DEVICE_ID , oldDeviceId );
119119
120120 ModuleRequests .pushAsync (internalConfig , request );
121-
122- sendDIDSignal (internalConfig , deviceId , oldDeviceId );
123- }
124- }
125-
126- @ Override
127- public void onDeviceId (InternalConfig config , final Config .DID deviceId , final Config .DID oldDeviceId ) {
128- L .d ("[ModuleDeviceIdCore] onDeviceId [" + deviceId + "]" );
129-
130- SessionImpl session = SDKCore .instance .getSession ();
131-
132- if (deviceId == null && oldDeviceId != null ) {
133- // device id is unset
134- if (session != null ) {
135- L .d ("[ModuleDeviceIdCore] Ending session because device id was unset from [" + oldDeviceId .id + "]" );
136- session .end (null , null , oldDeviceId .id );
137- }
138-
139- sendDIDSignal (config , null , oldDeviceId );
140- } else if (deviceId != null && oldDeviceId == null ) {
141- // device id just acquired
142- if (this .tasks == null ) {
143- this .tasks = new Tasks ("deviceId" , L );
144- }
145- tasks .run (new Tasks .Task <Object >(0L ) {
146- @ Override
147- public Object call () throws Exception {
148- // put device_id parameter into existing requests
149- L .i ("[ModuleDeviceIdCore] Adding device_id to previous requests" );
150- boolean success = transformRequests (config , deviceId .id );
151- if (success ) {
152- L .i ("[ModuleDeviceIdCore] First transform: success" );
153- } else {
154- L .w ("[ModuleDeviceIdCore] First transform: failure" );
155- }
156-
157- // do it second time in case new requests were added during first attempt
158- success = transformRequests (config , deviceId .id );
159- if (!success ) {
160- L .e ("[ModuleDeviceIdCore] Failed to put device_id into existing requests, following behaviour for unhandled requests is undefined." );
161- } else {
162- L .i ("[ModuleDeviceIdCore] Second transform: success" );
163- }
164- sendDIDSignal (config , deviceId , null );
165- return null ;
166- }
167- });
121+ SDKCore .instance .onSignal (internalConfig , SDKCore .Signal .DID .getIndex ());
168122 }
169123 }
170124
@@ -186,55 +140,25 @@ private boolean transformRequests(final InternalConfig config, final String devi
186140 });
187141 }
188142
189- /**
190- * Just a wrapper around {@link SDKCore#onSignal(InternalConfig, int)}} for {@link SDKCore.Signal#DID} case
191- *
192- * @param config InternalConfig to run in
193- * @param id new {@link Config.DID} if any
194- * @param old old {@link Config.DID} if any
195- */
196- private void sendDIDSignal (InternalConfig config , Config .DID id , Config .DID old ) {
197- L .d ("[ModuleDeviceIdCore] Sending device id signal: [" + id + "], was [" + old + "]" );
198- SDKCore .instance .onSignal (config , SDKCore .Signal .DID .getIndex ());
199- }
200-
201143 /**
202144 * Logging into app-specific account:
203145 * - reset device id and notify modules;
204146 * - send corresponding request to server.
205147 *
206- * @param config InternalConfig to run in
207148 * @param id device id to change to
208149 */
209- public void login (InternalConfig config , String id ) {
210- if (Utils .isEmptyOrNull (id )) {
211- L .e ("[ModuleDeviceIdCore] Empty id passed to login method" );
212- } else {
213- final Config .DID old = config .getDeviceId ();
214- config .setDeviceId (new Config .DID (Config .DID .STRATEGY_CUSTOM , id ));
215- config .storageProvider .setDeviceIdType (DeviceIdType .DEVELOPER_SUPPLIED .name ());
216- config .storageProvider .setDeviceID (id );
217-
218- // old session end & new session begin requests are supposed to happen here
219- SDKCore .instance .onDeviceId (config , config .getDeviceId (), old );
220- }
150+ public void login (String id ) {
151+ changeDeviceIdInternal (id , DeviceIdType .DEVELOPER_SUPPLIED , true );
221152 }
222153
223154 /**
224155 * Logging out from app-specific account and reverting back to previously used id if any:
225156 * - nullify device id and notify modules;
226157 * - send corresponding request to server.
227- *
228- * @param config context to run in
229158 */
230- public void logout (final InternalConfig config ) {
231- final Config .DID old = config .getDeviceId ();
232- config .removeDeviceId (old );
233- config .storageProvider .setDeviceID ("" );
234- config .storageProvider .setDeviceIdType ("" );
235-
236- SDKCore .instance .onDeviceId (config , null , old );
237- acquireId (config , new Config .DID (config .getDeviceIdStrategy (), null ));
159+ public void logout () {
160+ Config .DID did = acquireId (internalConfig );
161+ changeDeviceIdInternal (did .id , DeviceIdType .fromInt (did .strategy , L ), false );
238162 }
239163
240164 /**
@@ -244,39 +168,47 @@ public void logout(final InternalConfig config) {
244168 * <li>Begin new session with new id if previously ended a session</li>
245169 * </ul>
246170 *
247- * @param config context to run in
248171 * @param id new user id
249172 */
250- protected void changeDeviceIdInternal (InternalConfig config , String id , boolean withMerge ) {
173+ protected void changeDeviceIdInternal (String id , DeviceIdType type , boolean withMerge ) {
251174 if (Utils .isEmptyOrNull (id )) {
252175 L .w ("[ModuleDeviceIdCore] changeDeviceId, Empty id passed to changeDeviceId method" );
253176 return ;
254177 }
255-
256- final Config .DID old = config .getDeviceId ();
178+ final Config .DID old = internalConfig .getDeviceId ();
257179 if (old .id .equals (id )) {
258180 L .w ("[ModuleDeviceIdCore] changeDeviceId, Same id passed to changeDeviceId method, ignoring" );
259181 return ;
260182 }
261183
262- internalConfig . setDeviceId ( new Config .DID (Config . DID . STRATEGY_CUSTOM , id ) );
263- internalConfig .storageProvider .setDeviceIdType (DeviceIdType . DEVELOPER_SUPPLIED .name ());
184+ Config . DID did = new Config .DID (type . index , id );
185+ internalConfig .storageProvider .setDeviceIdType (type .name ());
264186 internalConfig .storageProvider .setDeviceID (id );
265- SDKCore .instance .notifyModulesDeviceIdChanged (old , withMerge );
187+ internalConfig .setDeviceId (did );
188+
189+ if (!withMerge ) {
190+ SessionImpl session = SDKCore .instance .getSession ();
191+ if (session != null ) {
192+ L .d ("[ModuleDeviceIdCore] changeDeviceIdInternal, Ending session because device id was unset from [" + old .id + "]" );
193+ session .end (null , null , old .id );
194+ }
195+ changeOldRequestsDeviceIds (did );
196+ }
197+
198+ SDKCore .instance .notifyModulesDeviceIdChanged (old .id , withMerge );
266199 }
267200
268201 /**
269202 * Gets id of the strategy supplied. In case strategy is not available, returns a fallback strategy.
270203 * In case strategy is available but id cannot be acquired right now, returns null.
271204 *
272205 * @param config InternalConfig to run in
273- * @param holder DID object which holds strategy and possibly other info for id generation
274206 */
275- protected void acquireId (final InternalConfig config , final Config . DID holder ) {
276- L .i ("[ModuleDeviceIdCore] acquireId, Acquiring device id of strategy [" + holder . strategy + "]" );
207+ protected Config . DID acquireId (final InternalConfig config ) {
208+ L .i ("[ModuleDeviceIdCore] acquireId, Acquiring device id of strategy [" + config . getDeviceIdStrategy () + "]" );
277209 Config .DID did = null ;
278210
279- int index = holder . strategy ;
211+ int index = config . getDeviceIdStrategy () ;
280212
281213 while (index >= 0 ) {
282214 DeviceIdGenerator generator = generators .get (index );
@@ -300,10 +232,39 @@ protected void acquireId(final InternalConfig config, final Config.DID holder) {
300232 L .i ("[ModuleDeviceIdCore] acquireId, no custom device id. SDK has generated a random device id." );
301233 }
302234 L .d ("[ModuleDeviceIdCore] acquireId, Got device id: " + did );
303- SDKCore .instance .onDeviceId (config , did , null );
304235 } else {
305236 L .i ("[ModuleDeviceIdCore] acquireId, No device id of strategy [" + config .getDeviceIdStrategy () + "] is available yet" );
306237 }
238+
239+ return did ;
240+ }
241+
242+ private void changeOldRequestsDeviceIds (Config .DID deviceId ) {
243+ if (this .tasks == null ) {
244+ this .tasks = new Tasks ("deviceId" , L );
245+ }
246+ tasks .run (new Tasks .Task <Object >(0L ) {
247+ @ Override
248+ public Object call () {
249+ // put device_id parameter into existing requests
250+ L .i ("[ModuleDeviceIdCore] changeOldRequestsDeviceIds, Adding device_id to previous requests" );
251+ boolean success = transformRequests (internalConfig , deviceId .id );
252+ if (success ) {
253+ L .i ("[ModuleDeviceIdCore] changeOldRequestsDeviceIds, First transform: success" );
254+ } else {
255+ L .w ("[ModuleDeviceIdCore] changeOldRequestsDeviceIds, First transform: failure" );
256+ }
257+
258+ // do it second time in case new requests were added during first attempt
259+ success = transformRequests (internalConfig , deviceId .id );
260+ if (!success ) {
261+ L .e ("[ModuleDeviceIdCore] changeOldRequestsDeviceIds, Failed to put device_id into existing requests, following behaviour for unhandled requests is undefined." );
262+ } else {
263+ L .i ("[ModuleDeviceIdCore] changeOldRequestsDeviceIds, Second transform: success" );
264+ }
265+ return null ;
266+ }
267+ });
307268 }
308269
309270 @ Override
@@ -316,8 +277,7 @@ public void stop(InternalConfig config, boolean clear) {
316277 }
317278
318279 protected DeviceIdType getTypeInternal () {
319- //todo impl after merge, delete the Countly one
320- return null ;
280+ return DeviceIdType .fromInt (internalConfig .getDeviceId ().strategy , L );
321281 }
322282
323283 protected String getIDInternal () {
@@ -355,7 +315,7 @@ public DeviceIdType getType() {
355315 */
356316 public void changeWithMerge (String id ) {
357317 synchronized (Countly .instance ()) {
358- changeDeviceIdInternal (internalConfig , id , true );
318+ changeDeviceIdInternal (id , DeviceIdType . DEVELOPER_SUPPLIED , true );
359319 }
360320 }
361321
@@ -366,7 +326,7 @@ public void changeWithMerge(String id) {
366326 */
367327 public void changeWithoutMerge (String id ) {
368328 synchronized (Countly .instance ()) {
369- changeDeviceIdInternal (internalConfig , id , false );
329+ changeDeviceIdInternal (id , DeviceIdType . DEVELOPER_SUPPLIED , false );
370330 }
371331 }
372332 }
0 commit comments