Skip to content

Commit d88d037

Browse files
refactor: remove realm concept (#158)
1 parent 4fae7ab commit d88d037

File tree

6 files changed

+30
-42
lines changed

6 files changed

+30
-42
lines changed

sdk-java/src/main/java/ly/count/sdk/java/Config.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,10 @@ public static Config.Feature byIndex(int index) {
124124
public static final class DID {
125125
public static final int STRATEGY_UUID = 0;
126126
public static final int STRATEGY_CUSTOM = 10;
127-
public static final int REALM_DID = 0;
128-
129-
public int realm;
130127
public int strategy;
131128
public String id;
132129

133-
public DID(int realm, int strategy, String id) {
134-
this.realm = realm;
130+
public DID(int strategy, String id) {
135131
this.strategy = strategy;
136132
this.id = id;
137133
}
@@ -142,8 +138,7 @@ public boolean equals(Object obj) {
142138
return false;
143139
}
144140
DID did = (DID) obj;
145-
return did.realm == realm && did.strategy == strategy &&
146-
(Objects.equals(did.id, id));
141+
return did.strategy == strategy && (Objects.equals(did.id, id));
147142
}
148143

149144
@Override
@@ -153,7 +148,7 @@ public int hashCode() {
153148

154149
@Override
155150
public String toString() {
156-
return "DID " + id + " (" + realm + ", " + strategy + ")";
151+
return "DID " + id + " (" + strategy + ")";
157152
}
158153
}
159154

sdk-java/src/main/java/ly/count/sdk/java/internal/InternalConfig.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,11 @@ public static Long storageId() {
9090
}
9191

9292
public DID getDeviceId() {
93-
return getDeviceId(DID.REALM_DID);
94-
}
95-
96-
public DID getDeviceId(int realm) {
97-
for (DID DID : dids) {
98-
if (DID.realm == realm) {
99-
return DID;
100-
}
93+
if (dids.isEmpty()) {
94+
return null;
95+
} else {
96+
return dids.get(0);
10197
}
102-
return null;
10398
}
10499

105100
public DID setDeviceId(DID id) {
@@ -108,12 +103,7 @@ public DID setDeviceId(DID id) {
108103
configLog.e("DID cannot be null");
109104
}
110105
}
111-
DID old = null;
112-
for (DID did : dids) {
113-
if (did.realm == id.realm) {
114-
old = did;
115-
}
116-
}
106+
DID old = getDeviceId();
117107
if (old != null) {
118108
dids.remove(old);
119109
}

sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleDeviceIdCore.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,18 @@ public void initFinished(final InternalConfig config) {
7777

7878
if (Utils.isNotEmpty(config.getCustomDeviceId())) {
7979
// developer specified id on SDK init
80-
Config.DID did = new Config.DID(Config.DID.REALM_DID, Config.DID.STRATEGY_CUSTOM, config.getCustomDeviceId());
80+
Config.DID did = new Config.DID(Config.DID.STRATEGY_CUSTOM, config.getCustomDeviceId());
8181
L.d("[ModuleDeviceIdCore] initFinished, Got developer id [" + did + "]");
8282
SDKCore.instance.onDeviceId(config, did, null);
8383
} else {
8484
// regular flow - acquire id using specified strategy
85-
Config.DID did = new Config.DID(Config.DID.REALM_DID, config.getDeviceIdStrategy(), null);
85+
Config.DID did = new Config.DID(config.getDeviceIdStrategy(), null);
8686
acquireId(config, did);
8787
}
8888
} else {
8989
// second or next app launch, notify id is available
9090
Config.DID loadedDid = config.getDeviceId();
91-
L.d("[ModuleDeviceIdCore] initFinished, Loading previously saved device id:[" + loadedDid.id + "] realm:[" + loadedDid.realm + "] strategy:[" + loadedDid.strategy + "]");
91+
L.d("[ModuleDeviceIdCore] initFinished, Loading previously saved device id:[" + loadedDid.id + "] strategy:[" + loadedDid.strategy + "]");
9292
SDKCore.instance.onDeviceId(config, loadedDid, loadedDid);
9393
}
9494
}
@@ -99,7 +99,7 @@ public void deviceIdChanged(Config.DID oldDeviceId, boolean withMerge) {
9999
Config.DID deviceId = internalConfig.getDeviceId();
100100
SessionImpl session = SDKCore.instance.getSession();
101101

102-
if (deviceId != null && oldDeviceId != null && deviceId.realm == Config.DID.REALM_DID && !deviceId.equals(oldDeviceId)) {
102+
if (deviceId != null && oldDeviceId != null && !deviceId.equals(oldDeviceId)) {
103103
// device id changed
104104
if (session != null && session.isActive()) {
105105
// end previous session
@@ -129,15 +129,15 @@ public void onDeviceId(InternalConfig config, final Config.DID deviceId, final C
129129

130130
SessionImpl session = SDKCore.instance.getSession();
131131

132-
if (deviceId == null && oldDeviceId != null && oldDeviceId.realm == Config.DID.REALM_DID) {
132+
if (deviceId == null && oldDeviceId != null) {
133133
// device id is unset
134134
if (session != null) {
135135
L.d("[ModuleDeviceIdCore] Ending session because device id was unset from [" + oldDeviceId.id + "]");
136136
session.end(null, null, oldDeviceId.id);
137137
}
138138

139139
sendDIDSignal(config, null, oldDeviceId);
140-
} else if (deviceId != null && oldDeviceId == null && deviceId.realm == Config.DID.REALM_DID) {
140+
} else if (deviceId != null && oldDeviceId == null) {
141141
// device id just acquired
142142
if (this.tasks == null) {
143143
this.tasks = new Tasks("deviceId", L);
@@ -211,7 +211,7 @@ public void login(InternalConfig config, String id) {
211211
L.e("[ModuleDeviceIdCore] Empty id passed to login method");
212212
} else {
213213
final Config.DID old = config.getDeviceId();
214-
config.setDeviceId(new Config.DID(Config.DID.REALM_DID, Config.DID.STRATEGY_CUSTOM, id));
214+
config.setDeviceId(new Config.DID(Config.DID.STRATEGY_CUSTOM, id));
215215
config.storageProvider.setDeviceIdType(DeviceIdType.DEVELOPER_SUPPLIED.name());
216216
config.storageProvider.setDeviceID(id);
217217

@@ -234,7 +234,7 @@ public void logout(final InternalConfig config) {
234234
config.storageProvider.setDeviceIdType("");
235235

236236
SDKCore.instance.onDeviceId(config, null, old);
237-
acquireId(config, new Config.DID(Config.DID.REALM_DID, config.getDeviceIdStrategy(), null));
237+
acquireId(config, new Config.DID(config.getDeviceIdStrategy(), null));
238238
}
239239

240240
/**
@@ -254,8 +254,14 @@ protected void changeDeviceIdInternal(InternalConfig config, String id, boolean
254254
}
255255

256256
final Config.DID old = config.getDeviceId();
257-
config.setDeviceId(new Config.DID(Config.DID.REALM_DID, Config.DID.STRATEGY_CUSTOM, id));
258-
Storage.push(config, config);
257+
if (old.id.equals(id)) {
258+
L.w("[ModuleDeviceIdCore] changeDeviceId, Same id passed to changeDeviceId method, ignoring");
259+
return;
260+
}
261+
262+
internalConfig.setDeviceId(new Config.DID(Config.DID.STRATEGY_CUSTOM, id));
263+
internalConfig.storageProvider.setDeviceIdType(DeviceIdType.DEVELOPER_SUPPLIED.name());
264+
internalConfig.storageProvider.setDeviceID(id);
259265
SDKCore.instance.notifyModulesDeviceIdChanged(old, withMerge);
260266
}
261267

@@ -267,7 +273,7 @@ protected void changeDeviceIdInternal(InternalConfig config, String id, boolean
267273
* @param holder DID object which holds strategy and possibly other info for id generation
268274
*/
269275
protected void acquireId(final InternalConfig config, final Config.DID holder) {
270-
L.i("[ModuleDeviceIdCore] acquireId, Acquiring device id of strategy [" + holder.strategy + " / " + holder.realm + "]");
276+
L.i("[ModuleDeviceIdCore] acquireId, Acquiring device id of strategy [" + holder.strategy + "]");
271277
Config.DID did = null;
272278

273279
int index = holder.strategy;
@@ -280,7 +286,7 @@ protected void acquireId(final InternalConfig config, final Config.DID holder) {
280286
} else {
281287
String id = generator.generate(config);
282288
if (Utils.isNotEmpty(id)) {
283-
did = new Config.DID(holder.realm, index, id);
289+
did = new Config.DID(index, id);
284290
break;
285291
} else {
286292
L.w("[ModuleDeviceIdCore] Device id strategy [" + index + "] didn't return. Falling back to next one.");

sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleSessions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void stop(InternalConfig config, boolean clear) {
6464
*/
6565
public void onDeviceId(final InternalConfig config, final Config.DID deviceId, final Config.DID oldDeviceId) {
6666
L.d("[ModuleSessions] onDeviceId " + deviceId + ", old " + oldDeviceId);
67-
if (deviceId != null && oldDeviceId != null && deviceId.realm == Config.DID.REALM_DID && !deviceId.equals(oldDeviceId) && getSession() == null) {
67+
if (deviceId != null && oldDeviceId != null && !deviceId.equals(oldDeviceId) && getSession() == null) {
6868
session(config, null).begin();
6969
}
7070
}

sdk-java/src/main/java/ly/count/sdk/java/internal/SDKCore.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ protected void setDeviceIdFromStorageIfExist(InternalConfig config) {
420420
return;
421421
}
422422

423-
config.setDeviceId(new Config.DID(Config.DID.REALM_DID, DeviceIdType.toInt(deviceIdType), deviceId));
423+
config.setDeviceId(new Config.DID(DeviceIdType.toInt(deviceIdType), deviceId));
424424
}
425425

426426
public void init(final InternalConfig givenConfig) {
@@ -588,7 +588,7 @@ public void onCrash(final InternalConfig config, Throwable t, boolean fatal, Str
588588

589589
public void onDeviceId(InternalConfig config, Config.DID id, Config.DID old) {
590590
L.d("onDeviceId " + id + ", old " + old);
591-
if (id != null && (!id.equals(old) || !id.equals(config.getDeviceId(id.realm)))) {
591+
if (id != null && (!id.equals(old) || !id.equals(config.getDeviceId()))) {
592592
sdkStorage.setDeviceID(id.id);
593593
sdkStorage.setDeviceIdType(DeviceIdType.fromInt(id.strategy, L).name());
594594
config.setDeviceId(id);
@@ -603,7 +603,7 @@ public void onDeviceId(InternalConfig config, Config.DID id, Config.DID old) {
603603
module.onDeviceId(config, id, old);
604604
}
605605

606-
if (id != null && id.realm == Config.DID.REALM_DID) {
606+
if (id != null) {
607607
user.id = id.id;
608608
L.d("[SDKCore] 5");
609609
}

sdk-java/src/test/java/ly/count/sdk/java/internal/MigrationHelperTests.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,6 @@ public void applyMigrations_0to1_initCountlyDevSupplied() throws IOException {
248248
public void applyMigrations_0to1_initCountlyCorrupted() throws IOException {
249249
Files.write(TestUtils.createFile("config_0").toPath(), MOCK_OLD_CONFIG_FILE_corrupted); //mock a sdk config file, to simulate storage is not empty
250250
Countly.instance().init(TestUtils.getBaseConfig(null));
251-
ScenarioDeviceIdInitTests.waitForNoNullDeviceID(Countly.instance());//wait for id to generate
252251

253252
Assert.assertTrue(Countly.instance().getDeviceId().startsWith("CLY_"));
254253
Assert.assertEquals(DeviceIdType.SDK_GENERATED, Countly.instance().getDeviceIdType());
@@ -263,7 +262,6 @@ public void applyMigrations_0to1_initCountlyCorrupted() throws IOException {
263262
public void applyMigrations_0to1_initCountlyNoDeviceId() throws IOException {
264263
Files.write(TestUtils.createFile("config_0").toPath(), MOCK_OLD_CONFIG_FILE_noDeviceId); //mock a sdk config file, to simulate storage is not empty
265264
Countly.instance().init(TestUtils.getBaseConfig(null));
266-
ScenarioDeviceIdInitTests.waitForNoNullDeviceID(Countly.instance());//wait for id to generate
267265

268266
Assert.assertTrue(Countly.instance().getDeviceId().startsWith("CLY_"));
269267
Assert.assertEquals(DeviceIdType.SDK_GENERATED, Countly.instance().getDeviceIdType());
@@ -278,7 +276,6 @@ public void applyMigrations_0to1_initCountlyNoDeviceId() throws IOException {
278276
public void applyMigrations_0to1_initCountlyNoDeviceId_customDeviceId() throws IOException {
279277
Files.write(TestUtils.createFile("config_0").toPath(), MOCK_OLD_CONFIG_FILE_noDeviceId); //mock a sdk config file, to simulate storage is not empty
280278
Countly.instance().init(TestUtils.getBaseConfig());
281-
ScenarioDeviceIdInitTests.waitForNoNullDeviceID(Countly.instance());//wait for id to generate
282279

283280
Assert.assertEquals(TestUtils.DEVICE_ID, Countly.instance().getDeviceId());
284281
Assert.assertEquals(DeviceIdType.DEVELOPER_SUPPLIED, Countly.instance().getDeviceIdType());

0 commit comments

Comments
 (0)