Merge "Merge "Cache isSystemOrSystemUI on ContextImpl creation" into rvc-dev am: 9859a326c1 am: 360b08a9a4" into rvc-d1-dev-plus-aosp

This commit is contained in:
Automerger Merge Worker
2020-04-28 04:44:15 +00:00
committed by Android (Google) Code Review
2 changed files with 48 additions and 7 deletions

View File

@@ -250,6 +250,10 @@ class ContextImpl extends Context {
private final Object mSync = new Object();
/**
* Whether this is created from {@link #createSystemContext(ActivityThread)} or
* {@link #createSystemUiContext(ContextImpl, int)} or any {@link Context} that system UI uses.
*/
private boolean mIsSystemOrSystemUiContext;
private boolean mIsUiContext;
private boolean mIsAssociatedWithDisplay;
@@ -1922,16 +1926,18 @@ class ContextImpl extends Context {
/** @hide */
@Override
public boolean isUiContext() {
return mIsSystemOrSystemUiContext || mIsUiContext || isSystemOrSystemUI();
return mIsSystemOrSystemUiContext || mIsUiContext;
}
/**
* Temporary workaround to permit incorrect usages of Context by SystemUI.
* TODO(b/149790106): Fix usages and remove.
* TODO(b/147647877): Fix usages and remove.
*/
private boolean isSystemOrSystemUI() {
return ActivityThread.isSystem() || checkPermission("android.permission.STATUS_BAR_SERVICE",
Binder.getCallingPid(), Binder.getCallingUid()) == PERMISSION_GRANTED;
private static boolean isSystemOrSystemUI(Context context) {
return ActivityThread.isSystem() || context.checkPermission(
"android.permission.STATUS_BAR_SERVICE",
Binder.getCallingPid(),
Binder.getCallingUid()) == PERMISSION_GRANTED;
}
private static boolean isUiComponent(String name) {
@@ -2467,7 +2473,7 @@ class ContextImpl extends Context {
@Override
public Display getDisplay() {
if (!mIsSystemOrSystemUiContext && !mIsAssociatedWithDisplay && !isSystemOrSystemUI()) {
if (!mIsSystemOrSystemUiContext && !mIsAssociatedWithDisplay) {
throw new UnsupportedOperationException("Tried to obtain display from a Context not "
+ "associated with one. Only visual Contexts (such as Activity or one created "
+ "with Context#createWindowContext) or ones created with "
@@ -2643,6 +2649,7 @@ class ContextImpl extends Context {
ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, null,
0, null, opPackageName);
context.setResources(packageInfo.getResources());
context.mIsSystemOrSystemUiContext = isSystemOrSystemUI(context);
return context;
}
@@ -2672,6 +2679,7 @@ class ContextImpl extends Context {
activityInfo.splitName, activityToken, null, 0, classLoader, null);
context.mIsUiContext = true;
context.mIsAssociatedWithDisplay = true;
context.mIsSystemOrSystemUiContext = isSystemOrSystemUI(context);
// Clamp display ID to DEFAULT_DISPLAY if it is INVALID_DISPLAY.
displayId = (displayId != Display.INVALID_DISPLAY) ? displayId : Display.DEFAULT_DISPLAY;

View File

@@ -18,12 +18,15 @@ package android.content;
import static android.view.Display.DEFAULT_DISPLAY;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import android.app.ActivityThread;
import android.hardware.display.DisplayManager;
import android.os.UserHandle;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;
@@ -32,7 +35,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
/**
*
* Build/Install/Run:
* atest FrameworksCoreTests:ContextTest
*/
@@ -47,6 +49,14 @@ public class ContextTest {
assertEquals(systemContext.getDisplay().getDisplayId(), systemContext.getDisplayId());
}
@Test
public void testDisplayIdForSystemUiContext() {
final Context systemUiContext =
ActivityThread.currentActivityThread().getSystemUiContext();
assertEquals(systemUiContext.getDisplay().getDisplayId(), systemUiContext.getDisplayId());
}
@Test
public void testDisplayIdForTestContext() {
final Context testContext =
@@ -94,4 +104,27 @@ public class ContextTest {
InstrumentationRegistry.getInstrumentation().getTargetContext();
testContext.startActivityAsUser(new Intent(), new UserHandle(UserHandle.USER_ALL));
}
@Test
public void testIsUiContext_appContext_returnsFalse() {
final Context appContext = ApplicationProvider.getApplicationContext();
assertThat(appContext.isUiContext()).isFalse();
}
@Test
public void testIsUiContext_systemContext_returnsTrue() {
final Context systemContext =
ActivityThread.currentActivityThread().getSystemContext();
assertThat(systemContext.isUiContext()).isTrue();
}
@Test
public void testIsUiContext_systemUiContext_returnsTrue() {
final Context systemUiContext =
ActivityThread.currentActivityThread().getSystemUiContext();
assertThat(systemUiContext.isUiContext()).isTrue();
}
}