-
Notifications
You must be signed in to change notification settings - Fork 624
Replace getForegroundActivity with applyToForegroundActivity[Task] #3350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,14 +20,25 @@ | |
import androidx.annotation.GuardedBy; | ||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.annotation.VisibleForTesting; | ||
import com.google.android.gms.tasks.SuccessContinuation; | ||
import com.google.android.gms.tasks.Task; | ||
import com.google.android.gms.tasks.TaskCompletionSource; | ||
import com.google.android.gms.tasks.Tasks; | ||
import java.util.ArrayDeque; | ||
import java.util.Queue; | ||
import java.util.concurrent.Executor; | ||
|
||
class FirebaseAppDistributionLifecycleNotifier implements Application.ActivityLifecycleCallbacks { | ||
|
||
/** An {@link Executor} that runs tasks on the current thread. */ | ||
private static final Executor DIRECT_EXECUTOR = Runnable::run; | ||
|
||
/** A functional interface for a function that takes an activity and does something with it. */ | ||
interface ActivityConsumer<T> { | ||
void consume(Activity activity); | ||
} | ||
Comment on lines
+38
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept this as a "Consumer" instead of a "Function" like we talked about, because otherwise we would have had to change our void handlers like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing them from |
||
|
||
private static FirebaseAppDistributionLifecycleNotifier instance; | ||
private final Object lock = new Object(); | ||
|
||
|
@@ -54,7 +65,8 @@ class FirebaseAppDistributionLifecycleNotifier implements Application.ActivityLi | |
@GuardedBy("lock") | ||
private final Queue<OnActivityDestroyedListener> onDestroyedListeners = new ArrayDeque<>(); | ||
|
||
private FirebaseAppDistributionLifecycleNotifier() {} | ||
@VisibleForTesting | ||
FirebaseAppDistributionLifecycleNotifier() {} | ||
|
||
static synchronized FirebaseAppDistributionLifecycleNotifier getInstance() { | ||
if (instance == null) { | ||
|
@@ -84,13 +96,43 @@ interface OnActivityDestroyedListener { | |
} | ||
|
||
/** | ||
* Get a {@link Task} that will succeed with a result of the app's foregrounded {@link Activity}, | ||
* when one is available. | ||
* | ||
* <p>The returned task will never fail. It will instead remain pending indefinitely until some | ||
* activity comes to the foreground. | ||
* Apply a function to a foreground activity, when one is available, returning a {@link Task} that | ||
* will complete immediately after the function is applied. | ||
*/ | ||
Task<Void> applyToForegroundActivity(ActivityConsumer consumer) { | ||
return getForegroundActivity() | ||
.onSuccessTask( | ||
// Use direct executor to ensure the consumer is called while Activity is in foreground | ||
DIRECT_EXECUTOR, | ||
activity -> { | ||
try { | ||
consumer.consume(activity); | ||
return Tasks.forResult(null); | ||
} catch (Throwable t) { | ||
return Tasks.forException(FirebaseAppDistributionException.wrap(t)); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Apply a function to a foreground activity, when one is available, returning a {@link Task} that | ||
* will complete with the result of the Task returned by that function. | ||
*/ | ||
Task<Activity> getForegroundActivity() { | ||
<T> Task<T> applyToForegroundActivityTask(SuccessContinuation<Activity, T> continuation) { | ||
return getForegroundActivity() | ||
.onSuccessTask( | ||
// Use direct executor to ensure the consumer is called while Activity is in foreground | ||
DIRECT_EXECUTOR, | ||
activity -> { | ||
try { | ||
return continuation.then(activity); | ||
} catch (Throwable t) { | ||
return Tasks.forException(FirebaseAppDistributionException.wrap(t)); | ||
} | ||
}); | ||
} | ||
|
||
private Task<Activity> getForegroundActivity() { | ||
synchronized (lock) { | ||
if (currentActivity != null) { | ||
return Tasks.forResult(currentActivity); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Equivalent to the executor used internally by the Tasks API in certain cases: TaskExecutors.