Merge "ContentSuggestionsManager - API updates."

This commit is contained in:
Zak Cohen
2019-03-15 16:09:39 +00:00
committed by Android (Google) Code Review
5 changed files with 77 additions and 20 deletions

View File

@@ -985,6 +985,7 @@ package android.app.contentsuggestions {
public final class ContentSuggestionsManager {
method public void classifyContentSelections(@NonNull android.app.contentsuggestions.ClassificationsRequest, @NonNull java.util.concurrent.Executor, @NonNull android.app.contentsuggestions.ContentSuggestionsManager.ClassificationsCallback);
method public boolean isEnabled();
method public void notifyInteraction(@NonNull String, @NonNull android.os.Bundle);
method public void provideContextImage(int, @NonNull android.os.Bundle);
method public void suggestContentSelections(@NonNull android.app.contentsuggestions.SelectionsRequest, @NonNull java.util.concurrent.Executor, @NonNull android.app.contentsuggestions.ContentSuggestionsManager.SelectionsCallback);

View File

@@ -1188,7 +1188,7 @@ final class SystemServiceRegistry {
Context.CONTENT_SUGGESTIONS_SERVICE);
IContentSuggestionsManager service =
IContentSuggestionsManager.Stub.asInterface(b);
return new ContentSuggestionsManager(service);
return new ContentSuggestionsManager(ctx.getUserId(), service);
}
});

View File

