Skip to content

Commit 8ba9e4e

Browse files
WIP: vuid enabled add to optimizely manager
1 parent eebed3c commit 8ba9e4e

File tree

4 files changed

+78
-42
lines changed

4 files changed

+78
-42
lines changed

android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ public class OptimizelyManager {
9494

9595
@NonNull private UserProfileService userProfileService;
9696
@Nullable private ODPManager odpManager;
97-
@Nullable private final String vuid;
98-
97+
private VuidManager vuidManager;
9998
@Nullable private OptimizelyStartListener optimizelyStartListener;
10099
private boolean returnInMainThreadFromAsyncInit = true;
101100

@@ -117,7 +116,7 @@ public class OptimizelyManager {
117116
@NonNull NotificationCenter notificationCenter,
118117
@Nullable List<OptimizelyDecideOption> defaultDecideOptions,
119118
@Nullable ODPManager odpManager,
120-
@Nullable String vuid,
119+
VuidManager vuidManger,
121120
@Nullable String clientEngineName,
122121
@Nullable String clientVersion) {
123122

@@ -140,7 +139,7 @@ public class OptimizelyManager {
140139
this.eventProcessor = eventProcessor;
141140
this.errorHandler = errorHandler;
142141
this.userProfileService = userProfileService;
143-
this.vuid = vuid;
142+
this.vuidManager = vuidManger;
144143
this.odpManager = odpManager;
145144
this.notificationCenter = notificationCenter;
146145
this.defaultDecideOptions = defaultDecideOptions;
@@ -652,8 +651,7 @@ private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull Stri
652651
builder.withDefaultDecideOptions(defaultDecideOptions);
653652
builder.withODPManager(odpManager);
654653
Optimizely optimizely = builder.build();
655-
656-
return new OptimizelyClient(optimizely, LoggerFactory.getLogger(OptimizelyClient.class), vuid);
654+
return new OptimizelyClient(optimizely, LoggerFactory.getLogger(OptimizelyClient.class), vuidManager.getVuid());
657655
}
658656

659657
@NonNull
@@ -792,8 +790,7 @@ public static class Builder {
792790
private int timeoutForODPSegmentFetchInSecs = 10;
793791
private int timeoutForODPEventDispatchInSecs = 10;
794792
private boolean odpEnabled = true;
795-
private String vuid = null;
796-
793+
private boolean vuidEnabled = false;
797794
private String customSdkName = null;
798795
private String customSdkVersion = null;
799796

@@ -823,6 +820,10 @@ public Builder withSDKKey(String sdkKey) {
823820
this.sdkKey = sdkKey;
824821
return this;
825822
}
823+
public Builder withVuidEnabled() {
824+
this.vuidEnabled = true;
825+
return this;
826+
}
826827

827828
/**
828829
* Override the default {@link Logger}.
@@ -1036,10 +1037,10 @@ public Builder withODPDisabled() {
10361037
* @param vuid a user-defined vuid value
10371038
* @return this {@link Builder} instance
10381039
*/
1039-
public Builder withVuid(String vuid) {
1040-
this.vuid = vuid;
1041-
return this;
1042-
}
1040+
// public Builder withVuid(String vuid) {
1041+
// this.vuid = vuid;
1042+
// return this;
1043+
// }
10431044

10441045
/**
10451046
* Override the SDK name and version (for client SDKs like flutter-sdk wrapping the core android-sdk) to be included in events.
@@ -1120,17 +1121,15 @@ public OptimizelyManager build(Context context) {
11201121

11211122
}
11221123

1123-
if (vuid == null) {
1124-
vuid = VuidManager.Companion.getShared(context).getVuid();
1125-
}
1124+
VuidManager vuidManager = new VuidManager(context, vuidEnabled);
11261125

11271126
ODPManager odpManager = null;
11281127
if (odpEnabled) {
11291128
// Pass common data for android-sdk only to java-core sdk. All ODP events will include these data.
11301129
Map<String, Object> commonData = OptimizelyDefaultAttributes.buildODPCommonData(context, logger);
11311130

11321131
// Pass common identifiers for android-sdk only to java-core sdk. All ODP events will include these identifiers.
1133-
Map<String, String> commonIdentifiers = (vuid != null) ? Collections.singletonMap("vuid", vuid) : Collections.emptyMap();
1132+
Map<String, String> commonIdentifiers = (vuidEnabled) ? Collections.singletonMap("vuid", vuidManager.getVuid()) : Collections.emptyMap();
11341133

11351134
ODPApiManager odpApiManager = new DefaultODPApiManager(
11361135
context,
@@ -1165,7 +1164,7 @@ public OptimizelyManager build(Context context) {
11651164
notificationCenter,
11661165
defaultDecideOptions,
11671166
odpManager,
1168-
vuid,
1167+
vuidManager,
11691168
customSdkName,
11701169
customSdkVersion
11711170
);

odp/src/androidTest/java/com/optimizely/ab/android/odp/VuidManagerTest.kt

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,35 @@ class VuidManagerTest {
9090

9191
@Test
9292
fun autoLoaded() {
93-
val vuid1 = VuidManager.getShared(context).vuid
93+
val vuidManager1 = VuidManager(context, true)
94+
val vuid1 = vuidManager1.vuid
9495
assertTrue("vuid should be auto loaded when constructed", vuid1.startsWith("vuid_"))
96+
val savedVuid = vuidManager1.load(context)
97+
assertEquals("Vuid should be same", vuid1, savedVuid)
9598

96-
val vuid2 = VuidManager.getShared(context).vuid
97-
assertEquals("the same vuid should be returned when getting a singleton", vuid1, vuid2)
99+
val vuidManager2 = VuidManager(context, false)
100+
val vuid2 = vuidManager2.vuid
101+
assertTrue(vuid2 == "")
102+
assertFalse("vuid should be auto loaded when constructed", vuid2.startsWith("vuid_"))
98103

99-
// remove shared instance, so will be re-instantiated
100-
VuidManager.removeSharedForTesting()
101-
102-
val vuid3 = VuidManager.getShared(context).vuid
103-
assertEquals("the saved vuid should be returned when instantiated again", vuid2, vuid3)
104104

105-
// remove saved vuid
106-
cleanSharedPrefs()
107-
// remove shared instance, so will be re-instantiated
108-
VuidManager.removeSharedForTesting()
109105

110-
val vuid4 = VuidManager.getShared(context).vuid
111-
assertNotEquals("a new vuid should be returned when storage cleared and re-instantiated", vuid3, vuid4)
112-
assertTrue(vuid4.startsWith("vuid_"))
106+
// val vuid2 = VuidManager.getShared(context).vuid
107+
// assertEquals("the same vuid should be returned when getting a singleton", vuid1, vuid2)
108+
//
109+
// // remove shared instance, so will be re-instantiated
110+
// VuidManager.removeSharedForTesting()
111+
//
112+
// val vuid3 = VuidManager.getShared(context).vuid
113+
// assertEquals("the saved vuid should be returned when instantiated again", vuid2, vuid3)
114+
//
115+
// // remove saved vuid
116+
// cleanSharedPrefs()
117+
// // remove shared instance, so will be re-instantiated
118+
// VuidManager.removeSharedForTesting()
119+
//
120+
// val vuid4 = VuidManager.getShared(context).vuid
121+
// assertNotEquals("a new vuid should be returned when storage cleared and re-instantiated", vuid3, vuid4)
122+
// assertTrue(vuid4.startsWith("vuid_"))
113123
}
114124
}

odp/src/main/java/com/optimizely/ab/android/odp/VuidManager.kt

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,42 @@ import androidx.annotation.VisibleForTesting
1919
import com.optimizely.ab.android.shared.OptlyStorage
2020
import java.util.UUID
2121

22-
class VuidManager private constructor(context: Context) {
23-
var vuid = ""
22+
class VuidManager constructor(context: Context, isEnabled: Boolean = false) {
23+
private var _vuid = ""
2424
private val keyForVuid = "vuid" // stored in the private "optly" storage
25+
private var isEnabled: Boolean = isEnabled
2526

2627
init {
27-
this.vuid = load(context)
28+
if (isEnabled) {
29+
this._vuid = load(context)
30+
} else {
31+
this.remove(context)
32+
}
33+
34+
}
35+
36+
fun getVuid(): String? {
37+
return if (isEnabled) this._vuid else null
38+
}
39+
fun isEnabled(): Boolean {
40+
return isEnabled
2841
}
2942

3043
companion object {
31-
@Volatile
32-
private var INSTANCE: VuidManager? = null
44+
// @Volatile
45+
// private var INSTANCE: VuidManager? = null
3346

3447
@Synchronized
35-
fun getShared(context: Context): VuidManager = INSTANCE ?: VuidManager(context).also { INSTANCE = it }
48+
// fun getShared(context: Context): VuidManager = INSTANCE ?: VuidManager(context).also { INSTANCE = it }
3649

3750
fun isVuid(visitorId: String): Boolean {
3851
return visitorId.startsWith("vuid_", ignoreCase = true)
3952
}
4053

41-
@VisibleForTesting
42-
fun removeSharedForTesting() {
43-
INSTANCE = null
44-
}
54+
// @VisibleForTesting
55+
// fun removeSharedForTesting() {
56+
// INSTANCE = null
57+
// }
4558
}
4659

4760
@VisibleForTesting
@@ -69,4 +82,10 @@ class VuidManager private constructor(context: Context) {
6982
val storage = OptlyStorage(context)
7083
storage.saveString(keyForVuid, vuid)
7184
}
85+
86+
@VisibleForTesting
87+
fun remove(context: Context) {
88+
val storage = OptlyStorage(context)
89+
storage.removeString(keyForVuid)
90+
}
7291
}

shared/src/main/java/com/optimizely/ab/android/shared/OptlyStorage.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public long getLong(String key, long defaultValue) {
7272
public void saveString(String key, String value) {
7373
getWritablePrefs().putString(key, value).apply();
7474
}
75+
/**
76+
* Remove a string value
77+
* @param key a String key
78+
* @hide
79+
*/
80+
public void removeString(String key) {
81+
getWritablePrefs().remove(key).apply();
82+
}
7583

7684
/**
7785
* Get a string value

0 commit comments

Comments
 (0)