| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 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 |
|
| 138 |
|
| 139 |
|
| 140 |
public abstract void onProcessContextImage( |
| 141 |
int taskId, @Nullable Bitmap contextImage, @NonNull Bundle extras); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public abstract void onSuggestContentSelections(@NonNull SelectionsRequest request, |
| 148 |
@NonNull ContentSuggestionsManager.SelectionsCallback callback); |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public abstract void onClassifyContentSelections(@NonNull ClassificationsRequest request, |
| 155 |
@NonNull ContentSuggestionsManager.ClassificationsCallback callback); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 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 |
} |