Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit 412d1f51 authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

Update API and finish Tasks implementation

parent 17b8371b
Loading
Loading
Loading
Loading
+2 −3
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2016, JetBrains s.r.o.
 * SPDX-FileCopyrightText: 2021, microG Project Team
 * SPDX-FileCopyrightText: 2016 JetBrains s.r.o.
 * SPDX-FileCopyrightText: 2021 microG Project Team
 * SPDX-License-Identifier: Apache-2.0
 */

package com.google.android.gms.tasks

import com.google.android.gms.tasks.*
import kotlinx.coroutines.*
import kotlin.coroutines.*

+61 −1
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2020, microG Project Team
 * SPDX-FileCopyrightText: 2020 microG Project Team
 * SPDX-License-Identifier: Apache-2.0 AND CC-BY-4.0
 * Notice: Portions of this file are reproduced from work created and shared by Google and used
 *         according to terms described in the Creative Commons 4.0 Attribution License.
@@ -10,6 +10,66 @@ package com.google.android.gms.tasks;

import org.microg.gms.common.PublicApi;

/**
 * Propagates notification that operations should be canceled.
 * <p/>
 * Developers writing methods that return a Task should take a {@code CancellationToken} as a parameter if they wish to
 * make the Task cancelable (see below code snippet). A {@code CancellationToken} can only be created by creating a new
 * instance of {@link CancellationTokenSource}. {@code CancellationToken} is immutable and must be canceled by calling
 * {@link CancellationTokenSource#cancel()} on the {@link CancellationTokenSource} that creates it. It can only be
 * canceled once. If canceled, it should not be passed to future operations.
 * <p/>
 * When {@link CancellationTokenSource#cancel()} is called, all the Tasks with the {@code CancellationToken} from that
 * {@link CancellationTokenSource} will be canceled. This operation only flags those Tasks as canceled, and the API
 * author is responsible for stopping whatever the Task is actually doing to free up the resources.
 * <p/>
 * Cancellable {@link Task} example:
 * <pre>
 * public Task<Integer> doSomething(CancellationToken token) {
 *
 *     // Attach a listener that will be called once cancellation is requested.
 *     token.onCanceledRequested(new OnTokenCanceledListener() {
 *         &#64;Override
 *         public void onCanceled() {
 *             // Some other operations to cancel this Task, such as free resources...
 *         }
 *     });
 *
 *     final TaskCompletionSource<Integer> tcs = new TaskCompletionSource<>(token);
 *
 *     // do something...
 *
 * }
 *
 * CancellationTokenSource cts = new CancellationTokenSource();
 * Task<Integer> task = doSomething(cts.getToken());
 * cts.cancel();
 * </pre>
 * Cancellable {@link Task} example in {@link android.app.Activity} context:
 * <pre>
 * public class MyActivity extends Activity {
 *     // ...
 *
 *     &#64;Override
 *     public void onStart() {
 *         super.onStart();
 *
 *         // Typically use one cancellation source per lifecycle.
 *         cancellationSource = new TaskCancellationSource();
 *
 *         // That source's token can be passed to multiple calls.
 *         doSomethingCancellable(cancellationSource.getToken())
 *             .onSuccessTask(result -> doSomethingElse(result, cancellationSource.getToken()));
 *     }
 *
 *     &#64;Override
 *     public void onStop() {
 *         super.onStop();
 *         cancellationSource.cancel();
 *     }
 * }
 * </pre>
 */
