| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public final class CancellationSignal { |
| 25 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 26 |
private boolean mIsCanceled; |
| 27 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 28 |
private OnCancelListener mOnCancelListener; |
| 29 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 30 |
private ICancellationSignal mRemote; |
| 31 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 32 |
private boolean mCancelInProgress; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public CancellationSignal() { |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public boolean isCanceled() { |
| 46 |
synchronized (this) { |
| 47 |
return mIsCanceled; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public void throwIfCanceled() { |
| 57 |
if (isCanceled()) { |
| 58 |
throw new OperationCanceledException(); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public void cancel() { |
| 67 |
final OnCancelListener listener; |
| 68 |
final ICancellationSignal remote; |
| 69 |
synchronized (this) { |
| 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) { |
| 87 |
} |
| 88 |
} |
| 89 |
} finally { |
| 90 |
synchronized (this) { |
| 91 |
mCancelInProgress = false; |
| 92 |
notifyAll(); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public void setOnCancelListener(OnCancelListener listener) { |
| 114 |
synchronized (this) { |
| 115 |
waitForCancelFinishedLocked(); |
| 116 |
|
| 117 |
if (mOnCancelListener == listener) { |
| 118 |
return; |
| 119 |
} |
| 120 |
mOnCancelListener = listener; |
| 121 |
if (!mIsCanceled || listener == null) { |
| 122 |
return; |
| 123 |
} |
| 124 |
} |
| 125 |
listener.onCancel(); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public void setRemote(ICancellationSignal remote) { |
| 142 |
synchronized (this) { |
| 143 |
waitForCancelFinishedLocked(); |
| 144 |
|
| 145 |
if (mRemote == remote) { |
| 146 |
return; |
| 147 |
} |
| 148 |
mRemote = remote; |
| 149 |
if (!mIsCanceled || remote == null) { |
| 150 |
return; |
| 151 |
} |
| 152 |
} |
| 153 |
try { |
| 154 |
remote.cancel(); |
| 155 |
} catch (RemoteException ex) { |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 160 |
private void waitForCancelFinishedLocked() { |
| 161 |
while (mCancelInProgress) { |
| 162 |
try { |
| 163 |
wait(); |
| 164 |
} catch (InterruptedException ex) { |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public static ICancellationSignal createTransport() { |
| 178 |
return new Transport(); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public static CancellationSignal fromTransport(ICancellationSignal transport) { |
| 190 |
if (transport instanceof Transport) { |
| 191 |
return ((Transport)transport).mCancellationSignal; |
| 192 |
} |
| 193 |
return null; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public interface OnCancelListener { |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
void onCancel(); |
| 204 |
} |
| 205 |
|
| 206 |
private static final class Transport extends ICancellationSignal.Stub { |
| 207 |
final CancellationSignal mCancellationSignal = new CancellationSignal(); |
| 208 |
|
| 209 |
@Override |
| 210 |
public void cancel() throws RemoteException { |
| 211 |
mCancellationSignal.cancel(); |
| 212 |
} |
| 213 |
} |
| 214 |
} |