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 | import java.util.ArrayList; |
| 27 | |
| 28 | /** |
| 29 | * Base class representing a remote service that can queue multiple pending requests while not |
| 30 | * bound. |
| 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 | * @hide |
| 35 | */ |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 36 | public abstract class AbstractMultiplePendingRequestsRemoteService<S |
| 37 | extends AbstractMultiplePendingRequestsRemoteService<S, I>, I extends IInterface> |
| 38 | extends AbstractRemoteService<S, I> { |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 39 | |
| 40 | private final int mInitialCapacity; |
| 41 | |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 42 | protected ArrayList<BasePendingRequest<S, I>> mPendingRequests; |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 43 | |
| 44 | public AbstractMultiplePendingRequestsRemoteService(@NonNull Context context, |
| 45 | @NonNull String serviceInterface, @NonNull ComponentName componentName, int userId, |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 46 | @NonNull VultureCallback<S> callback, boolean bindInstantServiceAllowed, |
| 47 | boolean verbose, int initialCapacity) { |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 48 | super(context, serviceInterface, componentName, userId, callback, bindInstantServiceAllowed, |
| 49 | verbose); |
| 50 | mInitialCapacity = initialCapacity; |
| 51 | } |
| 52 | |
| 53 | @Override // from AbstractRemoteService |
| 54 | void handlePendingRequests() { |
| 55 | if (mPendingRequests != null) { |
| 56 | final int size = mPendingRequests.size(); |
| 57 | if (mVerbose) Slog.v(mTag, "Sending " + size + " pending requests"); |
| 58 | for (int i = 0; i < size; i++) { |
| 59 | mPendingRequests.get(i).run(); |
| 60 | } |
| 61 | mPendingRequests = null; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | @Override // from AbstractRemoteService |
| 66 | protected void handleOnDestroy() { |
| 67 | if (mPendingRequests != null) { |
| 68 | final int size = mPendingRequests.size(); |
| 69 | if (mVerbose) Slog.v(mTag, "Canceling " + size + " pending requests"); |
| 70 | for (int i = 0; i < size; i++) { |
| 71 | mPendingRequests.get(i).cancel(); |
| 72 | } |
| 73 | mPendingRequests = null; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Override // from AbstractRemoteService |
| 78 | public void dump(@NonNull String prefix, @NonNull PrintWriter pw) { |
| 79 | super.dump(prefix, pw); |
| 80 | |
| 81 | pw.append(prefix).append("initialCapacity=").append(String.valueOf(mInitialCapacity)) |
| 82 | .println(); |
| 83 | final int size = mPendingRequests == null ? 0 : mPendingRequests.size(); |
| 84 | pw.append(prefix).append("pendingRequests=").append(String.valueOf(size)).println(); |
| 85 | } |
| 86 | |
| 87 | @Override // from AbstractRemoteService |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 88 | void handlePendingRequestWhileUnBound(@NonNull BasePendingRequest<S, I> pendingRequest) { |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 89 | if (mPendingRequests == null) { |
| 90 | mPendingRequests = new ArrayList<>(mInitialCapacity); |
| 91 | } |
| 92 | mPendingRequests.add(pendingRequest); |
| 93 | if (mVerbose) { |
| 94 | Slog.v(mTag, "queued " + mPendingRequests.size() + " requests; last=" + pendingRequest); |
| 95 | } |
| 96 | } |
| 97 | } |