From 7c9746d4ef8ab3c500b501ab32933c5172a856ab Mon Sep 17 00:00:00 2001 From: Alan Viverette Date: Wed, 19 Nov 2014 17:02:16 -0800 Subject: [PATCH] Move default token handling into WindowManagerImpl BUG: 18451795 Change-Id: I1fc6db988ad879fded5318f33d08a4f09da38907 --- .../accessibilityservice/AccessibilityService.java | 25 +++++++++++++-- core/java/android/view/WindowManagerGlobal.java | 18 ----------- core/java/android/view/WindowManagerImpl.java | 36 ++++++++++++++++++++-- 3 files changed, 57 insertions(+), 22 deletions(-) diff --git a/core/java/android/accessibilityservice/AccessibilityService.java b/core/java/android/accessibilityservice/AccessibilityService.java index a9eaf291a684..3f1845a8fcc3 100644 --- a/core/java/android/accessibilityservice/AccessibilityService.java +++ b/core/java/android/accessibilityservice/AccessibilityService.java @@ -16,6 +16,7 @@ package android.accessibilityservice; +import android.annotation.NonNull; import android.app.Service; import android.content.Context; import android.content.Intent; @@ -27,6 +28,7 @@ import android.util.Log; import android.view.KeyEvent; import android.view.WindowManager; import android.view.WindowManagerGlobal; +import android.view.WindowManagerImpl; import android.view.accessibility.AccessibilityEvent; import android.view.accessibility.AccessibilityInteractionClient; import android.view.accessibility.AccessibilityNodeInfo; @@ -618,6 +620,23 @@ public abstract class AccessibilityService extends Service { } } + @Override + public Object getSystemService(@ServiceName @NonNull String name) { + if (getBaseContext() == null) { + throw new IllegalStateException( + "System services not available to Activities before onCreate()"); + } + + // Guarantee that we always return the same window manager instance. + if (WINDOW_SERVICE.equals(name)) { + if (mWindowManager == null) { + mWindowManager = (WindowManager) getBaseContext().getSystemService(name); + } + return mWindowManager; + } + return super.getSystemService(name); + } + /** * Implement to return the implementation of the internal accessibility * service interface. @@ -645,8 +664,10 @@ public abstract class AccessibilityService extends Service { mConnectionId = connectionId; mWindowToken = windowToken; - // Let the window manager know about our shiny new token. - WindowManagerGlobal.getInstance().setDefaultToken(mWindowToken); + // The client may have already obtained the window manager, so + // update the default token on whatever manager we gave them. + final WindowManagerImpl wm = (WindowManagerImpl) getSystemService(WINDOW_SERVICE); + wm.setDefaultToken(windowToken); } @Override diff --git a/core/java/android/view/WindowManagerGlobal.java b/core/java/android/view/WindowManagerGlobal.java index 82b1073c8bfc..5926d5f5cb75 100644 --- a/core/java/android/view/WindowManagerGlobal.java +++ b/core/java/android/view/WindowManagerGlobal.java @@ -118,9 +118,6 @@ public final class WindowManagerGlobal { private Runnable mSystemPropertyUpdater; - /** Default token to apply to added views. */ - private IBinder mDefaultToken; - private WindowManagerGlobal() { } @@ -172,17 +169,6 @@ public final class WindowManagerGlobal { } } - /** - * Sets the default token to use in {@link #addView} when no parent window - * token is available and no token has been explicitly set in the view's - * layout params. - * - * @param token Default window token to apply to added views. - */ - public void setDefaultToken(IBinder token) { - mDefaultToken = token; - } - public String[] getViewRootNames() { synchronized (mLock) { final int numRoots = mRoots.size(); @@ -230,10 +216,6 @@ public final class WindowManagerGlobal { } } - if (wparams.token == null && mDefaultToken != null) { - wparams.token = mDefaultToken; - } - ViewRootImpl root; View panelParentView = null; diff --git a/core/java/android/view/WindowManagerImpl.java b/core/java/android/view/WindowManagerImpl.java index 52d79f84f9a1..98e9f54c13f9 100644 --- a/core/java/android/view/WindowManagerImpl.java +++ b/core/java/android/view/WindowManagerImpl.java @@ -16,6 +16,9 @@ package android.view; +import android.annotation.NonNull; +import android.os.IBinder; + /** * Provides low-level communication with the system window manager for * operations that are bound to a particular context, display or parent window. @@ -47,6 +50,8 @@ public final class WindowManagerImpl implements WindowManager { private final Display mDisplay; private final Window mParentWindow; + private IBinder mDefaultToken; + public WindowManagerImpl(Display display) { this(display, null); } @@ -64,16 +69,43 @@ public final class WindowManagerImpl implements WindowManager { return new WindowManagerImpl(display, mParentWindow); } + /** + * Sets the window token to assign when none is specified by the client or + * available from the parent window. + * + * @param token The default token to assign. + */ + public void setDefaultToken(IBinder token) { + mDefaultToken = token; + } + @Override - public void addView(View view, ViewGroup.LayoutParams params) { + public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) { + applyDefaultToken(params); mGlobal.addView(view, params, mDisplay, mParentWindow); } @Override - public void updateViewLayout(View view, ViewGroup.LayoutParams params) { + public void updateViewLayout(@NonNull View view, @NonNull ViewGroup.LayoutParams params) { + applyDefaultToken(params); mGlobal.updateViewLayout(view, params); } + private void applyDefaultToken(@NonNull ViewGroup.LayoutParams params) { + // Only use the default token if we don't have a parent window. + if (mDefaultToken != null && mParentWindow == null) { + if (!(params instanceof WindowManager.LayoutParams)) { + throw new IllegalArgumentException("Params must be WindowManager.LayoutParams"); + } + + // Only use the default token if we don't already have a token. + final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params; + if (wparams.token == null) { + wparams.token = mDefaultToken; + } + } + } + @Override public void removeView(View view) { mGlobal.removeView(view, false); -- 2.11.0