23
23
import androidx .annotation .Keep ;
24
24
import androidx .annotation .Nullable ;
25
25
import androidx .annotation .VisibleForTesting ;
26
- import com .google .firebase .FirebaseApp ;
27
- import com .google .firebase .StartupTime ;
28
26
import com .google .firebase .inject .Provider ;
29
27
import com .google .firebase .perf .logging .AndroidLogger ;
30
28
import com .google .firebase .perf .util .Optional ;
@@ -54,15 +52,14 @@ public class RemoteConfigManager {
54
52
private static final long TIME_AFTER_WHICH_A_FETCH_IS_CONSIDERED_STALE_MS =
55
53
TimeUnit .HOURS .toMillis (12 );
56
54
private static final long FETCH_NEVER_HAPPENED_TIMESTAMP_MS = 0 ;
57
- private static final long MIN_APP_START_CONFIG_FETCH_DELAY_MS = 5000 ;
58
- private static final int RANDOM_APP_START_CONFIG_FETCH_DELAY_MS = 25000 ;
55
+ private static final long MIN_CONFIG_FETCH_DELAY_MS = 5000 ;
56
+ private static final int RANDOM_CONFIG_FETCH_DELAY_MS = 25000 ;
57
+ private final long rcmInitTimestamp = getCurrentSystemTimeMillis ();
59
58
60
59
private final DeviceCacheManager cache ;
61
60
private final ConcurrentHashMap <String , FirebaseRemoteConfigValue > allRcConfigMap ;
62
61
private final Executor executor ;
63
- private final long appStartTimeInMs ;
64
- private final long appStartConfigFetchDelayInMs ;
65
-
62
+ private final long remoteConfigFetchDelayInMs ;
66
63
private long firebaseRemoteConfigLastFetchTimestampMs = FETCH_NEVER_HAPPENED_TIMESTAMP_MS ;
67
64
68
65
@ Nullable private Provider <RemoteConfigComponent > firebaseRemoteConfigProvider ;
@@ -80,44 +77,23 @@ private RemoteConfigManager() {
80
77
TimeUnit .SECONDS ,
81
78
new LinkedBlockingQueue <Runnable >()),
82
79
/* firebaseRemoteConfig= */ null , // set once FirebaseRemoteConfig is initialized
83
- MIN_APP_START_CONFIG_FETCH_DELAY_MS
84
- + new Random ().nextInt (RANDOM_APP_START_CONFIG_FETCH_DELAY_MS ),
85
- getInitialStartupMillis ());
86
- }
87
-
88
- @ VisibleForTesting
89
- @ SuppressWarnings ("FirebaseUseExplicitDependencies" )
90
- static long getInitialStartupMillis () {
91
- StartupTime startupTime = null ;
92
- try {
93
- startupTime = FirebaseApp .getInstance ().get (StartupTime .class );
94
- } catch (IllegalStateException ex ) {
95
- // This can happen if you start a trace before Firebase is init
96
- logger .debug ("Unable to get StartupTime instance." );
97
- }
98
- if (startupTime != null ) {
99
- return startupTime .getEpochMillis ();
100
- } else {
101
- return System .currentTimeMillis ();
102
- }
80
+ MIN_CONFIG_FETCH_DELAY_MS + new Random ().nextInt (RANDOM_CONFIG_FETCH_DELAY_MS ));
103
81
}
104
82
105
83
@ VisibleForTesting
106
84
RemoteConfigManager (
107
85
DeviceCacheManager cache ,
108
86
Executor executor ,
109
87
FirebaseRemoteConfig firebaseRemoteConfig ,
110
- long appStartConfigFetchDelayInMs ,
111
- long appStartTimeInMs ) {
88
+ long remoteConfigFetchDelayInMs ) {
112
89
this .cache = cache ;
113
90
this .executor = executor ;
114
91
this .firebaseRemoteConfig = firebaseRemoteConfig ;
115
92
this .allRcConfigMap =
116
93
firebaseRemoteConfig == null
117
94
? new ConcurrentHashMap <>()
118
95
: new ConcurrentHashMap <>(firebaseRemoteConfig .getAll ());
119
- this .appStartTimeInMs = appStartTimeInMs ;
120
- this .appStartConfigFetchDelayInMs = appStartConfigFetchDelayInMs ;
96
+ this .remoteConfigFetchDelayInMs = remoteConfigFetchDelayInMs ;
121
97
}
122
98
123
99
/** Gets the singleton instance. */
@@ -329,7 +305,7 @@ public boolean isLastFetchFailed() {
329
305
*
330
306
* <ol>
331
307
* <li>Firebase Remote Config is available,
332
- * <li>Time-since-app-start has passed a randomized delay-time (b/187985523), and
308
+ * <li>Time has passed a randomized delay-time (b/187985523), and
333
309
* <li>At least 12 hours have passed since the previous fetch.
334
310
* </ol>
335
311
*/
@@ -408,17 +384,17 @@ public boolean isFirebaseRemoteConfigAvailable() {
408
384
/** Returns true if a RC fetch should be made, false otherwise. */
409
385
private boolean shouldFetchAndActivateRemoteConfigValues () {
410
386
long currentTimeInMs = getCurrentSystemTimeMillis ();
411
- return hasAppStartConfigFetchDelayElapsed (currentTimeInMs )
387
+ return hasRemoteConfigFetchDelayElapsed (currentTimeInMs )
412
388
&& hasLastFetchBecomeStale (currentTimeInMs );
413
389
}
414
390
415
391
/**
416
- * Delay fetch by some random time since app start . This is to prevent b/187985523.
392
+ * Delay fetch by some random time. This is to prevent b/187985523.
417
393
*
418
394
* @return true if the random delay has elapsed, false otherwise
419
395
*/
420
- private boolean hasAppStartConfigFetchDelayElapsed (long currentTimeInMs ) {
421
- return (currentTimeInMs - appStartTimeInMs ) >= appStartConfigFetchDelayInMs ;
396
+ private boolean hasRemoteConfigFetchDelayElapsed (long currentTimeInMs ) {
397
+ return (currentTimeInMs - rcmInitTimestamp ) >= remoteConfigFetchDelayInMs ;
422
398
}
423
399
424
400
// We want to fetch once when the app starts and every 12 hours after that.
0 commit comments