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

Unverified Commit 7c3dcc94 authored by Arian's avatar Arian Committed by Michael Bestas
Browse files

udfps: Restore illumination dot for global hbm

UdfpsSurfaceView.java is imported from android-12.1.0_r22

Change-Id: I4a4e85a7437a9a444a4f952fd62e4fe12f56ce5a
parent a2ac939d
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -18,4 +18,9 @@
    android:id="@+id/udfps_touch_overlay"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.android.systemui.biometrics.UdfpsSurfaceView
        android:id="@+id/hbm_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="invisible"/>
</com.android.systemui.biometrics.ui.view.UdfpsTouchOverlay>
+5 −3
Original line number Diff line number Diff line
@@ -81,6 +81,7 @@ import com.android.systemui.biometrics.udfps.NormalizedTouchData;
import com.android.systemui.biometrics.udfps.SinglePointerTouchProcessor;
import com.android.systemui.biometrics.udfps.TouchProcessor;
import com.android.systemui.biometrics.udfps.TouchProcessorResult;
import com.android.systemui.biometrics.ui.view.UdfpsTouchOverlay;
import com.android.systemui.biometrics.ui.viewmodel.DefaultUdfpsTouchOverlayViewModel;
import com.android.systemui.biometrics.ui.viewmodel.DeviceEntryUdfpsTouchOverlayViewModel;
import com.android.systemui.bouncer.domain.interactor.AlternateBouncerInteractor;
@@ -865,8 +866,9 @@ public class UdfpsController implements DozeReceiver, Dumpable {
        if (!isOptical()) {
            return;
        }
        if (mUdfpsDisplayMode != null) {
            mUdfpsDisplayMode.disable(null);
        UdfpsTouchOverlay udfpsView = (UdfpsTouchOverlay) view;
        if (udfpsView.isDisplayConfigured()) {
            udfpsView.unconfigureDisplay();
        }
    }

@@ -1073,7 +1075,7 @@ public class UdfpsController implements DozeReceiver, Dumpable {
            if (mIgnoreRefreshRate) {
                dispatchOnUiReady(requestId);
            } else {
                    mUdfpsDisplayMode.enable(() -> dispatchOnUiReady(requestId));
                ((UdfpsTouchOverlay) view).configureDisplay(() -> dispatchOnUiReady(requestId));
            }
        }

+7 −0
Original line number Diff line number Diff line
@@ -192,6 +192,7 @@ constructor(
                    (inflater.inflate(R.layout.udfps_touch_overlay, null, false)
                            as UdfpsTouchOverlay)
                        .apply {
                            setUdfpsDisplayModeProvider(udfpsDisplayModeProvider)
                            // This view overlaps the sensor area
                            // prevent it from being selectable during a11y
                            if (requestReason.isImportantForAccessibility()) {
@@ -213,6 +214,7 @@ constructor(
                                        udfpsOverlayInteractor = udfpsOverlayInteractor,
                                    )
                            }
                            sensorRect = sensorBounds
                        }

                getTouchOverlay()?.apply {
@@ -285,6 +287,11 @@ constructor(
    fun hide(): Boolean {
        val wasShowing = isShowing

        overlayTouchView?.apply {
            if (isDisplayConfigured) {
                unconfigureDisplay()
            }
        }
        udfpsDisplayModeProvider.disable(null)
        getTouchOverlay()?.apply {
            if (this.parent != null) {
+145 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.systemui.biometrics;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Surface View for providing the Global High-Brightness Mode (GHBM) illumination for UDFPS.
 */
public class UdfpsSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "UdfpsSurfaceView";

    /**
     * Notifies {@link UdfpsView} when to enable GHBM illumination.
     */
    interface GhbmIlluminationListener {
        /**
         * @param surface the surface for which GHBM should be enabled.
         * @param onDisplayConfigured a runnable that should be run after GHBM is enabled.
         */
        void enableGhbm(@NonNull Surface surface, @Nullable Runnable onDisplayConfigured);
    }

    @NonNull private final SurfaceHolder mHolder;
    @NonNull private final Paint mSensorPaint;

    @Nullable private GhbmIlluminationListener mGhbmIlluminationListener;
    @Nullable private Runnable mOnDisplayConfigured;
    boolean mAwaitingSurfaceToStartIllumination;
    boolean mHasValidSurface;

    public UdfpsSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);

        // Make this SurfaceView draw on top of everything else in this window. This allows us to
        // 1) Always show the HBM circle on top of everything else, and
        // 2) Properly composite this view with any other animations in the same window no matter
        //    what contents are added in which order to this view hierarchy.
        setZOrderOnTop(true);

        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setFormat(PixelFormat.RGBA_8888);

        mSensorPaint = new Paint(0 /* flags */);
        mSensorPaint.setAntiAlias(true);
        mSensorPaint.setARGB(255, 255, 255, 255);
        mSensorPaint.setStyle(Paint.Style.FILL);
    }

    @Override public void surfaceCreated(SurfaceHolder holder) {
        mHasValidSurface = true;
        if (mAwaitingSurfaceToStartIllumination) {
            doIlluminate(mOnDisplayConfigured);
            mOnDisplayConfigured = null;
            mAwaitingSurfaceToStartIllumination = false;
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // Unused.
    }

    @Override public void surfaceDestroyed(SurfaceHolder holder) {
        mHasValidSurface = false;
    }

    public void setGhbmIlluminationListener(@Nullable GhbmIlluminationListener listener) {
        mGhbmIlluminationListener = listener;
    }

    /**
     * Note: there is no corresponding method to stop GHBM illumination. It is expected that
     * {@link UdfpsView} will hide this view, which would destroy the surface and remove the
     * illumination dot.
     */
    public void startGhbmIllumination(@Nullable Runnable onDisplayConfigured) {
        if (mGhbmIlluminationListener == null) {
            Log.e(TAG, "startIllumination | mGhbmIlluminationListener is null");
            return;
        }

        if (mHasValidSurface) {
            doIlluminate(onDisplayConfigured);
        } else {
            mAwaitingSurfaceToStartIllumination = true;
            mOnDisplayConfigured = onDisplayConfigured;
        }
    }

    private void doIlluminate(@Nullable Runnable onDisplayConfigured) {
        if (mGhbmIlluminationListener == null) {
            Log.e(TAG, "doIlluminate | mGhbmIlluminationListener is null");
            return;
        }

        mGhbmIlluminationListener.enableGhbm(mHolder.getSurface(), onDisplayConfigured);
    }

    /**
     * Immediately draws the illumination dot on this SurfaceView's surface.
     */
    public void drawIlluminationDot(@NonNull RectF sensorRect) {
        if (!mHasValidSurface) {
            Log.e(TAG, "drawIlluminationDot | the surface is destroyed or was never created.");
            return;
        }
        Canvas canvas = null;
        try {
            canvas = mHolder.lockCanvas();
            canvas.drawOval(sensorRect, mSensorPaint);
        } finally {
            // Make sure the surface is never left in a bad state.
            if (canvas != null) {
                mHolder.unlockCanvasAndPost(canvas);
            }
        }
    }
}
+55 −1
Original line number Diff line number Diff line
@@ -16,11 +16,65 @@
package com.android.systemui.biometrics.ui.view

import android.content.Context
import android.graphics.Rect
import android.graphics.RectF
import android.util.AttributeSet
import android.view.Surface
import android.widget.FrameLayout

import com.android.systemui.biometrics.UdfpsDisplayModeProvider
import com.android.systemui.biometrics.UdfpsSurfaceView
import com.android.systemui.res.R

/**
 * A translucent (not visible to the user) view that receives touches to send to FingerprintManager
 * for fingerprint authentication.
 */
class UdfpsTouchOverlay(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs)
class UdfpsTouchOverlay(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) {
    private var ghbmView: UdfpsSurfaceView? = null
    private var udfpsDisplayMode: UdfpsDisplayModeProvider? = null

    // sensorRect may be bigger than the sensor. True sensor dimensions are defined in
    // overlayParams.sensorBounds
    var sensorRect = Rect()

    /** True after the call to [configureDisplay] and before the call to [unconfigureDisplay]. */
    var isDisplayConfigured: Boolean = false
        private set

    override fun onFinishInflate() {
        ghbmView = findViewById(R.id.hbm_view)
    }

   fun setUdfpsDisplayModeProvider(udfpsDisplayModeProvider: UdfpsDisplayModeProvider?) {
        udfpsDisplayMode = udfpsDisplayModeProvider
    }

    fun configureDisplay(onDisplayConfigured: Runnable) {
        isDisplayConfigured = true
        val gView = ghbmView
        if (gView != null) {
            gView.setGhbmIlluminationListener(this::doIlluminate)
            gView.visibility = VISIBLE
            gView.startGhbmIllumination(onDisplayConfigured)
        } else {
            doIlluminate(null /* surface */, onDisplayConfigured)
        }
    }

    private fun doIlluminate(surface: Surface?, onDisplayConfigured: Runnable?) {
        udfpsDisplayMode?.enable {
            onDisplayConfigured?.run()
            ghbmView?.drawIlluminationDot(RectF(sensorRect))
        }
    }

    fun unconfigureDisplay() {
        isDisplayConfigured = false
        ghbmView?.let { view ->
            view.setGhbmIlluminationListener(null)
            view.visibility = INVISIBLE
        }
        udfpsDisplayMode?.disable(null /* onDisabled */)
    }
}