diff --git a/quickstep/src/com/android/launcher3/taskbar/growth/NudgeView.kt b/quickstep/src/com/android/launcher3/taskbar/growth/NudgeView.kt new file mode 100644 index 0000000000..17ee50e367 --- /dev/null +++ b/quickstep/src/com/android/launcher3/taskbar/growth/NudgeView.kt @@ -0,0 +1,180 @@ +/* + * 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.launcher3.taskbar.growth + +import android.animation.AnimatorSet +import android.animation.ValueAnimator +import android.content.Context +import android.util.AttributeSet +import android.view.MotionEvent +import android.view.ViewGroup +import android.view.animation.Interpolator +import android.window.OnBackInvokedDispatcher +import androidx.core.view.updateLayoutParams +import com.android.app.animation.Interpolators +import com.android.launcher3.AbstractFloatingView +import com.android.launcher3.R +import com.android.launcher3.anim.AnimatorListeners +import com.android.launcher3.views.ActivityContext + +private const val ENTER_DURATION_MS = 300L +private const val EXIT_DURATION_MS = 150L + +/** Floating nudge. */ +class NudgeView +@JvmOverloads +constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : + AbstractFloatingView(context, attrs, defStyleAttr) { + + private val activityContext: ActivityContext = ActivityContext.lookupContext(context) + + private val enterYDelta = resources.getDimension(R.dimen.nudge_enter_y_delta) + private val exitYDelta = resources.getDimension(R.dimen.nudge_exit_y_delta) + + /** Container where the nudge's body should be inflated. */ + lateinit var content: ViewGroup + private set + + /** Callback invoked when the nudge is being closed. */ + var onCloseCallback: () -> Unit = {} + private var openCloseAnimator: AnimatorSet? = null + /** Used to set whether users can tap outside the current nudge window to dismiss it */ + var allowTouchDismissal = true + + /** Animates the nudge into view. */ + fun show() { + if (isOpen) { + return + } + + mIsOpen = true + activityContext.dragLayer.addView(this) + + // Make sure we have enough height to display all of the content, which can be an issue on + // large text and display scaling configurations. If we run out of height, remove the width + // constraint to reduce the number of lines of text and hopefully free up some height. + activityContext.dragLayer.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED) + if ( + measuredHeight + activityContext.deviceProfile.taskbarHeight >= + activityContext.deviceProfile.availableHeightPx + ) { + updateLayoutParams { width = LayoutParams.MATCH_PARENT } + } + + openCloseAnimator = createOpenCloseAnimator(isOpening = true).apply { start() } + } + + override fun onFinishInflate() { + super.onFinishInflate() + + content = requireViewById(R.id.content) + } + + override fun handleClose(animate: Boolean) { + if (!isOpen) { + return + } + + onCloseCallback() + if (!animate) { + return closeComplete() + } + + openCloseAnimator?.cancel() + openCloseAnimator = createOpenCloseAnimator(isOpening = false) + openCloseAnimator?.apply { + addListener(AnimatorListeners.forEndCallback(this@NudgeView::closeComplete)) + start() + } + } + + override fun isOfType(type: Int): Boolean = type and TYPE_NUDGE != 0 + + override fun onControllerInterceptTouchEvent(ev: MotionEvent?): Boolean { + if ( + ev?.action == MotionEvent.ACTION_DOWN && + !activityContext.dragLayer.isEventOverView(this, ev) && + allowTouchDismissal + ) { + close(true) + } + return false + } + + override fun onAttachedToWindow() { + super.onAttachedToWindow() + findOnBackInvokedDispatcher() + ?.registerOnBackInvokedCallback(OnBackInvokedDispatcher.PRIORITY_DEFAULT, this) + } + + override fun onDetachedFromWindow() { + super.onDetachedFromWindow() + findOnBackInvokedDispatcher()?.unregisterOnBackInvokedCallback(this) + // TODO: b/396507770 - investigate and coordinate with letterbox flow. + } + + private fun closeComplete() { + openCloseAnimator?.cancel() + openCloseAnimator = null + mIsOpen = false + activityContext.dragLayer.removeView(this) + } + + private fun createOpenCloseAnimator(isOpening: Boolean): AnimatorSet { + val duration: Long + val alphaValues: FloatArray + val translateYValues: FloatArray + val fadeInterpolator: Interpolator + val translateYInterpolator: Interpolator + + if (isOpening) { + duration = ENTER_DURATION_MS + alphaValues = floatArrayOf(0f, 1f) + translateYValues = floatArrayOf(enterYDelta, 0f) + fadeInterpolator = Interpolators.STANDARD + translateYInterpolator = Interpolators.EMPHASIZED_DECELERATE + } else { + duration = EXIT_DURATION_MS + alphaValues = floatArrayOf(1f, 0f) + translateYValues = floatArrayOf(0f, exitYDelta) + fadeInterpolator = Interpolators.EMPHASIZED_ACCELERATE + translateYInterpolator = Interpolators.EMPHASIZED_ACCELERATE + } + + val fade = + ValueAnimator.ofFloat(*alphaValues).apply { + interpolator = fadeInterpolator + addUpdateListener { + val alpha = it.animatedValue as Float + content.alpha = alpha + } + } + + val translateY = + ValueAnimator.ofFloat(*translateYValues).apply { + interpolator = translateYInterpolator + addUpdateListener { + val translationY = it.animatedValue as Float + content.translationY = translationY + } + } + + return AnimatorSet().apply { + this.duration = duration + playTogether(fade, translateY) + } + } +} diff --git a/src/com/android/launcher3/AbstractFloatingView.java b/src/com/android/launcher3/AbstractFloatingView.java index b90200b88c..1e403633f7 100644 --- a/src/com/android/launcher3/AbstractFloatingView.java +++ b/src/com/android/launcher3/AbstractFloatingView.java @@ -77,6 +77,7 @@ public abstract class AbstractFloatingView extends LinearLayout implements Touch TYPE_TASKBAR_PINNING_POPUP, TYPE_PIN_IME_POPUP, TYPE_ONE_GRID_MIGRATION_EDU, + TYPE_NUDGE, }) @Retention(RetentionPolicy.SOURCE) public @interface FloatingViewType {} @@ -106,6 +107,7 @@ public abstract class AbstractFloatingView extends LinearLayout implements Touch public static final int TYPE_TASKBAR_PINNING_POPUP = 1 << 21; public static final int TYPE_PIN_IME_POPUP = 1 << 22; public static final int TYPE_ONE_GRID_MIGRATION_EDU = 1 << 23; + public static final int TYPE_NUDGE = 1 << 24; public static final int TYPE_ALL = TYPE_FOLDER | TYPE_ACTION_POPUP | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_WIDGET_RESIZE_FRAME | TYPE_WIDGETS_FULL_SHEET @@ -114,14 +116,15 @@ public abstract class AbstractFloatingView extends LinearLayout implements Touch | TYPE_ICON_SURFACE | TYPE_DRAG_DROP_POPUP | TYPE_PIN_WIDGET_FROM_EXTERNAL_POPUP | TYPE_TASKBAR_EDUCATION_DIALOG | TYPE_TASKBAR_ALL_APPS | TYPE_OPTIONS_POPUP_DIALOG | TYPE_ADD_TO_HOME_CONFIRMATION | TYPE_TASKBAR_OVERLAY_PROXY - | TYPE_TASKBAR_PINNING_POPUP | TYPE_PIN_IME_POPUP | TYPE_ONE_GRID_MIGRATION_EDU; + | TYPE_TASKBAR_PINNING_POPUP | TYPE_PIN_IME_POPUP | TYPE_ONE_GRID_MIGRATION_EDU + | TYPE_NUDGE; // Type of popups which should be kept open during launcher rebind public static final int TYPE_REBIND_SAFE = TYPE_WIDGETS_FULL_SHEET | TYPE_WIDGETS_BOTTOM_SHEET | TYPE_ON_BOARD_POPUP | TYPE_DISCOVERY_BOUNCE | TYPE_ALL_APPS_EDU | TYPE_ICON_SURFACE | TYPE_TASKBAR_EDUCATION_DIALOG | TYPE_TASKBAR_ALL_APPS | TYPE_OPTIONS_POPUP_DIALOG | TYPE_TASKBAR_OVERLAY_PROXY - | TYPE_PIN_IME_POPUP | TYPE_ONE_GRID_MIGRATION_EDU; + | TYPE_PIN_IME_POPUP | TYPE_ONE_GRID_MIGRATION_EDU | TYPE_NUDGE; /** Type of popups that should get exclusive accessibility focus. */ public static final int TYPE_ACCESSIBLE = TYPE_ALL & ~TYPE_DISCOVERY_BOUNCE & ~TYPE_LISTENER @@ -136,7 +139,7 @@ public abstract class AbstractFloatingView extends LinearLayout implements Touch // Floating views that are exclusive to the taskbar overlay window. public static final int TYPE_TASKBAR_OVERLAYS = - TYPE_TASKBAR_ALL_APPS | TYPE_TASKBAR_EDUCATION_DIALOG; + TYPE_TASKBAR_ALL_APPS | TYPE_TASKBAR_EDUCATION_DIALOG | TYPE_NUDGE; // Floating views that a TouchController should not try to intercept touches from. public static final int TYPE_TOUCH_CONTROLLER_NO_INTERCEPT = TYPE_ALL & ~TYPE_DISCOVERY_BOUNCE