2323import androidx .annotation .Keep ;
2424import androidx .annotation .Nullable ;
2525import androidx .annotation .VisibleForTesting ;
26- import com .google .firebase .FirebaseApp ;
27- import com .google .firebase .StartupTime ;
2826import com .google .firebase .inject .Provider ;
2927import com .google .firebase .perf .logging .AndroidLogger ;
3028import com .google .firebase .perf .util .Optional ;
@@ -54,15 +52,14 @@ public class RemoteConfigManager {
5452 private static final long TIME_AFTER_WHICH_A_FETCH_IS_CONSIDERED_STALE_MS =
5553 TimeUnit .HOURS .toMillis (12 );
5654 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 ();
5958
6059 private final DeviceCacheManager cache ;
6160 private final ConcurrentHashMap <String , FirebaseRemoteConfigValue > allRcConfigMap ;
6261 private final Executor executor ;
63- private final long appStartTimeInMs ;
6462 private final long appStartConfigFetchDelayInMs ;
65-
6663 private long firebaseRemoteConfigLastFetchTimestampMs = FETCH_NEVER_HAPPENED_TIMESTAMP_MS ;
6764
6865 @ Nullable private Provider <RemoteConfigComponent > firebaseRemoteConfigProvider ;
@@ -80,43 +77,22 @@ private RemoteConfigManager() {
8077 TimeUnit .SECONDS ,
8178 new LinkedBlockingQueue <Runnable >()),
8279 /* 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 ));
10381 }
10482
10583 @ VisibleForTesting
10684 RemoteConfigManager (
10785 DeviceCacheManager cache ,
10886 Executor executor ,
10987 FirebaseRemoteConfig firebaseRemoteConfig ,
110- long appStartConfigFetchDelayInMs ,
111- long appStartTimeInMs ) {
88+ long appStartConfigFetchDelayInMs ) {
11289 this .cache = cache ;
11390 this .executor = executor ;
11491 this .firebaseRemoteConfig = firebaseRemoteConfig ;
11592 this .allRcConfigMap =
11693 firebaseRemoteConfig == null
11794 ? new ConcurrentHashMap <>()
11895 : new ConcurrentHashMap <>(firebaseRemoteConfig .getAll ());
119- this .appStartTimeInMs = appStartTimeInMs ;
12096 this .appStartConfigFetchDelayInMs = appStartConfigFetchDelayInMs ;
12197 }
12298
@@ -329,7 +305,7 @@ public boolean isLastFetchFailed() {
329305 *
330306 * <ol>
331307 * <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
333309 * <li>At least 12 hours have passed since the previous fetch.
334310 * </ol>
335311 */
@@ -408,17 +384,17 @@ public boolean isFirebaseRemoteConfigAvailable() {
408384 /** Returns true if a RC fetch should be made, false otherwise. */
409385 private boolean shouldFetchAndActivateRemoteConfigValues () {
410386 long currentTimeInMs = getCurrentSystemTimeMillis ();
411- return hasAppStartConfigFetchDelayElapsed (currentTimeInMs )
387+ return hasRemoteConfigFetchDelayElapsed (currentTimeInMs )
412388 && hasLastFetchBecomeStale (currentTimeInMs );
413389 }
414390
415391 /**
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.
417393 *
418394 * @return true if the random delay has elapsed, false otherwise
419395 */
420- private boolean hasAppStartConfigFetchDelayElapsed (long currentTimeInMs ) {
421- return (currentTimeInMs - appStartTimeInMs ) >= appStartConfigFetchDelayInMs ;
396+ private boolean hasRemoteConfigFetchDelayElapsed (long currentTimeInMs ) {
397+ return (currentTimeInMs - rcmInitTimestamp ) >= appStartConfigFetchDelayInMs ;
422398 }
423399
424400 // We want to fetch once when the app starts and every 12 hours after that.
0 commit comments