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

Commit f24b2e90 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Remove WindowContextCleaner" into main

parents c797a4d4 0eeda7f6
Loading
Loading
Loading
Loading
+1 −14
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import static android.os.StrictMode.vmIncorrectContextUseEnabled;
import static android.permission.flags.Flags.shouldRegisterAttributionSource;
import static android.view.WindowManager.LayoutParams.WindowType;
import static android.window.WindowContext.registerCleaner;

import android.annotation.CallbackExecutor;
import android.annotation.IntDef;
@@ -3431,7 +3430,7 @@ class ContextImpl extends Context {
        // if this Context is not a WindowContext. WindowContext finalization is handled in
        // WindowContext class.
        try {
            if (!isCleanerEnabled() && mToken instanceof WindowTokenClient && mOwnsToken) {
            if (mToken instanceof WindowTokenClient && mOwnsToken) {
                WindowTokenClientController.getInstance()
                        .detachIfNeeded((WindowTokenClient) mToken);
            }
@@ -3440,15 +3439,6 @@ class ContextImpl extends Context {
        }
    }

    /**
     * Returns {@code true} if {@link WindowContext#registerCleaner} is enabled.
     */
    private static boolean isCleanerEnabled() {
        return com.android.window.flags.Flags.cleanUpWindowContextWithCleaner()
                // Cleaner only works on SystemUiContext or WindowContext.
                && com.android.window.flags.Flags.trackSystemUiContextBeforeWms();
    }

    @UnsupportedAppUsage
    static ContextImpl createSystemContext(ActivityThread mainThread) {
        LoadedApk packageInfo = new LoadedApk(mainThread);
@@ -3489,9 +3479,6 @@ class ContextImpl extends Context {
        WindowTokenClientController.getInstance().attachToDisplayContent(token, displayId);
        context.mContextType = CONTEXT_TYPE_SYSTEM_OR_SYSTEM_UI;
        context.mOwnsToken = true;
        if (isCleanerEnabled()) {
            registerCleaner(systemUiContext);
        }
        return systemUiContext;
    }

+1 −50
Original line number Diff line number Diff line
@@ -43,8 +43,6 @@ import com.android.window.flags.Flags;

import java.lang.ref.Reference;

import sun.misc.Cleaner;

/**
 * {@link WindowContext} is a context for non-activity windows such as
 * {@link android.view.WindowManager.LayoutParams#TYPE_APPLICATION_OVERLAY} windows or system
@@ -98,7 +96,6 @@ public class WindowContext extends ContextWrapper implements WindowProvider,
        mOptions = options;
        mWindowManager = createWindowContextWindowManager(this);
        WindowTokenClient token = (WindowTokenClient) requireNonNull(getWindowContextToken());
        registerCleaner(this);
        mController = new WindowContextController(requireNonNull(token));

        if (!Flags.reparentToDefaultWithDisplayRemoval()
@@ -106,7 +103,6 @@ public class WindowContext extends ContextWrapper implements WindowProvider,
            throw new UnsupportedOperationException(
                    Flags.FLAG_REPARENT_TO_DEFAULT_WITH_DISPLAY_REMOVAL + " is not enabled");
        }

        Reference.reachabilityFence(this);
    }

@@ -147,9 +143,7 @@ public class WindowContext extends ContextWrapper implements WindowProvider,
    @Override
    protected void finalize() throws Throwable {
        try {
            if (!Flags.cleanUpWindowContextWithCleaner()) {
            release();
            }
        } finally {
            super.finalize();
        }
@@ -334,47 +328,4 @@ public class WindowContext extends ContextWrapper implements WindowProvider,
            return mDecorView;
        }
    }

    /**
     * Registers a {@link WindowContext} or a {@link SystemUiContext} with a cleaner.
     *
     * @throws IllegalArgumentException if the context is not a {@link WindowContext} or
     * {@link SystemUiContext}.
     */
    public static void registerCleaner(@NonNull Context context) {
        if (!Flags.cleanUpWindowContextWithCleaner()) {
            return;
        }

        if (!(context instanceof WindowContext) && !(context instanceof SystemUiContext)) {
            throw new IllegalArgumentException("The context must be either WindowContext or"
                    + " SystemUiContext.");
        }
        final ContextWrapper wrapper = (ContextWrapper) context;
        Cleaner.create(context, new WindowContextCleaner(wrapper));
    }

    /**
     * A {@link WindowContext} cleaner that applies when the {@link WindowContext} is going to be
     * garbage-collected.
     */
    private static class WindowContextCleaner implements Runnable {

        @NonNull
        private final Context mBaseContext;

        WindowContextCleaner(@NonNull ContextWrapper wrapper) {
            // Cache the base Context to prevent hold the reference of WindowContext. The cleaner
            // will work only if all strong references of WindowContext are gone.
            mBaseContext = requireNonNull(wrapper.getBaseContext());
        }

        @Override
        public void run() {
            final WindowTokenClient token =
                    (WindowTokenClient) requireNonNull(mBaseContext.getWindowContextToken());
            WindowTokenClientController.getInstance().detachIfNeeded(token);
            mBaseContext.destroy();
        }
    }
}
+0 −11
Original line number Diff line number Diff line
@@ -205,17 +205,6 @@ flag {
    }
}

flag {
    namespace: "windowing_sdk"
    name: "clean_up_window_context_with_cleaner"
    description: "Using Cleaner to clean up WindowContext instead of overriding finalize"
    bug: "339727951"
    is_fixed_read_only: true
    metadata {
        purpose: PURPOSE_BUGFIX
    }
}

flag {
    namespace: "windowing_sdk"
    name: "reparent_to_default_with_display_removal"