1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package android.accounts;
17
18import java.util.concurrent.Future;
19import java.util.concurrent.TimeUnit;
20import java.util.concurrent.ExecutionException;
21import java.util.concurrent.TimeoutException;
22import java.io.IOException;
23
24/**
25 * A <tt>AccountManagerFuture</tt> represents the result of an asynchronous
26 * {@link AccountManager} call.  Methods are provided to check if the computation is
27 * complete, to wait for its completion, and to retrieve the result of
28 * the computation.  The result can only be retrieved using method
29 * <tt>get</tt> when the computation has completed, blocking if
30 * necessary until it is ready.  Cancellation is performed by the
31 * <tt>cancel</tt> method.  Additional methods are provided to
32 * determine if the task completed normally or was cancelled. Once a
33 * computation has completed, the computation cannot be cancelled.
34 * If you would like to use a <tt>Future</tt> for the sake
35 * of cancellability but not provide a usable result, you can
36 * declare types of the form <tt>Future&lt;?&gt;</tt> and
37 * return <tt>null</tt> as a result of the underlying task.
38 */
39public interface AccountManagerFuture<V> {
40    /**
41     * Attempts to cancel execution of this task.  This attempt will
42     * fail if the task has already completed, has already been cancelled,
43     * or could not be cancelled for some other reason. If successful,
44     * and this task has not started when <tt>cancel</tt> is called,
45     * this task should never run.  If the task has already started,
46     * then the <tt>mayInterruptIfRunning</tt> parameter determines
47     * whether the thread executing this task should be interrupted in
48     * an attempt to stop the task.
49     *
50     * <p>After this method returns, subsequent calls to {@link #isDone} will
51     * always return <tt>true</tt>.  Subsequent calls to {@link #isCancelled}
52     * will always return <tt>true</tt> if this method returned <tt>true</tt>.
53     *
54     * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
55     * task should be interrupted; otherwise, in-progress tasks are allowed
56     * to complete
57     * @return <tt>false</tt> if the task could not be cancelled,
58     * typically because it has already completed normally;
59     * <tt>true</tt> otherwise
60     */
61    boolean cancel(boolean mayInterruptIfRunning);
62
63    /**
64     * Returns <tt>true</tt> if this task was cancelled before it completed
65     * normally.
66     *
67     * @return <tt>true</tt> if this task was cancelled before it completed
68     */
69    boolean isCancelled();
70
71    /**
72     * Returns <tt>true</tt> if this task completed.
73     *
74     * Completion may be due to normal termination, an exception, or
75     * cancellation -- in all of these cases, this method will return
76     * <tt>true</tt>.
77     *
78     * @return <tt>true</tt> if this task completed
79     */
80    boolean isDone();
81
82    /**
83     * Accessor for the future result the {@link AccountManagerFuture} represents. This
84     * call will block until the result is available. In order to check if the result is
85     * available without blocking, one may call {@link #isDone()} and  {@link #isCancelled()}.
86     * If the request that generated this result fails or is canceled then an exception
87     * will be thrown rather than the call returning normally.
88     * @return the actual result
89     * @throws android.accounts.OperationCanceledException if the request was canceled for any
90     * reason
91     * @throws android.accounts.AuthenticatorException if there was an error communicating with
92     * the authenticator or if the authenticator returned an invalid response
93     * @throws java.io.IOException if the authenticator returned an error response that indicates
94     * that it encountered an IOException while communicating with the authentication server
95     */
96    V getResult() throws OperationCanceledException, IOException, AuthenticatorException;
97
98    /**
99     * Accessor for the future result the {@link AccountManagerFuture} represents. This
100     * call will block until the result is available. In order to check if the result is
101     * available without blocking, one may call {@link #isDone()} and  {@link #isCancelled()}.
102     * If the request that generated this result fails or is canceled then an exception
103     * will be thrown rather than the call returning normally. If a timeout is specified then
104     * the request will automatically be canceled if it does not complete in that amount of time.
105     * @param timeout the maximum time to wait
106     * @param unit the time unit of the timeout argument. This must not be null.
107     * @return the actual result
108     * @throws android.accounts.OperationCanceledException if the request was canceled for any
109     * reason
110     * @throws android.accounts.AuthenticatorException if there was an error communicating with
111     * the authenticator or if the authenticator returned an invalid response
112     * @throws java.io.IOException if the authenticator returned an error response that indicates
113     * that it encountered an IOException while communicating with the authentication server
114     */
115    V getResult(long timeout, TimeUnit unit)
116            throws OperationCanceledException, IOException, AuthenticatorException;
117}