Skip to content

Commit 3feb7f6

Browse files
committed
Migrate RC to common executors.
1 parent b5a821d commit 3feb7f6

File tree

5 files changed

+33
-36
lines changed

5 files changed

+33
-36
lines changed

firebase-config/firebase-config.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ android {
4949
}
5050

5151
dependencies {
52+
implementation project(':firebase-annotations')
5253
implementation project(':firebase-common')
5354
implementation project(':firebase-abt')
5455
implementation project(':firebase-components')

firebase-config/src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfig.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.google.firebase.remoteconfig;
1616

17-
import android.annotation.SuppressLint;
1817
import android.content.Context;
1918
import android.util.Log;
2019
import androidx.annotation.NonNull;
@@ -26,6 +25,7 @@
2625
import com.google.firebase.FirebaseApp;
2726
import com.google.firebase.abt.AbtException;
2827
import com.google.firebase.abt.FirebaseABTesting;
28+
import com.google.firebase.concurrent.FirebaseExecutors;
2929
import com.google.firebase.installations.FirebaseInstallationsApi;
3030
import com.google.firebase.installations.InstallationTokenResult;
3131
import com.google.firebase.remoteconfig.internal.ConfigCacheClient;
@@ -280,14 +280,13 @@ public Task<Boolean> activate() {
280280
*
281281
* @return {@link Task} representing the {@code fetch} call.
282282
*/
283-
// TODO(b/258275481): Use an explicit executor in continuations.
284-
@SuppressLint("TaskMainThread")
285283
@NonNull
286284
public Task<Void> fetch() {
287285
Task<FetchResponse> fetchTask = fetchHandler.fetch();
288286

289287
// Convert Task type to Void.
290-
return fetchTask.onSuccessTask((unusedFetchResponse) -> Tasks.forResult(null));
288+
return fetchTask.onSuccessTask(
289+
FirebaseExecutors.directExecutor(), (unusedFetchResponse) -> Tasks.forResult(null));
291290
}
292291

293292
/**
@@ -309,14 +308,13 @@ public Task<Void> fetch() {
309308
* this many seconds ago, configs are served from the backend instead of local storage.
310309
* @return {@link Task} representing the {@code fetch} call.
311310
*/
312-
// TODO(b/258275481): Use an explicit executor in continuations.
313-
@SuppressLint("TaskMainThread")
314311
@NonNull
315312
public Task<Void> fetch(long minimumFetchIntervalInSeconds) {
316313
Task<FetchResponse> fetchTask = fetchHandler.fetch(minimumFetchIntervalInSeconds);
317314

318315
// Convert Task type to Void.
319-
return fetchTask.onSuccessTask((unusedFetchResponse) -> Tasks.forResult(null));
316+
return fetchTask.onSuccessTask(
317+
FirebaseExecutors.directExecutor(), (unusedFetchResponse) -> Tasks.forResult(null));
320318
}
321319

322320
/**
@@ -589,8 +587,6 @@ private boolean processActivatePutTask(Task<ConfigContainer> putTask) {
589587
*
590588
* @return A task with result {@code null} on failure.
591589
*/
592-
// TODO(b/258275481): Use an explicit executor in continuations.
593-
@SuppressLint("TaskMainThread")
594590
private Task<Void> setDefaultsWithStringsMapAsync(Map<String, String> defaultsStringMap) {
595591
ConfigContainer defaultConfigs = null;
596592
try {
@@ -602,7 +598,8 @@ private Task<Void> setDefaultsWithStringsMapAsync(Map<String, String> defaultsSt
602598

603599
Task<ConfigContainer> putTask = defaultConfigsCache.put(defaultConfigs);
604600
// Convert Task type to Void.
605-
return putTask.onSuccessTask((unusedContainer) -> Tasks.forResult(null));
601+
return putTask.onSuccessTask(
602+
FirebaseExecutors.directExecutor(), (unusedContainer) -> Tasks.forResult(null));
606603
}
607604

608605
/**

firebase-config/src/main/java/com/google/firebase/remoteconfig/RemoteConfigComponent.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
package com.google.firebase.remoteconfig;
1616

17-
import android.annotation.SuppressLint;
1817
import android.content.Context;
1918
import android.content.SharedPreferences;
2019
import androidx.annotation.GuardedBy;
@@ -27,6 +26,7 @@
2726
import com.google.firebase.FirebaseApp;
2827
import com.google.firebase.abt.FirebaseABTesting;
2928
import com.google.firebase.analytics.connector.AnalyticsConnector;
29+
import com.google.firebase.annotations.concurrent.Background;
3030
import com.google.firebase.inject.Provider;
3131
import com.google.firebase.installations.FirebaseInstallationsApi;
3232
import com.google.firebase.remoteconfig.internal.ConfigCacheClient;
@@ -40,8 +40,6 @@
4040
import java.util.Map;
4141
import java.util.Random;
4242
import java.util.concurrent.Executor;
43-
import java.util.concurrent.ExecutorService;
44-
import java.util.concurrent.Executors;
4543

4644
/**
4745
* Component for providing multiple Firebase Remote Config (FRC) instances. Firebase Android
@@ -76,7 +74,7 @@ public class RemoteConfigComponent {
7674
private final Map<String, FirebaseRemoteConfig> frcNamespaceInstances = new HashMap<>();
7775

7876
private final Context context;
79-
private final ExecutorService executorService;
77+
private final Executor executor;
8078
private final FirebaseApp firebaseApp;
8179
private final FirebaseInstallationsApi firebaseInstallations;
8280
private final FirebaseABTesting firebaseAbt;
@@ -88,17 +86,16 @@ public class RemoteConfigComponent {
8886
private Map<String, String> customHeaders = new HashMap<>();
8987

9088
/** Firebase Remote Config Component constructor. */
91-
// TODO(b/258275481): Migrate to go/firebase-android-executors
92-
@SuppressLint("ThreadPoolCreation")
9389
RemoteConfigComponent(
9490
Context context,
91+
@Background Executor executor,
9592
FirebaseApp firebaseApp,
9693
FirebaseInstallationsApi firebaseInstallations,
9794
FirebaseABTesting firebaseAbt,
9895
Provider<AnalyticsConnector> analyticsConnector) {
9996
this(
10097
context,
101-
Executors.newCachedThreadPool(),
98+
executor,
10299
firebaseApp,
103100
firebaseInstallations,
104101
firebaseAbt,
@@ -110,14 +107,14 @@ public class RemoteConfigComponent {
110107
@VisibleForTesting
111108
protected RemoteConfigComponent(
112109
Context context,
113-
ExecutorService executorService,
110+
Executor executor,
114111
FirebaseApp firebaseApp,
115112
FirebaseInstallationsApi firebaseInstallations,
116113
FirebaseABTesting firebaseAbt,
117114
Provider<AnalyticsConnector> analyticsConnector,
118115
boolean loadGetDefault) {
119116
this.context = context;
120-
this.executorService = executorService;
117+
this.executor = executor;
121118
this.firebaseApp = firebaseApp;
122119
this.firebaseInstallations = firebaseInstallations;
123120
this.firebaseAbt = firebaseAbt;
@@ -130,7 +127,7 @@ protected RemoteConfigComponent(
130127
// while another test has already cleared the component but hasn't gotten a new one yet.
131128
if (loadGetDefault) {
132129
// Loads the default namespace's configs from disk on App startup.
133-
Tasks.call(executorService, this::getDefault);
130+
Tasks.call(executor, this::getDefault);
134131
}
135132
}
136133

@@ -167,7 +164,7 @@ public synchronized FirebaseRemoteConfig get(String namespace) {
167164
namespace,
168165
firebaseInstallations,
169166
firebaseAbt,
170-
executorService,
167+
executor,
171168
fetchedCacheClient,
172169
activatedCacheClient,
173170
defaultsCacheClient,
@@ -214,15 +211,13 @@ public synchronized void setCustomHeaders(Map<String, String> customHeaders) {
214211
this.customHeaders = customHeaders;
215212
}
216213

217-
// TODO(b/258275481): Migrate to go/firebase-android-executors
218-
@SuppressLint("ThreadPoolCreation")
219214
private ConfigCacheClient getCacheClient(String namespace, String configStoreType) {
220215
String fileName =
221216
String.format(
222217
"%s_%s_%s_%s.json",
223218
FIREBASE_REMOTE_CONFIG_FILE_NAME_PREFIX, appId, namespace, configStoreType);
224219
return ConfigCacheClient.getInstance(
225-
Executors.newCachedThreadPool(), ConfigStorageClient.getInstance(context, fileName));
220+
executor, ConfigStorageClient.getInstance(context, fileName));
226221
}
227222

228223
@VisibleForTesting
@@ -244,7 +239,7 @@ synchronized ConfigFetchHandler getFetchHandler(
244239
return new ConfigFetchHandler(
245240
firebaseInstallations,
246241
isPrimaryApp(firebaseApp) ? analyticsConnector : () -> null,
247-
executorService,
242+
executor,
248243
DEFAULT_CLOCK,
249244
DEFAULT_RANDOM,
250245
fetchedCacheClient,
@@ -255,8 +250,7 @@ synchronized ConfigFetchHandler getFetchHandler(
255250

256251
private ConfigGetParameterHandler getGetHandler(
257252
ConfigCacheClient activatedCacheClient, ConfigCacheClient defaultsCacheClient) {
258-
return new ConfigGetParameterHandler(
259-
executorService, activatedCacheClient, defaultsCacheClient);
253+
return new ConfigGetParameterHandler(executor, activatedCacheClient, defaultsCacheClient);
260254
}
261255

262256
@VisibleForTesting

firebase-config/src/main/java/com/google/firebase/remoteconfig/RemoteConfigRegistrar.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
import com.google.firebase.abt.FirebaseABTesting.OriginService;
2121
import com.google.firebase.abt.component.AbtComponent;
2222
import com.google.firebase.analytics.connector.AnalyticsConnector;
23+
import com.google.firebase.annotations.concurrent.Blocking;
2324
import com.google.firebase.components.Component;
2425
import com.google.firebase.components.ComponentRegistrar;
2526
import com.google.firebase.components.Dependency;
27+
import com.google.firebase.components.Qualified;
2628
import com.google.firebase.installations.FirebaseInstallationsApi;
2729
import com.google.firebase.platforminfo.LibraryVersionComponent;
2830
import java.util.Arrays;
2931
import java.util.List;
32+
import java.util.concurrent.Executor;
3033

3134
/**
3235
* Registrar for setting up Firebase Remote Config's dependency injections in Firebase Android
@@ -41,10 +44,12 @@ public class RemoteConfigRegistrar implements ComponentRegistrar {
4144

4245
@Override
4346
public List<Component<?>> getComponents() {
47+
Qualified<Executor> blockingExecutor = Qualified.qualified(Blocking.class, Executor.class);
4448
return Arrays.asList(
4549
Component.builder(RemoteConfigComponent.class)
4650
.name(LIBRARY_NAME)
4751
.add(Dependency.required(Context.class))
52+
.add(Dependency.required(blockingExecutor))
4853
.add(Dependency.required(FirebaseApp.class))
4954
.add(Dependency.required(FirebaseInstallationsApi.class))
5055
.add(Dependency.required(AbtComponent.class))
@@ -53,6 +58,7 @@ public List<Component<?>> getComponents() {
5358
container ->
5459
new RemoteConfigComponent(
5560
container.get(Context.class),
61+
container.get(blockingExecutor),
5662
container.get(FirebaseApp.class),
5763
container.get(FirebaseInstallationsApi.class),
5864
container.get(AbtComponent.class).get(OriginService.REMOTE_CONFIG),

firebase-config/src/main/java/com/google/firebase/remoteconfig/internal/ConfigCacheClient.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.concurrent.CountDownLatch;
3333
import java.util.concurrent.ExecutionException;
3434
import java.util.concurrent.Executor;
35-
import java.util.concurrent.ExecutorService;
3635
import java.util.concurrent.TimeUnit;
3736
import java.util.concurrent.TimeoutException;
3837

@@ -56,7 +55,7 @@ public class ConfigCacheClient {
5655
@GuardedBy("ConfigCacheClient.class")
5756
private static final Map<String, ConfigCacheClient> clientInstances = new HashMap<>();
5857

59-
private final ExecutorService executorService;
58+
private final Executor executor;
6059
private final ConfigStorageClient storageClient;
6160

6261
/**
@@ -71,8 +70,8 @@ public class ConfigCacheClient {
7170
* Creates a new cache client that executes async calls through {@code executorService} and is
7271
* backed by {@code storageClient}.
7372
*/
74-
private ConfigCacheClient(ExecutorService executorService, ConfigStorageClient storageClient) {
75-
this.executorService = executorService;
73+
private ConfigCacheClient(Executor executor, ConfigStorageClient storageClient) {
74+
this.executor = executor;
7675
this.storageClient = storageClient;
7776

7877
cachedContainerTask = null;
@@ -126,9 +125,9 @@ public Task<ConfigContainer> put(ConfigContainer configContainer) {
126125
*/
127126
public Task<ConfigContainer> put(
128127
ConfigContainer configContainer, boolean shouldUpdateInMemoryContainer) {
129-
return Tasks.call(executorService, () -> storageClient.write(configContainer))
128+
return Tasks.call(executor, () -> storageClient.write(configContainer))
130129
.onSuccessTask(
131-
executorService,
130+
executor,
132131
(unusedVoid) -> {
133132
if (shouldUpdateInMemoryContainer) {
134133
updateInMemoryConfigContainer(configContainer);
@@ -165,7 +164,7 @@ public synchronized Task<ConfigContainer> get() {
165164
*/
166165
if (cachedContainerTask == null
167166
|| (cachedContainerTask.isComplete() && !cachedContainerTask.isSuccessful())) {
168-
cachedContainerTask = Tasks.call(executorService, storageClient::read);
167+
cachedContainerTask = Tasks.call(executor, storageClient::read);
169168
}
170169
return cachedContainerTask;
171170
}
@@ -200,10 +199,10 @@ synchronized Task<ConfigContainer> getCachedContainerTask() {
200199
* underlying file name.
201200
*/
202201
public static synchronized ConfigCacheClient getInstance(
203-
ExecutorService executorService, ConfigStorageClient storageClient) {
202+
Executor executor, ConfigStorageClient storageClient) {
204203
String fileName = storageClient.getFileName();
205204
if (!clientInstances.containsKey(fileName)) {
206-
clientInstances.put(fileName, new ConfigCacheClient(executorService, storageClient));
205+
clientInstances.put(fileName, new ConfigCacheClient(executor, storageClient));
207206
}
208207
return clientInstances.get(fileName);
209208
}

0 commit comments

Comments
 (0)