Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 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 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 18 | import java.util.concurrent.TimeUnit; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | |
| 21 | /** |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 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<?></tt> and |
| 34 | * return <tt>null</tt> as a result of the underlying task. |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 35 | */ |
Fred Quintana | f7ae77c | 2009-10-02 17:19:31 -0700 | [diff] [blame] | 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 | */ |
| 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 | */ |
| 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 | */ |
| 77 | boolean isDone(); |
| 78 | |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 79 | /** |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 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 |
| 88 | * @throws android.accounts.AuthenticatorException if there was an error communicating with |
| 89 | * the authenticator or if the authenticator returned an invalid response |
| 90 | * @throws java.io.IOException if the authenticator returned an error response that indicates |
| 91 | * that it encountered an IOException while communicating with the authentication server |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 92 | */ |
| 93 | V getResult() throws OperationCanceledException, IOException, AuthenticatorException; |
| 94 | |
| 95 | /** |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 96 | * Accessor for the future result the {@link AccountManagerFuture} represents. This |
| 97 | * call will block until the result is available. In order to check if the result is |
| 98 | * available without blocking, one may call {@link #isDone()} and {@link #isCancelled()}. |
| 99 | * If the request that generated this result fails or is canceled then an exception |
| 100 | * will be thrown rather than the call returning normally. If a timeout is specified then |
| 101 | * the request will automatically be canceled if it does not complete in that amount of time. |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 102 | * @param timeout the maximum time to wait |
Fred Quintana | 756b735 | 2009-10-21 13:43:10 -0700 | [diff] [blame] | 103 | * @param unit the time unit of the timeout argument. This must not be null. |
| 104 | * @return the actual result |
| 105 | * @throws android.accounts.OperationCanceledException if the request was canceled for any |
| 106 | * reason |
| 107 | * @throws android.accounts.AuthenticatorException if there was an error communicating with |
| 108 | * the authenticator or if the authenticator returned an invalid response |
| 109 | * @throws java.io.IOException if the authenticator returned an error response that indicates |
| 110 | * that it encountered an IOException while communicating with the authentication server |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 111 | */ |
| 112 | V getResult(long timeout, TimeUnit unit) |
| 113 | throws OperationCanceledException, IOException, AuthenticatorException; |
Fred Quintana | ffd0cb04 | 2009-08-15 21:45:26 -0700 | [diff] [blame] | 114 | } |