Debian Codegraph @

core/java/android/service/selectiontoolbar/SelectionToolbarRenderService.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 com.android.internal.util.function.pooled.PooledLambda.obtainMessage;
20
21 import android.annotation.CallSuper;
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.app.Service;
25 import android.content.Intent;
26 import android.os.Handler;
27 import android.os.IBinder;
28 import android.os.Looper;
29 import android.os.RemoteException;
30 import android.util.Log;
31 import android.util.Pair;
32 import android.util.SparseArray;
33 import android.view.selectiontoolbar.ISelectionToolbarCallback;
34 import android.view.selectiontoolbar.ShowInfo;
35 import android.view.selectiontoolbar.ToolbarMenuItem;
36 import android.view.selectiontoolbar.WidgetInfo;
37
38 /**
39 * Service for rendering selection toolbar.
40 *
41 * @hide
42 */
43 public abstract class SelectionToolbarRenderService extends Service {
44
45 private static final String TAG = "SelectionToolbarRenderService";
46
47 // TODO(b/215497659): read from DeviceConfig
48 // The timeout to clean the cache if the client forgot to call dismiss()
49 private static final int CACHE_CLEAN_AFTER_SHOW_TIMEOUT_IN_MS = 10 * 60 * 1000; // 10 minutes
50
51 /**
52 * The {@link Intent} that must be declared as handled by the service.
53 *
54 * <p>To be supported, the service must also require the
55 * {@link android.Manifest.permission#BIND_SELECTION_TOOLBAR_RENDER_SERVICE} permission so
56 * that other applications can not abuse it.
57 */
58 public static final String SERVICE_INTERFACE =
59 "android.service.selectiontoolbar.SelectionToolbarRenderService";
60
61 private Handler mHandler;
62 private ISelectionToolbarRenderServiceCallback mServiceCallback;
63
64 private final SparseArray<Pair<RemoteCallbackWrapper, CleanCacheRunnable>> mCache =
65 new SparseArray<>();
66
67 /**
68 * Binder to receive calls from system server.
69 */
70 private final ISelectionToolbarRenderService mInterface =
71 new ISelectionToolbarRenderService.Stub() {
72
73 @Override
74 public void onShow(int callingUid, ShowInfo showInfo, ISelectionToolbarCallback callback) {
75 if (mCache.indexOfKey(callingUid) < 0) {
76 mCache.put(callingUid, new Pair<>(new RemoteCallbackWrapper(callback),
77 new CleanCacheRunnable(callingUid)));
78 }
79 Pair<RemoteCallbackWrapper, CleanCacheRunnable> toolbarPair = mCache.get(callingUid);
80 CleanCacheRunnable cleanRunnable = toolbarPair.second;
81 mHandler.removeCallbacks(cleanRunnable);
82 mHandler.sendMessage(obtainMessage(SelectionToolbarRenderService::onShow,
83 SelectionToolbarRenderService.this, callingUid, showInfo,
84 toolbarPair.first));
85 mHandler.postDelayed(cleanRunnable, CACHE_CLEAN_AFTER_SHOW_TIMEOUT_IN_MS);
86 }
87
88 @Override
89 public void onHide(long widgetToken) {
90 mHandler.sendMessage(obtainMessage(SelectionToolbarRenderService::onHide,
91 SelectionToolbarRenderService.this, widgetToken));
92 }
93
94 @Override
95 public void onDismiss(int callingUid, long widgetToken) {
96 mHandler.sendMessage(obtainMessage(SelectionToolbarRenderService::onDismiss,
97 SelectionToolbarRenderService.this, widgetToken));
98 Pair<RemoteCallbackWrapper, CleanCacheRunnable> toolbarPair = mCache.get(callingUid);
99 if (toolbarPair != null) {
100 mHandler.removeCallbacks(toolbarPair.second);
101 mCache.remove(callingUid);
102 }
103 }
104
105 @Override
106 public void onConnected(IBinder callback) {
107 mHandler.sendMessage(obtainMessage(SelectionToolbarRenderService::handleOnConnected,
108 SelectionToolbarRenderService.this, callback));
109 }
110 };
111
112 @CallSuper
113 @Override
114 public void onCreate() {
115 super.onCreate();
116 mHandler = new Handler(Looper.getMainLooper(), null, true);
117 }
118
119 @Override
120 @Nullable
121 public final IBinder onBind(@NonNull Intent intent) {
122 if (SERVICE_INTERFACE.equals(intent.getAction())) {
123 return mInterface.asBinder();
124 }
125 Log.w(TAG, "Tried to bind to wrong intent (should be " + SERVICE_INTERFACE + ": " + intent);
126 return null;
127 }
128
129 private void handleOnConnected(@NonNull IBinder callback) {
130 mServiceCallback = ISelectionToolbarRenderServiceCallback.Stub.asInterface(callback);
131 }
132
133 protected void transferTouch(@NonNull IBinder source, @NonNull IBinder target) {
134 final ISelectionToolbarRenderServiceCallback callback = mServiceCallback;
135 if (callback == null) {
136 Log.e(TAG, "transferTouch(): no server callback");
137 return;
138 }
139 try {
140 callback.transferTouch(source, target);
141 } catch (RemoteException e) {
142 // no-op
143 }
144 }
145
146 /**
147 * Called when showing the selection toolbar.
148 */
149 public abstract void onShow(int callingUid, ShowInfo showInfo,
150 RemoteCallbackWrapper callbackWrapper);
151
152 /**
153 * Called when hiding the selection toolbar.
154 */
155 public abstract void onHide(long widgetToken);
156
157
158 /**
159 * Called when dismissing the selection toolbar.
160 */
161 public abstract void onDismiss(long widgetToken);
162
163 /**
164 * Called when showing the selection toolbar for a specific timeout. This avoids the client
165 * forgot to call dismiss to clean the state.
166 */
167 public void onToolbarShowTimeout(int callingUid) {
168 // no-op
169 }
170
171 /**
172 * Callback to notify the client toolbar events.
173 */
174 public static final class RemoteCallbackWrapper implements SelectionToolbarRenderCallback {
175
176 private final ISelectionToolbarCallback mRemoteCallback;
177
178 RemoteCallbackWrapper(ISelectionToolbarCallback remoteCallback) {
179 // TODO(b/215497659): handle if the binder dies.
180 mRemoteCallback = remoteCallback;
181 }
182
183 @Override
184 public void onShown(WidgetInfo widgetInfo) {
185 try {
186 mRemoteCallback.onShown(widgetInfo);
187 } catch (RemoteException e) {
188 // no-op
189 }
190 }
191
192 @Override
193 public void onToolbarShowTimeout() {
194 try {
195 mRemoteCallback.onToolbarShowTimeout();
196 } catch (RemoteException e) {
197 // no-op
198 }
199 }
200
201 @Override
202 public void onWidgetUpdated(WidgetInfo widgetInfo) {
203 try {
204 mRemoteCallback.onWidgetUpdated(widgetInfo);
205 } catch (RemoteException e) {
206 // no-op
207 }
208 }
209
210 @Override
211 public void onMenuItemClicked(ToolbarMenuItem item) {
212 try {
213 mRemoteCallback.onMenuItemClicked(item);
214 } catch (RemoteException e) {
215 // no-op
216 }
217 }
218
219 @Override
220 public void onError(int errorCode) {
221 try {
222 mRemoteCallback.onError(errorCode);
223 } catch (RemoteException e) {
224 // no-op
225 }
226 }
227 }
228
229 private class CleanCacheRunnable implements Runnable {
230
231 int mCleanUid;
232
233 CleanCacheRunnable(int cleanUid) {
234 mCleanUid = cleanUid;
235 }
236
237 @Override
238 public void run() {
239 Pair<RemoteCallbackWrapper, CleanCacheRunnable> toolbarPair = mCache.get(mCleanUid);
240 if (toolbarPair != null) {
241 Log.w(TAG, "CleanCacheRunnable: remove " + mCleanUid + " from cache.");
242 mCache.remove(mCleanUid);
243 onToolbarShowTimeout(mCleanUid);
244 }
245 }
246 }
247
248 /**
249 * A listener to notify the service to the transfer touch focus.
250 */
251 public interface TransferTouchListener {
252 /**
253 * Notify the service to transfer the touch focus.
254 */
255 void onTransferTouch(IBinder source, IBinder target);
256 }
257 }