Skip to content

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

Merged
merged 4 commits into from
Jan 27, 2022

Conversation

lfkellogg
Copy link
Contributor

@lfkellogg lfkellogg commented Jan 25, 2022

This allows the handler to be run immediately after the foreground activity is resumed. When relying on a Task handler, you're actually enqueueing the handler to either the main thread queue or our own executor's queue, and there's no guarantee that the activity is still in the foreground when it's called. While a race condition should be rare, it's better to avoid it if possible.

An alternative would be to pass in a "direct executor" that runs the listener in the same thread that sets the result, but this approach enforces that approach so the caller doesn't have to remember to do that.

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Jan 25, 2022

Coverage Report 1

Affected Products

  • firebase-appdistribution

    Overall coverage changed from ? (0784d9c) to 76.31% (0d896be) by ?.

    33 individual files with coverage change

    FilenameBase (0784d9c)Merge (0d896be)Diff
    AabUpdater.java?98.68%?
    ApkInstaller.java?96.88%?
    ApkUpdater.java?73.44%?
    AppDistributionRelease.java?100.00%?
    AppDistributionReleaseInternal.java?100.00%?
    AppIconSource.java?85.71%?
    AutoValue_AppDistributionRelease.java?65.45%?
    AutoValue_AppDistributionReleaseInternal.java?67.86%?
    AutoValue_UpdateProgress.java?65.96%?
    BinaryType.java?100.00%?
    Constants.java?0.00%?
    FirebaseAppDistribution.java?95.60%?
    FirebaseAppDistributionException.java?88.24%?
    FirebaseAppDistributionFileProvider.java?0.00%?
    FirebaseAppDistributionLifecycleNotifier.java?72.53%?
    FirebaseAppDistributionNotificationsManager.java?92.68%?
    FirebaseAppDistributionRegistrar.java?90.91%?
    FirebaseAppDistributionTesterApiClient.java?91.21%?
    HttpsUrlConnectionFactory.java?50.00%?
    InstallActivity.java?2.53%?
    LogWrapper.java?64.71%?
    NewReleaseFetcher.java?87.95%?
    OnProgressListener.java?0.00%?
    ReleaseIdentificationUtils.java?15.52%?
    ReleaseUtils.java?76.92%?
    SignInResultActivity.java?0.00%?
    SignInStorage.java?57.14%?
    TaskUtils.java?92.86%?
    TesterSignInManager.java?93.75%?
    UpdateProgress.java?100.00%?
    UpdateStatus.java?100.00%?
    UpdateTask.java?100.00%?
    UpdateTaskImpl.java?82.81%?

Test Logs

Notes

  • Commit (0d896be) is created by Prow via merging PR base commit (0784d9c) and head commit (bd2a8e0).
  • Run gradle <product>:checkCoverage to produce HTML coverage reports locally. After gradle commands finished, report files can be found under <product-build-dir>/reports/jacoco/.

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/crYu3NQ7Rf.html

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Jan 25, 2022

Size Report 1

Affected Products

  • firebase-appdistribution

    TypeBase (0784d9c)Merge (0d896be)Diff
    aar124 kB123 kB-1.51 kB (-1.2%)
    apk (release)1.55 MB1.55 MB-260 B (-0.0%)

Test Logs

Notes

  • Commit (0d896be) is created by Prow via merging PR base commit (0784d9c) and head commit (bd2a8e0).

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/FKcGrhJbRk.html

@lfkellogg lfkellogg force-pushed the lk/get-foreground-with-consumer branch from dcddb94 to fc64da9 Compare January 25, 2022 21:05
Base automatically changed from lk/aab-intent-from-background to master January 25, 2022 21:33
@lfkellogg lfkellogg force-pushed the lk/get-foreground-with-consumer branch from fc64da9 to 48347d2 Compare January 25, 2022 21:40
@lfkellogg lfkellogg force-pushed the lk/get-foreground-with-consumer branch from 48347d2 to e585921 Compare January 25, 2022 22:14
@lfkellogg
Copy link
Contributor Author

/test copyright-check


