Debian Codegraph @

core/java/android/service/contentsuggestions/ContentSuggestionsService.java

raw symbols SCIP
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
17 package android.service.contentsuggestions;
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.annotation.SystemApi;
25 import android.app.Service;
26 import android.app.contentsuggestions.ClassificationsRequest;
27 import android.app.contentsuggestions.ContentSuggestionsManager;
28 import android.app.contentsuggestions.IClassificationsCallback;
29 import android.app.contentsuggestions.ISelectionsCallback;
30 import android.app.contentsuggestions.SelectionsRequest;
31 import android.content.Intent;
32 import android.graphics.Bitmap;
33 import android.graphics.ColorSpace;
34 import android.hardware.HardwareBuffer;
35 import android.os.Bundle;
36 import android.os.Handler;
37 import android.os.IBinder;
38 import android.os.Looper;
39 import android.os.RemoteException;
40 import android.util.Log;
41 import android.util.Slog;
42
43 /**
44 * @hide
45 */
46 @SystemApi
47 public abstract class ContentSuggestionsService extends Service {
48
49 private static final String TAG = ContentSuggestionsService.class.getSimpleName();
50
51 private Handler mHandler;
52
53 /**
54 * The action for the intent used to define the content suggestions service.
55 *
56 * <p>To be supported, the service must also require the
57 * * {@link android.Manifest.permission#BIND_CONTENT_SUGGESTIONS_SERVICE} permission so
58 * * that other applications can not abuse it.
59 */
60 public static final String SERVICE_INTERFACE =
61 "android.service.contentsuggestions.ContentSuggestionsService";
62
63 private final IContentSuggestionsService mInterface = new IContentSuggestionsService.Stub() {
64 @Override
65 public void provideContextImage(int taskId, HardwareBuffer contextImage,
66 int colorSpaceId, Bundle imageContextRequestExtras) {
67 if (imageContextRequestExtras.containsKey(ContentSuggestionsManager.EXTRA_BITMAP)
68 && contextImage != null) {
69 throw new IllegalArgumentException("Two bitmaps provided; expected one.");
70 }
71
72 Bitmap wrappedBuffer = null;
73 if (imageContextRequestExtras.containsKey(ContentSuggestionsManager.EXTRA_BITMAP)) {
74 wrappedBuffer = imageContextRequestExtras.getParcelable(
75 ContentSuggestionsManager.EXTRA_BITMAP);
76 } else {
77 if (contextImage != null) {
78 ColorSpace colorSpace = null;
79 if (colorSpaceId >= 0 && colorSpaceId < ColorSpace.Named.values().length) {
80 colorSpace = ColorSpace.get(ColorSpace.Named.values()[colorSpaceId]);
81 }
82 wrappedBuffer = Bitmap.wrapHardwareBuffer(contextImage, colorSpace);
83 contextImage.close();
84 }
85 }
86
87 mHandler.sendMessage(
88 obtainMessage(ContentSuggestionsService::onProcessContextImage,
89 ContentSuggestionsService.this, taskId,
90 wrappedBuffer,
91 imageContextRequestExtras));
92 }
93
94 @Override
95 public void suggestContentSelections(SelectionsRequest request,
96 ISelectionsCallback callback) {
97 mHandler.sendMessage(obtainMessage(
98 ContentSuggestionsService::onSuggestContentSelections,
99 ContentSuggestionsService.this, request, wrapSelectionsCallback(callback)));
100
101 }
102
103 @Override
104 public void classifyContentSelections(ClassificationsRequest request,
105 IClassificationsCallback callback) {
106 mHandler.sendMessage(obtainMessage(
107 ContentSuggestionsService::onClassifyContentSelections,
108 ContentSuggestionsService.this, request, wrapClassificationCallback(callback)));
109 }
110
111 @Override
112 public void notifyInteraction(String requestId, Bundle interaction) {
113 mHandler.sendMessage(
114 obtainMessage(ContentSuggestionsService::onNotifyInteraction,
115 ContentSuggestionsService.this, requestId, interaction));
116 }
117 };
118
119 @CallSuper
120 @Override
121 public void onCreate() {
122 super.onCreate();
123 mHandler = new Handler(Looper.getMainLooper(), null, true);
124 }
125
126 /** @hide */
127 @Override
128 public final IBinder onBind(Intent intent) {
129 if (SERVICE_INTERFACE.equals(intent.getAction())) {
130 return mInterface.asBinder();
131 }
132 Log.w(TAG, "Tried to bind to wrong intent (should be " + SERVICE_INTERFACE + ": " + intent);
133 return null;
134 }
135
136 /**
137 * Called by the system to provide the snapshot for the task associated with the given
138 * {@param taskId}.
139 */
140 public abstract void onProcessContextImage(
141 int taskId, @Nullable Bitmap contextImage, @NonNull Bundle extras);
142
143 /**
144 * Content selections have been request through {@link ContentSuggestionsManager}, implementer
145 * should reply on the callback with selections.
146 */
147 public abstract void onSuggestContentSelections(@NonNull SelectionsRequest request,
148 @NonNull ContentSuggestionsManager.SelectionsCallback callback);
149
150 /**
151 * Content classifications have been request through {@link ContentSuggestionsManager},
152 * implementer should reply on the callback with classifications.
153 */
154 public abstract void onClassifyContentSelections(@NonNull ClassificationsRequest request,
155 @NonNull ContentSuggestionsManager.ClassificationsCallback callback);
156
157 /**
158 * User interactions have been reported through {@link ContentSuggestionsManager}, implementer
159 * should handle those interactions.
160 */
161 public abstract void onNotifyInteraction(
162 @NonNull String requestId, @NonNull Bundle interaction);
163
164 private ContentSuggestionsManager.SelectionsCallback wrapSelectionsCallback(
165 ISelectionsCallback callback) {
166 return (statusCode, selections) -> {
167 try {
168 callback.onContentSelectionsAvailable(statusCode, selections);
169 } catch (RemoteException e) {
170 Slog.e(TAG, "Error sending result: " + e);
171 }
172 };
173 }
174
175 private ContentSuggestionsManager.ClassificationsCallback wrapClassificationCallback(
176 IClassificationsCallback callback) {
177 return ((statusCode, classifications) -> {
178 try {
179 callback.onContentClassificationsAvailable(statusCode, classifications);
180 } catch (RemoteException e) {
181 Slog.e(TAG, "Error sending result: " + e);
182 }
183 });
184 }
185 }