1818
1919import android .content .Context ;
2020import android .content .IntentFilter ;
21+ import android .graphics .Path ;
2122import android .net .wifi .WifiManager ;
2223import android .util .Log ;
2324
2930import com .optimizely .ab .android .event_handler .EventRescheduler ;
3031import com .optimizely .ab .android .sdk .OptimizelyClient ;
3132import com .optimizely .ab .android .sdk .OptimizelyManager ;
33+ import com .optimizely .ab .android .sdk .cmab .CmabClientHelperAndroid ;
34+ import com .optimizely .ab .android .sdk .cmab .DefaultCmabClient ;
3235import com .optimizely .ab .android .test_app .R ;
3336import com .optimizely .ab .bucketing .UserProfileService ;
37+ import com .optimizely .ab .cmab .client .CmabClient ;
3438import com .optimizely .ab .config .Variation ;
3539import com .optimizely .ab .config .parser .JsonParseException ;
3640import com .optimizely .ab .error .ErrorHandler ;
@@ -69,6 +73,9 @@ public class APISamplesInJava {
6973
7074 static public void samplesAll (Context context ) {
7175 samplesForCmab (context );
76+ samplesForCmabConfig (context );
77+ samplesForCmabConfig2 (context );
78+
7279 samplesForDecide (context );
7380 samplesForInitialization (context );
7481 samplesForOptimizelyConfig (context );
@@ -98,19 +105,19 @@ static public void samplesAll(Context context) {
98105
99106
100107 static public void samplesForCmab (Context context ) {
101- List <OptimizelyDecideOption > defaultDecideOptions = Arrays .asList (
108+ List <OptimizelyDecideOption > options = Arrays .asList (
102109 OptimizelyDecideOption .INCLUDE_REASONS ,
103- OptimizelyDecideOption .IGNORE_USER_PROFILE_SERVICE
110+ OptimizelyDecideOption .IGNORE_USER_PROFILE_SERVICE ,
111+ OptimizelyDecideOption .IGNORE_CMAB_CACHE
104112 );
105113
106114 // we use cmab test project=4552646833471488 datafile for this testing
107115
108116 OptimizelyManager optimizelyManager = OptimizelyManager .builder ()
109117 .withSDKKey ("4ft9p1vSXYM5hLATwWdRc" )
110- .withDefaultDecideOptions (defaultDecideOptions )
111118 .build (context );
112119 // we use raw datafile for this testing
113- OptimizelyClient optimizelyClient = optimizelyManager .initialize (context , R .raw .datafile_full , false , false );
120+ OptimizelyClient optimizelyClient = optimizelyManager .initialize (context , R .raw .datafile_full );
114121
115122 String userId = "user_123" ;
116123 Map <String , Object > attributes = new HashMap <>();
@@ -122,7 +129,6 @@ static public void samplesForCmab(Context context) {
122129
123130 // decide (decideSync)
124131
125- List <OptimizelyDecideOption > options = Arrays .asList (OptimizelyDecideOption .INCLUDE_REASONS );
126132 Log .d ("Samples" ,"=================================================================" );
127133 Log .d ("Samples" ,"[CMAB] calling sync decision for cmab..." );
128134 Log .d ("Samples" ,"=================================================================" );
@@ -152,13 +158,113 @@ static public void samplesForCmab(Context context) {
152158 });
153159
154160 try {
155- latch .await (5 , TimeUnit .SECONDS );
161+ latch .await (60 , TimeUnit .SECONDS );
162+ Log .d ("Samples" , "[CMAB] Latch released. Async operation completed." );
163+ } catch (InterruptedException e ) {
164+ e .printStackTrace ();
165+ Thread .currentThread ().interrupt ();
166+ }
167+
168+ }
169+
170+ static public void samplesForCmabConfig (Context context ) {
171+ List <OptimizelyDecideOption > options = Arrays .asList (
172+ OptimizelyDecideOption .INCLUDE_REASONS ,
173+ OptimizelyDecideOption .IGNORE_USER_PROFILE_SERVICE ,
174+ OptimizelyDecideOption .IGNORE_CMAB_CACHE
175+ );
176+
177+ // we use cmab test project=4552646833471488 datafile for this testing
178+
179+ OptimizelyManager optimizelyManager = OptimizelyManager .builder ()
180+ .withSDKKey ("4ft9p1vSXYM5hLATwWdRc" )
181+ .withCmabCacheSize (50 )
182+ .withCmabCacheTimeout (10 , TimeUnit .SECONDS )
183+ .build (context );
184+ // we use raw datafile for this testing
185+ OptimizelyClient optimizelyClient = optimizelyManager .initialize (context , R .raw .datafile_full );
186+
187+ String userId = "user_123" ;
188+ Map <String , Object > attributes = new HashMap <>();
189+ attributes .put ("country" , "us" );
190+ attributes .put ("extra-1" , 100 );
191+ OptimizelyUserContext user = optimizelyClient .createUserContext (userId , attributes );
192+
193+ String flagKey = "cmab-flag" ;
194+
195+ // decideAsync
196+
197+ Log .d ("Samples" ,"=================================================================" );
198+ Log .d ("Samples" ,"[CMAB] calling async decision for cmab with custom cmab cache..." );
199+ Log .d ("Samples" ,"=================================================================" );
200+ final CountDownLatch latch = new CountDownLatch (1 );
201+ user .decideAsync (flagKey , options , (OptimizelyDecision optDecision ) -> {
202+ Log .d ("Samples" ,"=================================================================" );
203+ Log .d ("Samples" ,"[CMAB] async decision for cmab with custom cmab cache: " + optDecision .toString ());
204+ if (!optDecision .getEnabled ()) {
205+ Log .e ("Samples" ,"[ERROR] " + flagKey + " is expected to be enabled for this user!" );
206+ }
207+ Log .d ("Samples" ,"=================================================================" );
208+ latch .countDown ();
209+ });
210+
211+ try {
212+ latch .await (60 , TimeUnit .SECONDS );
156213 Log .d ("Samples" , "[CMAB] Latch released. Async operation completed." );
157214 } catch (InterruptedException e ) {
158215 e .printStackTrace ();
159216 Thread .currentThread ().interrupt ();
160217 }
218+ }
219+
220+ static public void samplesForCmabConfig2 (Context context ) {
221+ List <OptimizelyDecideOption > options = Arrays .asList (
222+ OptimizelyDecideOption .INCLUDE_REASONS ,
223+ OptimizelyDecideOption .IGNORE_USER_PROFILE_SERVICE ,
224+ OptimizelyDecideOption .IGNORE_CMAB_CACHE
225+ );
226+
227+ // custom CmabClient
228+
229+ CmabClientHelperAndroid cmabHelper = new CmabClientHelperAndroid ();
230+ cmabHelper .setCmabPredictionEndpoint ("https://prediction.cmab.optimizely.com/predict/%s" );
231+ CmabClient customCmabClient = new DefaultCmabClient (context , cmabHelper );
232+
233+ OptimizelyManager optimizelyManager = OptimizelyManager .builder ()
234+ .withSDKKey ("4ft9p1vSXYM5hLATwWdRc" )
235+ .withCmabClient (customCmabClient )
236+ .build (context );
237+ OptimizelyClient optimizelyClient = optimizelyManager .initialize (context , R .raw .datafile_full );
238+
239+ String userId = "user_123" ;
240+ Map <String , Object > attributes = new HashMap <>();
241+ attributes .put ("country" , "us" );
242+ attributes .put ("extra-1" , 100 );
243+ OptimizelyUserContext user = optimizelyClient .createUserContext (userId , attributes );
161244
245+ String flagKey = "cmab-flag" ;
246+
247+ Log .d ("Samples" ,"=================================================================" );
248+ Log .d ("Samples" ,"[CMAB] calling async decision for cmab with custom CmabClient..." );
249+ Log .d ("Samples" ,"=================================================================" );
250+ final CountDownLatch latch = new CountDownLatch (1 );
251+ user .decideAsync (flagKey , options , (OptimizelyDecision optDecision ) -> {
252+ Log .d ("Samples" ,"=================================================================" );
253+ Log .d ("Samples" ,"[CMAB] async decision for cmab with custom CmabClient: " + optDecision .toString ());
254+ if (!optDecision .getEnabled ()) {
255+ Log .e ("Samples" ,"[ERROR] " + flagKey + " is expected to be enabled for this user!" );
256+ }
257+ Log .d ("Samples" ,"=================================================================" );
258+ latch .countDown ();
259+ });
260+
261+ try {
262+ latch .await (60 , TimeUnit .SECONDS );
263+ Log .d ("Samples" , "[CMAB] Latch released. Async operation completed." );
264+ } catch (InterruptedException e ) {
265+ e .printStackTrace ();
266+ Thread .currentThread ().interrupt ();
267+ }
162268 }
163269
164270 static public void samplesForDecide (Context context ) {
0 commit comments