diff options
author | 2018-12-17 13:11:48 -0800 | |
---|---|---|
committer | 2019-06-27 15:14:45 +0100 | |
commit | 9d431e1cc85b88afc1e06782e832e8453decdc95 (patch) | |
tree | 693c7fad2c9a187a2540fe510edfb7df0f47fd65 | |
parent | 482c2c994d56a684c5945fccce7ce5ea6a60396c (diff) |
Enable views to be placed in windowless surfaces.
For purposes of parcelling SurfaceControl to embed view hierarchies across
processes. We also want these Surfaces to receive input, but we can't
let the clients call setInputWindowInfo directly (or we could have issues
with clients stealing focus, etc...) and so we provide a method
blessInputSurface which has the WM configure a surface for input with
a minimal and safe set of parameters.
Test: WindowlessWmTests
Bug: 111373437
Bug: 134365580
Change-Id: I45fde62ba9b810e783d62c4dd5442abd038734d5
-rw-r--r-- | core/java/android/view/IWindowSession.aidl | 6 | ||||
-rw-r--r-- | core/java/android/view/InputChannel.java | 9 | ||||
-rw-r--r-- | core/java/android/view/ViewRootImpl.java | 7 | ||||
-rw-r--r-- | core/java/android/view/WindowlessViewRoot.java | 41 | ||||
-rw-r--r-- | core/java/android/view/WindowlessWindowManager.java | 109 | ||||
-rw-r--r-- | core/jni/android_view_InputChannel.cpp | 11 | ||||
-rw-r--r-- | services/core/java/com/android/server/wm/Session.java | 12 | ||||
-rw-r--r-- | services/core/java/com/android/server/wm/WindowManagerService.java | 50 | ||||
-rw-r--r-- | tests/WindowlessWmTest/Android.bp | 22 | ||||
-rw-r--r-- | tests/WindowlessWmTest/AndroidManifest.xml | 28 | ||||
-rw-r--r-- | tests/WindowlessWmTest/src/com/android/test/viewembed/WindowlessWmTest.java | 78 |
11 files changed, 371 insertions, 2 deletions
diff --git a/core/java/android/view/IWindowSession.aidl b/core/java/android/view/IWindowSession.aidl index 37b6f1317fbf..4bfd1387bef4 100644 --- a/core/java/android/view/IWindowSession.aidl +++ b/core/java/android/view/IWindowSession.aidl @@ -309,4 +309,10 @@ interface IWindowSession { * Called when the system gesture exclusion has changed. */ void reportSystemGestureExclusionChanged(IWindow window, in List<Rect> exclusionRects); + + /** + * Request the server to call setInputWindowInfo on a given Surface, and return + * an input channel where the client can receive input. + */ + void blessInputSurface(int displayId, in SurfaceControl surface, out InputChannel outInputChannel); } diff --git a/core/java/android/view/InputChannel.java b/core/java/android/view/InputChannel.java index ecb727c79016..831e9ee43546 100644 --- a/core/java/android/view/InputChannel.java +++ b/core/java/android/view/InputChannel.java @@ -55,6 +55,7 @@ public final class InputChannel implements Parcelable { private static native InputChannel[] nativeOpenInputChannelPair(String name); private native void nativeDispose(boolean finalized); + private native void nativeRelease(); private native void nativeTransferTo(InputChannel other); private native void nativeReadFromParcel(Parcel parcel); private native void nativeWriteToParcel(Parcel parcel); @@ -120,6 +121,14 @@ public final class InputChannel implements Parcelable { } /** + * Release the Java objects hold over the native InputChannel. If other references + * still exist in native-land, then the channel may continue to exist. + */ + public void release() { + nativeRelease(); + } + + /** * Transfers ownership of the internal state of the input channel to another * instance and invalidates this instance. This is used to pass an input channel * as an out parameter in a binder call. diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java index 6b30f5eb5412..09be4b211e5b 100644 --- a/core/java/android/view/ViewRootImpl.java +++ b/core/java/android/view/ViewRootImpl.java @@ -609,10 +609,13 @@ public final class ViewRootImpl implements ViewParent, } private String mTag = TAG; - public ViewRootImpl(Context context, Display display) { + this(context, display, WindowManagerGlobal.getWindowSession()); + } + + public ViewRootImpl(Context context, Display display, IWindowSession session) { mContext = context; - mWindowSession = WindowManagerGlobal.getWindowSession(); + mWindowSession = session; mDisplay = display; mBasePackageName = context.getBasePackageName(); mThread = Thread.currentThread(); diff --git a/core/java/android/view/WindowlessViewRoot.java b/core/java/android/view/WindowlessViewRoot.java new file mode 100644 index 000000000000..75057a26afae --- /dev/null +++ b/core/java/android/view/WindowlessViewRoot.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2019 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.view; + +import android.content.res.Resources; +import android.content.Context; +import android.view.SurfaceControl; +import android.view.View; + +/** + * Utility class for adding a view hierarchy to a SurfaceControl. + * + * See WindowlessWmTest for example usage. + * @hide + */ +public class WindowlessViewRoot { + ViewRootImpl mViewRoot; + WindowlessWindowManager mWm; + public WindowlessViewRoot(Context c, Display d, SurfaceControl rootSurface) { + mWm = new WindowlessWindowManager(c.getResources().getConfiguration(), rootSurface); + mViewRoot = new ViewRootImpl(c, d, mWm); + } + + public void addView(View view, WindowManager.LayoutParams attrs) { + mViewRoot.setView(view, attrs, null); + } +} diff --git a/core/java/android/view/WindowlessWindowManager.java b/core/java/android/view/WindowlessWindowManager.java new file mode 100644 index 000000000000..dfeb4b5c6edf --- /dev/null +++ b/core/java/android/view/WindowlessWindowManager.java @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2019 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.view; + +import android.content.res.Configuration; +import android.graphics.Rect; +import android.os.IBinder; +import android.os.RemoteException; +import android.util.MergedConfiguration; +import android.util.Log; +import android.view.IWindowSession; +import android.view.SurfaceControl; +import android.view.SurfaceSession; + +import java.util.HashMap; + +/** +* A simplistic implementation of IWindowSession. Rather than managing Surfaces +* as children of the display, it manages Surfaces as children of a given root. +* +* By parcelling the root surface, the app can offer another app content for embedding. +* @hide +*/ +class WindowlessWindowManager extends IWindowSession.Default { + private final static String TAG = "WindowlessWindowManager"; + + /** + * Used to store SurfaceControl we've built for clients to + * reconfigure them if relayout is called. + */ + final HashMap<IBinder, SurfaceControl> mScForWindow = new HashMap<IBinder, SurfaceControl>(); + final SurfaceSession mSurfaceSession = new SurfaceSession(); + final SurfaceControl mRootSurface; + final Configuration mConfiguration; + IWindowSession mRealWm; + + private int mForceHeight = -1; + private int mForceWidth = -1; + + WindowlessWindowManager(Configuration c, SurfaceControl rootSurface) { + mRootSurface = rootSurface; + mConfiguration = new Configuration(c); + mRealWm = WindowManagerGlobal.getWindowSession(); + } + + public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs, + int viewVisibility, int displayId, Rect outFrame, Rect outContentInsets, + Rect outStableInsets, Rect outOutsets, + DisplayCutout.ParcelableWrapper outDisplayCutout, InputChannel outInputChannel, + InsetsState outInsetsState) { + final SurfaceControl.Builder b = new SurfaceControl.Builder(mSurfaceSession) + .setParent(mRootSurface) + .setName(attrs.getTitle().toString()); + final SurfaceControl sc = b.build(); + synchronized (this) { + mScForWindow.put(window.asBinder(), sc); + } + + if ((attrs.inputFeatures & + WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) { + try { + mRealWm.blessInputSurface(displayId, sc, outInputChannel); + } catch (RemoteException e) { + Log.e(TAG, "Failed to bless surface: " + e); + } + } + + return WindowManagerGlobal.ADD_OKAY | WindowManagerGlobal.ADD_FLAG_APP_VISIBLE; + } + + @Override + public int relayout(IWindow window, int seq, WindowManager.LayoutParams attrs, + int requestedWidth, int requestedHeight, int viewFlags, int flags, long frameNumber, + Rect outFrame, Rect outOverscanInsets, Rect outContentInsets, Rect outVisibleInsets, + Rect outStableInsets, Rect outsets, Rect outBackdropFrame, + DisplayCutout.ParcelableWrapper cutout, MergedConfiguration mergedConfiguration, + SurfaceControl outSurfaceControl, InsetsState outInsetsState) { + SurfaceControl sc = null; + synchronized (this) { + sc = mScForWindow.get(window.asBinder()); + } + if (sc == null) { + throw new IllegalArgumentException( + "Invalid window token (never added or removed already)"); + } + SurfaceControl.Transaction t = new SurfaceControl.Transaction(); + t.show(sc).setBufferSize(sc, requestedWidth, requestedHeight).apply(); + outSurfaceControl.copyFrom(sc); + outFrame.set(0, 0, requestedWidth, requestedHeight); + + mergedConfiguration.setConfiguration(mConfiguration, mConfiguration); + + return 0; + } +} diff --git a/core/jni/android_view_InputChannel.cpp b/core/jni/android_view_InputChannel.cpp index 9819d9a496c6..7e3b083607e0 100644 --- a/core/jni/android_view_InputChannel.cpp +++ b/core/jni/android_view_InputChannel.cpp @@ -175,6 +175,15 @@ static void android_view_InputChannel_nativeDispose(JNIEnv* env, jobject obj, jb } } +static void android_view_InputChannel_nativeRelease(JNIEnv* env, jobject obj, jboolean finalized) { + NativeInputChannel* nativeInputChannel = + android_view_InputChannel_getNativeInputChannel(env, obj); + if (nativeInputChannel) { + android_view_InputChannel_setNativeInputChannel(env, obj, NULL); + delete nativeInputChannel; + } +} + static void android_view_InputChannel_nativeTransferTo(JNIEnv* env, jobject obj, jobject otherObj) { if (android_view_InputChannel_getNativeInputChannel(env, otherObj) != NULL) { @@ -274,6 +283,8 @@ static const JNINativeMethod gInputChannelMethods[] = { (void*)android_view_InputChannel_nativeOpenInputChannelPair }, { "nativeDispose", "(Z)V", (void*)android_view_InputChannel_nativeDispose }, + { "nativeRelease", "()V", + (void*)android_view_InputChannel_nativeRelease }, { "nativeTransferTo", "(Landroid/view/InputChannel;)V", (void*)android_view_InputChannel_nativeTransferTo }, { "nativeReadFromParcel", "(Landroid/os/Parcel;)V", diff --git a/services/core/java/com/android/server/wm/Session.java b/services/core/java/com/android/server/wm/Session.java index 8d1cc71f7a6f..9f8f2651389a 100644 --- a/services/core/java/com/android/server/wm/Session.java +++ b/services/core/java/com/android/server/wm/Session.java @@ -612,4 +612,16 @@ class Session extends IWindowSession.Stub implements IBinder.DeathRecipient { boolean hasAlertWindowSurfaces() { return !mAlertWindowSurfaces.isEmpty(); } + + public void blessInputSurface(int displayId, SurfaceControl surface, + InputChannel outInputChannel) { + final int callerUid = Binder.getCallingUid(); + final int callerPid = Binder.getCallingPid(); + final long identity = Binder.clearCallingIdentity(); + try { + mService.blessInputSurface(callerUid, callerPid, displayId, surface, outInputChannel); + } finally { + Binder.restoreCallingIdentity(identity); + } + } } diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java index 264eedafc56e..fc0779a7b38a 100644 --- a/services/core/java/com/android/server/wm/WindowManagerService.java +++ b/services/core/java/com/android/server/wm/WindowManagerService.java @@ -216,6 +216,7 @@ import android.view.InputChannel; import android.view.InputDevice; import android.view.InputEvent; import android.view.InputEventReceiver; +import android.view.InputWindowHandle; import android.view.InsetsState; import android.view.KeyEvent; import android.view.MagnificationSpec; @@ -7748,4 +7749,53 @@ public class WindowManagerService extends IWindowManager.Stub 0 /* configChanges */, !PRESERVE_WINDOWS, true /* notifyClients */); } } + + /** + * Assigns an InputChannel to a SurfaceControl and configures it to receive + * touch input according to it's on-screen geometry. + * + * Used by WindowlessWindowManager to enable input on SurfaceControl embedded + * views. + */ + void blessInputSurface(int callingUid, int callingPid, int displayId, SurfaceControl surface, + InputChannel outInputChannel) { + String name = "Blessed Surface"; + InputChannel[] inputChannels = InputChannel.openInputChannelPair(name); + InputChannel inputChannel = inputChannels[0]; + InputChannel clientChannel = inputChannels[1]; + + clientChannel.transferTo(outInputChannel); + clientChannel.dispose(); + + IBinder token = new Binder(); + mInputManager.registerInputChannel(inputChannel, token); + + // Prevent the java finalizer from breaking the input channel. But we won't + // do any further management so we just release the java ref and let the + // InputDispatcher hold the last ref. + inputChannel.release(); + + InputWindowHandle h = new InputWindowHandle(null, null, displayId); + h.token = token; + h.name = name; + h.layoutParamsFlags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; + h.layoutParamsType = 0; + h.dispatchingTimeoutNanos = -1; + h.canReceiveKeys = false; + h.hasFocus = false; + h.hasWallpaper = false; + h.paused = false; + + h.ownerUid = callingUid; + h.ownerPid = callingPid; + + h.inputFeatures = 0; + + h.replaceTouchableRegionWithCrop(null); + + SurfaceSession s = new SurfaceSession(); + SurfaceControl.Transaction t = new SurfaceControl.Transaction(); + t.setInputWindowInfo(surface, h); + t.apply(); + } } diff --git a/tests/WindowlessWmTest/Android.bp b/tests/WindowlessWmTest/Android.bp new file mode 100644 index 000000000000..2ace3f363ef9 --- /dev/null +++ b/tests/WindowlessWmTest/Android.bp @@ -0,0 +1,22 @@ +// +// Copyright (C) 2019 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. +// + +android_test { + name: "WindowlessWmTest", + srcs: ["**/*.java"], + platform_apis: true, + certificate: "platform", +} diff --git a/tests/WindowlessWmTest/AndroidManifest.xml b/tests/WindowlessWmTest/AndroidManifest.xml new file mode 100644 index 000000000000..babfd76d91e8 --- /dev/null +++ b/tests/WindowlessWmTest/AndroidManifest.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2019 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. +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.test.viewembed"> + + <application> + <activity android:name="WindowlessWmTest" android:label="View Embedding Test"> + <intent-filter> + <action android:name="android.intent.action.MAIN"/> + <category android:name="android.intent.category.LAUNCHER"/> + </intent-filter> + </activity> + </application> + + +</manifest> diff --git a/tests/WindowlessWmTest/src/com/android/test/viewembed/WindowlessWmTest.java b/tests/WindowlessWmTest/src/com/android/test/viewembed/WindowlessWmTest.java new file mode 100644 index 000000000000..5a146da1a3f2 --- /dev/null +++ b/tests/WindowlessWmTest/src/com/android/test/viewembed/WindowlessWmTest.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2019 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 com.android.test.viewembed; + +import android.app.Activity; +import android.os.Bundle; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.PixelFormat; +import android.view.Gravity; +import android.view.WindowlessViewRoot; +import android.view.SurfaceHolder; +import android.view.SurfaceHolder.Callback; +import android.view.SurfaceView; +import android.view.View; +import android.view.WindowManager; +import android.widget.Button; +import android.widget.FrameLayout; + + +public class WindowlessWmTest extends Activity implements SurfaceHolder.Callback{ + SurfaceView mView; + WindowlessViewRoot mVr; + + protected void onCreate(Bundle savedInstanceState) { + FrameLayout content = new FrameLayout(this); + super.onCreate(savedInstanceState); + mView = new SurfaceView(this); + content.addView(mView, new FrameLayout.LayoutParams( + 500, 500, Gravity.CENTER_HORIZONTAL | Gravity.TOP)); + setContentView(content); + + mView.setZOrderOnTop(true); + mView.getHolder().addCallback(this); + } + + @Override + public void surfaceCreated(SurfaceHolder holder) { + mVr = new WindowlessViewRoot(this, this.getDisplay(), + mView.getSurfaceControl()); + Button v = new Button(this); + v.setBackgroundColor(Color.BLUE); + v.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + v.setBackgroundColor(Color.RED); + } + }); + WindowManager.LayoutParams lp = + new WindowManager.LayoutParams(500, 500, WindowManager.LayoutParams.TYPE_APPLICATION, + 0, PixelFormat.OPAQUE); + mVr.addView(v, lp); + } + + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { + Canvas canvas = holder.lockCanvas(); + canvas.drawColor(Color.GREEN); + holder.unlockCanvasAndPost(canvas); + } + + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + } +} |