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