| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.infra; |
| 18 |
|
| 19 |
import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.app.ActivityManager; |
| 24 |
import android.app.ApplicationExitInfo; |
| 25 |
import android.app.IActivityManager; |
| 26 |
import android.content.ComponentName; |
| 27 |
import android.content.Context; |
| 28 |
import android.content.Intent; |
| 29 |
import android.content.ServiceConnection; |
| 30 |
import android.content.pm.ParceledListSlice; |
| 31 |
import android.os.Handler; |
| 32 |
import android.os.IBinder; |
| 33 |
import android.os.IBinder.DeathRecipient; |
| 34 |
import android.os.IInterface; |
| 35 |
import android.os.RemoteException; |
| 36 |
import android.os.SystemClock; |
| 37 |
import android.os.UserHandle; |
| 38 |
import android.util.Slog; |
| 39 |
import android.util.TimeUtils; |
| 40 |
|
| 41 |
import com.android.internal.annotations.GuardedBy; |
| 42 |
|
| 43 |
import java.io.PrintWriter; |
| 44 |
import java.lang.ref.WeakReference; |
| 45 |
import java.util.ArrayList; |
| 46 |
import java.util.List; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
@Deprecated |
| 72 |
public abstract class AbstractRemoteService<S extends AbstractRemoteService<S, I>, |
| 73 |
I extends IInterface> implements DeathRecipient { |
| 74 |
private static final int SERVICE_NOT_EXIST = -1; |
| 75 |
private static final int MSG_BIND = 1; |
| 76 |
private static final int MSG_UNBIND = 2; |
| 77 |
|
| 78 |
public static final long PERMANENT_BOUND_TIMEOUT_MS = 0; |
| 79 |
|
| 80 |
protected static final int LAST_PRIVATE_MSG = MSG_UNBIND; |
| 81 |
|
| 82 |
|
| 83 |
public final boolean mVerbose; |
| 84 |
|
| 85 |
protected final String mTag = getClass().getSimpleName(); |
| 86 |
protected final Handler mHandler; |
| 87 |
protected final ComponentName mComponentName; |
| 88 |
|
| 89 |
private final Context mContext; |
| 90 |
private final Intent mIntent; |
| 91 |
private final VultureCallback<S> mVultureCallback; |
| 92 |
private final int mUserId; |
| 93 |
private final ServiceConnection mServiceConnection = new RemoteServiceConnection(); |
| 94 |
private final int mBindingFlags; |
| 95 |
protected I mService; |
| 96 |
|
| 97 |
private boolean mBound; |
| 98 |
private boolean mConnecting; |
| 99 |
private boolean mDestroyed; |
| 100 |
private boolean mServiceDied; |
| 101 |
private boolean mCompleted; |
| 102 |
|
| 103 |
|
| 104 |
private long mNextUnbind; |
| 105 |
private int mServiceExitReason; |
| 106 |
private int mServiceExitSubReason; |
| 107 |
|
| 108 |
|
| 109 |
protected final ArrayList<BasePendingRequest<S, I>> mUnfinishedRequests = new ArrayList<>(); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public interface VultureCallback<T> { |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
void onServiceDied(T service); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
AbstractRemoteService(@NonNull Context context, @NonNull String serviceInterface, |
| 127 |
@NonNull ComponentName componentName, int userId, @NonNull VultureCallback<S> callback, |
| 128 |
@NonNull Handler handler, int bindingFlags, boolean verbose) { |
| 129 |
mContext = context; |
| 130 |
mVultureCallback = callback; |
| 131 |
mVerbose = verbose; |
| 132 |
mComponentName = componentName; |
| 133 |
mIntent = new Intent(serviceInterface).setComponent(mComponentName); |
| 134 |
mUserId = userId; |
| 135 |
mHandler = new Handler(handler.getLooper()); |
| 136 |
mBindingFlags = bindingFlags; |
| 137 |
mServiceExitReason = SERVICE_NOT_EXIST; |
| 138 |
mServiceExitSubReason = SERVICE_NOT_EXIST; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public final void destroy() { |
| 145 |
mHandler.sendMessage(obtainMessage(AbstractRemoteService::handleDestroy, this)); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public final boolean isDestroyed() { |
| 152 |
return mDestroyed; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
@NonNull |
| 159 |
public final ComponentName getComponentName() { |
| 160 |
return mComponentName; |
| 161 |
} |
| 162 |
|
| 163 |
private void handleOnConnectedStateChangedInternal(boolean connected) { |
| 164 |
handleOnConnectedStateChanged(connected); |
| 165 |
if (connected) { |
| 166 |
handlePendingRequests(); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
abstract void handlePendingRequests(); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
protected void handleOnConnectedStateChanged(boolean state) { |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
@NonNull |
| 188 |
protected abstract I getServiceInterface(@NonNull IBinder service); |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
protected abstract long getTimeoutIdleBindMillis(); |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
protected long getRemoteRequestMillis() { |
| 206 |
throw new UnsupportedOperationException("not implemented by " + getClass()); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
@Nullable |
| 214 |
public final I getServiceInterface() { |
| 215 |
return mService; |
| 216 |
} |
| 217 |
|
| 218 |
private void handleDestroy() { |
| 219 |
if (checkIfDestroyed()) return; |
| 220 |
handleOnDestroy(); |
| 221 |
handleEnsureUnbound(); |
| 222 |
mDestroyed = true; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
protected abstract void handleOnDestroy(); |
| 231 |
|
| 232 |
@Override |
| 233 |
public void binderDied() { |
| 234 |
mHandler.sendMessage(obtainMessage(AbstractRemoteService::handleBinderDied, this)); |
| 235 |
} |
| 236 |
|
| 237 |
private void handleBinderDied() { |
| 238 |
if (checkIfDestroyed()) return; |
| 239 |
if (mService != null) { |
| 240 |
mService.asBinder().unlinkToDeath(this, 0); |
| 241 |
} |
| 242 |
updateServicelicationExitInfo(mComponentName, mUserId); |
| 243 |
mConnecting = true; |
| 244 |
mService = null; |
| 245 |
mServiceDied = true; |
| 246 |
cancelScheduledUnbind(); |
| 247 |
@SuppressWarnings("unchecked") |
| 248 |
final S castService = (S) this; |
| 249 |
mVultureCallback.onServiceDied(castService); |
| 250 |
handleBindFailure(); |
| 251 |
} |
| 252 |
|
| 253 |
private void updateServicelicationExitInfo(ComponentName componentName, int userId) { |
| 254 |
IActivityManager am = ActivityManager.getService(); |
| 255 |
String packageName = componentName.getPackageName(); |
| 256 |
ParceledListSlice<ApplicationExitInfo> plistSlice = null; |
| 257 |
try { |
| 258 |
plistSlice = am.getHistoricalProcessExitReasons(packageName, 0, 1, userId); |
| 259 |
} catch (RemoteException e) { |
| 260 |
|
| 261 |
} |
| 262 |
if (plistSlice == null) { |
| 263 |
mServiceExitReason = ApplicationExitInfo.REASON_UNKNOWN; |
| 264 |
mServiceExitSubReason = ApplicationExitInfo.SUBREASON_UNKNOWN; |
| 265 |
return; |
| 266 |
} |
| 267 |
List<ApplicationExitInfo> list = plistSlice.getList(); |
| 268 |
if (list.isEmpty()) { |
| 269 |
mServiceExitReason = ApplicationExitInfo.REASON_UNKNOWN; |
| 270 |
mServiceExitSubReason = ApplicationExitInfo.SUBREASON_UNKNOWN; |
| 271 |
return; |
| 272 |
} |
| 273 |
ApplicationExitInfo info = list.get(0); |
| 274 |
mServiceExitReason = info.getReason(); |
| 275 |
mServiceExitSubReason = info.getSubReason(); |
| 276 |
if (mVerbose) { |
| 277 |
Slog.v(mTag, "updateServicelicationExitInfo: exitReason=" |
| 278 |
+ ApplicationExitInfo.reasonCodeToString(mServiceExitReason) |
| 279 |
+ " exitSubReason= " + ApplicationExitInfo.subreasonToString( |
| 280 |
mServiceExitSubReason)); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public void dump(@NonNull String prefix, @NonNull PrintWriter pw) { |
| 291 |
String tab = " "; |
| 292 |
pw.append(prefix).append("service:").println(); |
| 293 |
pw.append(prefix).append(tab).append("userId=") |
| 294 |
.append(String.valueOf(mUserId)).println(); |
| 295 |
pw.append(prefix).append(tab).append("componentName=") |
| 296 |
.append(mComponentName.flattenToString()).println(); |
| 297 |
pw.append(prefix).append(tab).append("destroyed=") |
| 298 |
.append(String.valueOf(mDestroyed)).println(); |
| 299 |
pw.append(prefix).append(tab).append("numUnfinishedRequests=") |
| 300 |
.append(String.valueOf(mUnfinishedRequests.size())).println(); |
| 301 |
pw.append(prefix).append(tab).append("bound=") |
| 302 |
.append(String.valueOf(mBound)); |
| 303 |
final boolean bound = handleIsBound(); |
| 304 |
pw.append(prefix).append(tab).append("connected=") |
| 305 |
.append(String.valueOf(bound)); |
| 306 |
final long idleTimeout = getTimeoutIdleBindMillis(); |
| 307 |
if (bound) { |
| 308 |
if (idleTimeout > 0) { |
| 309 |
pw.append(" (unbind in : "); |
| 310 |
TimeUtils.formatDuration(mNextUnbind - SystemClock.elapsedRealtime(), pw); |
| 311 |
pw.append(")"); |
| 312 |
} else { |
| 313 |
pw.append(" (permanently bound)"); |
| 314 |
} |
| 315 |
} |
| 316 |
pw.println(); |
| 317 |
if (mServiceExitReason != SERVICE_NOT_EXIST) { |
| 318 |
pw.append(prefix).append(tab).append("serviceExistReason=") |
| 319 |
.append(ApplicationExitInfo.reasonCodeToString(mServiceExitReason)); |
| 320 |
pw.println(); |
| 321 |
} |
| 322 |
if (mServiceExitSubReason != SERVICE_NOT_EXIST) { |
| 323 |
pw.append(prefix).append(tab).append("serviceExistSubReason=") |
| 324 |
.append(ApplicationExitInfo.subreasonToString(mServiceExitSubReason)); |
| 325 |
pw.println(); |
| 326 |
} |
| 327 |
pw.append(prefix).append("mBindingFlags=").println(mBindingFlags); |
| 328 |
pw.append(prefix).append("idleTimeout=") |
| 329 |
.append(Long.toString(idleTimeout / 1000)).append("s\n"); |
| 330 |
pw.append(prefix).append("requestTimeout="); |
| 331 |
try { |
| 332 |
pw.append(Long.toString(getRemoteRequestMillis() / 1000)).append("s\n"); |
| 333 |
} catch (UnsupportedOperationException e) { |
| 334 |
pw.append("not supported\n"); |
| 335 |
} |
| 336 |
pw.println(); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
protected void scheduleRequest(@NonNull BasePendingRequest<S, I> pendingRequest) { |
| 347 |
mHandler.sendMessage(obtainMessage( |
| 348 |
AbstractRemoteService::handlePendingRequest, this, pendingRequest)); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
void finishRequest(@NonNull BasePendingRequest<S, I> finshedRequest) { |
| 357 |
mHandler.sendMessage( |
| 358 |
obtainMessage(AbstractRemoteService::handleFinishRequest, this, finshedRequest)); |
| 359 |
} |
| 360 |
|
| 361 |
private void handleFinishRequest(@NonNull BasePendingRequest<S, I> finshedRequest) { |
| 362 |
mUnfinishedRequests.remove(finshedRequest); |
| 363 |
|
| 364 |
if (mUnfinishedRequests.isEmpty()) { |
| 365 |
scheduleUnbind(); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
protected void scheduleAsyncRequest(@NonNull AsyncRequest<I> request) { |
| 376 |
|
| 377 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
| 378 |
final MyAsyncPendingRequest<S, I> asyncRequest = new MyAsyncPendingRequest(this, request); |
| 379 |
mHandler.sendMessage( |
| 380 |
obtainMessage(AbstractRemoteService::handlePendingRequest, this, asyncRequest)); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
protected void executeAsyncRequest(@NonNull AsyncRequest<I> request) { |
| 391 |
|
| 392 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
| 393 |
final MyAsyncPendingRequest<S, I> asyncRequest = new MyAsyncPendingRequest(this, request); |
| 394 |
handlePendingRequest(asyncRequest); |
| 395 |
} |
| 396 |
|
| 397 |
private void cancelScheduledUnbind() { |
| 398 |
mHandler.removeMessages(MSG_UNBIND); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
protected void scheduleBind() { |
| 408 |
if (mHandler.hasMessages(MSG_BIND)) { |
| 409 |
if (mVerbose) Slog.v(mTag, "scheduleBind(): already scheduled"); |
| 410 |
return; |
| 411 |
} |
| 412 |
mHandler.sendMessage(obtainMessage(AbstractRemoteService::handleEnsureBound, this) |
| 413 |
.setWhat(MSG_BIND)); |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
protected void scheduleUnbind() { |
| 421 |
scheduleUnbind(true); |
| 422 |
} |
| 423 |
|
| 424 |
private void scheduleUnbind(boolean delay) { |
| 425 |
long unbindDelay = getTimeoutIdleBindMillis(); |
| 426 |
|
| 427 |
if (unbindDelay <= PERMANENT_BOUND_TIMEOUT_MS) { |
| 428 |
if (mVerbose) Slog.v(mTag, "not scheduling unbind when value is " + unbindDelay); |
| 429 |
return; |
| 430 |
} |
| 431 |
|
| 432 |
if (!delay) { |
| 433 |
unbindDelay = 0; |
| 434 |
} |
| 435 |
|
| 436 |
cancelScheduledUnbind(); |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
mNextUnbind = SystemClock.elapsedRealtime() + unbindDelay; |
| 441 |
if (mVerbose) Slog.v(mTag, "unbinding in " + unbindDelay + "ms: " + mNextUnbind); |
| 442 |
mHandler.sendMessageDelayed(obtainMessage(AbstractRemoteService::handleUnbind, this) |
| 443 |
.setWhat(MSG_UNBIND), unbindDelay); |
| 444 |
} |
| 445 |
|
| 446 |
private void handleUnbind() { |
| 447 |
if (checkIfDestroyed()) return; |
| 448 |
|
| 449 |
handleEnsureUnbound(); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
protected final void handlePendingRequest(@NonNull BasePendingRequest<S, I> pendingRequest) { |
| 457 |
if (checkIfDestroyed() || mCompleted) return; |
| 458 |
|
| 459 |
if (!handleIsBound()) { |
| 460 |
if (mVerbose) Slog.v(mTag, "handlePendingRequest(): queuing " + pendingRequest); |
| 461 |
handlePendingRequestWhileUnBound(pendingRequest); |
| 462 |
handleEnsureBound(); |
| 463 |
} else { |
| 464 |
if (mVerbose) Slog.v(mTag, "handlePendingRequest(): " + pendingRequest); |
| 465 |
|
| 466 |
mUnfinishedRequests.add(pendingRequest); |
| 467 |
cancelScheduledUnbind(); |
| 468 |
|
| 469 |
pendingRequest.run(); |
| 470 |
if (pendingRequest.isFinal()) { |
| 471 |
mCompleted = true; |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
abstract void handlePendingRequestWhileUnBound( |
| 480 |
@NonNull BasePendingRequest<S, I> pendingRequest); |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
abstract void handleBindFailure(); |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
private boolean handleIsBound() { |
| 491 |
return mService != null; |
| 492 |
} |
| 493 |
|
| 494 |
private void handleEnsureBound() { |
| 495 |
if (handleIsBound() || mConnecting) return; |
| 496 |
|
| 497 |
if (mVerbose) Slog.v(mTag, "ensureBound()"); |
| 498 |
mConnecting = true; |
| 499 |
|
| 500 |
final int flags = Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE |
| 501 |
| Context.BIND_INCLUDE_CAPABILITIES | mBindingFlags; |
| 502 |
|
| 503 |
final boolean willBind = mContext.bindServiceAsUser(mIntent, mServiceConnection, flags, |
| 504 |
mHandler, new UserHandle(mUserId)); |
| 505 |
mBound = true; |
| 506 |
|
| 507 |
if (!willBind) { |
| 508 |
Slog.w(mTag, "could not bind to " + mIntent + " using flags " + flags); |
| 509 |
mConnecting = false; |
| 510 |
|
| 511 |
if (!mServiceDied) { |
| 512 |
handleBinderDied(); |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
private void handleEnsureUnbound() { |
| 518 |
if (!handleIsBound() && !mConnecting) return; |
| 519 |
|
| 520 |
if (mVerbose) Slog.v(mTag, "ensureUnbound()"); |
| 521 |
mConnecting = false; |
| 522 |
if (handleIsBound()) { |
| 523 |
handleOnConnectedStateChangedInternal(false); |
| 524 |
if (mService != null) { |
| 525 |
mService.asBinder().unlinkToDeath(this, 0); |
| 526 |
mService = null; |
| 527 |
} |
| 528 |
} |
| 529 |
mNextUnbind = 0; |
| 530 |
if (mBound) { |
| 531 |
mContext.unbindService(mServiceConnection); |
| 532 |
mBound = false; |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
private class RemoteServiceConnection implements ServiceConnection { |
| 537 |
@Override |
| 538 |
public void onServiceConnected(ComponentName name, IBinder service) { |
| 539 |
if (mVerbose) Slog.v(mTag, "onServiceConnected()"); |
| 540 |
if (mDestroyed || !mConnecting) { |
| 541 |
|
| 542 |
Slog.wtf(mTag, "onServiceConnected() was dispatched after unbindService."); |
| 543 |
return; |
| 544 |
} |
| 545 |
mConnecting = false; |
| 546 |
try { |
| 547 |
service.linkToDeath(AbstractRemoteService.this, 0); |
| 548 |
} catch (RemoteException re) { |
| 549 |
handleBinderDied(); |
| 550 |
return; |
| 551 |
} |
| 552 |
mService = getServiceInterface(service); |
| 553 |
mServiceExitReason = SERVICE_NOT_EXIST; |
| 554 |
mServiceExitSubReason = SERVICE_NOT_EXIST; |
| 555 |
handleOnConnectedStateChangedInternal(true); |
| 556 |
mServiceDied = false; |
| 557 |
} |
| 558 |
|
| 559 |
@Override |
| 560 |
public void onServiceDisconnected(ComponentName name) { |
| 561 |
if (mVerbose) Slog.v(mTag, "onServiceDisconnected()"); |
| 562 |
mConnecting = true; |
| 563 |
mService = null; |
| 564 |
} |
| 565 |
|
| 566 |
@Override |
| 567 |
public void onBindingDied(ComponentName name) { |
| 568 |
if (mVerbose) Slog.v(mTag, "onBindingDied()"); |
| 569 |
scheduleUnbind(false); |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
private boolean checkIfDestroyed() { |
| 574 |
if (mDestroyed) { |
| 575 |
if (mVerbose) { |
| 576 |
Slog.v(mTag, "Not handling operation as service for " + mComponentName |
| 577 |
+ " is already destroyed"); |
| 578 |
} |
| 579 |
} |
| 580 |
return mDestroyed; |
| 581 |
} |
| 582 |
|
| 583 |
@Override |
| 584 |
public String toString() { |
| 585 |
return getClass().getSimpleName() + "[" + mComponentName |
| 586 |
+ " " + System.identityHashCode(this) |
| 587 |
+ (mService != null ? " (bound)" : " (unbound)") |
| 588 |
+ (mDestroyed ? " (destroyed)" : "") |
| 589 |
+ "]"; |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
public abstract static class BasePendingRequest<S extends AbstractRemoteService<S, I>, |
| 603 |
I extends IInterface> implements Runnable { |
| 604 |
protected final String mTag = getClass().getSimpleName(); |
| 605 |
protected final Object mLock = new Object(); |
| 606 |
|
| 607 |
final WeakReference<S> mWeakService; |
| 608 |
|
| 609 |
@GuardedBy("mLock") |
| 610 |
boolean mCancelled; |
| 611 |
|
| 612 |
@GuardedBy("mLock") |
| 613 |
boolean mCompleted; |
| 614 |
|
| 615 |
BasePendingRequest(@NonNull S service) { |
| 616 |
mWeakService = new WeakReference<>(service); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
protected final S getService() { |
| 623 |
return mWeakService.get(); |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
protected final boolean finish() { |
| 633 |
synchronized (mLock) { |
| 634 |
if (mCompleted || mCancelled) { |
| 635 |
return false; |
| 636 |
} |
| 637 |
mCompleted = true; |
| 638 |
} |
| 639 |
|
| 640 |
S service = mWeakService.get(); |
| 641 |
if (service != null) { |
| 642 |
service.finishRequest(this); |
| 643 |
} |
| 644 |
|
| 645 |
onFinished(); |
| 646 |
|
| 647 |
return true; |
| 648 |
} |
| 649 |
|
| 650 |
void onFinished() { } |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
protected void onFailed() { } |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
@GuardedBy("mLock") |
| 662 |
protected final boolean isCancelledLocked() { |
| 663 |
return mCancelled; |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
public boolean cancel() { |
| 672 |
synchronized (mLock) { |
| 673 |
if (mCancelled || mCompleted) { |
| 674 |
return false; |
| 675 |
} |
| 676 |
mCancelled = true; |
| 677 |
} |
| 678 |
|
| 679 |
S service = mWeakService.get(); |
| 680 |
if (service != null) { |
| 681 |
service.finishRequest(this); |
| 682 |
} |
| 683 |
|
| 684 |
onCancel(); |
| 685 |
return true; |
| 686 |
} |
| 687 |
|
| 688 |
void onCancel() {} |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
protected boolean isFinal() { |
| 694 |
return false; |
| 695 |
} |
| 696 |
|
| 697 |
protected boolean isRequestCompleted() { |
| 698 |
synchronized (mLock) { |
| 699 |
return mCompleted; |
| 700 |
} |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
public abstract static class PendingRequest<S extends AbstractRemoteService<S, I>, |
| 718 |
I extends IInterface> extends BasePendingRequest<S, I> { |
| 719 |
|
| 720 |
private final Runnable mTimeoutTrigger; |
| 721 |
private final Handler mServiceHandler; |
| 722 |
|
| 723 |
protected PendingRequest(S service) { |
| 724 |
super(service); |
| 725 |
mServiceHandler = service.mHandler; |
| 726 |
|
| 727 |
mTimeoutTrigger = () -> { |
| 728 |
synchronized (mLock) { |
| 729 |
if (mCancelled) { |
| 730 |
return; |
| 731 |
} |
| 732 |
mCompleted = true; |
| 733 |
} |
| 734 |
|
| 735 |
final S remoteService = mWeakService.get(); |
| 736 |
if (remoteService != null) { |
| 737 |
|
| 738 |
Slog.w(mTag, "timed out after " + service.getRemoteRequestMillis() + " ms"); |
| 739 |
remoteService.finishRequest(this); |
| 740 |
onTimeout(remoteService); |
| 741 |
} else { |
| 742 |
Slog.w(mTag, "timed out (no service)"); |
| 743 |
} |
| 744 |
}; |
| 745 |
mServiceHandler.postAtTime(mTimeoutTrigger, |
| 746 |
SystemClock.uptimeMillis() + service.getRemoteRequestMillis()); |
| 747 |
} |
| 748 |
|
| 749 |
@Override |
| 750 |
final void onFinished() { |
| 751 |
mServiceHandler.removeCallbacks(mTimeoutTrigger); |
| 752 |
} |
| 753 |
|
| 754 |
@Override |
| 755 |
final void onCancel() { |
| 756 |
mServiceHandler.removeCallbacks(mTimeoutTrigger); |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
protected abstract void onTimeout(S remoteService); |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
public interface AsyncRequest<I extends IInterface> { |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
void run(@NonNull I binder) throws RemoteException; |
| 777 |
} |
| 778 |
|
| 779 |
private static final class MyAsyncPendingRequest<S extends AbstractRemoteService<S, I>, |
| 780 |
I extends IInterface> extends BasePendingRequest<S, I> { |
| 781 |
private static final String TAG = MyAsyncPendingRequest.class.getSimpleName(); |
| 782 |
|
| 783 |
private final AsyncRequest<I> mRequest; |
| 784 |
|
| 785 |
protected MyAsyncPendingRequest(@NonNull S service, @NonNull AsyncRequest<I> request) { |
| 786 |
super(service); |
| 787 |
|
| 788 |
mRequest = request; |
| 789 |
} |
| 790 |
|
| 791 |
@Override |
| 792 |
public void run() { |
| 793 |
final S remoteService = getService(); |
| 794 |
if (remoteService == null) return; |
| 795 |
try { |
| 796 |
mRequest.run(remoteService.mService); |
| 797 |
} catch (RemoteException e) { |
| 798 |
Slog.w(TAG, "exception handling async request (" + this + "): " + e); |
| 799 |
} finally { |
| 800 |
finish(); |
| 801 |
} |
| 802 |
} |
| 803 |
} |
| 804 |
} |