/* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.service.autofill; import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.SystemApi; import android.app.Service; import android.content.Intent; import android.content.IntentSender; import android.graphics.PixelFormat; import android.os.BaseBundle; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Looper; import android.os.RemoteCallback; import android.os.RemoteException; import android.util.Log; import android.util.LruCache; import android.util.Size; import android.view.Display; import android.view.SurfaceControlViewHost; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.window.InputTransferToken; import java.io.FileDescriptor; import java.io.PrintWriter; import java.lang.ref.WeakReference; /** * A service that renders an inline presentation view given the {@link InlinePresentation}. * * {@hide} */ @SystemApi public abstract class InlineSuggestionRenderService extends Service { private static final String TAG = "InlineSuggestionRenderService"; /** * The {@link Intent} that must be declared as handled by the service. * *
To be supported, the service must also require the
* {@link android.Manifest.permission#BIND_INLINE_SUGGESTION_RENDER_SERVICE} permission so that
* other applications can not abuse it.
*/
public static final String SERVICE_INTERFACE =
"android.service.autofill.InlineSuggestionRenderService";
private final Handler mMainHandler = new Handler(Looper.getMainLooper(), null, true);
private IInlineSuggestionUiCallback mCallback;
/**
* A local LRU cache keeping references to the inflated {@link SurfaceControlViewHost}s, so
* they can be released properly when no longer used. Each view needs to be tracked separately,
* therefore for simplicity we use the hash code of the value object as key in the cache.
*/
private final LruCache This class is thread safe, because all the outside calls are piped into a single
* handler thread to be processed.
*/
private final class InlineSuggestionUiImpl {
@Nullable
private SurfaceControlViewHost mViewHost;
@NonNull
private final Handler mHandler;
private final int mUserId;
private final int mSessionId;
InlineSuggestionUiImpl(SurfaceControlViewHost viewHost, Handler handler, int userId,
int sessionId) {
this.mViewHost = viewHost;
this.mHandler = handler;
this.mUserId = userId;
this.mSessionId = sessionId;
}
/**
* Call {@link SurfaceControlViewHost#release()} to release it. After this, this view is
* not usable, and any further calls to the
* {@link #getSurfacePackage(ISurfacePackageResultCallback)} will get {@code null} result.
*/
public void releaseSurfaceControlViewHost() {
mHandler.post(() -> {
if (mViewHost == null) {
return;
}
Log.v(TAG, "Releasing inline suggestion view host");
mViewHost.release();
mViewHost = null;
InlineSuggestionRenderService.this.mActiveInlineSuggestions.remove(
InlineSuggestionUiImpl.this);
Log.v(TAG, "Removed the inline suggestion from the cache, current size="
+ InlineSuggestionRenderService.this.mActiveInlineSuggestions.size());
});
}
/**
* Sends back a new {@link android.view.SurfaceControlViewHost.SurfacePackage} if the view
* is not released, {@code null} otherwise.
*/
public void getSurfacePackage(ISurfacePackageResultCallback callback) {
Log.d(TAG, "getSurfacePackage");
mHandler.post(() -> {
try {
callback.onResult(mViewHost == null ? null : mViewHost.getSurfacePackage());
} catch (RemoteException e) {
Log.w(TAG, "RemoteException calling onSurfacePackage");
}
});
}
}
/** @hide */
@Override
protected final void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw,
@NonNull String[] args) {
pw.println("mActiveInlineSuggestions: " + mActiveInlineSuggestions.size());
for (InlineSuggestionUiImpl impl : mActiveInlineSuggestions.snapshot().keySet()) {
pw.printf("ui: [%s] - [%d] [%d]\n", impl, impl.mUserId, impl.mSessionId);
}
}
@Override
@Nullable
public final IBinder onBind(@NonNull Intent intent) {
BaseBundle.setShouldDefuse(true);
if (SERVICE_INTERFACE.equals(intent.getAction())) {
return new IInlineSuggestionRenderService.Stub() {
@Override
public void renderSuggestion(@NonNull IInlineSuggestionUiCallback callback,
@NonNull InlinePresentation presentation, int width, int height,
@Nullable IBinder hostInputToken, int displayId, int userId,
int sessionId) {
mMainHandler.sendMessage(
obtainMessage(InlineSuggestionRenderService::handleRenderSuggestion,
InlineSuggestionRenderService.this, callback, presentation,
width, height, hostInputToken, displayId, userId, sessionId));
}
@Override
public void getInlineSuggestionsRendererInfo(@NonNull RemoteCallback callback) {
mMainHandler.sendMessage(obtainMessage(
InlineSuggestionRenderService::handleGetInlineSuggestionsRendererInfo,
InlineSuggestionRenderService.this, callback));
}
@Override
public void destroySuggestionViews(int userId, int sessionId) {
mMainHandler.sendMessage(obtainMessage(
InlineSuggestionRenderService::handleDestroySuggestionViews,
InlineSuggestionRenderService.this, userId, sessionId));
}
}.asBinder();
}
Log.w(TAG, "Tried to bind to wrong intent (should be " + SERVICE_INTERFACE + ": " + intent);
return null;
}
/**
* Starts the {@link IntentSender} from the client app.
*
* @param intentSender the {@link IntentSender} to start the attribution UI from the client
* app.
*/
public final void startIntentSender(@NonNull IntentSender intentSender) {
if (mCallback == null) return;
try {
mCallback.onStartIntentSender(intentSender);
} catch (RemoteException e) {
e.rethrowFromSystemServer();
}
}
/**
* Returns the metadata about the renderer. Returns {@code Bundle.Empty} if no metadata is
* provided.
*/
@NonNull
public Bundle onGetInlineSuggestionsRendererInfo() {
return Bundle.EMPTY;
}
/**
* Renders the slice into a view.
*/
@Nullable
public View onRenderSuggestion(@NonNull InlinePresentation presentation, int width,
int height) {
Log.e(TAG, "service implementation (" + getClass() + " does not implement "
+ "onRenderSuggestion()");
return null;
}
}