Skip to content

Commit 728b530

Browse files
committed
Migrate some executors in fad/in-app-feedback (#4350)
* Migrate executors in fad/in-app-feedback * Add an import and format
1 parent 4b31fa4 commit 728b530

File tree

4 files changed

+31
-30
lines changed

4 files changed

+31
-30
lines changed

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/impl/FirebaseAppDistributionImpl.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class FirebaseAppDistributionImpl implements FirebaseAppDistribution {
6868
private final SequentialReference<AppDistributionReleaseInternal> cachedNewRelease;
6969
private final ReleaseIdentifier releaseIdentifier;
7070
private final ScreenshotTaker screenshotTaker;
71-
private final Executor taskExecutor;
71+
private final Executor blockingExecutor;
7272
private final FirebaseAppDistributionNotificationsManager notificationsManager;
7373
private TaskCache<UpdateTask> updateIfNewReleaseAvailableTaskCache = new TaskCache<>();
7474
private TaskCache<Task<AppDistributionRelease>> checkForNewReleaseTaskCache = new TaskCache<>();
@@ -94,7 +94,7 @@ class FirebaseAppDistributionImpl implements FirebaseAppDistribution {
9494
@NonNull ReleaseIdentifier releaseIdentifier,
9595
@NonNull ScreenshotTaker screenshotTaker,
9696
@NonNull @Lightweight Executor lightweightExecutor,
97-
@NonNull Executor taskExecutor) {
97+
@NonNull Executor blockingExecutor) {
9898
this.firebaseApp = firebaseApp;
9999
this.testerSignInManager = testerSignInManager;
100100
this.newReleaseFetcher = newReleaseFetcher;
@@ -106,7 +106,7 @@ class FirebaseAppDistributionImpl implements FirebaseAppDistribution {
106106
this.cachedNewRelease = new SequentialReference<>(lightweightExecutor);
107107
this.lightweightExecutor = lightweightExecutor;
108108
this.screenshotTaker = screenshotTaker;
109-
this.taskExecutor = taskExecutor;
109+
this.blockingExecutor = blockingExecutor;
110110
this.notificationsManager =
111111
new FirebaseAppDistributionNotificationsManager(firebaseApp.getApplicationContext());
112112
lifecycleNotifier.addOnActivityDestroyedListener(this::onActivityDestroyed);
@@ -332,13 +332,13 @@ public void startFeedback(@NonNull CharSequence infoText) {
332332
screenshotTaker
333333
.takeScreenshot()
334334
.addOnFailureListener(
335-
taskExecutor,
335+
blockingExecutor,
336336
e -> {
337337
LogWrapper.w(TAG, "Failed to take screenshot for feedback", e);
338338
doStartFeedback(infoText, null);
339339
})
340340
.addOnSuccessListener(
341-
taskExecutor, screenshotUri -> doStartFeedback(infoText, screenshotUri));
341+
blockingExecutor, screenshotUri -> doStartFeedback(infoText, screenshotUri));
342342
}
343343

344344
@Override
@@ -378,17 +378,18 @@ private void doStartFeedback(CharSequence infoText, @Nullable Uri screenshotUri)
378378
testerSignInManager
379379
.signInTester()
380380
.addOnFailureListener(
381-
taskExecutor,
381+
blockingExecutor,
382382
e -> {
383383
feedbackInProgress.set(false);
384384
LogWrapper.e(TAG, "Failed to sign in tester. Could not collect feedback.", e);
385385
})
386386
.onSuccessTask(
387-
taskExecutor,
387+
blockingExecutor,
388388
unused ->
389389
releaseIdentifier
390390
.identifyRelease()
391391
.addOnFailureListener(
392+
blockingExecutor,
392393
e -> {
393394
feedbackInProgress.set(false);
394395
LogWrapper.e(TAG, "Failed to identify release", e);
@@ -399,11 +400,12 @@ private void doStartFeedback(CharSequence infoText, @Nullable Uri screenshotUri)
399400
.show();
400401
})
401402
.onSuccessTask(
402-
taskExecutor,
403+
blockingExecutor,
403404
releaseName ->
404405
// in development-mode the releaseName might be null
405406
launchFeedbackActivity(releaseName, infoText, screenshotUri)
406407
.addOnFailureListener(
408+
blockingExecutor,
407409
e -> {
408410
feedbackInProgress.set(false);
409411
LogWrapper.e(TAG, "Failed to launch feedback flow", e);

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/impl/FirebaseAppDistributionRegistrar.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
package com.google.firebase.appdistribution.impl;
1616

17-
import android.annotation.SuppressLint;
1817
import android.app.Application;
1918
import android.content.Context;
2019
import androidx.annotation.Keep;
2120
import androidx.annotation.NonNull;
2221
import com.google.firebase.FirebaseApp;
22+
import com.google.firebase.annotations.concurrent.Background;
2323
import com.google.firebase.annotations.concurrent.Blocking;
2424
import com.google.firebase.annotations.concurrent.Lightweight;
2525
import com.google.firebase.appdistribution.FirebaseAppDistribution;
@@ -34,10 +34,9 @@
3434
import java.util.Arrays;
3535
import java.util.List;
3636
import java.util.concurrent.Executor;
37-
import java.util.concurrent.Executors;
3837

3938
/**
40-
* Registers FirebaseAppDistribution.
39+
* Registers FirebaseAppDistribution and related components.
4140
*
4241
* @hide
4342
*/
@@ -49,6 +48,7 @@ public class FirebaseAppDistributionRegistrar implements ComponentRegistrar {
4948

5049
@Override
5150
public @NonNull List<Component<?>> getComponents() {
51+
Qualified<Executor> backgroundExecutor = Qualified.qualified(Background.class, Executor.class);
5252
Qualified<Executor> blockingExecutor = Qualified.qualified(Blocking.class, Executor.class);
5353
Qualified<Executor> lightweightExecutor =
5454
Qualified.qualified(Lightweight.class, Executor.class);
@@ -57,16 +57,21 @@ public class FirebaseAppDistributionRegistrar implements ComponentRegistrar {
5757
.name(LIBRARY_NAME)
5858
.add(Dependency.required(FirebaseApp.class))
5959
.add(Dependency.requiredProvider(FirebaseInstallationsApi.class))
60+
.add(Dependency.required(backgroundExecutor))
6061
.add(Dependency.required(blockingExecutor))
6162
.add(Dependency.required(lightweightExecutor))
62-
.factory(c -> buildFirebaseAppDistribution(c, blockingExecutor, lightweightExecutor))
63+
.factory(
64+
c ->
65+
buildFirebaseAppDistribution(
66+
c, backgroundExecutor, blockingExecutor, lightweightExecutor))
6367
// construct FirebaseAppDistribution instance on startup so we can register for
6468
// activity lifecycle callbacks before the API is called
6569
.alwaysEager()
6670
.build(),
6771
Component.builder(FeedbackSender.class)
6872
.add(Dependency.required(FirebaseApp.class))
6973
.add(Dependency.requiredProvider(FirebaseInstallationsApi.class))
74+
.add(Dependency.required(backgroundExecutor))
7075
.add(Dependency.required(blockingExecutor))
7176
.factory(c -> buildFeedbackSender(c, c.get(blockingExecutor)))
7277
.build(),
@@ -87,13 +92,13 @@ private FeedbackSender buildFeedbackSender(
8792
return new FeedbackSender(testerApiClient, blockingExecutor);
8893
}
8994

90-
// TODO(b/258264924): Migrate to go/firebase-android-executors
91-
@SuppressLint("ThreadPoolCreation")
9295
private FirebaseAppDistribution buildFirebaseAppDistribution(
9396
ComponentContainer container,
97+
Qualified<Executor> backgroundExecutorType,
9498
Qualified<Executor> blockingExecutorType,
9599
Qualified<Executor> lightweightExecutorType) {
96100
FirebaseApp firebaseApp = container.get(FirebaseApp.class);
101+
Executor backgroundExecutor = container.get(backgroundExecutorType);
97102
Executor blockingExecutor = container.get(blockingExecutorType);
98103
Executor lightweightExecutor = container.get(lightweightExecutorType);
99104
Context context = firebaseApp.getApplicationContext();
@@ -120,9 +125,9 @@ private FirebaseAppDistribution buildFirebaseAppDistribution(
120125
signInStorage,
121126
lifecycleNotifier,
122127
releaseIdentifier,
123-
new ScreenshotTaker(firebaseApp, lifecycleNotifier),
128+
new ScreenshotTaker(firebaseApp, lifecycleNotifier, backgroundExecutor),
124129
lightweightExecutor,
125-
Executors.newSingleThreadExecutor());
130+
blockingExecutor);
126131

127132
if (context instanceof Application) {
128133
Application firebaseApplication = (Application) context;

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/impl/ScreenshotTaker.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import java.io.FileOutputStream;
3838
import java.io.IOException;
3939
import java.util.concurrent.Executor;
40-
import java.util.concurrent.Executors;
4140

4241
/** A class that takes screenshots of the host app. */
4342
class ScreenshotTaker {
@@ -47,23 +46,15 @@ class ScreenshotTaker {
4746

4847
private final FirebaseApp firebaseApp;
4948
private final FirebaseAppDistributionLifecycleNotifier lifecycleNotifier;
50-
private final Executor taskExecutor;
49+
private final Executor backgroundExecutor;
5150

52-
// TODO(b/258264924): Migrate to go/firebase-android-executors
53-
@SuppressLint("ThreadPoolCreation")
54-
ScreenshotTaker(
55-
FirebaseApp firebaseApp, FirebaseAppDistributionLifecycleNotifier lifecycleNotifier) {
56-
this(firebaseApp, lifecycleNotifier, Executors.newSingleThreadExecutor());
57-
}
58-
59-
@VisibleForTesting
6051
ScreenshotTaker(
6152
FirebaseApp firebaseApp,
6253
FirebaseAppDistributionLifecycleNotifier lifecycleNotifier,
63-
Executor taskExecutor) {
54+
Executor backgroundExecutor) {
6455
this.firebaseApp = firebaseApp;
6556
this.lifecycleNotifier = lifecycleNotifier;
66-
this.taskExecutor = taskExecutor;
57+
this.backgroundExecutor = backgroundExecutor;
6758
}
6859

6960
/**
@@ -83,7 +74,7 @@ Task<Uri> takeScreenshot() {
8374
/** Deletes any previously taken screenshot. */
8475
Task<Void> deleteScreenshot() {
8576
return TaskUtils.runAsyncInTask(
86-
taskExecutor,
77+
backgroundExecutor,
8778
() -> {
8879
firebaseApp.getApplicationContext().deleteFile(SCREENSHOT_FILE_NAME);
8980
return null;
@@ -159,7 +150,7 @@ private Task<Uri> writeToFile(@Nullable Bitmap bitmap) {
159150
return Tasks.forResult(null);
160151
}
161152
return TaskUtils.runAsyncInTask(
162-
taskExecutor,
153+
backgroundExecutor,
163154
() -> {
164155
try (FileOutputStream outputStream = openFileOutputStream()) {
165156
// PNG is a lossless format, the compression factor (100) is ignored

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/impl/SignInStorage.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class SignInStorage {
3030
this.signInSharedPreferences =
3131
new Lazy(
3232
() ->
33+
// TODO(lkellogg): This constructs a SharedPreferences object, which touches disk
34+
// and therefore should be run on a @Background executor. This could ideally be
35+
// done by a new shared component.
3336
applicationContext.getSharedPreferences(
3437
SIGNIN_PREFERENCES_NAME, Context.MODE_PRIVATE));
3538
}

0 commit comments

Comments
 (0)