Merge "Cache isSystemOrSystemUI on ContextImpl creation" into rvc-dev am: 9859a326c1
Change-Id: Icbf8fcfad9eb2645faf1eff0cdbb6b0388b1773f
This commit is contained in:
@@ -250,6 +250,10 @@ class ContextImpl extends Context {
|
|||||||
|
|
||||||
private final Object mSync = new Object();
|
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 mIsSystemOrSystemUiContext;
|
||||||
private boolean mIsUiContext;
|
private boolean mIsUiContext;
|
||||||
private boolean mIsAssociatedWithDisplay;
|
private boolean mIsAssociatedWithDisplay;
|
||||||
@@ -1922,16 +1926,18 @@ class ContextImpl extends Context {
|
|||||||
/** @hide */
|
/** @hide */
|
||||||
@Override
|
@Override
|
||||||
public boolean isUiContext() {
|
public boolean isUiContext() {
|
||||||
return mIsSystemOrSystemUiContext || mIsUiContext || isSystemOrSystemUI();
|
return mIsSystemOrSystemUiContext || mIsUiContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Temporary workaround to permit incorrect usages of Context by SystemUI.
|
* 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() {
|
private static boolean isSystemOrSystemUI(Context context) {
|
||||||
return ActivityThread.isSystem() || checkPermission("android.permission.STATUS_BAR_SERVICE",
|
return ActivityThread.isSystem() || context.checkPermission(
|
||||||
Binder.getCallingPid(), Binder.getCallingUid()) == PERMISSION_GRANTED;
|
"android.permission.STATUS_BAR_SERVICE",
|
||||||
|
Binder.getCallingPid(),
|
||||||
|
Binder.getCallingUid()) == PERMISSION_GRANTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isUiComponent(String name) {
|
private static boolean isUiComponent(String name) {
|
||||||
@@ -2467,7 +2473,7 @@ class ContextImpl extends Context {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Display getDisplay() {
|
public Display getDisplay() {
|
||||||
if (!mIsSystemOrSystemUiContext && !mIsAssociatedWithDisplay && !isSystemOrSystemUI()) {
|
if (!mIsSystemOrSystemUiContext && !mIsAssociatedWithDisplay) {
|
||||||
throw new UnsupportedOperationException("Tried to obtain display from a Context not "
|
throw new UnsupportedOperationException("Tried to obtain display from a Context not "
|
||||||
+ "associated with one. Only visual Contexts (such as Activity or one created "
|
+ "associated with one. Only visual Contexts (such as Activity or one created "
|
||||||
+ "with Context#createWindowContext) or ones created with "
|
+ "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,
|
ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, null,
|
||||||
0, null, opPackageName);
|
0, null, opPackageName);
|
||||||
context.setResources(packageInfo.getResources());
|
context.setResources(packageInfo.getResources());
|
||||||
|
context.mIsSystemOrSystemUiContext = isSystemOrSystemUI(context);
|
||||||
return context;
|
return context;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2672,6 +2679,7 @@ class ContextImpl extends Context {
|
|||||||
activityInfo.splitName, activityToken, null, 0, classLoader, null);
|
activityInfo.splitName, activityToken, null, 0, classLoader, null);
|
||||||
context.mIsUiContext = true;
|
context.mIsUiContext = true;
|
||||||
context.mIsAssociatedWithDisplay = true;
|
context.mIsAssociatedWithDisplay = true;
|
||||||
|
context.mIsSystemOrSystemUiContext = isSystemOrSystemUI(context);
|
||||||
|
|
||||||
// Clamp display ID to DEFAULT_DISPLAY if it is INVALID_DISPLAY.
|
// Clamp display ID to DEFAULT_DISPLAY if it is INVALID_DISPLAY.
|
||||||
displayId = (displayId != Display.INVALID_DISPLAY) ? displayId : Display.DEFAULT_DISPLAY;
|
displayId = (displayId != Display.INVALID_DISPLAY) ? displayId : Display.DEFAULT_DISPLAY;
|
||||||
|
|||||||
@@ -18,12 +18,15 @@ package android.content;
|
|||||||
|
|
||||||
import static android.view.Display.DEFAULT_DISPLAY;
|
import static android.view.Display.DEFAULT_DISPLAY;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import android.app.ActivityThread;
|
import android.app.ActivityThread;
|
||||||
import android.hardware.display.DisplayManager;
|
import android.hardware.display.DisplayManager;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
|
|
||||||
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
import androidx.test.filters.SmallTest;
|
import androidx.test.filters.SmallTest;
|
||||||
import androidx.test.platform.app.InstrumentationRegistry;
|
import androidx.test.platform.app.InstrumentationRegistry;
|
||||||
@@ -32,7 +35,6 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Build/Install/Run:
|
* Build/Install/Run:
|
||||||
* atest FrameworksCoreTests:ContextTest
|
* atest FrameworksCoreTests:ContextTest
|
||||||
*/
|
*/
|
||||||
@@ -47,6 +49,14 @@ public class ContextTest {
|
|||||||
assertEquals(systemContext.getDisplay().getDisplayId(), systemContext.getDisplayId());
|
assertEquals(systemContext.getDisplay().getDisplayId(), systemContext.getDisplayId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDisplayIdForSystemUiContext() {
|
||||||
|
final Context systemUiContext =
|
||||||
|
ActivityThread.currentActivityThread().getSystemUiContext();
|
||||||
|
|
||||||
|
assertEquals(systemUiContext.getDisplay().getDisplayId(), systemUiContext.getDisplayId());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDisplayIdForTestContext() {
|
public void testDisplayIdForTestContext() {
|
||||||
final Context testContext =
|
final Context testContext =
|
||||||
@@ -94,4 +104,27 @@ public class ContextTest {
|
|||||||
InstrumentationRegistry.getInstrumentation().getTargetContext();
|
InstrumentationRegistry.getInstrumentation().getTargetContext();
|
||||||
testContext.startActivityAsUser(new Intent(), new UserHandle(UserHandle.USER_ALL));
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user