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