blob: 3e92a0b196ee206ddec68cd0133979a688a6eefe [file] [log] [blame]
Felipe Leme39233ff2018-11-28 16:54:23 -08001/*
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
Philip P. Moltmann29ee0a42018-12-19 11:56:05 -080017package com.android.internal.infra;
Felipe Leme39233ff2018-11-28 16:54:23 -080018
19import android.annotation.NonNull;
20import android.content.ComponentName;
21import android.content.Context;
Felipe Lemeafb55b62018-12-06 11:36:09 -080022import android.os.IInterface;
Felipe Leme39233ff2018-11-28 16:54:23 -080023import android.util.Slog;
24
25import java.io.PrintWriter;
26
27/**
28 * Base class representing a remote service that can have only one pending requests while not bound.
29 *
30 * <p>If another request is received while not bound, the previous one will be canceled.
31 *
32 * @param <S> the concrete remote service class
Felipe Lemeafb55b62018-12-06 11:36:09 -080033 * @param <I> the interface of the binder service
Felipe Leme39233ff2018-11-28 16:54:23 -080034 *
35 * @hide
36 */
Felipe Lemeafb55b62018-12-06 11:36:09 -080037public abstract class AbstractSinglePendingRequestRemoteService<S
38 extends AbstractSinglePendingRequestRemoteService<S, I>, I extends IInterface>
39 extends AbstractRemoteService<S, I> {
Felipe Leme39233ff2018-11-28 16:54:23 -080040
Felipe Leme81299d02019-02-21 15:48:50 -080041 protected BasePendingRequest<S, I> mPendingRequest;
Felipe Leme39233ff2018-11-28 16:54:23 -080042
43 public AbstractSinglePendingRequestRemoteService(@NonNull Context context,
44 @NonNull String serviceInterface, @NonNull ComponentName componentName, int userId,
Felipe Lemeafb55b62018-12-06 11:36:09 -080045 @NonNull VultureCallback<S> callback, boolean bindInstantServiceAllowed,
Felipe Leme39233ff2018-11-28 16:54:23 -080046 boolean verbose) {
47 super(context, serviceInterface, componentName, userId, callback, bindInstantServiceAllowed,
48 verbose);
49 }
50
51 @Override // from AbstractRemoteService
52 void handlePendingRequests() {
53 if (mPendingRequest != null) {
Felipe Leme81299d02019-02-21 15:48:50 -080054 final BasePendingRequest<S, I> pendingRequest = mPendingRequest;
Felipe Leme39233ff2018-11-28 16:54:23 -080055 mPendingRequest = null;
56 handlePendingRequest(pendingRequest);
57 }
58 }
59
60 @Override // from AbstractRemoteService
61 protected void handleOnDestroy() {
62 if (mPendingRequest != null) {
63 mPendingRequest.cancel();
64 mPendingRequest = null;
65 }
66 }
67
68 @Override // from AbstractRemoteService
69 public void dump(@NonNull String prefix, @NonNull PrintWriter pw) {
70 super.dump(prefix, pw);
71 pw.append(prefix).append("hasPendingRequest=")
72 .append(String.valueOf(mPendingRequest != null)).println();
73 }
74
75 @Override // from AbstractRemoteService
Felipe Leme81299d02019-02-21 15:48:50 -080076 void handlePendingRequestWhileUnBound(@NonNull BasePendingRequest<S, I> pendingRequest) {
Felipe Leme39233ff2018-11-28 16:54:23 -080077 if (mPendingRequest != null) {
78 if (mVerbose) {
Felipe Leme749b8892018-12-03 16:30:30 -080079 Slog.v(mTag, "handlePendingRequestWhileUnBound(): cancelling " + mPendingRequest
80 + " to handle " + pendingRequest);
Felipe Leme39233ff2018-11-28 16:54:23 -080081 }
82 mPendingRequest.cancel();
83 }
84 mPendingRequest = pendingRequest;
85 }
86}