@PublicApi
public abstract class CancellationToken {
    /**
+7 −5
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2016, microG Project Team
 * SPDX-FileCopyrightText: 2016 microG Project Team
 * SPDX-License-Identifier: Apache-2.0 AND CC-BY-4.0
 * Notice: Portions of this file are reproduced from work created and shared by Google and used
 *         according to terms described in the Creative Commons 4.0 Attribution License.
@@ -28,8 +28,9 @@ public interface Continuation<TResult, TContinuationResult> {
     * <p/>
     * To suppress specific failures call {@link Task#getResult(Class)} and catch the exception
     * types of interest:
     * <pre>task.continueWith(new Continuation<String, String>() {
     *     @Override
     * <pre>
     * task.continueWith(new Continuation<String, String>() {
     *     &#64;Override
     *     public String then(Task<String> task) {
     *         try {
     *             return task.getResult(IOException.class);
@@ -42,8 +43,9 @@ public interface Continuation<TResult, TContinuationResult> {
     * }</pre>
     * <p/>
     * To suppress all failures guard any calls to {@link Task#getResult()} with {@link Task#isSuccessful()}:
     * <pre>task.continueWith(new Continuation<String, String>() {
     *     @Override
     * <pre>
     * task.continueWith(new Continuation<String, String>() {
     *     &#64;Override
     *     public String then(Task<String> task) {
     *         if (task.isSuccessful()) {
     *             return task.getResult();
+16 −2
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2020, microG Project Team
 * SPDX-FileCopyrightText: 2020 microG Project Team
 * SPDX-License-Identifier: Apache-2.0 AND CC-BY-4.0
 * Notice: Portions of this file are reproduced from work created and shared by Google and used
 *         according to terms described in the Creative Commons 4.0 Attribution License.
@@ -19,7 +19,21 @@ public interface SuccessContinuation<TResult, TContinuationResult> {
    /**
     * Returns the result of applying this SuccessContinuation to Task.
     * <p>
     * The SuccessContinuation only happens then the Task is successful. If the previous Task fails, the onSuccessTask continuation will be skipped and failure listeners will be invoked.
     * The SuccessContinuation only happens then the Task is successful. If the previous Task fails, the onSuccessTask
     * continuation will be skipped and failure listeners will be invoked.
     * <p>
     * <pre>
     * private Task<String> doSomething(String string) {
     *     // do something
     * }
     * task.onSuccessTask(new SuccessContinuation<String, String>() {
     *     &#64;NonNull
     *     &#64;Override
     *     public Task<String> then(String string) {
     *         return doSomething(string);
     *     }
     * });
     * </pre>
     *
     * @param result the result of completed Task
     * @throws Exception if the result couldn't be produced
+11 −1
Original line number Diff line number Diff line
/*
 * SPDX-FileCopyrightText: 2016, microG Project Team
 * SPDX-FileCopyrightText: 2016 microG Project Team
 * SPDX-License-Identifier: Apache-2.0 AND CC-BY-4.0
 * Notice: Portions of this file are reproduced from work created and shared by Google and used
 *         according to terms described in the Creative Commons 4.0 Attribution License.
@@ -177,6 +177,8 @@ public abstract class Task<TResult> {
     * Continuation to this Task.
     * <p/>
     * The Continuation will be called on the main application thread.
     * <p/>
     * If the previous Task is canceled, the returned Task will also be canceled and the Continuation would not execute.
     *
     * @see Continuation#then(Task)
     */
@@ -186,6 +188,8 @@ public abstract class Task<TResult> {

    /**
     * Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.
     * <p/>
     * If the previous Task is canceled, the returned Task will also be canceled and the Continuation would not execute.
     *
     * @param executor the executor to use to call the Continuation
     * @see Continuation#then(Task)
@@ -199,6 +203,9 @@ public abstract class Task<TResult> {
     * Continuation to this Task.
     * <p/>
     * The Continuation will be called on the main application thread.
     * <p/>
     * If the previous Task is canceled, the Continuation would still execute and task.isCanceled() is true can be
     * observed in the Continuation.
     *
     * @see Continuation#then(Task)
     */
@@ -208,6 +215,9 @@ public abstract class Task<TResult> {

    /**
     * Returns a new Task that will be completed with the result of applying the specified Continuation to this Task.
     * <p/>
     * If the previous Task is canceled, the Continuation would still execute and task.isCanceled() is true can be
     * observed in the Continuation.
     *
     * @param executor the executor to use to call the Continuation
     * @see Continuation#then(Task)
Loading