39
39
import com .optimizely .ab .android .event_handler .EventIntentService ;
40
40
import com .optimizely .ab .android .user_profile .DefaultUserProfileService ;
41
41
import com .optimizely .ab .bucketing .UserProfileService ;
42
+ import com .optimizely .ab .config .ProjectConfig ;
42
43
import com .optimizely .ab .config .parser .ConfigParseException ;
43
44
import com .optimizely .ab .error .ErrorHandler ;
44
45
import com .optimizely .ab .event .EventHandler ;
50
51
51
52
import java .io .IOException ;
52
53
import java .io .InputStream ;
54
+ import java .util .Map ;
55
+ import java .util .Set ;
53
56
54
57
/**
55
58
* Handles loading the Optimizely data file
@@ -156,10 +159,16 @@ protected OptimizelyClient initialize(@NonNull Context context,@Nullable String
156
159
return optimizelyClient ;
157
160
}
158
161
try {
159
- if (datafile !=null )
162
+ if (datafile !=null ) {
163
+ if (getUserProfileService () instanceof DefaultUserProfileService ) {
164
+ DefaultUserProfileService defaultUserProfileService = (DefaultUserProfileService ) getUserProfileService ();
165
+ defaultUserProfileService .start ();
166
+ }
160
167
optimizelyClient = buildOptimizely (context , datafile );
161
- else
168
+ }
169
+ else {
162
170
logger .error ("Invalid datafile" );
171
+ }
163
172
} catch (ConfigParseException e ) {
164
173
logger .error ("Unable to parse compiled data file" , e );
165
174
} catch (Exception e ) {
@@ -192,8 +201,13 @@ public OptimizelyClient initialize(@NonNull Context context, @RawRes Integer dat
192
201
try {
193
202
194
203
String datafile ;
204
+ Boolean datafileInCache = isDatafileCached (context );
195
205
datafile = getDatafile (context , datafileRes );
206
+
196
207
optimizelyClient = initialize (context , datafile , true );
208
+ if (datafileInCache ) {
209
+ cleanupUserProfileCache (getUserProfileService ());
210
+ }
197
211
}catch (NullPointerException e ){
198
212
logger .error ("Unable to find compiled data file in raw resource" ,e );
199
213
}
@@ -202,6 +216,36 @@ public OptimizelyClient initialize(@NonNull Context context, @RawRes Integer dat
202
216
return optimizelyClient ;
203
217
}
204
218
219
+ private void cleanupUserProfileCache (UserProfileService userProfileService ) {
220
+ final DefaultUserProfileService defaultUserProfileService ;
221
+ if (userProfileService instanceof DefaultUserProfileService ) {
222
+ defaultUserProfileService = (DefaultUserProfileService )userProfileService ;
223
+ }
224
+ else {
225
+ return ;
226
+ }
227
+
228
+ final ProjectConfig config = optimizelyClient .getProjectConfig ();
229
+ if (config == null ) {
230
+ return ;
231
+ }
232
+
233
+ new Thread (new Runnable () {
234
+ @ Override
235
+ public void run () {
236
+ try {
237
+ Set <String > experimentIds = config .getExperimentIdMapping ().keySet ();
238
+
239
+ defaultUserProfileService .removeInvalidExperiments (experimentIds );
240
+ }
241
+ catch (Exception e ) {
242
+ logger .error ("Error removing invalid experiments from default user profile service." , e );
243
+ }
244
+ }
245
+ }).start ();
246
+
247
+ }
248
+
205
249
/** This function will first try to get datafile from Cache, if file is not cached yet
206
250
* than it will read from Raw file
207
251
* @param context
@@ -366,6 +410,7 @@ void injectOptimizely(@NonNull final Context context, final @NonNull UserProfile
366
410
((DefaultUserProfileService ) userProfileService ).startInBackground (new DefaultUserProfileService .StartCallback () {
367
411
@ Override
368
412
public void onStartComplete (UserProfileService userProfileService ) {
413
+ cleanupUserProfileCache (userProfileService );
369
414
if (optimizelyStartListener != null ) {
370
415
logger .info ("Sending Optimizely instance to listener" );
371
416
notifyStartListener ();
0 commit comments