Skip to content

Commit

Permalink
WIP: vuid enabled add to optimizely manager
Browse files Browse the repository at this point in the history
  • Loading branch information
muzahidul-opti committed Oct 19, 2024
1 parent eebed3c commit 8ba9e4e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public class OptimizelyManager {

@NonNull private UserProfileService userProfileService;
@Nullable private ODPManager odpManager;
@Nullable private final String vuid;

private VuidManager vuidManager;
@Nullable private OptimizelyStartListener optimizelyStartListener;
private boolean returnInMainThreadFromAsyncInit = true;

Expand All @@ -117,7 +116,7 @@ public class OptimizelyManager {
@NonNull NotificationCenter notificationCenter,
@Nullable List<OptimizelyDecideOption> defaultDecideOptions,
@Nullable ODPManager odpManager,
@Nullable String vuid,
VuidManager vuidManger,
@Nullable String clientEngineName,
@Nullable String clientVersion) {

Expand All @@ -140,7 +139,7 @@ public class OptimizelyManager {
this.eventProcessor = eventProcessor;
this.errorHandler = errorHandler;
this.userProfileService = userProfileService;
this.vuid = vuid;
this.vuidManager = vuidManger;
this.odpManager = odpManager;
this.notificationCenter = notificationCenter;
this.defaultDecideOptions = defaultDecideOptions;
Expand Down Expand Up @@ -652,8 +651,7 @@ private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull Stri
builder.withDefaultDecideOptions(defaultDecideOptions);
builder.withODPManager(odpManager);
Optimizely optimizely = builder.build();

return new OptimizelyClient(optimizely, LoggerFactory.getLogger(OptimizelyClient.class), vuid);
return new OptimizelyClient(optimizely, LoggerFactory.getLogger(OptimizelyClient.class), vuidManager.getVuid());
}

@NonNull
Expand Down Expand Up @@ -792,8 +790,7 @@ public static class Builder {
private int timeoutForODPSegmentFetchInSecs = 10;
private int timeoutForODPEventDispatchInSecs = 10;
private boolean odpEnabled = true;
private String vuid = null;

private boolean vuidEnabled = false;
private String customSdkName = null;
private String customSdkVersion = null;

Expand Down Expand Up @@ -823,6 +820,10 @@ public Builder withSDKKey(String sdkKey) {
this.sdkKey = sdkKey;
return this;
}
public Builder withVuidEnabled() {
this.vuidEnabled = true;
return this;
}

/**
* Override the default {@link Logger}.
Expand Down Expand Up @@ -1036,10 +1037,10 @@ public Builder withODPDisabled() {
* @param vuid a user-defined vuid value
* @return this {@link Builder} instance
*/
public Builder withVuid(String vuid) {
this.vuid = vuid;
return this;
}
// public Builder withVuid(String vuid) {
// this.vuid = vuid;
// return this;
// }

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

}

if (vuid == null) {
vuid = VuidManager.Companion.getShared(context).getVuid();
}
VuidManager vuidManager = new VuidManager(context, vuidEnabled);

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

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

ODPApiManager odpApiManager = new DefaultODPApiManager(
context,
Expand Down Expand Up @@ -1165,7 +1164,7 @@ public OptimizelyManager build(Context context) {
notificationCenter,
defaultDecideOptions,
odpManager,
vuid,
vuidManager,
customSdkName,
customSdkVersion
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,35 @@ class VuidManagerTest {

@Test
fun autoLoaded() {
val vuid1 = VuidManager.getShared(context).vuid
val vuidManager1 = VuidManager(context, true)
val vuid1 = vuidManager1.vuid
assertTrue("vuid should be auto loaded when constructed", vuid1.startsWith("vuid_"))
val savedVuid = vuidManager1.load(context)
assertEquals("Vuid should be same", vuid1, savedVuid)

val vuid2 = VuidManager.getShared(context).vuid
assertEquals("the same vuid should be returned when getting a singleton", vuid1, vuid2)
val vuidManager2 = VuidManager(context, false)
val vuid2 = vuidManager2.vuid
assertTrue(vuid2 == "")
assertFalse("vuid should be auto loaded when constructed", vuid2.startsWith("vuid_"))

// remove shared instance, so will be re-instantiated
VuidManager.removeSharedForTesting()

val vuid3 = VuidManager.getShared(context).vuid
assertEquals("the saved vuid should be returned when instantiated again", vuid2, vuid3)

// remove saved vuid
cleanSharedPrefs()
// remove shared instance, so will be re-instantiated
VuidManager.removeSharedForTesting()

val vuid4 = VuidManager.getShared(context).vuid
assertNotEquals("a new vuid should be returned when storage cleared and re-instantiated", vuid3, vuid4)
assertTrue(vuid4.startsWith("vuid_"))
// val vuid2 = VuidManager.getShared(context).vuid
// assertEquals("the same vuid should be returned when getting a singleton", vuid1, vuid2)
//
// // remove shared instance, so will be re-instantiated
// VuidManager.removeSharedForTesting()
//
// val vuid3 = VuidManager.getShared(context).vuid
// assertEquals("the saved vuid should be returned when instantiated again", vuid2, vuid3)
//
// // remove saved vuid
// cleanSharedPrefs()
// // remove shared instance, so will be re-instantiated
// VuidManager.removeSharedForTesting()
//
// val vuid4 = VuidManager.getShared(context).vuid
// assertNotEquals("a new vuid should be returned when storage cleared and re-instantiated", vuid3, vuid4)
// assertTrue(vuid4.startsWith("vuid_"))
}
}
39 changes: 29 additions & 10 deletions odp/src/main/java/com/optimizely/ab/android/odp/VuidManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,42 @@ import androidx.annotation.VisibleForTesting
import com.optimizely.ab.android.shared.OptlyStorage
import java.util.UUID

class VuidManager private constructor(context: Context) {
var vuid = ""
class VuidManager constructor(context: Context, isEnabled: Boolean = false) {
private var _vuid = ""
private val keyForVuid = "vuid" // stored in the private "optly" storage
private var isEnabled: Boolean = isEnabled

init {
this.vuid = load(context)
if (isEnabled) {
this._vuid = load(context)
} else {
this.remove(context)
}

}

fun getVuid(): String? {
return if (isEnabled) this._vuid else null
}
fun isEnabled(): Boolean {
return isEnabled
}

companion object {
@Volatile
private var INSTANCE: VuidManager? = null
// @Volatile
// private var INSTANCE: VuidManager? = null

@Synchronized
fun getShared(context: Context): VuidManager = INSTANCE ?: VuidManager(context).also { INSTANCE = it }
// fun getShared(context: Context): VuidManager = INSTANCE ?: VuidManager(context).also { INSTANCE = it }

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

@VisibleForTesting
fun removeSharedForTesting() {
INSTANCE = null
}
// @VisibleForTesting
// fun removeSharedForTesting() {
// INSTANCE = null
// }
}

@VisibleForTesting
Expand Down Expand Up @@ -69,4 +82,10 @@ class VuidManager private constructor(context: Context) {
val storage = OptlyStorage(context)
storage.saveString(keyForVuid, vuid)
}

@VisibleForTesting
fun remove(context: Context) {
val storage = OptlyStorage(context)
storage.removeString(keyForVuid)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public long getLong(String key, long defaultValue) {
public void saveString(String key, String value) {
getWritablePrefs().putString(key, value).apply();
}
/**
* Remove a string value
* @param key a String key
* @hide
*/
public void removeString(String key) {
getWritablePrefs().remove(key).apply();
}

/**
* Get a string value
Expand Down

0 comments on commit 8ba9e4e

Please sign in to comment.