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  * @deprecated Use {@link ServiceConnector} to manage remote service connections
37  *
38  * @hide
39  */
40 @Deprecated
41 public abstract class AbstractSinglePendingRequestRemoteService<S
42         extends AbstractSinglePendingRequestRemoteService<S, I>, I extends IInterface>
43         extends AbstractRemoteService<S, I> {
44 
45     protected BasePendingRequest<S, I> mPendingRequest;
46 
AbstractSinglePendingRequestRemoteService(@onNull Context context, @NonNull String serviceInterface, @NonNull ComponentName componentName, int userId, @NonNull VultureCallback<S> callback, @NonNull Handler handler, int bindingFlags, boolean verbose)47     public AbstractSinglePendingRequestRemoteService(@NonNull Context context,
48             @NonNull String serviceInterface, @NonNull ComponentName componentName, int userId,
49             @NonNull VultureCallback<S> callback, @NonNull Handler handler,
50             int bindingFlags, boolean verbose) {
51         super(context, serviceInterface, componentName, userId, callback, handler, bindingFlags,
52                 verbose);
53     }
54 
55     @Override // from AbstractRemoteService
handlePendingRequests()56     void handlePendingRequests() {
57         if (mPendingRequest != null) {
58             final BasePendingRequest<S, I> pendingRequest = mPendingRequest;
59             mPendingRequest = null;
60             handlePendingRequest(pendingRequest);
61         }
62     }
63 
64     @Override // from AbstractRemoteService
handleOnDestroy()65     protected void handleOnDestroy() {
66         if (mPendingRequest != null) {
67             mPendingRequest.cancel();
68             mPendingRequest = null;
69         }
70     }
71 
72     @Override // from AbstractRemoteService
handleBindFailure()73     void handleBindFailure() {
74         if (mPendingRequest != null) {
75             if (mVerbose) Slog.v(mTag, "Sending failure to " + mPendingRequest);
76             mPendingRequest.onFailed();
77             mPendingRequest = null;
78         }
79     }
80 
81     @Override // from AbstractRemoteService
dump(@onNull String prefix, @NonNull PrintWriter pw)82     public void dump(@NonNull String prefix, @NonNull PrintWriter pw) {
83         super.dump(prefix, pw);
84         pw.append(prefix).append("hasPendingRequest=")
85                 .append(String.valueOf(mPendingRequest != null)).println();
86     }
87 
88     @Override // from AbstractRemoteService
handlePendingRequestWhileUnBound(@onNull BasePendingRequest<S, I> pendingRequest)89     void handlePendingRequestWhileUnBound(@NonNull BasePendingRequest<S, I> pendingRequest) {
90         if (mPendingRequest != null) {
91             if (mVerbose) {
92                 Slog.v(mTag, "handlePendingRequestWhileUnBound(): cancelling " + mPendingRequest
93                         + " to handle " + pendingRequest);
94             }
95             mPendingRequest.cancel();
96         }
97         mPendingRequest = pendingRequest;
98     }
99 }
100