@Before
public void setup() {
activity = Robolectric.buildActivity(TestActivity.class).create().get();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome tests!

import java.util.ArrayDeque;
import java.util.Queue;

class FirebaseAppDistributionLifecycleNotifier implements Application.ActivityLifecycleCallbacks {

/** A functional interface for a function that takes an activity and does something with it. */
interface ActivityConsumer {
void consume(Activity activity) throws FirebaseAppDistributionException;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Would like to chat about this consume stuff since I am not familiar with it.

@lfkellogg
Copy link
Contributor Author

/retest

@lfkellogg
Copy link
Contributor Author

/test smoke-tests

@lfkellogg lfkellogg force-pushed the lk/get-foreground-with-consumer branch from a368452 to 27db9e6 Compare January 26, 2022 22:10
@lfkellogg lfkellogg changed the title Add getForegroundActivity overload with consumer Replace getForegroundActivity with applyToForegroundActivity[Task] Jan 27, 2022
Comment on lines +38 to +40
interface ActivityConsumer<T> {
void consume(Activity activity);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 openRedirectUrlInPlay to return a meaningless value, which felt unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing them from void to Void solves that problem, but if there's no use case for any return values right now, keeping it as void consume(...) works for me.

}

/** A {@link SuccessContinuation} that takes an activity and returns a new {@link Task}. */
interface ActivityContinuation<T> extends SuccessContinuation<Activity, T> {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leveraging the Task API's SuccessContinuation interface here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't decide if extending SuccessContinuation to bind to a specific type makes it cleaner (because there's one less type to worry about) or makes my head hurt (because I have to remember that there's an is a relationship here). I'm ok keeping it like this if y'all think it is better. 🙃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're right I actually like just keeping it as a SuccessContinuation. We're still enforcing that it accepts an Activity. No need to add another type 👍

Updated.

*/
Task<Activity> getForegroundActivity() {
<T> Task<T> applyToForegroundActivityTask(ActivityContinuation<T> continuation) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I named this applyToForegroundActivityTask as a parallel to onSuccessTask, since it seemed analogous: it accepts a function that returns a Task (a continuation).


class FirebaseAppDistributionLifecycleNotifier implements Application.ActivityLifecycleCallbacks {

/** An {@link Executor} that runs tasks on the current thread. */
private static final Executor DIRECT_EXECUTOR = Runnable::run;
Copy link
Contributor Author

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.

@lfkellogg
Copy link
Contributor Author

/test smoke-tests

2 similar comments
@lfkellogg
Copy link
Contributor Author

/test smoke-tests

@lfkellogg
Copy link
Contributor Author

/test smoke-tests

Copy link
Contributor

@mrichards mrichards left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some tricky APIs in here that are hard to grok (namely applyToForegroundActivityTask), but I like that you tried to follow the convention from the Tasks API where possible. Let's give this a shot and see how it goes.

Comment on lines +38 to +40
interface ActivityConsumer<T> {
void consume(Activity activity);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing them from void to Void solves that problem, but if there's no use case for any return values right now, keeping it as void consume(...) works for me.

}

/** A {@link SuccessContinuation} that takes an activity and returns a new {@link Task}. */
interface ActivityContinuation<T> extends SuccessContinuation<Activity, T> {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't decide if extending SuccessContinuation to bind to a specific type makes it cleaner (because there's one less type to worry about) or makes my head hurt (because I have to remember that there's an is a relationship here). I'm ok keeping it like this if y'all think it is better. 🙃

@lfkellogg
Copy link
Contributor Author

/test smoke-tests

@google-oss-bot
Copy link
Contributor

@lfkellogg: The following test failed, say /retest to rerun them all:

Test name Commit Details Rerun command
smoke-tests bd2a8e0 link /test smoke-tests

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@lfkellogg lfkellogg merged commit d131c6e into master Jan 27, 2022
@lfkellogg lfkellogg deleted the lk/get-foreground-with-consumer branch January 27, 2022 21:56
@firebase firebase locked and limited conversation to collaborators Feb 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants