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

Commit 51059540 authored by Miranda Kephart's avatar Miranda Kephart Committed by Matt Casey
Browse files

Simplify ScreenshotController/ViewProxy interface

Moves the internal insets and onAttachStateChange listeners into the
view proxy, since these don't depend on the ScreenshotController
directly and can be handled internally.

Bug: 329659738
Test: manual, atest
Flag: NONE
Change-Id: Ic87f36708b7a01e693bc134c0dc4043a5bfb4251
Merged-In: Ic87f36708b7a01e693bc134c0dc4043a5bfb4251
parent 40cc02f9
Loading
Loading
Loading
Loading
+38 −8
Original line number Diff line number Diff line
@@ -22,12 +22,14 @@ import android.content.Context
import android.graphics.Bitmap
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.Display
import android.view.LayoutInflater
import android.view.ScrollCaptureResponse
import android.view.View
import android.view.ViewTreeObserver
import android.view.WindowInsets
import android.window.OnBackInvokedCallback
import android.window.OnBackInvokedDispatcher
import com.android.internal.logging.UiEventLogger
import com.android.systemui.flags.FeatureFlags
@@ -40,7 +42,6 @@ import com.android.systemui.res.R
class LegacyScreenshotViewProxy(context: Context) : ScreenshotViewProxy {
    override val view: ScreenshotView =
        LayoutInflater.from(context).inflate(R.layout.screenshot, null) as ScreenshotView
    override val internalInsetsListener: ViewTreeObserver.OnComputeInternalInsetsListener
    override val screenshotPreview: View

