14
14
15
15
package com .google .firebase .perf .config ;
16
16
17
+ import static com .google .firebase .perf .config .ConfigurationConstants .ExperimentTTID ;
18
+
17
19
import android .content .Context ;
18
20
import android .content .pm .PackageInfo ;
19
21
import android .content .pm .PackageManager .NameNotFoundException ;
@@ -53,6 +55,7 @@ public class RemoteConfigManager {
53
55
private static final long MIN_APP_START_CONFIG_FETCH_DELAY_MS = 5000 ;
54
56
private static final int RANDOM_APP_START_CONFIG_FETCH_DELAY_MS = 25000 ;
55
57
58
+ private final DeviceCacheManager cache ;
56
59
private final ConcurrentHashMap <String , FirebaseRemoteConfigValue > allRcConfigMap ;
57
60
private final Executor executor ;
58
61
private final long appStartTimeInMs ;
@@ -65,29 +68,25 @@ public class RemoteConfigManager {
65
68
66
69
private RemoteConfigManager () {
67
70
this (
71
+ DeviceCacheManager .getInstance (),
68
72
new ThreadPoolExecutor (
69
73
/* corePoolSize= */ 0 ,
70
74
/* maximumPoolSize= */ 1 ,
71
75
/* keepAliveTime= */ 0L ,
72
76
TimeUnit .SECONDS ,
73
77
new LinkedBlockingQueue <Runnable >()),
74
- /* firebaseRemoteConfig= */ null // set once FirebaseRemoteConfig is initialized
75
- );
76
- }
77
-
78
- RemoteConfigManager (Executor executor , FirebaseRemoteConfig firebaseRemoteConfig ) {
79
- this (
80
- executor ,
81
- firebaseRemoteConfig ,
78
+ /* firebaseRemoteConfig= */ null , // set once FirebaseRemoteConfig is initialized
82
79
MIN_APP_START_CONFIG_FETCH_DELAY_MS
83
80
+ new Random ().nextInt (RANDOM_APP_START_CONFIG_FETCH_DELAY_MS ));
84
81
}
85
82
86
83
@ VisibleForTesting
87
84
RemoteConfigManager (
85
+ DeviceCacheManager cache ,
88
86
Executor executor ,
89
87
FirebaseRemoteConfig firebaseRemoteConfig ,
90
88
long appStartConfigFetchDelayInMs ) {
89
+ this .cache = cache ;
91
90
this .executor = executor ;
92
91
this .firebaseRemoteConfig = firebaseRemoteConfig ;
93
92
this .allRcConfigMap =
@@ -315,7 +314,7 @@ private void triggerRemoteConfigFetchIfNecessary() {
315
314
return ;
316
315
}
317
316
if (allRcConfigMap .isEmpty ()) { // Initial fetch.
318
- syncConfigValues (firebaseRemoteConfig .getAll ());
317
+ allRcConfigMap . putAll (firebaseRemoteConfig .getAll ());
319
318
}
320
319
if (shouldFetchAndActivateRemoteConfigValues ()) {
321
320
triggerFirebaseRemoteConfigFetchAndActivateOnSuccessfulFetch ();
@@ -342,6 +341,19 @@ protected void syncConfigValues(Map<String, FirebaseRemoteConfigValue> newlyFetc
342
341
allRcConfigMap .remove (existingKey );
343
342
}
344
343
}
344
+
345
+ // Save to device cache upon successful RC fetchAndActivate
346
+ ExperimentTTID flag = ExperimentTTID .getInstance ();
347
+ FirebaseRemoteConfigValue rcValue = allRcConfigMap .get (flag .getRemoteConfigFlag ());
348
+ if (rcValue != null ) {
349
+ try {
350
+ cache .setValue (flag .getDeviceCacheFlag (), rcValue .asBoolean ());
351
+ } catch (Exception exception ) {
352
+ logger .debug ("ExperimentTTID remote config flag has invalid value, expected boolean." );
353
+ }
354
+ } else {
355
+ logger .debug ("ExperimentTTID remote config flag does not exist." );
356
+ }
345
357
}
346
358
347
359
@ VisibleForTesting
0 commit comments