Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 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 | |
Philip P. Moltmann | 29ee0a4 | 2018-12-19 11:56:05 -0800 | [diff] [blame] | 17 | package com.android.internal.infra; |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 22 | import android.os.IInterface; |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 23 | import android.util.Slog; |
| 24 | |
| 25 | import 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 Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 33 | * @param <I> the interface of the binder service |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 34 | * |
| 35 | * @hide |
| 36 | */ |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 37 | public abstract class AbstractSinglePendingRequestRemoteService<S |
| 38 | extends AbstractSinglePendingRequestRemoteService<S, I>, I extends IInterface> |
| 39 | extends AbstractRemoteService<S, I> { |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 40 | |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 41 | protected BasePendingRequest<S, I> mPendingRequest; |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 42 | |
| 43 | public AbstractSinglePendingRequestRemoteService(@NonNull Context context, |
| 44 | @NonNull String serviceInterface, @NonNull ComponentName componentName, int userId, |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 45 | @NonNull VultureCallback<S> callback, boolean bindInstantServiceAllowed, |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 46 | 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 Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 54 | final BasePendingRequest<S, I> pendingRequest = mPendingRequest; |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 55 | 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 Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 76 | void handlePendingRequestWhileUnBound(@NonNull BasePendingRequest<S, I> pendingRequest) { |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 77 | if (mPendingRequest != null) { |
| 78 | if (mVerbose) { |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 79 | Slog.v(mTag, "handlePendingRequestWhileUnBound(): cancelling " + mPendingRequest |
| 80 | + " to handle " + pendingRequest); |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 81 | } |
| 82 | mPendingRequest.cancel(); |
| 83 | } |
| 84 | mPendingRequest = pendingRequest; |
| 85 | } |
| 86 | } |