    override var defaultDisplay: Int = Display.DEFAULT_DISPLAY
@@ -51,6 +52,9 @@ class LegacyScreenshotViewProxy(context: Context) : ScreenshotViewProxy {
        set(value) {
            view.setDefaultTimeoutMillis(value)
        }
    override var onBackInvokedCallback: OnBackInvokedCallback = OnBackInvokedCallback {
        Log.wtf(TAG, "OnBackInvoked called before being set!")
    }
    override var onKeyListener: View.OnKeyListener? = null
        set(value) {
            view.setOnKeyListener(value)
@@ -84,7 +88,35 @@ class LegacyScreenshotViewProxy(context: Context) : ScreenshotViewProxy {
        get() = view.isPendingSharedTransition

    init {
        internalInsetsListener = view

        view.addOnAttachStateChangeListener(
            object : View.OnAttachStateChangeListener {
                override fun onViewAttachedToWindow(view: View) {
                    if (LogConfig.DEBUG_INPUT) {
                        Log.d(TAG, "Registering Predictive Back callback")
                    }
                    view
                        .findOnBackInvokedDispatcher()
                        ?.registerOnBackInvokedCallback(
                            OnBackInvokedDispatcher.PRIORITY_DEFAULT,
                            onBackInvokedCallback
                        )
                }

                override fun onViewDetachedFromWindow(view: View) {
                    if (LogConfig.DEBUG_INPUT) {
                        Log.d(TAG, "Unregistering Predictive Back callback")
                    }
                    view
                        .findOnBackInvokedDispatcher()
                        ?.unregisterOnBackInvokedCallback(onBackInvokedCallback)
                }
            }
        )
        if (LogConfig.DEBUG_WINDOW) {
            Log.d(TAG, "adding OnComputeInternalInsetsListener")
        }
        view.viewTreeObserver.addOnComputeInternalInsetsListener(view)
        screenshotPreview = view.screenshotPreview
    }

@@ -139,12 +171,6 @@ class LegacyScreenshotViewProxy(context: Context) : ScreenshotViewProxy {

    override fun announceForAccessibility(string: String) = view.announceForAccessibility(string)

    override fun addOnAttachStateChangeListener(listener: View.OnAttachStateChangeListener) =
        view.addOnAttachStateChangeListener(listener)

    override fun findOnBackInvokedDispatcher(): OnBackInvokedDispatcher? =
        view.findOnBackInvokedDispatcher()

    override fun getViewTreeObserver(): ViewTreeObserver = view.viewTreeObserver

    override fun post(runnable: Runnable) {
@@ -156,4 +182,8 @@ class LegacyScreenshotViewProxy(context: Context) : ScreenshotViewProxy {
            return LegacyScreenshotViewProxy(context)
        }
    }

    companion object {
        private const val TAG = "LegacyScreenshotViewProxy"
    }
}
+6 −34
Original line number Diff line number Diff line
@@ -76,8 +76,6 @@ import android.view.WindowManager;
import android.view.WindowManagerGlobal;
import android.view.accessibility.AccessibilityManager;
import android.widget.Toast;
import android.window.OnBackInvokedCallback;
import android.window.OnBackInvokedDispatcher;
import android.window.WindowContext;

import com.android.internal.app.ChooserActivity;
@@ -265,13 +263,6 @@ public class ScreenshotController {
    private final UserManager mUserManager;
    private final AssistContentRequester mAssistContentRequester;

    private final OnBackInvokedCallback mOnBackInvokedCallback = () -> {
        if (DEBUG_INPUT) {
            Log.d(TAG, "Predictive Back callback dispatched");
        }
        respondToKeyDismissal();
    };

    private final MessageContainerController mMessageContainerController;
    private Bitmap mScreenBitmap;
    private SaveImageInBackgroundTask mSaveInBgTask;
@@ -594,27 +585,13 @@ public class ScreenshotController {
        }

        mMessageContainerController.setView(mViewProxy.getView());
        mViewProxy.addOnAttachStateChangeListener(
                new View.OnAttachStateChangeListener() {
                    @Override
                    public void onViewAttachedToWindow(@NonNull View v) {
                        if (DEBUG_INPUT) {
                            Log.d(TAG, "Registering Predictive Back callback");
                        }
                        mViewProxy.findOnBackInvokedDispatcher().registerOnBackInvokedCallback(
                                OnBackInvokedDispatcher.PRIORITY_DEFAULT, mOnBackInvokedCallback);
                    }

                    @Override
                    public void onViewDetachedFromWindow(@NonNull View v) {
        mViewProxy.setLogger(mUiEventLogger);
        mViewProxy.setOnBackInvokedCallback(() -> {
            if (DEBUG_INPUT) {
                            Log.d(TAG, "Unregistering Predictive Back callback");
                        }
                        mViewProxy.findOnBackInvokedDispatcher()
                                .unregisterOnBackInvokedCallback(mOnBackInvokedCallback);
                Log.d(TAG, "Predictive Back callback dispatched");
            }
            respondToKeyDismissal();
        });
        mViewProxy.setLogger(mUiEventLogger);
        mViewProxy.setCallbacks(new ScreenshotView.ScreenshotViewCallback() {
            @Override
            public void onUserInteraction() {
@@ -656,11 +633,6 @@ public class ScreenshotController {
            return false;
        });

        if (DEBUG_WINDOW) {
            Log.d(TAG, "adding OnComputeInternalInsetsListener");
        }
        mViewProxy.getViewTreeObserver().addOnComputeInternalInsetsListener(
                mViewProxy.getInternalInsetsListener());
        if (DEBUG_WINDOW) {
            Log.d(TAG, "setContentView: " + mViewProxy.getView());
        }
+2 −4
Original line number Diff line number Diff line
@@ -28,18 +28,18 @@ import android.view.View.OnKeyListener
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.view.WindowInsets
import android.window.OnBackInvokedDispatcher
import android.window.OnBackInvokedCallback
import com.android.internal.logging.UiEventLogger
import com.android.systemui.flags.FeatureFlags

/** Abstraction of the surface between ScreenshotController and ScreenshotView */
interface ScreenshotViewProxy {
    val view: ViewGroup
    val internalInsetsListener: ViewTreeObserver.OnComputeInternalInsetsListener
    val screenshotPreview: View

    var defaultDisplay: Int
    var defaultTimeoutMillis: Long
    var onBackInvokedCallback: OnBackInvokedCallback
    var onKeyListener: OnKeyListener?
    var flags: FeatureFlags?
    var packageName: String
@@ -78,8 +78,6 @@ interface ScreenshotViewProxy {
    fun stopInputListening()
    fun requestFocus()
    fun announceForAccessibility(string: String)
    fun addOnAttachStateChangeListener(listener: View.OnAttachStateChangeListener)
    fun findOnBackInvokedDispatcher(): OnBackInvokedDispatcher?
    fun getViewTreeObserver(): ViewTreeObserver
    fun post(runnable: Runnable)