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  */
16 package android.accounts;
17 
18 import java.util.concurrent.TimeUnit;
19 import java.io.IOException;
20 
21 /**
22  * A <tt>AccountManagerFuture</tt> represents the result of an asynchronous
23  * {@link AccountManager} call.  Methods are provided to check if the computation is
24  * complete, to wait for its completion, and to retrieve the result of
25  * the computation.  The result can only be retrieved using method
26  * <tt>get</tt> when the computation has completed, blocking if
27  * necessary until it is ready.  Cancellation is performed by the
28  * <tt>cancel</tt> method.  Additional methods are provided to
29  * determine if the task completed normally or was cancelled. Once a
30  * computation has completed, the computation cannot be cancelled.
31  * If you would like to use a <tt>Future</tt> for the sake
32  * of cancellability but not provide a usable result, you can
33  * declare types of the form <tt>Future&lt;?&gt;</tt> and
34  * return <tt>null</tt> as a result of the underlying task.
35  */
36 public interface AccountManagerFuture<V> {
37     /**
38      * Attempts to cancel execution of this task.  This attempt will
39      * fail if the task has already completed, has already been cancelled,
40      * or could not be cancelled for some other reason. If successful,
41      * and this task has not started when <tt>cancel</tt> is called,
42      * this task should never run.  If the task has already started,
43      * then the <tt>mayInterruptIfRunning</tt> parameter determines
44      * whether the thread executing this task should be interrupted in
45      * an attempt to stop the task.
46      *
47      * <p>After this method returns, subsequent calls to {@link #isDone} will
48      * always return <tt>true</tt>.  Subsequent calls to {@link #isCancelled}
49      * will always return <tt>true</tt> if this method returned <tt>true</tt>.
50      *
51      * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
52      * task should be interrupted; otherwise, in-progress tasks are allowed
53      * to complete
54      * @return <tt>false</tt> if the task could not be cancelled,
55      * typically because it has already completed normally;
56      * <tt>true</tt> otherwise
57      */
cancel(boolean mayInterruptIfRunning)58     boolean cancel(boolean mayInterruptIfRunning);
59 
60     /**
61      * Returns <tt>true</tt> if this task was cancelled before it completed
62      * normally.
63      *
64      * @return <tt>true</tt> if this task was cancelled before it completed
65      */
isCancelled()66     boolean isCancelled();
67 
68     /**
69      * Returns <tt>true</tt> if this task completed.
70      *
71      * Completion may be due to normal termination, an exception, or
72      * cancellation -- in all of these cases, this method will return
73      * <tt>true</tt>.
74      *
75      * @return <tt>true</tt> if this task completed
76      */
isDone()77     boolean isDone();
78 
79     /**
80      * Accessor for the future result the {@link AccountManagerFuture} represents. This
81      * call will block until the result is available. In order to check if the result is
82      * available without blocking, one may call {@link #isDone()} and  {@link #isCancelled()}.
83      * If the request that generated this result fails or is canceled then an exception
84      * will be thrown rather than the call returning normally.
85      * @return the actual result
86      * @throws android.accounts.OperationCanceledException if the request was canceled for any
87      * reason (including if it is forbidden
88      * by policy to modify an account (of that type))
89      * @throws android.accounts.AuthenticatorException if there was an error communicating with
90      * the authenticator or if the authenticator returned an invalid response
91      * @throws java.io.IOException if the authenticator returned an error response that indicates
92      * that it encountered an IOException while communicating with the authentication server
93      */
getResult()94     V getResult() throws OperationCanceledException, IOException, AuthenticatorException;
95 
96     /**
97      * Accessor for the future result the {@link AccountManagerFuture} represents. This
98      * call will block until the result is available. In order to check if the result is
99      * available without blocking, one may call {@link #isDone()} and  {@link #isCancelled()}.
100      * If the request that generated this result fails or is canceled then an exception
101      * will be thrown rather than the call returning normally. If a timeout is specified then
102      * the request will automatically be canceled if it does not complete in that amount of time.
103      * @param timeout the maximum time to wait
104      * @param unit the time unit of the timeout argument. This must not be null.
105      * @return the actual result
106      * @throws android.accounts.OperationCanceledException if the request was canceled for any
107      * reason
108      * @throws android.accounts.AuthenticatorException if there was an error communicating with
109      * the authenticator or if the authenticator returned an invalid response
110      * @throws java.io.IOException if the authenticator returned an error response that indicates
111      * that it encountered an IOException while communicating with the authentication server
112      */
getResult(long timeout, TimeUnit unit)113     V getResult(long timeout, TimeUnit unit)
114             throws OperationCanceledException, IOException, AuthenticatorException;
115 }