Debian Codegraph @

core/java/android/service/selectiontoolbar/DefaultSelectionToolbarRenderService.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 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
17 package android.service.selectiontoolbar;
18
19 import static android.view.selectiontoolbar.SelectionToolbarManager.ERROR_DO_NOT_ALLOW_MULTIPLE_TOOL_BAR;
20 import static android.view.selectiontoolbar.SelectionToolbarManager.NO_TOOLBAR_ID;
21
22 import android.util.Pair;
23 import android.util.Slog;
24 import android.util.SparseArray;
25 import android.view.selectiontoolbar.ShowInfo;
26
27 import java.io.FileDescriptor;
28 import java.io.PrintWriter;
29 import java.util.UUID;
30
31 /**
32 * The default implementation of {@link SelectionToolbarRenderService}.
33 *
34 * <p><b>NOTE:<b/> The requests are handled on the service main thread.
35 *
36 * @hide
37 */
38 // TODO(b/214122495): fix class not found then move to system service folder
39 public final class DefaultSelectionToolbarRenderService extends SelectionToolbarRenderService {
40
41 private static final String TAG = "DefaultSelectionToolbarRenderService";
42
43 // TODO(b/215497659): handle remove if the client process dies.
44 // Only show one toolbar, dismiss the old ones and remove from cache
45 private final SparseArray<Pair<Long, RemoteSelectionToolbar>> mToolbarCache =
46 new SparseArray<>();
47
48 /**
49 * Only allow one package to create one toolbar.
50 */
51 private boolean canShowToolbar(int uid, ShowInfo showInfo) {
52 if (showInfo.getWidgetToken() != NO_TOOLBAR_ID) {
53 return true;
54 }
55 return mToolbarCache.indexOfKey(uid) < 0;
56 }
57
58 @Override
59 public void onShow(int callingUid, ShowInfo showInfo,
60 SelectionToolbarRenderService.RemoteCallbackWrapper callbackWrapper) {
61 if (!canShowToolbar(callingUid, showInfo)) {
62 Slog.e(TAG, "Do not allow multiple toolbar for the app.");
63 callbackWrapper.onError(ERROR_DO_NOT_ALLOW_MULTIPLE_TOOL_BAR);
64 return;
65 }
66 long widgetToken = showInfo.getWidgetToken() == NO_TOOLBAR_ID
67 ? UUID.randomUUID().getMostSignificantBits()
68 : showInfo.getWidgetToken();
69
70 if (mToolbarCache.indexOfKey(callingUid) < 0) {
71 RemoteSelectionToolbar toolbar = new RemoteSelectionToolbar(this,
72 widgetToken, showInfo,
73 callbackWrapper, this::transferTouch);
74 mToolbarCache.put(callingUid, new Pair<>(widgetToken, toolbar));
75 }
76 Slog.v(TAG, "onShow() for " + widgetToken);
77 Pair<Long, RemoteSelectionToolbar> toolbarPair = mToolbarCache.get(callingUid);
78 if (toolbarPair.first == widgetToken) {
79 toolbarPair.second.show(showInfo);
80 } else {
81 Slog.w(TAG, "onShow() for unknown " + widgetToken);
82 }
83 }
84
85 @Override
86 public void onHide(long widgetToken) {
87 RemoteSelectionToolbar toolbar = getRemoteSelectionToolbarByTokenLocked(widgetToken);
88 if (toolbar != null) {
89 Slog.v(TAG, "onHide() for " + widgetToken);
90 toolbar.hide(widgetToken);
91 }
92 }
93
94 @Override
95 public void onDismiss(long widgetToken) {
96 RemoteSelectionToolbar toolbar = getRemoteSelectionToolbarByTokenLocked(widgetToken);
97 if (toolbar != null) {
98 Slog.v(TAG, "onDismiss() for " + widgetToken);
99 toolbar.dismiss(widgetToken);
100 removeRemoteSelectionToolbarByTokenLocked(widgetToken);
101 }
102 }
103
104 @Override
105 public void onToolbarShowTimeout(int callingUid) {
106 Slog.w(TAG, "onToolbarShowTimeout for callingUid = " + callingUid);
107 Pair<Long, RemoteSelectionToolbar> toolbarPair = mToolbarCache.get(callingUid);
108 if (toolbarPair != null) {
109 RemoteSelectionToolbar remoteToolbar = toolbarPair.second;
110 remoteToolbar.dismiss(toolbarPair.first);
111 remoteToolbar.onToolbarShowTimeout();
112 mToolbarCache.remove(callingUid);
113 }
114 }
115
116 private RemoteSelectionToolbar getRemoteSelectionToolbarByTokenLocked(long widgetToken) {
117 for (int i = 0; i < mToolbarCache.size(); i++) {
118 Pair<Long, RemoteSelectionToolbar> toolbarPair = mToolbarCache.valueAt(i);
119 if (toolbarPair.first == widgetToken) {
120 return toolbarPair.second;
121 }
122 }
123 return null;
124 }
125
126 private void removeRemoteSelectionToolbarByTokenLocked(long widgetToken) {
127 for (int i = 0; i < mToolbarCache.size(); i++) {
128 Pair<Long, RemoteSelectionToolbar> toolbarPair = mToolbarCache.valueAt(i);
129 if (toolbarPair.first == widgetToken) {
130 mToolbarCache.remove(mToolbarCache.keyAt(i));
131 return;
132 }
133 }
134 }
135
136 @Override
137 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
138 int size = mToolbarCache.size();
139 pw.print("number selectionToolbar: "); pw.println(size);
140 String pfx = " ";
141 for (int i = 0; i < size; i++) {
142 pw.print("#"); pw.println(i);
143 int callingUid = mToolbarCache.keyAt(i);
144 pw.print(pfx); pw.print("callingUid: "); pw.println(callingUid);
145 Pair<Long, RemoteSelectionToolbar> toolbarPair = mToolbarCache.valueAt(i);
146 RemoteSelectionToolbar selectionToolbar = toolbarPair.second;
147 pw.print(pfx); pw.print("selectionToolbar: ");
148 selectionToolbar.dump(pfx, pw);
149 pw.println();
150 }
151 }
152 }
153