New APIs for ContentCaptureService: onConnected() and onDisconnected()

Bug: 117944706
Test: atest CtsContentCaptureServiceTestCases:android.contentcaptureservice.cts.BlankActivityTest#testDisconnected
Test: atest CtsContentCaptureServiceTestCases

Change-Id: Iba3c1ae774221946a550fad95539d3a9771ae3d7
This commit is contained in:
Felipe Leme
2018-12-21 09:29:27 -08:00
committed by Winson Chung
parent 50b33dce59
commit bb98ed6521
6 changed files with 75 additions and 3 deletions

View File

@@ -31,6 +31,7 @@ import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.service.autofill.AutofillService;
import android.util.ArrayMap;
import android.util.Log;
import android.util.Slog;
@@ -81,6 +82,12 @@ public abstract class ContentCaptureService extends Service {
*/
private final IContentCaptureService mServerInterface = new IContentCaptureService.Stub() {
@Override
public void onConnectedStateChanged(boolean state) {
mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnConnectedStateChanged,
ContentCaptureService.this, state));
}
@Override
public void onSessionStarted(ContentCaptureContext context, String sessionId, int uid,
IResultReceiver clientReceiver) {
@@ -203,6 +210,15 @@ public abstract class ContentCaptureService extends Service {
return null;
}
/**
* Called when the Android system connects to service.
*
* <p>You should generally do initialization here rather than in {@link #onCreate}.
*/
public void onConnected() {
Slog.i(TAG, "bound to " + getClass().getName());
}
/**
* Creates a new content capture session.
*
@@ -257,6 +273,15 @@ public abstract class ContentCaptureService extends Service {
if (VERBOSE) Log.v(TAG, "onDestroyContentCaptureSession(id=" + sessionId + ")");
}
/**
* Called when the Android system disconnects from the service.
*
* <p> At this point this service may no longer be an active {@link AutofillService}.
*/
public void onDisconnected() {
Slog.i(TAG, "unbinding from " + getClass().getName());
}
@Override
@CallSuper
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
@@ -271,6 +296,14 @@ public abstract class ContentCaptureService extends Service {
}
}
private void handleOnConnectedStateChanged(boolean state) {
if (state) {
onConnected();
} else {
onDisconnected();
}
}
//TODO(b/111276913): consider caching the InteractionSessionId for the lifetime of the session,
// so we don't need to create a temporary InteractionSessionId for each event.

View File

@@ -29,6 +29,7 @@ import java.util.List;
* @hide
*/
oneway interface IContentCaptureService {
void onConnectedStateChanged(boolean state);
void onSessionStarted(in ContentCaptureContext context, String sessionId, int uid,
in IResultReceiver clientReceiver);
void onSessionFinished(String sessionId);

View File

@@ -63,7 +63,8 @@ import java.lang.ref.WeakReference;
//TODO(b/117779333): improve javadoc above instead of using Autofill as an example
public abstract class AbstractRemoteService<S extends AbstractRemoteService<S, I>,
I extends IInterface> implements DeathRecipient {
private static final int MSG_UNBIND = 1;
private static final int MSG_BIND = 1;
private static final int MSG_UNBIND = 2;
protected static final long PERMANENT_BOUND_TIMEOUT_MS = 0;
@@ -106,7 +107,7 @@ public abstract class AbstractRemoteService<S extends AbstractRemoteService<S, I
void onServiceDied(T service);
}
// NOTE: must be package-protected so this class is not extend outside
// NOTE: must be package-protected so this class is not extended outside
AbstractRemoteService(@NonNull Context context, @NonNull String serviceInterface,
@NonNull ComponentName componentName, int userId, @NonNull VultureCallback<S> callback,
boolean bindInstantServiceAllowed, boolean verbose) {
@@ -284,6 +285,25 @@ public abstract class AbstractRemoteService<S extends AbstractRemoteService<S, I
mHandler.removeMessages(MSG_UNBIND);
}
/**
* Schedules a request to bind to the remote service.
*
* <p>Typically used on constructor for implementations that need a permanent connection to
* the remote service.
*/
protected void scheduleBind() {
if (mHandler.hasMessages(MSG_BIND)) {
if (mVerbose) Slog.v(mTag, "scheduleBind(): already scheduled");
return;
}
mHandler.sendMessage(obtainMessage(AbstractRemoteService::handleEnsureBound, this)
.setWhat(MSG_BIND));
}
/**
* Schedules a request to automatically unbind from the service after the
* {@link #getTimeoutIdleBindMillis() idle timeout} expires.
*/
protected void scheduleUnbind() {
final long unbindDelay = getTimeoutIdleBindMillis();