1 /* 2 * Copyright (C) 2018 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 17 package com.android.internal.infra; 18 19 import android.annotation.NonNull; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.os.Handler; 23 import android.os.IInterface; 24 import android.util.Slog; 25 26 import java.io.PrintWriter; 27 28 /** 29 * Base class representing a remote service that can have only one pending requests while not bound. 30 * 31 * <p>If another request is received while not bound, the previous one will be canceled. 32 * 33 * @param <S> the concrete remote service class 34 * @param <I> the interface of the binder service 35 * 36 * @hide 37 */ 38 public abstract class AbstractSinglePendingRequestRemoteService<S 39 extends AbstractSinglePendingRequestRemoteService<S, I>, I extends IInterface> 40 extends AbstractRemoteService<S, I> { 41 42 protected BasePendingRequest<S, I> mPendingRequest; 43 AbstractSinglePendingRequestRemoteService(@onNull Context context, @NonNull String serviceInterface, @NonNull ComponentName componentName, int userId, @NonNull VultureCallback<S> callback, @NonNull Handler handler, int bindingFlags, boolean verbose)44 public AbstractSinglePendingRequestRemoteService(@NonNull Context context, 45 @NonNull String serviceInterface, @NonNull ComponentName componentName, int userId, 46 @NonNull VultureCallback<S> callback, @NonNull Handler handler, 47 int bindingFlags, boolean verbose) { 48 super(context, serviceInterface, componentName, userId, callback, handler, bindingFlags, 49 verbose); 50 } 51 52 @Override // from AbstractRemoteService handlePendingRequests()53 void handlePendingRequests() { 54 if (mPendingRequest != null) { 55 final BasePendingRequest<S, I> pendingRequest = mPendingRequest; 56 mPendingRequest = null; 57 handlePendingRequest(pendingRequest); 58 } 59 } 60 61 @Override // from AbstractRemoteService handleOnDestroy()62 protected void handleOnDestroy() { 63 handleCancelPendingRequest(); 64 } 65 handleCancelPendingRequest()66 protected BasePendingRequest<S, I> handleCancelPendingRequest() { 67 BasePendingRequest<S, I> pendingRequest = mPendingRequest; 68 if (pendingRequest != null) { 69 pendingRequest.cancel(); 70 mPendingRequest = null; 71 } 72 return pendingRequest; 73 } 74 75 @Override // from AbstractRemoteService handleBindFailure()76 void handleBindFailure() { 77 if (mPendingRequest != null) { 78 if (mVerbose) Slog.v(mTag, "Sending failure to " + mPendingRequest); 79 mPendingRequest.onFailed(); 80 mPendingRequest = null; 81 } 82 } 83 84 @Override // from AbstractRemoteService dump(@onNull String prefix, @NonNull PrintWriter pw)85 public void dump(@NonNull String prefix, @NonNull PrintWriter pw) { 86 super.dump(prefix, pw); 87 pw.append(prefix).append("hasPendingRequest=") 88 .append(String.valueOf(mPendingRequest != null)).println(); 89 } 90 91 @Override // from AbstractRemoteService handlePendingRequestWhileUnBound(@onNull BasePendingRequest<S, I> pendingRequest)92 void handlePendingRequestWhileUnBound(@NonNull BasePendingRequest<S, I> pendingRequest) { 93 if (mPendingRequest != null) { 94 if (mVerbose) { 95 Slog.v(mTag, "handlePendingRequestWhileUnBound(): cancelling " + mPendingRequest 96 + " to handle " + pendingRequest); 97 } 98 mPendingRequest.cancel(); 99 } 100 mPendingRequest = pendingRequest; 101 } 102 } 103