| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.contentsuggestions; |
| 18 |
|
| 19 |
import android.annotation.CallbackExecutor; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.RequiresPermission; |
| 23 |
import android.annotation.SystemApi; |
| 24 |
import android.annotation.TestApi; |
| 25 |
import android.annotation.UserIdInt; |
| 26 |
import android.graphics.Bitmap; |
| 27 |
import android.os.Binder; |
| 28 |
import android.os.Bundle; |
| 29 |
import android.os.RemoteException; |
| 30 |
import android.util.Log; |
| 31 |
|
| 32 |
import com.android.internal.util.SyncResultReceiver; |
| 33 |
|
| 34 |
import java.util.List; |
| 35 |
import java.util.concurrent.Executor; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
@SystemApi |
| 50 |
public final class ContentSuggestionsManager { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public static final String EXTRA_BITMAP = "android.contentsuggestions.extra.BITMAP"; |
| 61 |
|
| 62 |
private static final String TAG = ContentSuggestionsManager.class.getSimpleName(); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private static final int SYNC_CALLS_TIMEOUT_MS = 5000; |
| 68 |
|
| 69 |
@Nullable |
| 70 |
private final IContentSuggestionsManager mService; |
| 71 |
|
| 72 |
@NonNull |
| 73 |
private final int mUser; |
| 74 |
|
| 75 |
|
| 76 |
public ContentSuggestionsManager( |
| 77 |
@UserIdInt int userId, @Nullable IContentSuggestionsManager service) { |
| 78 |
mService = service; |
| 79 |
mUser = userId; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public void provideContextImage( |
| 91 |
@NonNull Bitmap bitmap, @NonNull Bundle imageContextRequestExtras) { |
| 92 |
if (mService == null) { |
| 93 |
Log.e(TAG, "provideContextImage called, but no ContentSuggestionsManager configured"); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
try { |
| 98 |
mService.provideContextBitmap(mUser, bitmap, imageContextRequestExtras); |
| 99 |
} catch (RemoteException e) { |
| 100 |
throw e.rethrowFromSystemServer(); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public void provideContextImage( |
| 113 |
int taskId, @NonNull Bundle imageContextRequestExtras) { |
| 114 |
if (mService == null) { |
| 115 |
Log.e(TAG, "provideContextImage called, but no ContentSuggestionsManager configured"); |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
try { |
| 120 |
mService.provideContextImage(mUser, taskId, imageContextRequestExtras); |
| 121 |
} catch (RemoteException e) { |
| 122 |
throw e.rethrowFromSystemServer(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public void suggestContentSelections( |
| 138 |
@NonNull SelectionsRequest request, |
| 139 |
@NonNull @CallbackExecutor Executor callbackExecutor, |
| 140 |
@NonNull SelectionsCallback callback) { |
| 141 |
if (mService == null) { |
| 142 |
Log.e(TAG, |
| 143 |
"suggestContentSelections called, but no ContentSuggestionsManager configured"); |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
try { |
| 148 |
mService.suggestContentSelections( |
| 149 |
mUser, request, new SelectionsCallbackWrapper(callback, callbackExecutor)); |
| 150 |
} catch (RemoteException e) { |
| 151 |
throw e.rethrowFromSystemServer(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public void classifyContentSelections( |
| 165 |
@NonNull ClassificationsRequest request, |
| 166 |
@NonNull @CallbackExecutor Executor callbackExecutor, |
| 167 |
@NonNull ClassificationsCallback callback) { |
| 168 |
if (mService == null) { |
| 169 |
Log.e(TAG, "classifyContentSelections called, " |
| 170 |
+ "but no ContentSuggestionsManager configured"); |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
try { |
| 175 |
mService.classifyContentSelections( |
| 176 |
mUser, request, new ClassificationsCallbackWrapper(callback, callbackExecutor)); |
| 177 |
} catch (RemoteException e) { |
| 178 |
throw e.rethrowFromSystemServer(); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public void notifyInteraction( |
| 189 |
@NonNull String requestId, @NonNull Bundle interaction) { |
| 190 |
if (mService == null) { |
| 191 |
Log.e(TAG, "notifyInteraction called, but no ContentSuggestionsManager configured"); |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
try { |
| 196 |
mService.notifyInteraction(mUser, requestId, interaction); |
| 197 |
} catch (RemoteException e) { |
| 198 |
throw e.rethrowFromSystemServer(); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public boolean isEnabled() { |
| 209 |
if (mService == null) { |
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
SyncResultReceiver receiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS); |
| 214 |
try { |
| 215 |
mService.isEnabled(mUser, receiver); |
| 216 |
return receiver.getIntResult() != 0; |
| 217 |
} catch (RemoteException e) { |
| 218 |
throw e.rethrowFromSystemServer(); |
| 219 |
} catch (SyncResultReceiver.TimeoutException e) { |
| 220 |
throw new RuntimeException("Fail to get the enable status."); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
@TestApi |
| 230 |
@RequiresPermission(android.Manifest.permission.MANAGE_CONTENT_SUGGESTIONS) |
| 231 |
public void resetTemporaryService(@UserIdInt int userId) { |
| 232 |
if (mService == null) { |
| 233 |
Log.e(TAG, "resetTemporaryService called, but no ContentSuggestionsManager " |
| 234 |
+ "configured"); |
| 235 |
return; |
| 236 |
} |
| 237 |
try { |
| 238 |
mService.resetTemporaryService(userId); |
| 239 |
} catch (RemoteException e) { |
| 240 |
throw e.rethrowFromSystemServer(); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
@TestApi |
| 255 |
@RequiresPermission(android.Manifest.permission.MANAGE_CONTENT_SUGGESTIONS) |
| 256 |
public void setTemporaryService( |
| 257 |
@UserIdInt int userId, @NonNull String serviceName, int duration) { |
| 258 |
if (mService == null) { |
| 259 |
Log.e(TAG, "setTemporaryService called, but no ContentSuggestionsManager " |
| 260 |
+ "configured"); |
| 261 |
return; |
| 262 |
} |
| 263 |
try { |
| 264 |
mService.setTemporaryService(userId, serviceName, duration); |
| 265 |
} catch (RemoteException e) { |
| 266 |
throw e.rethrowFromSystemServer(); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
@TestApi |
| 276 |
@RequiresPermission(android.Manifest.permission.MANAGE_CONTENT_SUGGESTIONS) |
| 277 |
public void setDefaultServiceEnabled(@UserIdInt int userId, boolean enabled) { |
| 278 |
if (mService == null) { |
| 279 |
Log.e(TAG, "setDefaultServiceEnabled called, but no ContentSuggestionsManager " |
| 280 |
+ "configured"); |
| 281 |
return; |
| 282 |
} |
| 283 |
try { |
| 284 |
mService.setDefaultServiceEnabled(userId, enabled); |
| 285 |
} catch (RemoteException e) { |
| 286 |
throw e.rethrowFromSystemServer(); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public interface SelectionsCallback { |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
void onContentSelectionsAvailable( |
| 304 |
int statusCode, @NonNull List<ContentSelection> selections); |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
public interface ClassificationsCallback { |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
void onContentClassificationsAvailable(int statusCode, |
| 320 |
@NonNull List<ContentClassification> classifications); |
| 321 |
} |
| 322 |
|
| 323 |
private static class SelectionsCallbackWrapper extends ISelectionsCallback.Stub { |
| 324 |
private final SelectionsCallback mCallback; |
| 325 |
private final Executor mExecutor; |
| 326 |
|
| 327 |
SelectionsCallbackWrapper( |
| 328 |
@NonNull SelectionsCallback callback, @NonNull Executor executor) { |
| 329 |
mCallback = callback; |
| 330 |
mExecutor = executor; |
| 331 |
} |
| 332 |
|
| 333 |
@Override |
| 334 |
public void onContentSelectionsAvailable( |
| 335 |
int statusCode, List<ContentSelection> selections) { |
| 336 |
final long identity = Binder.clearCallingIdentity(); |
| 337 |
try { |
| 338 |
mExecutor.execute(() -> |
| 339 |
mCallback.onContentSelectionsAvailable(statusCode, selections)); |
| 340 |
} finally { |
| 341 |
Binder.restoreCallingIdentity(identity); |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
private static final class ClassificationsCallbackWrapper extends |
| 347 |
IClassificationsCallback.Stub { |
| 348 |
private final ClassificationsCallback mCallback; |
| 349 |
private final Executor mExecutor; |
| 350 |
|
| 351 |
ClassificationsCallbackWrapper(@NonNull ClassificationsCallback callback, |
| 352 |
@NonNull Executor executor) { |
| 353 |
mCallback = callback; |
| 354 |
mExecutor = executor; |
| 355 |
} |
| 356 |
|
| 357 |
@Override |
| 358 |
public void onContentClassificationsAvailable( |
| 359 |
int statusCode, List<ContentClassification> classifications) { |
| 360 |
final long identity = Binder.clearCallingIdentity(); |
| 361 |
try { |
| 362 |
mExecutor.execute(() -> |
| 363 |
mCallback.onContentClassificationsAvailable(statusCode, classifications)); |
| 364 |
} finally { |
| 365 |
Binder.restoreCallingIdentity(identity); |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
} |