Merge changes Ida26b961,I1d416b9c into rvc-dev am: 413ce328e5

Change-Id: Iebc23280e98ab0adf63ccab359847f4ef1457849
This commit is contained in:
Charles Chen
2020-04-27 09:12:47 +00:00
committed by Automerger Merge Worker
4 changed files with 97 additions and 47 deletions

View File

@@ -36,6 +36,7 @@ import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.os.RemoteException; import android.os.RemoteException;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.os.IResultReceiver; import com.android.internal.os.IResultReceiver;
import java.util.List; import java.util.List;
@@ -69,7 +70,8 @@ import java.util.List;
public final class WindowManagerImpl implements WindowManager { public final class WindowManagerImpl implements WindowManager {
@UnsupportedAppUsage @UnsupportedAppUsage
private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance(); private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
private final Context mContext; @VisibleForTesting
public final Context mContext;
private final Window mParentWindow; private final Window mParentWindow;
private IBinder mDefaultToken; private IBinder mDefaultToken;

View File

@@ -22,8 +22,6 @@ import android.content.Context;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
import android.view.WindowManager;
import android.view.WindowManagerImpl;
import android.view.contentcapture.ContentCaptureManager; import android.view.contentcapture.ContentCaptureManager;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
@@ -31,8 +29,8 @@ import com.android.internal.annotations.VisibleForTesting;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
/** /**
* Context for decor views which can be seeded with pure application context and not depend on the * Context for decor views which can be seeded with display context and not depend on the activity,
* activity, but still provide some of the facilities that Activity has, * but still provide some of the facilities that Activity has,
* e.g. themes, activity-based resources, etc. * e.g. themes, activity-based resources, etc.
* *
* @hide * @hide
@@ -40,80 +38,93 @@ import java.lang.ref.WeakReference;
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
public class DecorContext extends ContextThemeWrapper { public class DecorContext extends ContextThemeWrapper {
private PhoneWindow mPhoneWindow; private PhoneWindow mPhoneWindow;
private WindowManager mWindowManager; private Resources mResources;
private Resources mActivityResources;
private ContentCaptureManager mContentCaptureManager; private ContentCaptureManager mContentCaptureManager;
private WeakReference<Context> mActivityContext; private WeakReference<Context> mContext;
// TODO(b/149928768): Non-activity context can be passed.
@VisibleForTesting @VisibleForTesting
public DecorContext(Context context, Context activityContext) { public DecorContext(Context baseContext, PhoneWindow phoneWindow) {
super(context.createDisplayContext(activityContext.getDisplayNoVerify()), null); super(null /* base */, null);
mActivityContext = new WeakReference<>(activityContext); setPhoneWindow(phoneWindow);
mActivityResources = activityContext.getResources(); final Context displayContext = baseContext.createDisplayContext(
// TODO(b/149790106): Non-activity context can be passed.
phoneWindow.getContext().getDisplayNoVerify());
attachBaseContext(displayContext);
} }
void setPhoneWindow(PhoneWindow phoneWindow) { void setPhoneWindow(PhoneWindow phoneWindow) {
mPhoneWindow = phoneWindow; mPhoneWindow = phoneWindow;
mWindowManager = null; final Context context = phoneWindow.getContext();
mContext = new WeakReference<>(context);
mResources = context.getResources();
} }
@Override @Override
public Object getSystemService(String name) { public Object getSystemService(String name) {
if (Context.WINDOW_SERVICE.equals(name)) { if (Context.WINDOW_SERVICE.equals(name)) {
if (mWindowManager == null) { return mPhoneWindow.getWindowManager();
WindowManagerImpl wm =
(WindowManagerImpl) super.getSystemService(Context.WINDOW_SERVICE);
mWindowManager = wm.createLocalWindowManager(mPhoneWindow);
}
return mWindowManager;
} }
final Context context = mContext.get();
if (Context.CONTENT_CAPTURE_MANAGER_SERVICE.equals(name)) { if (Context.CONTENT_CAPTURE_MANAGER_SERVICE.equals(name)) {
if (mContentCaptureManager == null) { if (context != null && mContentCaptureManager == null) {
Context activityContext = mActivityContext.get(); mContentCaptureManager = (ContentCaptureManager) context.getSystemService(name);
if (activityContext != null) {
mContentCaptureManager = (ContentCaptureManager) activityContext
.getSystemService(name);
}
} }
return mContentCaptureManager; return mContentCaptureManager;
} }
return super.getSystemService(name); // TODO(b/154191411): Try to revisit this issue in S.
// We use application to get DisplayManager here because ViewRootImpl holds reference of
// DisplayManager and implicitly holds reference of mContext, which makes activity cannot
// be GC'd even after destroyed if mContext is an activity object.
if (Context.DISPLAY_SERVICE.equals(name)) {
return super.getSystemService(name);
}
// LayoutInflater and WallpaperManagerService should also be obtained from visual context
// instead of base context.
return (context != null) ? context.getSystemService(name) : super.getSystemService(name);
} }
@Override @Override
public Resources getResources() { public Resources getResources() {
Context activityContext = mActivityContext.get(); Context context = mContext.get();
// Attempt to update the local cached Resources from the activity context. If the activity // Attempt to update the local cached Resources from the activity context. If the activity
// is no longer around, return the old cached values. // is no longer around, return the old cached values.
if (activityContext != null) { if (context != null) {
mActivityResources = activityContext.getResources(); mResources = context.getResources();
} }
return mActivityResources; return mResources;
} }
@Override @Override
public AssetManager getAssets() { public AssetManager getAssets() {
return mActivityResources.getAssets(); return mResources.getAssets();
} }
@Override @Override
public AutofillOptions getAutofillOptions() { public AutofillOptions getAutofillOptions() {
Context activityContext = mActivityContext.get(); Context context = mContext.get();
if (activityContext != null) { if (context != null) {
return activityContext.getAutofillOptions(); return context.getAutofillOptions();
} }
return null; return null;
} }
@Override @Override
public ContentCaptureOptions getContentCaptureOptions() { public ContentCaptureOptions getContentCaptureOptions() {
Context activityContext = mActivityContext.get(); Context context = mContext.get();
if (activityContext != null) { if (context != null) {
return activityContext.getContentCaptureOptions(); return context.getContentCaptureOptions();
} }
return null; return null;
} }
@Override
public boolean isUiContext() {
Context context = mContext.get();
if (context != null) {
return context.isUiContext();
}
return false;
}
} }

