Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a74da271 authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Add ActivityThread.currentSystemContext().

We want to expose getSystemContext(), but unfortunately it returns
ContextImpl not Context, and we can't change the result type
because of the @UnsupportedAppUsage.

This means we'd need to create another getter with a return type of
Context, which means we'd have to update the callsites. So let's
just add a static currentSystemContext() just like the other current*()
methods, because that's what almost all clients would want to use anyway.

Bug: 402153527
Test: $ANDROID_BUILD_TOP/frameworks/base/ravenwood/scripts/run-ravenwood-tests.sh -s
Flag: EXEMPT refactoring only
Change-Id: I06aaa85bb50a7ebd69910272193ac54263cfcc00
parent 1eec3dda
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -304,6 +304,9 @@ import java.util.function.Consumer;
 *
 * {@hide}
 */
@android.ravenwood.annotation.RavenwoodPartiallyAllowlisted
@android.ravenwood.annotation.RavenwoodKeepPartialClass
@android.ravenwood.annotation.RavenwoodRedirectionClass("ActivityThread_ravenwood")
public final class ActivityThread extends ClientTransactionHandler
        implements ActivityThreadInternal {

@@ -2948,6 +2951,16 @@ public final class ActivityThread extends ClientTransactionHandler
        return am != null ? am.mInitialApplication : null;
    }

    /**
     * Same as {@code ActivityThread.currentActivityThread().getSystemContext()}, but
     * it'll return a {@link Context} (not a {@link ContextImpl}) and is supported on Ravenwood.
     */
    @android.ravenwood.annotation.RavenwoodRedirect
    public static Context currentSystemContext() {
        ActivityThread am = currentActivityThread();
        return am != null ? am.getSystemContext() : null;
    }

    @UnsupportedAppUsage
    public static IPackageManager getPackageManager() {
        if (sPackageManager != null) {
@@ -3239,6 +3252,17 @@ public final class ActivityThread extends ClientTransactionHandler

    @Override
    @UnsupportedAppUsage
    // It returns a ContextImpl, which is not supported on Ravenwood yet, and it might never be
    // supported. We want to change the return type to Context so support it on Ravenwood,
    // but the @UnsupportedAppUsage prevents us from doing it, so for now we just update
    // clients to use currentSystemContext() instead.
    // If any clients need to use getSystemContext() on a non-"current" ActivityThread, we'd need
    // add another getter with the return type of Context.
    //
    // (Class is only partially allow-listed, and this method can't have a ravenwood annotation.)
    // @android.ravenwood.annotation.RavenwoodThrow(
    //        reason = "ContextImpl is not supported on Ravenwood. You may wan to use "
    //        + " ActivityThread.currentSystemContext() instead")
    public ContextImpl getSystemContext() {
        synchronized (this) {
            if (mSystemContext == null) {
+41 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.app;

import android.content.Context;

import java.util.Objects;

/**
 * Inject Ravenwood methods to {@link ActivityThread}.
 */
@android.ravenwood.annotation.RavenwoodKeepWholeClass
public class ActivityThread_ravenwood {
    private ActivityThread_ravenwood() {
    }

    private static volatile Context sContext;

    /** Initializer called by Ravenwood. */
    public static void init(Context context) {
        sContext = Objects.requireNonNull(context);
    }

    /** Override {@link ActivityThread#currentSystemContext()}. */
    public static Context currentSystemContext() {
        return Objects.requireNonNull(sContext, "ActivityThread_ravenwood not initialized.");
    }
}
+5 −1
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import static org.mockito.Mockito.mock;

import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.ActivityThread_ravenwood;
import android.app.AppCompatCallbacks;
import android.app.Instrumentation;
import android.app.ResourcesManager;
@@ -67,7 +68,6 @@ import android.view.DisplayAdjustments;

import androidx.test.platform.app.InstrumentationRegistry;

import com.android.hoststubgen.hosthelper.HostTestUtils;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.os.RuntimeInit;
import com.android.ravenwood.RavenwoodRuntimeNative;
@@ -399,6 +399,10 @@ public class RavenwoodRuntimeEnvironmentController {
            targetContext.setApplicationContext(appContext);
        }

        // Set up ActivityThread.currentSystemContext(), which is technically a different
        // thing from the app context, but for now let's just do it this way.
        ActivityThread_ravenwood.init(appContext);

        final Supplier<Resources> systemResourcesLoader = () -> loadResources(null);

        var systemServerContext =
+30 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 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.ravenwoodtest.coretest;

import static org.junit.Assert.assertNotNull;

import android.app.ActivityThread;

import org.junit.Test;

public class RavenwoodActivityThreadTest {

    @Test
    public void testActivityThreadSystemContext() {
        assertNotNull(ActivityThread.currentSystemContext());
    }
}
+5 −0
Original line number Diff line number Diff line
@@ -2,6 +2,9 @@

com.android.internal.ravenwood.*

# android.app.ActivityThread is partially allowlited.
android.app.ActivityThread_ravenwood

com.android.server.FgThread
com.android.server.ServiceThread

@@ -621,3 +624,5 @@ android.text.util.Rfc822Token
android.text.util.Rfc822Tokenizer
android.graphics.drawable.Drawable$ConstantState
android.graphics.drawable.BitmapDrawable$BitmapState

android.app.ActivityThread_ravenwood
Loading