@@ -20,11 +20,14 @@ import android.annotation.CallbackExecutor;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.annotation.UserIdInt;
import android.os.Binder;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import com.android.internal.util.SyncResultReceiver;
import java.util.List;
import java.util.concurrent.Executor;
@@ -44,12 +47,22 @@ import java.util.concurrent.Executor;
public final class ContentSuggestionsManager {
private static final String TAG = ContentSuggestionsManager.class.getSimpleName();
/**
* Timeout for calls to system_server.
*/
private static final int SYNC_CALLS_TIMEOUT_MS = 5000;
@Nullable
private final IContentSuggestionsManager mService;
@NonNull
private final int mUser;
/** @hide */
public ContentSuggestionsManager(@Nullable IContentSuggestionsManager service) {
public ContentSuggestionsManager(
@UserIdInt int userId, @Nullable IContentSuggestionsManager service) {
mService = service;
mUser = userId;
}
/**
@@ -60,14 +73,15 @@ public final class ContentSuggestionsManager {
* @param imageContextRequestExtras sent with with request to provide implementation specific
* extra information.
*/
public void provideContextImage(int taskId, @NonNull Bundle imageContextRequestExtras) {
public void provideContextImage(
int taskId, @NonNull Bundle imageContextRequestExtras) {
if (mService == null) {
Log.e(TAG, "provideContextImage called, but no ContentSuggestionsManager configured");
return;
}
try {
mService.provideContextImage(taskId, imageContextRequestExtras);
mService.provideContextImage(mUser, taskId, imageContextRequestExtras);
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
@@ -96,7 +110,7 @@ public final class ContentSuggestionsManager {
try {
mService.suggestContentSelections(
request, new SelectionsCallbackWrapper(callback, callbackExecutor));
mUser, request, new SelectionsCallbackWrapper(callback, callbackExecutor));
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
@@ -123,7 +137,7 @@ public final class ContentSuggestionsManager {
try {
mService.classifyContentSelections(
request, new ClassificationsCallbackWrapper(callback, callbackExecutor));
mUser, request, new ClassificationsCallbackWrapper(callback, callbackExecutor));
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
@@ -135,19 +149,41 @@ public final class ContentSuggestionsManager {
* @param requestId the id for the associated interaction
* @param interaction to report back to the system content suggestions service.
*/
public void notifyInteraction(@NonNull String requestId, @NonNull Bundle interaction) {
public void notifyInteraction(
@NonNull String requestId, @NonNull Bundle interaction) {
if (mService == null) {
Log.e(TAG, "notifyInteraction called, but no ContentSuggestionsManager configured");
return;
}
try {
mService.notifyInteraction(requestId, interaction);
mService.notifyInteraction(mUser, requestId, interaction);
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
}
/**
* Indicates that Content Suggestions is available and enabled for the provided user. That is,
* has an implementation and not disabled through device management.
*
* @return {@code true} if Content Suggestions is enabled and available for the provided user.
*/
public boolean isEnabled() {
if (mService == null) {
return false;
}
SyncResultReceiver receiver = new SyncResultReceiver(SYNC_CALLS_TIMEOUT_MS);
try {
mService.isEnabled(mUser, receiver);
return receiver.getIntResult() != 0;
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
return false;
}
/**
* Callback to receive content selections from
* {@link #suggestContentSelections(SelectionsRequest, Executor, SelectionsCallback)}.

View File

@@ -21,17 +21,23 @@ import android.app.contentsuggestions.ISelectionsCallback;
import android.app.contentsuggestions.ClassificationsRequest;
import android.app.contentsuggestions.SelectionsRequest;
import android.os.Bundle;
import android.os.UserHandle;
import com.android.internal.os.IResultReceiver;
/** @hide */
oneway interface IContentSuggestionsManager {
void provideContextImage(
int userId,
int taskId,
in Bundle imageContextRequestExtras);
void suggestContentSelections(
int userId,
in SelectionsRequest request,
in ISelectionsCallback callback);
void classifyContentSelections(
int userId,
in ClassificationsRequest request,
in IClassificationsCallback callback);
void notifyInteraction(in String requestId, in Bundle interaction);
void notifyInteraction(int userId, in String requestId, in Bundle interaction);
void isEnabled(int userId, in IResultReceiver receiver);
}

View File

@@ -35,6 +35,7 @@ import android.os.UserHandle;
import android.os.UserManager;
import android.util.Slog;
import com.android.internal.os.IResultReceiver;
import com.android.server.LocalServices;
import com.android.server.infra.AbstractMasterSystemService;
import com.android.server.infra.FrameworkResourcesServiceNameResolver;
@@ -114,13 +115,14 @@ public class ContentSuggestionsManagerService extends
private class ContentSuggestionsManagerStub extends IContentSuggestionsManager.Stub {
@Override
public void provideContextImage(int taskId, @NonNull Bundle imageContextRequestExtras) {
public void provideContextImage(
int userId,
int taskId,
@NonNull Bundle imageContextRequestExtras) {
if (imageContextRequestExtras == null) {
throw new IllegalArgumentException("Expected non-null imageContextRequestExtras");
}
final int userId = UserHandle.getCallingUserId();
enforceCallerIsRecents(userId, "provideContextImage");
enforceCallerIsRecents(UserHandle.getCallingUserId(), "provideContextImage");
synchronized (mLock) {
final ContentSuggestionsPerUserService service = getServiceForUserLocked(userId);
@@ -136,10 +138,10 @@ public class ContentSuggestionsManagerService extends
@Override
public void suggestContentSelections(
int userId,
@NonNull SelectionsRequest selectionsRequest,
@NonNull ISelectionsCallback selectionsCallback) {
final int userId = UserHandle.getCallingUserId();
enforceCallerIsRecents(userId, "suggestContentSelections");
enforceCallerIsRecents(UserHandle.getCallingUserId(), "suggestContentSelections");
synchronized (mLock) {
final ContentSuggestionsPerUserService service = getServiceForUserLocked(userId);
@@ -155,10 +157,10 @@ public class ContentSuggestionsManagerService extends
@Override
public void classifyContentSelections(
int userId,
@NonNull ClassificationsRequest classificationsRequest,
@NonNull IClassificationsCallback callback) {
final int userId = UserHandle.getCallingUserId();
enforceCallerIsRecents(userId, "classifyContentSelections");
enforceCallerIsRecents(UserHandle.getCallingUserId(), "classifyContentSelections");
synchronized (mLock) {
final ContentSuggestionsPerUserService service = getServiceForUserLocked(userId);
@@ -173,9 +175,9 @@ public class ContentSuggestionsManagerService extends
}
@Override
public void notifyInteraction(@NonNull String requestId, @NonNull Bundle bundle) {
final int userId = UserHandle.getCallingUserId();
enforceCallerIsRecents(userId, "notifyInteraction");
public void notifyInteraction(
int userId, @NonNull String requestId, @NonNull Bundle bundle) {
enforceCallerIsRecents(UserHandle.getCallingUserId(), "notifyInteraction");
synchronized (mLock) {
final ContentSuggestionsPerUserService service = getServiceForUserLocked(userId);
@@ -189,6 +191,18 @@ public class ContentSuggestionsManagerService extends
}
}
@Override
public void isEnabled(int userId, @NonNull IResultReceiver receiver)
throws RemoteException {
enforceCallerIsRecents(UserHandle.getCallingUserId(), "isEnabled");
boolean isDisabled;
synchronized (mLock) {
isDisabled = isDisabledLocked(userId);
}
receiver.send(isDisabled ? 0 : 1, null);
}
public void onShellCommand(@Nullable FileDescriptor in, @Nullable FileDescriptor out,
@Nullable FileDescriptor err,
@NonNull String[] args, @Nullable ShellCallback callback,