blob: 293ffd34da8d5762ba3c3d437aa08adef94a162d [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;
Felipe Lemeafb55b62018-12-06 11:36:09 -080022import android.os.IInterface;
Felipe Leme39233ff2018-11-28 16:54:23 -080023import android.util.Slog;
24
25import java.io.PrintWriter;
26import 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 Lemeafb55b62018-12-06 11:36:09 -080033 * @param <I> the interface of the binder service
Felipe Leme39233ff2018-11-28 16:54:23 -080034 * @hide
35 */
Felipe Lemeafb55b62018-12-06 11:36:09 -080036public abstract class AbstractMultiplePendingRequestsRemoteService<S
37 extends AbstractMultiplePendingRequestsRemoteService<S, I>, I extends IInterface>
38 extends AbstractRemoteService<S, I> {
Felipe Leme39233ff2018-11-28 16:54:23 -080039
40 private final int mInitialCapacity;
41
Felipe Leme81299d02019-02-21 15:48:50 -080042 protected ArrayList<BasePendingRequest<S, I>> mPendingRequests;
Felipe Leme39233ff2018-11-28 16:54:23 -080043
44 public AbstractMultiplePendingRequestsRemoteService(@NonNull Context context,
45 @NonNull String serviceInterface, @NonNull ComponentName componentName, int userId,
Felipe Lemeafb55b62018-12-06 11:36:09 -080046 @NonNull VultureCallback<S> callback, boolean bindInstantServiceAllowed,
47 boolean verbose, int initialCapacity) {
Felipe Leme39233ff2018-11-28 16:54:23 -080048 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 Leme81299d02019-02-21 15:48:50 -080088 void handlePendingRequestWhileUnBound(@NonNull BasePendingRequest<S, I> pendingRequest) {
Felipe Leme39233ff2018-11-28 16:54:23 -080089 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}