View File

@@ -2358,7 +2358,7 @@ public class PhoneWindow extends Window implements MenuBuilder.Callback {
if (applicationContext == null) { if (applicationContext == null) {
context = getContext(); context = getContext();
} else { } else {
context = new DecorContext(applicationContext, getContext()); context = new DecorContext(applicationContext, this);
if (mTheme != -1) { if (mTheme != -1) {
context.setTheme(mTheme); context.setTheme(mTheme);
} }

View File

@@ -19,20 +19,26 @@ package com.android.internal.policy;
import static android.view.Display.DEFAULT_DISPLAY; import static android.view.Display.DEFAULT_DISPLAY;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import android.app.Activity;
import android.app.EmptyActivity;
import android.content.Context; import android.content.Context;
import android.hardware.display.DisplayManagerGlobal; import android.hardware.display.DisplayManagerGlobal;
import android.platform.test.annotations.Presubmit; import android.platform.test.annotations.Presubmit;
import android.view.Display; import android.view.Display;
import android.view.DisplayAdjustments; import android.view.DisplayAdjustments;
import android.view.DisplayInfo; import android.view.DisplayInfo;
import android.view.WindowManager;
import android.view.WindowManagerImpl;
import androidx.test.InstrumentationRegistry; import androidx.test.core.app.ApplicationProvider;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4; import androidx.test.runner.AndroidJUnit4;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -46,17 +52,22 @@ public final class DecorContextTest {
private Context mContext; private Context mContext;
private static final int EXTERNAL_DISPLAY = DEFAULT_DISPLAY + 1; private static final int EXTERNAL_DISPLAY = DEFAULT_DISPLAY + 1;
@Rule
public ActivityTestRule<EmptyActivity> mActivityRule =
new ActivityTestRule<>(EmptyActivity.class);
@Before @Before
public void setUp() throws Exception { public void setUp() {
mContext = InstrumentationRegistry.getContext(); mContext = ApplicationProvider.getApplicationContext();
} }
@Test @Test
public void testDecorContextWithDefaultDisplay() { public void testDecorContextWithDefaultDisplay() {
Display defaultDisplay = new Display(DisplayManagerGlobal.getInstance(), DEFAULT_DISPLAY, Display defaultDisplay = new Display(DisplayManagerGlobal.getInstance(), DEFAULT_DISPLAY,
new DisplayInfo(), DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS); new DisplayInfo(), DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS);
DecorContext context = new DecorContext(mContext.getApplicationContext(), final Context defaultDisplayContext = mContext.createDisplayContext(defaultDisplay);
mContext.createDisplayContext(defaultDisplay)); final PhoneWindow window = new PhoneWindow(defaultDisplayContext);
DecorContext context = new DecorContext(mContext.getApplicationContext(), window);
assertDecorContextDisplay(DEFAULT_DISPLAY, context); assertDecorContextDisplay(DEFAULT_DISPLAY, context);
} }
@@ -65,8 +76,9 @@ public final class DecorContextTest {
public void testDecorContextWithExternalDisplay() { public void testDecorContextWithExternalDisplay() {
Display display = new Display(DisplayManagerGlobal.getInstance(), EXTERNAL_DISPLAY, Display display = new Display(DisplayManagerGlobal.getInstance(), EXTERNAL_DISPLAY,
new DisplayInfo(), DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS); new DisplayInfo(), DisplayAdjustments.DEFAULT_DISPLAY_ADJUSTMENTS);
DecorContext context = new DecorContext(mContext.getApplicationContext(), final Context defaultDisplayContext = mContext.createDisplayContext(display);
mContext.createDisplayContext(display)); final PhoneWindow window = new PhoneWindow(defaultDisplayContext);
DecorContext context = new DecorContext(mContext.getApplicationContext(), window);
assertDecorContextDisplay(EXTERNAL_DISPLAY, context); assertDecorContextDisplay(EXTERNAL_DISPLAY, context);
} }
@@ -76,4 +88,29 @@ public final class DecorContextTest {
Display associatedDisplay = decorContext.getDisplay(); Display associatedDisplay = decorContext.getDisplay();
assertEquals(expectedDisplayId, associatedDisplay.getDisplayId()); assertEquals(expectedDisplayId, associatedDisplay.getDisplayId());
} }
@Test
public void testGetWindowManagerFromVisualDecorContext() throws Throwable {
mActivityRule.runOnUiThread(() -> {
Activity activity = mActivityRule.getActivity();
final DecorContext decorContext = new DecorContext(mContext.getApplicationContext(),
(PhoneWindow) activity.getWindow());
WindowManagerImpl actualWm = (WindowManagerImpl)
decorContext.getSystemService(WindowManager.class);
WindowManagerImpl expectedWm = (WindowManagerImpl)
activity.getSystemService(WindowManager.class);
// Verify that window manager is from activity not application context.
assertEquals(expectedWm.mContext, actualWm.mContext);
});
}
@Test
public void testIsUiContextFromVisualDecorContext() throws Throwable {
mActivityRule.runOnUiThread(() -> {
Activity activity = mActivityRule.getActivity();
final DecorContext decorContext = new DecorContext(mContext.getApplicationContext(),
(PhoneWindow) activity.getWindow());
assertTrue(decorContext.isUiContext());
});
}
} }