blob: 37f61bf650c77152c2056f6290d0cb4078ade0e6 [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;
Hai Zhanga4959e52019-03-06 12:21:07 -080022import android.os.Handler;
Felipe Lemeafb55b62018-12-06 11:36:09 -080023import android.os.IInterface;
Felipe Leme39233ff2018-11-28 16:54:23 -080024import android.util.Slog;
25
26import java.io.PrintWriter;
27import 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 Lemeafb55b62018-12-06 11:36:09 -080034 * @param <I> the interface of the binder service
Felipe Leme39233ff2018-11-28 16:54:23 -080035 * @hide
36 */
Felipe Lemeafb55b62018-12-06 11:36:09 -080037public abstract class AbstractMultiplePendingRequestsRemoteService<S
38 extends AbstractMultiplePendingRequestsRemoteService<S, I>, I extends IInterface>
39 extends AbstractRemoteService<S, I> {
Felipe Leme39233ff2018-11-28 16:54:23 -080040
41 private final int mInitialCapacity;
42
Felipe Leme81299d02019-02-21 15:48:50 -080043 protected ArrayList<BasePendingRequest<S, I>> mPendingRequests;
Felipe Leme39233ff2018-11-28 16:54:23 -080044
45 public AbstractMultiplePendingRequestsRemoteService(@NonNull Context context,
46 @NonNull String serviceInterface, @NonNull ComponentName componentName, int userId,
Hai Zhanga4959e52019-03-06 12:21:07 -080047 @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 Leme39233ff2018-11-28 16:54:23 -080051 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 Leme81299d02019-02-21 15:48:50 -080089 void handlePendingRequestWhileUnBound(@NonNull BasePendingRequest<S, I> pendingRequest) {
Felipe Leme39233ff2018-11-28 16:54:23 -080090 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}