Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 17 | package android.os; |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 18 | |
Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 19 | import android.os.ICancellationSignal; |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 20 | |
Artur Satayev | 70507ed | 2019-07-29 13:18:27 +0100 | [diff] [blame] | 21 | import dalvik.annotation.compat.UnsupportedAppUsage; |
| 22 | |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 23 | /** |
| 24 | * Provides the ability to cancel an operation in progress. |
| 25 | */ |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 26 | public final class CancellationSignal { |
Artur Satayev | 70507ed | 2019-07-29 13:18:27 +0100 | [diff] [blame] | 27 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 28 | private boolean mIsCanceled; |
Artur Satayev | 70507ed | 2019-07-29 13:18:27 +0100 | [diff] [blame] | 29 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 30 | private OnCancelListener mOnCancelListener; |
Artur Satayev | 70507ed | 2019-07-29 13:18:27 +0100 | [diff] [blame] | 31 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 32 | private ICancellationSignal mRemote; |
Artur Satayev | 70507ed | 2019-07-29 13:18:27 +0100 | [diff] [blame] | 33 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Jeff Brown | aeee2f5 | 2012-04-26 17:36:12 -0700 | [diff] [blame] | 34 | private boolean mCancelInProgress; |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 35 | |
| 36 | /** |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 37 | * Creates a cancellation signal, initially not canceled. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 38 | */ |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 39 | public CancellationSignal() { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Returns true if the operation has been canceled. |
| 44 | * |
| 45 | * @return True if the operation has been canceled. |
| 46 | */ |
| 47 | public boolean isCanceled() { |
| 48 | synchronized (this) { |
| 49 | return mIsCanceled; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Throws {@link OperationCanceledException} if the operation has been canceled. |
| 55 | * |
| 56 | * @throws OperationCanceledException if the operation has been canceled. |
| 57 | */ |
| 58 | public void throwIfCanceled() { |
| 59 | if (isCanceled()) { |
| 60 | throw new OperationCanceledException(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 65 | * Cancels the operation and signals the cancellation listener. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 66 | * If the operation has not yet started, then it will be canceled as soon as it does. |
| 67 | */ |
| 68 | public void cancel() { |
Jeff Brown | aeee2f5 | 2012-04-26 17:36:12 -0700 | [diff] [blame] | 69 | final OnCancelListener listener; |
| 70 | final ICancellationSignal remote; |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 71 | synchronized (this) { |
Jeff Brown | aeee2f5 | 2012-04-26 17:36:12 -0700 | [diff] [blame] | 72 | if (mIsCanceled) { |
| 73 | return; |
| 74 | } |
| 75 | mIsCanceled = true; |
| 76 | mCancelInProgress = true; |
| 77 | listener = mOnCancelListener; |
| 78 | remote = mRemote; |
| 79 | } |
| 80 | |
| 81 | try { |
| 82 | if (listener != null) { |
| 83 | listener.onCancel(); |
| 84 | } |
| 85 | if (remote != null) { |
| 86 | try { |
| 87 | remote.cancel(); |
| 88 | } catch (RemoteException ex) { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 89 | } |
Jeff Brown | aeee2f5 | 2012-04-26 17:36:12 -0700 | [diff] [blame] | 90 | } |
| 91 | } finally { |
| 92 | synchronized (this) { |
| 93 | mCancelInProgress = false; |
| 94 | notifyAll(); |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 100 | * Sets the cancellation listener to be called when canceled. |
| 101 | * |
| 102 | * This method is intended to be used by the recipient of a cancellation signal |
| 103 | * such as a database or a content provider to handle cancellation requests |
| 104 | * while performing a long-running operation. This method is not intended to be |
| 105 | * used by applications themselves. |
| 106 | * |
| 107 | * If {@link CancellationSignal#cancel} has already been called, then the provided |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 108 | * listener is invoked immediately. |
| 109 | * |
Jeff Brown | aeee2f5 | 2012-04-26 17:36:12 -0700 | [diff] [blame] | 110 | * This method is guaranteed that the listener will not be called after it |
| 111 | * has been removed. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 112 | * |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 113 | * @param listener The cancellation listener, or null to remove the current listener. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 114 | */ |
| 115 | public void setOnCancelListener(OnCancelListener listener) { |
| 116 | synchronized (this) { |
Jeff Brown | aeee2f5 | 2012-04-26 17:36:12 -0700 | [diff] [blame] | 117 | waitForCancelFinishedLocked(); |
| 118 | |
| 119 | if (mOnCancelListener == listener) { |
| 120 | return; |
| 121 | } |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 122 | mOnCancelListener = listener; |
Jeff Brown | aeee2f5 | 2012-04-26 17:36:12 -0700 | [diff] [blame] | 123 | if (!mIsCanceled || listener == null) { |
| 124 | return; |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
Jeff Brown | aeee2f5 | 2012-04-26 17:36:12 -0700 | [diff] [blame] | 127 | listener.onCancel(); |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sets the remote transport. |
| 132 | * |
Jeff Brown | aeee2f5 | 2012-04-26 17:36:12 -0700 | [diff] [blame] | 133 | * If {@link CancellationSignal#cancel} has already been called, then the provided |
| 134 | * remote transport is canceled immediately. |
| 135 | * |
| 136 | * This method is guaranteed that the remote transport will not be called after it |
| 137 | * has been removed. |
| 138 | * |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 139 | * @param remote The remote transport, or null to remove. |
| 140 | * |
| 141 | * @hide |
| 142 | */ |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 143 | public void setRemote(ICancellationSignal remote) { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 144 | synchronized (this) { |
Jeff Brown | aeee2f5 | 2012-04-26 17:36:12 -0700 | [diff] [blame] | 145 | waitForCancelFinishedLocked(); |
| 146 | |
| 147 | if (mRemote == remote) { |
| 148 | return; |
| 149 | } |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 150 | mRemote = remote; |
Jeff Brown | aeee2f5 | 2012-04-26 17:36:12 -0700 | [diff] [blame] | 151 | if (!mIsCanceled || remote == null) { |
| 152 | return; |
| 153 | } |
| 154 | } |
| 155 | try { |
| 156 | remote.cancel(); |
| 157 | } catch (RemoteException ex) { |
| 158 | } |
| 159 | } |
| 160 | |
Artur Satayev | 70507ed | 2019-07-29 13:18:27 +0100 | [diff] [blame] | 161 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Jeff Brown | aeee2f5 | 2012-04-26 17:36:12 -0700 | [diff] [blame] | 162 | private void waitForCancelFinishedLocked() { |
| 163 | while (mCancelInProgress) { |
| 164 | try { |
| 165 | wait(); |
| 166 | } catch (InterruptedException ex) { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Creates a transport that can be returned back to the caller of |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 173 | * a Binder function and subsequently used to dispatch a cancellation signal. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 174 | * |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 175 | * @return The new cancellation signal transport. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 176 | * |
| 177 | * @hide |
| 178 | */ |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 179 | public static ICancellationSignal createTransport() { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 180 | return new Transport(); |
| 181 | } |
| 182 | |
| 183 | /** |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 184 | * Given a locally created transport, returns its associated cancellation signal. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 185 | * |
| 186 | * @param transport The locally created transport, or null if none. |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 187 | * @return The associated cancellation signal, or null if none. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 188 | * |
| 189 | * @hide |
| 190 | */ |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 191 | public static CancellationSignal fromTransport(ICancellationSignal transport) { |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 192 | if (transport instanceof Transport) { |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 193 | return ((Transport)transport).mCancellationSignal; |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 194 | } |
| 195 | return null; |
| 196 | } |
| 197 | |
| 198 | /** |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 199 | * Listens for cancellation. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 200 | */ |
| 201 | public interface OnCancelListener { |
| 202 | /** |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 203 | * Called when {@link CancellationSignal#cancel} is invoked. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 204 | */ |
| 205 | void onCancel(); |
| 206 | } |
| 207 | |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 208 | private static final class Transport extends ICancellationSignal.Stub { |
| 209 | final CancellationSignal mCancellationSignal = new CancellationSignal(); |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 210 | |
| 211 | @Override |
| 212 | public void cancel() throws RemoteException { |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 213 | mCancellationSignal.cancel(); |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |