diff --git a/quickstep/res/values/config.xml b/quickstep/res/values/config.xml index e8cb5d5241..41b2384a35 100644 --- a/quickstep/res/values/config.xml +++ b/quickstep/res/values/config.xml @@ -37,8 +37,8 @@ com.android.launcher3.taskbar.TaskbarEduTooltipController com.android.quickstep.contextualeducation.SystemContextualEduStatsManager - - + + com.android.launcher3.uioverrides.SystemApiWrapper collapse %1$s + + + Circle to Search diff --git a/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java b/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java index 4014f069cc..29e1f4e409 100644 --- a/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java +++ b/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java @@ -77,6 +77,7 @@ import com.android.launcher3.util.PackageManagerHelper; import com.android.launcher3.util.PersistedItemArray; import com.android.quickstep.logging.SettingsChangeLogger; import com.android.quickstep.logging.StatsLogCompatManager; +import com.android.quickstep.util.ContextualSearchStateManager; import com.android.systemui.shared.system.SysUiStatsLog; import java.util.ArrayList; @@ -209,6 +210,8 @@ public class QuickstepModelDelegate extends ModelDelegate { @Override public void workspaceLoadComplete() { super.workspaceLoadComplete(); + // Initialize ContextualSearchStateManager. + ContextualSearchStateManager.INSTANCE.get(mContext); recreatePredictors(); } diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarEduTooltipController.kt b/quickstep/src/com/android/launcher3/taskbar/TaskbarEduTooltipController.kt index 06376d3fd2..ade8f8cdc6 100644 --- a/quickstep/src/com/android/launcher3/taskbar/TaskbarEduTooltipController.kt +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarEduTooltipController.kt @@ -49,6 +49,7 @@ import com.android.launcher3.util.OnboardingPrefs.TASKBAR_SEARCH_EDU_SEEN import com.android.launcher3.util.ResourceBasedOverride import com.android.launcher3.views.ActivityContext import com.android.launcher3.views.BaseDragLayer +import com.android.quickstep.util.ContextualSearchInvoker import com.android.quickstep.util.LottieAnimationColorUtils import java.io.PrintWriter @@ -80,7 +81,11 @@ open class TaskbarEduTooltipController(context: Context) : ResourceBasedOverride, LoggableTaskbarController { protected val activityContext: TaskbarActivityContext = ActivityContext.lookupContext(context) - open val shouldShowSearchEdu = false + open val shouldShowSearchEdu: Boolean + get() = + ContextualSearchInvoker.newInstance(activityContext) + .runContextualSearchInvocationChecksAndLogFailures() + private val isTooltipEnabled: Boolean get() { return !Utilities.isRunningInTestHarness() && @@ -351,19 +356,19 @@ open class TaskbarEduTooltipController(context: Context) : overlayContext.layoutInflater.inflate( R.layout.taskbar_edu_tooltip, overlayContext.dragLayer, - false + false, ) as TaskbarEduTooltip controllers.taskbarAutohideSuspendController.updateFlag( FLAG_AUTOHIDE_SUSPEND_EDU_OPEN, - true + true, ) tooltip.onCloseCallback = { this.tooltip = null controllers.taskbarAutohideSuspendController.updateFlag( FLAG_AUTOHIDE_SUSPEND_EDU_OPEN, - false + false, ) controllers.taskbarStashController.updateAndAnimateTransientTaskbar(true) } @@ -378,7 +383,7 @@ open class TaskbarEduTooltipController(context: Context) : override fun performAccessibilityAction( host: View, action: Int, - args: Bundle? + args: Bundle?, ): Boolean { if (action == R.id.close) { hide() @@ -396,13 +401,13 @@ open class TaskbarEduTooltipController(context: Context) : override fun onInitializeAccessibilityNodeInfo( host: View, - info: AccessibilityNodeInfo + info: AccessibilityNodeInfo, ) { super.onInitializeAccessibilityNodeInfo(host, info) info.addAction( AccessibilityNodeInfo.AccessibilityAction( R.id.close, - host.context?.getText(R.string.taskbar_edu_close) + host.context?.getText(R.string.taskbar_edu_close), ) ) } @@ -421,7 +426,7 @@ open class TaskbarEduTooltipController(context: Context) : return ResourceBasedOverride.Overrides.getObject( TaskbarEduTooltipController::class.java, context, - R.string.taskbar_edu_tooltip_controller_class + R.string.taskbar_edu_tooltip_controller_class, ) } } diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java index 78e7b470ff..f9a84a75be 100644 --- a/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarManager.java @@ -72,7 +72,7 @@ import com.android.launcher3.util.SimpleBroadcastReceiver; import com.android.quickstep.AllAppsActionManager; import com.android.quickstep.RecentsActivity; import com.android.quickstep.SystemUiProxy; -import com.android.quickstep.util.AssistUtils; +import com.android.quickstep.util.ContextualSearchInvoker; import com.android.systemui.shared.statusbar.phone.BarTransitions; import com.android.systemui.shared.system.QuickStepContract; import com.android.systemui.shared.system.QuickStepContract.SystemUiStateFlags; @@ -250,7 +250,7 @@ public class TaskbarManager { SystemUiProxy.INSTANCE.get(mContext), ContextualEduStatsManager.INSTANCE.get(mContext), new Handler(), - AssistUtils.newInstance(mContext)); + ContextualSearchInvoker.newInstance(mContext)); mComponentCallbacks = new ComponentCallbacks() { private Configuration mOldConfig = mContext.getResources().getConfiguration(); diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarNavButtonController.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarNavButtonController.java index 15c35b644e..894791479e 100644 --- a/quickstep/src/com/android/launcher3/taskbar/TaskbarNavButtonController.java +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarNavButtonController.java @@ -51,7 +51,7 @@ import com.android.launcher3.testing.TestLogging; import com.android.launcher3.testing.shared.TestProtocol; import com.android.quickstep.SystemUiProxy; import com.android.quickstep.TaskUtils; -import com.android.quickstep.util.AssistUtils; +import com.android.quickstep.util.ContextualSearchInvoker; import com.android.systemui.contextualeducation.GestureType; import com.android.systemui.shared.system.QuickStepContract.SystemUiStateFlags; @@ -113,7 +113,7 @@ public class TaskbarNavButtonController implements TaskbarControllers.LoggableTa private final SystemUiProxy mSystemUiProxy; private final ContextualEduStatsManager mContextualEduStatsManager; private final Handler mHandler; - private final AssistUtils mAssistUtils; + private final ContextualSearchInvoker mContextualSearchInvoker; @Nullable private StatsLogManager mStatsLogManager; private final Runnable mResetLongPress = this::resetScreenUnpin; @@ -124,13 +124,13 @@ public class TaskbarNavButtonController implements TaskbarControllers.LoggableTa SystemUiProxy systemUiProxy, ContextualEduStatsManager contextualEduStatsManager, Handler handler, - AssistUtils assistUtils) { + ContextualSearchInvoker contextualSearchInvoker) { mContext = context; mCallbacks = callbacks; mSystemUiProxy = systemUiProxy; mContextualEduStatsManager = contextualEduStatsManager; mHandler = handler; - mAssistUtils = assistUtils; + mContextualSearchInvoker = contextualSearchInvoker; } public void onButtonClick(@TaskbarButton int buttonType, View view) { @@ -344,8 +344,9 @@ public class TaskbarNavButtonController implements TaskbarControllers.LoggableTa if (mScreenPinned || !mAssistantLongPressEnabled) { return; } - // Attempt to start Assist with AssistUtils, otherwise fall back to SysUi's implementation. - if (!mAssistUtils.tryStartAssistOverride(INVOCATION_TYPE_HOME_BUTTON_LONG_PRESS)) { + // Attempt to start Contextual Search, otherwise fall back to SysUi's implementation. + if (!mContextualSearchInvoker.tryStartAssistOverride( + INVOCATION_TYPE_HOME_BUTTON_LONG_PRESS)) { Bundle args = new Bundle(); args.putInt(INVOCATION_TYPE_KEY, INVOCATION_TYPE_HOME_BUTTON_LONG_PRESS); mSystemUiProxy.startAssistant(args); diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarViewCallbacks.java b/quickstep/src/com/android/launcher3/taskbar/TaskbarViewCallbacks.java index 176be1ce5b..8bc1e12395 100644 --- a/quickstep/src/com/android/launcher3/taskbar/TaskbarViewCallbacks.java +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarViewCallbacks.java @@ -19,6 +19,7 @@ package com.android.launcher3.taskbar; import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_TASKBAR_ALLAPPS_BUTTON_LONG_PRESS; import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_TASKBAR_ALLAPPS_BUTTON_TAP; +import android.content.Context; import android.view.InputDevice; import android.view.MotionEvent; import android.view.View; @@ -64,7 +65,8 @@ public class TaskbarViewCallbacks { mActivity.getStatsLogManager().logger().log(LAUNCHER_TASKBAR_ALLAPPS_BUTTON_LONG_PRESS); } - public boolean isAllAppsButtonHapticFeedbackEnabled() { + /** @return true if haptic feedback should occur when long pressing the all apps button. */ + public boolean isAllAppsButtonHapticFeedbackEnabled(Context context) { return false; } diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarViewCallbacksFactory.kt b/quickstep/src/com/android/launcher3/taskbar/TaskbarViewCallbacksFactory.kt index ba0f5a01b0..704d6cf6c1 100644 --- a/quickstep/src/com/android/launcher3/taskbar/TaskbarViewCallbacksFactory.kt +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarViewCallbacksFactory.kt @@ -16,10 +16,14 @@ package com.android.launcher3.taskbar +import android.app.contextualsearch.ContextualSearchManager.ENTRYPOINT_LONG_PRESS_META import android.content.Context import com.android.launcher3.R +import com.android.launcher3.logging.StatsLogManager import com.android.launcher3.util.ResourceBasedOverride import com.android.launcher3.util.ResourceBasedOverride.Overrides +import com.android.quickstep.TopTaskTracker +import com.android.quickstep.util.ContextualSearchInvoker /** Creates [TaskbarViewCallbacks] instances. */ open class TaskbarViewCallbacksFactory : ResourceBasedOverride { @@ -28,7 +32,35 @@ open class TaskbarViewCallbacksFactory : ResourceBasedOverride { activity: TaskbarActivityContext, controllers: TaskbarControllers, taskbarView: TaskbarView, - ): TaskbarViewCallbacks = TaskbarViewCallbacks(activity, controllers, taskbarView) + ): TaskbarViewCallbacks { + return object : TaskbarViewCallbacks(activity, controllers, taskbarView) { + override fun triggerAllAppsButtonLongClick() { + super.triggerAllAppsButtonLongClick() + + val contextualSearchInvoked = + ContextualSearchInvoker.newInstance(activity).show(ENTRYPOINT_LONG_PRESS_META) + if (contextualSearchInvoked) { + val runningPackage = + TopTaskTracker.INSTANCE[activity].getCachedTopTask( + /* filterOnlyVisibleRecents */ true + ) + .getPackageName() + activity.statsLogManager + .logger() + .withPackageName(runningPackage) + .log(StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_OMNI_SUCCESSFUL_META) + } + } + + override fun isAllAppsButtonHapticFeedbackEnabled(context: Context): Boolean { + return longPressAllAppsToStartContextualSearch(context) + } + } + } + + open fun longPressAllAppsToStartContextualSearch(context: Context): Boolean = + ContextualSearchInvoker.newInstance(context) + .runContextualSearchInvocationChecksAndLogFailures() companion object { @JvmStatic diff --git a/quickstep/src/com/android/launcher3/taskbar/customization/TaskbarAllAppsButtonContainer.kt b/quickstep/src/com/android/launcher3/taskbar/customization/TaskbarAllAppsButtonContainer.kt index e6c0b2f087..c5f8aa091d 100644 --- a/quickstep/src/com/android/launcher3/taskbar/customization/TaskbarAllAppsButtonContainer.kt +++ b/quickstep/src/com/android/launcher3/taskbar/customization/TaskbarAllAppsButtonContainer.kt @@ -37,7 +37,7 @@ import com.android.launcher3.util.Executors.MAIN_EXECUTOR import com.android.launcher3.views.ActivityContext import com.android.launcher3.views.IconButtonView import com.android.quickstep.DeviceConfigWrapper -import com.android.quickstep.util.AssistStateManager +import com.android.quickstep.util.ContextualSearchStateManager /** Taskbar all apps button container for customizable taskbar. */ class TaskbarAllAppsButtonContainer @@ -79,17 +79,18 @@ constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 setOnClickListener(this::onAllAppsButtonClick) setOnLongClickListener(this::onAllAppsButtonLongClick) setOnTouchListener(this::onAllAppsButtonTouch) - isHapticFeedbackEnabled = taskbarViewCallbacks.isAllAppsButtonHapticFeedbackEnabled() + isHapticFeedbackEnabled = + taskbarViewCallbacks.isAllAppsButtonHapticFeedbackEnabled(mContext) allAppsTouchRunnable = Runnable { taskbarViewCallbacks.triggerAllAppsButtonLongClick() allAppsTouchTriggered = true } - val assistStateManager = AssistStateManager.INSTANCE[mContext] + val contextualSearchStateManager = ContextualSearchStateManager.INSTANCE[mContext] if ( DeviceConfigWrapper.get().customLpaaThresholds && - assistStateManager.lpnhDurationMillis.isPresent + contextualSearchStateManager.lpnhDurationMillis.isPresent ) { - allAppsButtonTouchDelayMs = assistStateManager.lpnhDurationMillis.get() + allAppsButtonTouchDelayMs = contextualSearchStateManager.lpnhDurationMillis.get() } } diff --git a/quickstep/src/com/android/quickstep/RecentsAnimationDeviceState.java b/quickstep/src/com/android/quickstep/RecentsAnimationDeviceState.java index 51317743cd..2fa201d034 100644 --- a/quickstep/src/com/android/quickstep/RecentsAnimationDeviceState.java +++ b/quickstep/src/com/android/quickstep/RecentsAnimationDeviceState.java @@ -70,7 +70,7 @@ import com.android.launcher3.util.NavigationMode; import com.android.launcher3.util.SettingsCache; import com.android.quickstep.TopTaskTracker.CachedTaskInfo; import com.android.quickstep.util.ActiveGestureLog; -import com.android.quickstep.util.AssistStateManager; +import com.android.quickstep.util.ContextualSearchStateManager; import com.android.quickstep.util.GestureExclusionManager; import com.android.quickstep.util.GestureExclusionManager.ExclusionListener; import com.android.quickstep.util.NavBarPosition; @@ -101,7 +101,7 @@ public class RecentsAnimationDeviceState implements DisplayInfoChangeListener, E private final DisplayController mDisplayController; private final GestureExclusionManager mExclusionManager; - private final AssistStateManager mAssistStateManager; + private final ContextualSearchStateManager mContextualSearchStateManager; private final RotationTouchHelper mRotationTouchHelper; private final TaskStackChangeListener mPipListener; @@ -152,7 +152,7 @@ public class RecentsAnimationDeviceState implements DisplayInfoChangeListener, E mContext = context; mDisplayController = DisplayController.INSTANCE.get(context); mExclusionManager = exclusionManager; - mAssistStateManager = AssistStateManager.INSTANCE.get(context); + mContextualSearchStateManager = ContextualSearchStateManager.INSTANCE.get(context); mIsOneHandedModeSupported = SystemProperties.getBoolean(SUPPORT_ONE_HANDED_MODE, false); mRotationTouchHelper = RotationTouchHelper.INSTANCE.get(context); if (isInstanceForTouches) { @@ -617,8 +617,9 @@ public class RecentsAnimationDeviceState implements DisplayInfoChangeListener, E : QUICKSTEP_TOUCH_SLOP_RATIO_TWO_BUTTON; float touchSlop = ViewConfiguration.get(mContext).getScaledTouchSlop(); - if (mAssistStateManager.getLPNHCustomSlopMultiplier().isPresent()) { - float customSlopMultiplier = mAssistStateManager.getLPNHCustomSlopMultiplier().get(); + if (mContextualSearchStateManager.getLPNHCustomSlopMultiplier().isPresent()) { + float customSlopMultiplier = + mContextualSearchStateManager.getLPNHCustomSlopMultiplier().get(); return customSlopMultiplier * slopMultiplier * touchSlop; } else { return slopMultiplier * touchSlop; diff --git a/quickstep/src/com/android/quickstep/SystemUiProxy.java b/quickstep/src/com/android/quickstep/SystemUiProxy.java index 5f028937b0..eeea528c60 100644 --- a/quickstep/src/com/android/quickstep/SystemUiProxy.java +++ b/quickstep/src/com/android/quickstep/SystemUiProxy.java @@ -64,7 +64,7 @@ import com.android.launcher3.util.MainThreadInitializedObject; import com.android.launcher3.util.Preconditions; import com.android.launcher3.util.SafeCloseable; import com.android.quickstep.util.ActiveGestureProtoLogProxy; -import com.android.quickstep.util.AssistUtils; +import com.android.quickstep.util.ContextualSearchInvoker; import com.android.quickstep.util.unfold.ProxyUnfoldTransitionProvider; import com.android.systemui.shared.recents.ISystemUiProxy; import com.android.systemui.shared.recents.model.ThumbnailData; @@ -311,8 +311,8 @@ public class SystemUiProxy implements ISystemUiProxy, NavHandle, SafeCloseable { setBackToLauncherCallback(mBackToLauncherCallback, mBackToLauncherRunner); setUnfoldAnimationListener(mUnfoldAnimationListener); setDesktopTaskListener(mDesktopTaskListener); - setAssistantOverridesRequested( - AssistUtils.newInstance(mContext).getSysUiAssistOverrideInvocationTypes()); + setAssistantOverridesRequested(ContextualSearchInvoker.newInstance(mContext) + .getSysUiAssistOverrideInvocationTypes()); mStateChangeCallbacks.forEach(Runnable::run); if (mUnfoldTransitionProvider != null) { diff --git a/quickstep/src/com/android/quickstep/TouchInteractionService.java b/quickstep/src/com/android/quickstep/TouchInteractionService.java index f0943dc798..41a8a3150f 100644 --- a/quickstep/src/com/android/quickstep/TouchInteractionService.java +++ b/quickstep/src/com/android/quickstep/TouchInteractionService.java @@ -122,8 +122,8 @@ import com.android.quickstep.inputconsumers.TrackpadStatusBarInputConsumer; import com.android.quickstep.util.ActiveGestureLog; import com.android.quickstep.util.ActiveGestureLog.CompoundString; import com.android.quickstep.util.ActiveGestureProtoLogProxy; -import com.android.quickstep.util.AssistStateManager; -import com.android.quickstep.util.AssistUtils; +import com.android.quickstep.util.ContextualSearchInvoker; +import com.android.quickstep.util.ContextualSearchStateManager; import com.android.quickstep.views.RecentsViewContainer; import com.android.systemui.shared.recents.IOverviewProxy; import com.android.systemui.shared.recents.ISystemUiProxy; @@ -297,7 +297,8 @@ public class TouchInteractionService extends Service { @Override public void onAssistantOverrideInvoked(int invocationType) { executeForTouchInteractionService(tis -> { - if (!AssistUtils.newInstance(tis).tryStartAssistOverride(invocationType)) { + if (!ContextualSearchInvoker.newInstance(tis) + .tryStartAssistOverride(invocationType)) { Log.w(TAG, "Failed to invoke Assist override"); } }); @@ -1640,8 +1641,8 @@ public class TouchInteractionService extends Service { } mTaskbarManager.dumpLogs("", pw); mDesktopVisibilityController.dumpLogs("", pw); - pw.println("AssistStateManager:"); - AssistStateManager.INSTANCE.get(this).dump("\t", pw); + pw.println("ContextualSearchStateManager:"); + ContextualSearchStateManager.INSTANCE.get(this).dump("\t", pw); SystemUiProxy.INSTANCE.get(this).dump(pw); DeviceConfigWrapper.get().dump(" ", pw); } diff --git a/quickstep/src/com/android/quickstep/dagger/QuickstepBaseAppComponent.java b/quickstep/src/com/android/quickstep/dagger/QuickstepBaseAppComponent.java index 341c86892a..335161b24c 100644 --- a/quickstep/src/com/android/quickstep/dagger/QuickstepBaseAppComponent.java +++ b/quickstep/src/com/android/quickstep/dagger/QuickstepBaseAppComponent.java @@ -21,6 +21,7 @@ import com.android.launcher3.dagger.LauncherBaseAppComponent; import com.android.launcher3.model.WellbeingModel; import com.android.quickstep.logging.SettingsChangeLogger; import com.android.quickstep.util.AsyncClockEventDelegate; +import com.android.quickstep.util.ContextualSearchHapticManager; /** * Launcher Quickstep base component for Dagger injection. @@ -36,4 +37,6 @@ public interface QuickstepBaseAppComponent extends LauncherBaseAppComponent { WellbeingModel getWellbeingModel(); AsyncClockEventDelegate getAsyncClockEventDelegate(); + + ContextualSearchHapticManager getContextualSearchHapticManager(); } diff --git a/quickstep/src/com/android/quickstep/inputconsumers/NavHandleLongPressHandler.java b/quickstep/src/com/android/quickstep/inputconsumers/NavHandleLongPressHandler.java index 1d00e533f5..1a825a42e3 100644 --- a/quickstep/src/com/android/quickstep/inputconsumers/NavHandleLongPressHandler.java +++ b/quickstep/src/com/android/quickstep/inputconsumers/NavHandleLongPressHandler.java @@ -16,25 +16,67 @@ package com.android.quickstep.inputconsumers; +import static android.app.contextualsearch.ContextualSearchManager.ENTRYPOINT_LONG_PRESS_NAV_HANDLE; + +import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_ASSISTANT_SUCCESSFUL_NAV_HANDLE; +import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_OMNI_GET_LONG_PRESS_RUNNABLE; +import static com.android.launcher3.logging.StatsLogManager.LauncherLatencyEvent.LAUNCHER_LATENCY_OMNI_RUNNABLE; + import android.content.Context; +import android.os.SystemClock; +import android.util.Log; +import android.view.ViewConfiguration; import androidx.annotation.Nullable; +import androidx.annotation.VisibleForTesting; +import com.android.launcher3.LauncherApplication; import com.android.launcher3.R; +import com.android.launcher3.logging.InstanceId; +import com.android.launcher3.logging.InstanceIdSequence; +import com.android.launcher3.logging.StatsLogManager; import com.android.launcher3.util.ResourceBasedOverride; +import com.android.launcher3.util.VibratorWrapper; +import com.android.quickstep.DeviceConfigWrapper; import com.android.quickstep.NavHandle; +import com.android.quickstep.TopTaskTracker; +import com.android.quickstep.util.ContextualSearchHapticManager; +import com.android.quickstep.util.ContextualSearchInvoker; +import com.android.quickstep.util.ContextualSearchStateManager; /** * Class for extending nav handle long press behavior */ public class NavHandleLongPressHandler implements ResourceBasedOverride { + private static final String TAG = "NavHandleLongPressHandler"; + + protected final Context mContext; + protected final VibratorWrapper mVibratorWrapper; + protected final ContextualSearchHapticManager mContextualSearchHapticManager; + protected final ContextualSearchInvoker mContextualSearchInvoker; + protected final StatsLogManager mStatsLogManager; + private boolean mPendingInvocation; + + public NavHandleLongPressHandler(Context context) { + mContext = context; + mStatsLogManager = StatsLogManager.newInstance(context); + mVibratorWrapper = VibratorWrapper.INSTANCE.get(mContext); + mContextualSearchHapticManager = ((LauncherApplication) context.getApplicationContext()) + .getAppComponent().getContextualSearchHapticManager(); + mContextualSearchInvoker = ContextualSearchInvoker.newInstance(mContext); + } + /** Creates NavHandleLongPressHandler as specified by overrides */ public static NavHandleLongPressHandler newInstance(Context context) { return Overrides.getObject(NavHandleLongPressHandler.class, context, R.string.nav_handle_long_press_handler_class); } + protected boolean isContextualSearchEntrypointEnabled(NavHandle navHandle) { + return DeviceConfigWrapper.get().getEnableLongPressNavHandle(); + } + /** * Called when nav handle is long pressed to get the Runnable that should be executed by the * caller to invoke long press behavior. If null is returned that means long press couldn't be @@ -46,8 +88,48 @@ public class NavHandleLongPressHandler implements ResourceBasedOverride { * * @param navHandle to handle this long press */ - public @Nullable Runnable getLongPressRunnable(NavHandle navHandle) { - return null; + @Nullable + @VisibleForTesting + final Runnable getLongPressRunnable(NavHandle navHandle) { + if (!isContextualSearchEntrypointEnabled(navHandle)) { + Log.i(TAG, "Contextual Search invocation failed: entry point disabled"); + mVibratorWrapper.cancelVibrate(); + return null; + } + + if (!mContextualSearchInvoker.runContextualSearchInvocationChecksAndLogFailures()) { + Log.i(TAG, "Contextual Search invocation failed: precondition not satisfied"); + mVibratorWrapper.cancelVibrate(); + return null; + } + + mPendingInvocation = true; + Log.i(TAG, "Contextual Search invocation: invocation runnable created"); + InstanceId instanceId = new InstanceIdSequence().newInstanceId(); + mStatsLogManager.logger().withInstanceId(instanceId).log( + LAUNCHER_OMNI_GET_LONG_PRESS_RUNNABLE); + long startTimeMillis = SystemClock.elapsedRealtime(); + return () -> { + mStatsLogManager.latencyLogger().withInstanceId(instanceId).withLatency( + SystemClock.elapsedRealtime() - startTimeMillis).log( + LAUNCHER_LATENCY_OMNI_RUNNABLE); + if (mContextualSearchInvoker.invokeContextualSearchUncheckedWithHaptic( + ENTRYPOINT_LONG_PRESS_NAV_HANDLE)) { + Log.i(TAG, "Contextual Search invocation successful"); + + String runningPackage = TopTaskTracker.INSTANCE.get(mContext).getCachedTopTask( + /* filterOnlyVisibleRecents */ true).getPackageName(); + mStatsLogManager.logger().withPackageName(runningPackage) + .log(LAUNCHER_LAUNCH_ASSISTANT_SUCCESSFUL_NAV_HANDLE); + } else { + mVibratorWrapper.cancelVibrate(); + if (DeviceConfigWrapper.get().getAnimateLpnh() + && !DeviceConfigWrapper.get().getShrinkNavHandleOnPress()) { + navHandle.animateNavBarLongPress( + /*isTouchDown*/false, /*shrink*/ false, /*durationMs*/160); + } + } + }; } /** @@ -55,7 +137,15 @@ public class NavHandleLongPressHandler implements ResourceBasedOverride { * * @param navHandle to handle the animation for this touch */ - public void onTouchStarted(NavHandle navHandle) {} + @VisibleForTesting + final void onTouchStarted(NavHandle navHandle) { + mPendingInvocation = false; + if (isContextualSearchEntrypointEnabled(navHandle) + && mContextualSearchInvoker.runContextualSearchInvocationChecksAndLogFailures()) { + Log.i(TAG, "Contextual Search invocation: touch started"); + startNavBarAnimation(navHandle); + } + } /** * Called when nav handle gesture is finished by the user lifting their finger or the system @@ -64,5 +154,46 @@ public class NavHandleLongPressHandler implements ResourceBasedOverride { * @param navHandle to handle the animation for this touch * @param reason why the touch ended */ - public void onTouchFinished(NavHandle navHandle, String reason) {} + @VisibleForTesting + final void onTouchFinished(NavHandle navHandle, String reason) { + Log.i(TAG, "Contextual Search invocation: touch finished with reason: " + reason); + + if (!DeviceConfigWrapper.get().getShrinkNavHandleOnPress() || !mPendingInvocation) { + mVibratorWrapper.cancelVibrate(); + } + + if (DeviceConfigWrapper.get().getAnimateLpnh()) { + if (DeviceConfigWrapper.get().getShrinkNavHandleOnPress()) { + navHandle.animateNavBarLongPress( + /*isTouchDown*/false, /*shrink*/ true, /*durationMs*/200); + } else { + navHandle.animateNavBarLongPress( + /*isTouchDown*/false, /*shrink*/ false, /*durationMs*/ 160); + } + } + } + + @VisibleForTesting + final void startNavBarAnimation(NavHandle navHandle) { + mContextualSearchHapticManager.vibrateForSearchHint(); + + if (DeviceConfigWrapper.get().getAnimateLpnh()) { + if (DeviceConfigWrapper.get().getShrinkNavHandleOnPress()) { + navHandle.animateNavBarLongPress( + /*isTouchDown*/ true, /*shrink*/true, /*durationMs*/200); + } else { + long longPressTimeout; + ContextualSearchStateManager contextualSearchStateManager = + ContextualSearchStateManager.INSTANCE.get(mContext); + if (contextualSearchStateManager.getLPNHDurationMillis().isPresent()) { + longPressTimeout = + contextualSearchStateManager.getLPNHDurationMillis().get().intValue(); + } else { + longPressTimeout = ViewConfiguration.getLongPressTimeout(); + } + navHandle.animateNavBarLongPress( + /*isTouchDown*/ true, /*shrink*/ false, /*durationMs*/ longPressTimeout); + } + } + } } diff --git a/quickstep/src/com/android/quickstep/inputconsumers/NavHandleLongPressInputConsumer.java b/quickstep/src/com/android/quickstep/inputconsumers/NavHandleLongPressInputConsumer.java index f4d36953b5..f5bef05e14 100644 --- a/quickstep/src/com/android/quickstep/inputconsumers/NavHandleLongPressInputConsumer.java +++ b/quickstep/src/com/android/quickstep/inputconsumers/NavHandleLongPressInputConsumer.java @@ -38,7 +38,7 @@ import com.android.quickstep.InputConsumer; import com.android.quickstep.NavHandle; import com.android.quickstep.RecentsAnimationDeviceState; import com.android.quickstep.TopTaskTracker; -import com.android.quickstep.util.AssistStateManager; +import com.android.quickstep.util.ContextualSearchStateManager; import com.android.systemui.shared.system.InputMonitorCompat; /** @@ -75,9 +75,11 @@ public class NavHandleLongPressInputConsumer extends DelegateInputConsumer { super(delegate, inputMonitor); mScreenWidth = DisplayController.INSTANCE.get(context).getInfo().currentSize.x; mDeepPressEnabled = DeviceConfigWrapper.get().getEnableLpnhDeepPress(); - AssistStateManager assistStateManager = AssistStateManager.INSTANCE.get(context); - if (assistStateManager.getLPNHDurationMillis().isPresent()) { - mLongPressTimeout = assistStateManager.getLPNHDurationMillis().get().intValue(); + ContextualSearchStateManager contextualSearchStateManager = + ContextualSearchStateManager.INSTANCE.get(context); + if (contextualSearchStateManager.getLPNHDurationMillis().isPresent()) { + mLongPressTimeout = + contextualSearchStateManager.getLPNHDurationMillis().get().intValue(); } else { mLongPressTimeout = ViewConfiguration.getLongPressTimeout(); } diff --git a/quickstep/src/com/android/quickstep/util/AssistStateManager.java b/quickstep/src/com/android/quickstep/util/AssistStateManager.java deleted file mode 100644 index 7acb28da11..0000000000 --- a/quickstep/src/com/android/quickstep/util/AssistStateManager.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2023 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.quickstep.util; - -import static com.android.launcher3.util.MainThreadInitializedObject.forOverride; - -import com.android.launcher3.R; -import com.android.launcher3.util.MainThreadInitializedObject; -import com.android.launcher3.util.ResourceBasedOverride; -import com.android.launcher3.util.SafeCloseable; - -import java.io.PrintWriter; -import java.util.Optional; - -/** Class to manage Assistant states. */ -public class AssistStateManager implements ResourceBasedOverride, SafeCloseable { - - public static final MainThreadInitializedObject INSTANCE = - forOverride(AssistStateManager.class, R.string.assist_state_manager_class); - - public AssistStateManager() {} - - /** Return {@code true} if the Settings toggle is enabled. */ - public boolean isSettingsAllEntrypointsEnabled() { - return false; - } - - /** Whether search supports showing on the lockscreen. */ - public boolean supportsShowWhenLocked() { - return false; - } - - /** Whether ContextualSearchService invocation path is available. */ - public boolean isContextualSearchServiceAvailable() { - return false; - } - - /** Get the Launcher overridden long press nav handle duration to trigger Assistant. */ - public Optional getLPNHDurationMillis() { - return Optional.empty(); - } - - /** - * Get the Launcher overridden long press nav handle touch slop multiplier to trigger Assistant. - */ - public Optional getLPNHCustomSlopMultiplier() { - return Optional.empty(); - } - - /** Get the Launcher overridden long press home duration to trigger Assistant. */ - public Optional getLPHDurationMillis() { - return Optional.empty(); - } - - /** Get the Launcher overridden long press home touch slop multiplier to trigger Assistant. */ - public Optional getLPHCustomSlopMultiplier() { - return Optional.empty(); - } - - /** Get the long press duration data source. */ - public int getDurationDataSource() { - return 0; - } - - /** Get the long press touch slop multiplier data source. */ - public int getSlopDataSource() { - return 0; - } - - /** Get the haptic bit overridden by AGSA. */ - public Optional getShouldPlayHapticOverride() { - return Optional.empty(); - } - - /** Dump states. */ - public void dump(String prefix, PrintWriter writer) {} - - @Override - public void close() {} -} diff --git a/quickstep/src/com/android/quickstep/util/AssistUtils.java b/quickstep/src/com/android/quickstep/util/AssistUtils.java deleted file mode 100644 index 11b6ea7d21..0000000000 --- a/quickstep/src/com/android/quickstep/util/AssistUtils.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2023 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.quickstep.util; - -import android.content.Context; - -import com.android.launcher3.R; -import com.android.launcher3.util.ResourceBasedOverride; - -/** Utilities to work with Assistant functionality. */ -public class AssistUtils implements ResourceBasedOverride { - - public AssistUtils() {} - - /** Creates AssistUtils as specified by overrides */ - public static AssistUtils newInstance(Context context) { - return Overrides.getObject(AssistUtils.class, context, R.string.assist_utils_class); - } - - /** @return Array of AssistUtils.INVOCATION_TYPE_* that we want to handle instead of SysUI. */ - public int[] getSysUiAssistOverrideInvocationTypes() { - return new int[0]; - } - - /** - * @return {@code true} if the override was handled, i.e. an assist surface was shown or the - * request should be ignored. {@code false} means the caller should start assist another way. - */ - public boolean tryStartAssistOverride(int invocationType) { - return false; - } -} diff --git a/quickstep/src/com/android/quickstep/util/ContextualSearchHapticManager.kt b/quickstep/src/com/android/quickstep/util/ContextualSearchHapticManager.kt new file mode 100644 index 0000000000..8c246a5240 --- /dev/null +++ b/quickstep/src/com/android/quickstep/util/ContextualSearchHapticManager.kt @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2024 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.quickstep.util + +import android.content.Context +import android.os.VibrationEffect +import android.os.VibrationEffect.Composition +import android.os.Vibrator +import com.android.launcher3.dagger.ApplicationContext +import com.android.launcher3.dagger.LauncherAppSingleton +import com.android.launcher3.util.VibratorWrapper +import com.android.quickstep.DeviceConfigWrapper.Companion.get +import javax.inject.Inject +import kotlin.math.pow + +/** Manages haptics relating to Contextual Search invocations. */ +@LauncherAppSingleton +class ContextualSearchHapticManager +@Inject +internal constructor(@ApplicationContext private val context: Context) { + + private var searchEffect = createSearchEffect() + private var contextualSearchStateManager = ContextualSearchStateManager.INSTANCE[context] + + private fun createSearchEffect() = + if ( + context + .getSystemService(Vibrator::class.java)!! + .areAllPrimitivesSupported(Composition.PRIMITIVE_TICK) + ) { + VibrationEffect.startComposition() + .addPrimitive(Composition.PRIMITIVE_TICK, 1f) + .compose() + } else { + // fallback for devices without composition support + VibrationEffect.createPredefined(VibrationEffect.EFFECT_HEAVY_CLICK) + } + + /** Indicates that search has been invoked. */ + fun vibrateForSearch() { + searchEffect.let { VibratorWrapper.INSTANCE[context].vibrate(it) } + } + + /** Indicates that search will be invoked if the current gesture is maintained. */ + fun vibrateForSearchHint() { + val navbarConfig = get() + // Whether we should play the hint (ramp up) haptic + val shouldVibrate: Boolean = + if ( + context + .getSystemService(Vibrator::class.java)!! + .areAllPrimitivesSupported(Composition.PRIMITIVE_LOW_TICK) + ) { + if (contextualSearchStateManager.shouldPlayHapticOverride.isPresent) { + contextualSearchStateManager.shouldPlayHapticOverride.get() + } else { + navbarConfig.enableSearchHapticHint + } + } else { + false + } + + if (shouldVibrate) { + val startScale = navbarConfig.lpnhHapticHintStartScalePercent / 100f + val endScale = navbarConfig.lpnhHapticHintEndScalePercent / 100f + val scaleExponent = navbarConfig.lpnhHapticHintScaleExponent + val iterations = navbarConfig.lpnhHapticHintIterations + val delayMs = navbarConfig.lpnhHapticHintDelay + val composition = VibrationEffect.startComposition() + for (i in 0 until iterations) { + val t = i / (iterations - 1f) + val scale = + ((1 - t) * startScale + t * endScale) + .toDouble() + .pow(scaleExponent.toDouble()) + .toFloat() + if (i == 0) { + // Adds a delay before the ramp starts + composition.addPrimitive(Composition.PRIMITIVE_LOW_TICK, scale, delayMs) + } else { + composition.addPrimitive(Composition.PRIMITIVE_LOW_TICK, scale) + } + } + VibratorWrapper.INSTANCE[context].vibrate(composition.compose()) + } + } +} diff --git a/quickstep/src/com/android/quickstep/util/ContextualSearchInvoker.kt b/quickstep/src/com/android/quickstep/util/ContextualSearchInvoker.kt new file mode 100644 index 0000000000..dcb72aa4f3 --- /dev/null +++ b/quickstep/src/com/android/quickstep/util/ContextualSearchInvoker.kt @@ -0,0 +1,224 @@ +/* + * Copyright (C) 2024 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.quickstep.util + +import android.app.contextualsearch.ContextualSearchManager +import android.app.contextualsearch.ContextualSearchManager.ENTRYPOINT_LONG_PRESS_HOME +import android.app.contextualsearch.ContextualSearchManager.FEATURE_CONTEXTUAL_SEARCH +import android.content.Context +import android.util.Log +import com.android.internal.app.AssistUtils +import com.android.launcher3.LauncherApplication +import com.android.launcher3.R +import com.android.launcher3.logging.StatsLogManager +import com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_ASSISTANT_FAILED_SERVICE_ERROR +import com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_OMNI_ATTEMPTED_OVER_KEYGUARD +import com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_OMNI_ATTEMPTED_OVER_NOTIFICATION_SHADE +import com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_OMNI_ATTEMPTED_SPLITSCREEN +import com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_OMNI_FAILED_NOT_AVAILABLE +import com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_OMNI_FAILED_SETTING_DISABLED +import com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_OMNI_SUCCESSFUL_HOME +import com.android.launcher3.util.ResourceBasedOverride +import com.android.quickstep.DeviceConfigWrapper +import com.android.quickstep.SystemUiProxy +import com.android.quickstep.TopTaskTracker +import com.android.systemui.shared.system.QuickStepContract + +/** Handles invocations and checks for Contextual Search. */ +open class ContextualSearchInvoker +internal constructor( + protected val context: Context, + private val contextualSearchStateManager: ContextualSearchStateManager, + private val topTaskTracker: TopTaskTracker, + private val systemUiProxy: SystemUiProxy, + protected val statsLogManager: StatsLogManager, + private val contextualSearchHapticManager: ContextualSearchHapticManager, + private val contextualSearchManager: ContextualSearchManager?, +) : ResourceBasedOverride { + constructor( + context: Context + ) : this( + context, + ContextualSearchStateManager.INSTANCE[context], + TopTaskTracker.INSTANCE[context], + SystemUiProxy.INSTANCE[context], + StatsLogManager.newInstance(context), + (context.applicationContext as LauncherApplication) + .appComponent + .contextualSearchHapticManager, + context.getSystemService(ContextualSearchManager::class.java), + ) + + /** @return Array of AssistUtils.INVOCATION_TYPE_* that we want to handle instead of SysUI. */ + open fun getSysUiAssistOverrideInvocationTypes(): IntArray { + val overrideInvocationTypes = com.android.launcher3.util.IntArray() + if (context.packageManager.hasSystemFeature(FEATURE_CONTEXTUAL_SEARCH)) { + overrideInvocationTypes.add(AssistUtils.INVOCATION_TYPE_HOME_BUTTON_LONG_PRESS) + } + return overrideInvocationTypes.toArray() + } + + /** + * @return `true` if the override was handled, i.e. an assist surface was shown or the request + * should be ignored. `false` means the caller should start assist another way. + */ + fun tryStartAssistOverride(invocationType: Int): Boolean { + if (invocationType == AssistUtils.INVOCATION_TYPE_HOME_BUTTON_LONG_PRESS) { + if (!context.packageManager.hasSystemFeature(FEATURE_CONTEXTUAL_SEARCH)) { + // When Contextual Search is disabled, fall back to Assistant. + return false + } + + val success = show(ENTRYPOINT_LONG_PRESS_HOME) + if (success) { + val runningPackage = + TopTaskTracker.INSTANCE[context].getCachedTopTask( + /* filterOnlyVisibleRecents */ true + ) + .getPackageName() + statsLogManager + .logger() + .withPackageName(runningPackage) + .log(LAUNCHER_LAUNCH_OMNI_SUCCESSFUL_HOME) + } + + // Regardless of success, do not fall back to other assistant. + return true + } + return false + } + + /** + * Invoke Contextual Search via ContextualSearchService if availability checks are successful + * + * @param entryPoint one of the ENTRY_POINT_* constants defined in this class + * @return true if invocation was successful, false otherwise + */ + fun show(entryPoint: Int): Boolean { + return if (!runContextualSearchInvocationChecksAndLogFailures()) false + else invokeContextualSearchUnchecked(entryPoint) + } + + /** + * Run availability checks and log errors to WW. If successful the caller is expected to call + * {@link invokeContextualSearchUnchecked} + * + * @return true if availability checks were successful, false otherwise. + */ + fun runContextualSearchInvocationChecksAndLogFailures(): Boolean { + if ( + contextualSearchManager == null || + !context.packageManager.hasSystemFeature(FEATURE_CONTEXTUAL_SEARCH) + ) { + Log.i(TAG, "Contextual Search invocation failed: no ContextualSearchManager") + statsLogManager.logger().log(LAUNCHER_LAUNCH_ASSISTANT_FAILED_SERVICE_ERROR) + return false + } + if (!contextualSearchStateManager.isContextualSearchSettingEnabled) { + Log.i(TAG, "Contextual Search invocation failed: setting disabled") + statsLogManager.logger().log(LAUNCHER_LAUNCH_OMNI_FAILED_SETTING_DISABLED) + return false + } + if (isNotificationShadeShowing()) { + Log.i(TAG, "Contextual Search invocation failed: notification shade") + statsLogManager.logger().log(LAUNCHER_LAUNCH_OMNI_ATTEMPTED_OVER_NOTIFICATION_SHADE) + return false + } + if (isKeyguardShowing()) { + Log.i(TAG, "Contextual Search invocation attempted: keyguard") + statsLogManager.logger().log(LAUNCHER_LAUNCH_OMNI_ATTEMPTED_OVER_KEYGUARD) + if (!contextualSearchStateManager.isInvocationAllowedOnKeyguard) { + Log.i(TAG, "Contextual Search invocation failed: keyguard not allowed") + return false + } else if (!contextualSearchStateManager.supportsShowWhenLocked()) { + Log.i(TAG, "Contextual Search invocation failed: AGA doesn't support keyguard") + return false + } + } + if (isInSplitscreen()) { + Log.i(TAG, "Contextual Search invocation attempted: splitscreen") + statsLogManager.logger().log(LAUNCHER_LAUNCH_OMNI_ATTEMPTED_SPLITSCREEN) + if (!contextualSearchStateManager.isInvocationAllowedInSplitscreen) { + Log.i(TAG, "Contextual Search invocation failed: splitscreen not allowed") + return false + } + } + if (!contextualSearchStateManager.isContextualSearchIntentAvailable) { + Log.i(TAG, "Contextual Search invocation failed: no matching CSS intent filter") + statsLogManager.logger().log(LAUNCHER_LAUNCH_OMNI_FAILED_NOT_AVAILABLE) + return false + } + + return true + } + + /** + * Invoke Contextual Search via ContextualSearchService and do haptic + * + * @param entryPoint Entry point identifier, passed to ContextualSearchService. + * @return true if invocation was successful, false otherwise + */ + fun invokeContextualSearchUncheckedWithHaptic(entryPoint: Int): Boolean { + return invokeContextualSearchUnchecked(entryPoint, withHaptic = true) + } + + private fun invokeContextualSearchUnchecked( + entryPoint: Int, + withHaptic: Boolean = false, + ): Boolean { + if (withHaptic && DeviceConfigWrapper.get().enableSearchHapticCommit) { + contextualSearchHapticManager.vibrateForSearch() + } + if (contextualSearchManager == null) { + return false + } + contextualSearchManager.startContextualSearch(entryPoint) + return true + } + + private fun isInSplitscreen(): Boolean { + return topTaskTracker.getRunningSplitTaskIds().isNotEmpty() + } + + private fun isNotificationShadeShowing(): Boolean { + return systemUiProxy.lastSystemUiStateFlags and SHADE_EXPANDED_SYSUI_FLAGS != 0L + } + + private fun isKeyguardShowing(): Boolean { + return systemUiProxy.lastSystemUiStateFlags and KEYGUARD_SHOWING_SYSUI_FLAGS != 0L + } + + companion object { + private const val TAG = "ContextualSearchInvoker" + const val SHADE_EXPANDED_SYSUI_FLAGS = + QuickStepContract.SYSUI_STATE_NOTIFICATION_PANEL_EXPANDED or + QuickStepContract.SYSUI_STATE_QUICK_SETTINGS_EXPANDED + const val KEYGUARD_SHOWING_SYSUI_FLAGS = + (QuickStepContract.SYSUI_STATE_BOUNCER_SHOWING or + QuickStepContract.SYSUI_STATE_STATUS_BAR_KEYGUARD_SHOWING or + QuickStepContract.SYSUI_STATE_STATUS_BAR_KEYGUARD_SHOWING_OCCLUDED) + + @JvmStatic + fun newInstance(context: Context): ContextualSearchInvoker { + return ResourceBasedOverride.Overrides.getObject( + ContextualSearchInvoker::class.java, + context, + R.string.contextual_search_invoker_class, + ) + } + } +} diff --git a/quickstep/src/com/android/quickstep/util/ContextualSearchStateManager.java b/quickstep/src/com/android/quickstep/util/ContextualSearchStateManager.java new file mode 100644 index 0000000000..142fc5843d --- /dev/null +++ b/quickstep/src/com/android/quickstep/util/ContextualSearchStateManager.java @@ -0,0 +1,281 @@ +/* + * Copyright (C) 2023 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.quickstep.util; + +import static android.app.contextualsearch.ContextualSearchManager.ACTION_LAUNCH_CONTEXTUAL_SEARCH; +import static android.app.contextualsearch.ContextualSearchManager.ENTRYPOINT_SYSTEM_ACTION; +import static android.app.contextualsearch.ContextualSearchManager.FEATURE_CONTEXTUAL_SEARCH; + +import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_OMNI_SUCCESSFUL_SYSTEM_ACTION; +import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR; +import static com.android.launcher3.util.MainThreadInitializedObject.forOverride; +import static com.android.quickstep.util.SystemActionConstants.SYSTEM_ACTION_ID_SEARCH_SCREEN; + +import android.annotation.Nullable; +import android.app.PendingIntent; +import android.app.RemoteAction; +import android.content.Context; +import android.content.IIntentReceiver; +import android.content.IIntentSender; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.graphics.drawable.Icon; +import android.net.Uri; +import android.os.Bundle; +import android.os.IBinder; +import android.os.RemoteException; +import android.provider.Settings; +import android.util.Log; +import android.view.accessibility.AccessibilityManager; + +import androidx.annotation.CallSuper; +import androidx.annotation.VisibleForTesting; + +import com.android.launcher3.R; +import com.android.launcher3.logging.StatsLogManager; +import com.android.launcher3.util.EventLogArray; +import com.android.launcher3.util.MainThreadInitializedObject; +import com.android.launcher3.util.ResourceBasedOverride; +import com.android.launcher3.util.SafeCloseable; +import com.android.launcher3.util.SettingsCache; +import com.android.launcher3.util.SimpleBroadcastReceiver; +import com.android.quickstep.DeviceConfigWrapper; +import com.android.quickstep.SystemUiProxy; +import com.android.quickstep.TopTaskTracker; + +import java.io.PrintWriter; +import java.util.Optional; + +/** Long-lived class to manage Contextual Search states like the user setting and availability. */ +public class ContextualSearchStateManager implements ResourceBasedOverride, SafeCloseable { + + public static final MainThreadInitializedObject INSTANCE = + forOverride(ContextualSearchStateManager.class, + R.string.contextual_search_state_manager_class); + + private static final String TAG = "ContextualSearchStMgr"; + private static final int MAX_DEBUG_EVENT_SIZE = 20; + private static final Uri SEARCH_ALL_ENTRYPOINTS_ENABLED_URI = + Settings.Secure.getUriFor(Settings.Secure.SEARCH_ALL_ENTRYPOINTS_ENABLED); + + private final Runnable mSysUiStateChangeListener = this::updateOverridesToSysUi; + private final SimpleBroadcastReceiver mContextualSearchPackageReceiver = + new SimpleBroadcastReceiver(UI_HELPER_EXECUTOR, (unused) -> requestUpdateProperties()); + private final SettingsCache.OnChangeListener mContextualSearchSettingChangedListener = + this::onContextualSearchSettingChanged; + protected final EventLogArray mEventLogArray = new EventLogArray(TAG, MAX_DEBUG_EVENT_SIZE); + + @Nullable private SettingsCache mSettingsCache; + // Cached value whether the ContextualSearch intent filter matched any enabled components. + private boolean mIsContextualSearchIntentAvailable; + private boolean mIsContextualSearchSettingEnabled; + + protected Context mContext; + protected String mContextualSearchPackage; + + public ContextualSearchStateManager() {} + + public ContextualSearchStateManager(Context context) { + mContext = context; + mContextualSearchPackage = mContext.getResources().getString( + com.android.internal.R.string.config_defaultContextualSearchPackageName); + + if (areAllContextualSearchFlagsDisabled() + || !context.getPackageManager().hasSystemFeature(FEATURE_CONTEXTUAL_SEARCH)) { + // If we had previously registered a SystemAction which is no longer valid, we need to + // unregister it here. + unregisterSearchScreenSystemAction(); + // Don't listen for stuff we aren't gonna use. + return; + } + + requestUpdateProperties(); + registerSearchScreenSystemAction(); + mContextualSearchPackageReceiver.registerPkgActions( + context, mContextualSearchPackage, Intent.ACTION_PACKAGE_ADDED, + Intent.ACTION_PACKAGE_CHANGED, Intent.ACTION_PACKAGE_REMOVED); + + mSettingsCache = SettingsCache.INSTANCE.get(context); + mSettingsCache.register(SEARCH_ALL_ENTRYPOINTS_ENABLED_URI, + mContextualSearchSettingChangedListener); + onContextualSearchSettingChanged( + mSettingsCache.getValue(SEARCH_ALL_ENTRYPOINTS_ENABLED_URI)); + SystemUiProxy.INSTANCE.get(mContext).addOnStateChangeListener(mSysUiStateChangeListener); + } + + /** Return {@code true} if the Settings toggle is enabled. */ + public final boolean isContextualSearchSettingEnabled() { + return mIsContextualSearchSettingEnabled; + } + + private void onContextualSearchSettingChanged(boolean isEnabled) { + mIsContextualSearchSettingEnabled = isEnabled; + } + + /** Whether search supports showing on the lockscreen. */ + protected boolean supportsShowWhenLocked() { + return false; + } + + /** Whether ContextualSearchService invocation path is available. */ + @VisibleForTesting + protected final boolean isContextualSearchIntentAvailable() { + return mIsContextualSearchIntentAvailable; + } + + /** Get the Launcher overridden long press nav handle duration to trigger Assistant. */ + public Optional getLPNHDurationMillis() { + return Optional.empty(); + } + + /** + * Get the Launcher overridden long press nav handle touch slop multiplier to trigger Assistant. + */ + public Optional getLPNHCustomSlopMultiplier() { + return Optional.empty(); + } + + /** Get the Launcher overridden long press home duration to trigger Assistant. */ + public Optional getLPHDurationMillis() { + return Optional.empty(); + } + + /** Get the Launcher overridden long press home touch slop multiplier to trigger Assistant. */ + public Optional getLPHCustomSlopMultiplier() { + return Optional.empty(); + } + + /** Get the long press duration data source. */ + public int getDurationDataSource() { + return 0; + } + + /** Get the long press touch slop multiplier data source. */ + public int getSlopDataSource() { + return 0; + } + + /** Get the haptic bit overridden by AGSA. */ + public Optional getShouldPlayHapticOverride() { + return Optional.empty(); + } + + protected boolean isInvocationAllowedOnKeyguard() { + return false; + } + + protected boolean isInvocationAllowedInSplitscreen() { + return true; + } + + @CallSuper + protected boolean areAllContextualSearchFlagsDisabled() { + return !DeviceConfigWrapper.get().getEnableLongPressNavHandle(); + } + + @CallSuper + protected void requestUpdateProperties() { + UI_HELPER_EXECUTOR.execute(() -> { + // Check that Contextual Search intent filters are enabled. + Intent csIntent = new Intent(ACTION_LAUNCH_CONTEXTUAL_SEARCH).setPackage( + mContextualSearchPackage); + mIsContextualSearchIntentAvailable = + !mContext.getPackageManager().queryIntentActivities(csIntent, + PackageManager.MATCH_DIRECT_BOOT_AWARE + | PackageManager.MATCH_DIRECT_BOOT_UNAWARE).isEmpty(); + + addEventLog("Updated isContextualSearchIntentAvailable", + mIsContextualSearchIntentAvailable); + }); + } + + protected final void updateOverridesToSysUi() { + // LPH commit haptic is always enabled + SystemUiProxy.INSTANCE.get(mContext).setOverrideHomeButtonLongPress( + getLPHDurationMillis().orElse(0L), getLPHCustomSlopMultiplier().orElse(0f), true); + Log.i(TAG, "Sent LPH override to sysui: " + getLPHDurationMillis().orElse(0L) + ";" + + getLPHCustomSlopMultiplier().orElse(0f)); + } + + private void registerSearchScreenSystemAction() { + PendingIntent searchScreenPendingIntent = new PendingIntent(new IIntentSender.Stub() { + @Override + public void send(int i, Intent intent, String s, IBinder iBinder, + IIntentReceiver iIntentReceiver, String s1, Bundle bundle) + throws RemoteException { + // Delayed slightly to minimize chance of capturing the System Actions dialog. + UI_HELPER_EXECUTOR.getHandler().postDelayed( + () -> { + boolean contextualSearchInvoked = + ContextualSearchInvoker.newInstance(mContext).show( + ENTRYPOINT_SYSTEM_ACTION); + if (contextualSearchInvoked) { + String runningPackage = + TopTaskTracker.INSTANCE.get(mContext).getCachedTopTask( + /* filterOnlyVisibleRecents */ + true).getPackageName(); + StatsLogManager.newInstance(mContext).logger() + .withPackageName(runningPackage) + .log(LAUNCHER_LAUNCH_OMNI_SUCCESSFUL_SYSTEM_ACTION); + } + }, 200); + } + }); + + mContext.getSystemService(AccessibilityManager.class).registerSystemAction(new RemoteAction( + Icon.createWithResource(mContext, R.drawable.ic_allapps_search), + mContext.getString(R.string.search_gesture_feature_title), + mContext.getString(R.string.search_gesture_feature_title), + searchScreenPendingIntent), + SYSTEM_ACTION_ID_SEARCH_SCREEN); + } + + private void unregisterSearchScreenSystemAction() { + mContext.getSystemService(AccessibilityManager.class).unregisterSystemAction( + SYSTEM_ACTION_ID_SEARCH_SCREEN); + } + + /** Dump states. */ + public final void dump(String prefix, PrintWriter writer) { + synchronized (mEventLogArray) { + mEventLogArray.dump(prefix, writer); + } + } + + @Override + public void close() { + mContextualSearchPackageReceiver.unregisterReceiverSafely(mContext); + unregisterSearchScreenSystemAction(); + + if (mSettingsCache != null) { + mSettingsCache.unregister(SEARCH_ALL_ENTRYPOINTS_ENABLED_URI, + mContextualSearchSettingChangedListener); + } + SystemUiProxy.INSTANCE.get(mContext).removeOnStateChangeListener(mSysUiStateChangeListener); + } + + protected final void addEventLog(String event) { + synchronized (mEventLogArray) { + mEventLogArray.addLog(event); + } + } + + protected final void addEventLog(String event, boolean extras) { + synchronized (mEventLogArray) { + mEventLogArray.addLog(event, extras); + } + } +} diff --git a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/TaskbarNavButtonControllerTest.java b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/TaskbarNavButtonControllerTest.java index 02d62186b1..253d9215fe 100644 --- a/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/TaskbarNavButtonControllerTest.java +++ b/quickstep/tests/multivalentTests/src/com/android/launcher3/taskbar/TaskbarNavButtonControllerTest.java @@ -39,7 +39,7 @@ import com.android.launcher3.logging.StatsLogManager; import com.android.launcher3.taskbar.TaskbarNavButtonController.TaskbarNavButtonCallbacks; import com.android.quickstep.SystemUiProxy; import com.android.quickstep.TouchInteractionService; -import com.android.quickstep.util.AssistUtils; +import com.android.quickstep.util.ContextualSearchInvoker; import com.android.systemui.contextualeducation.GestureType; import org.junit.Before; @@ -64,7 +64,7 @@ public class TaskbarNavButtonControllerTest { @Mock Handler mockHandler; @Mock - AssistUtils mockAssistUtils; + ContextualSearchInvoker mockContextualSearchInvoker; @Mock StatsLogManager mockStatsLogManager; @Mock @@ -109,7 +109,7 @@ public class TaskbarNavButtonControllerTest { mockSystemUiProxy, mockContextualEduStatsManager, mockHandler, - mockAssistUtils); + mockContextualSearchInvoker); } @Test @@ -166,40 +166,40 @@ public class TaskbarNavButtonControllerTest { @Test public void testLongPressHome_enabled_withoutOverride() { mNavButtonController.setAssistantLongPressEnabled(true /*assistantLongPressEnabled*/); - when(mockAssistUtils.tryStartAssistOverride(anyInt())).thenReturn(false); + when(mockContextualSearchInvoker.tryStartAssistOverride(anyInt())).thenReturn(false); mNavButtonController.onButtonLongClick(BUTTON_HOME, mockView); - verify(mockAssistUtils, times(1)).tryStartAssistOverride(anyInt()); + verify(mockContextualSearchInvoker, times(1)).tryStartAssistOverride(anyInt()); verify(mockSystemUiProxy, times(1)).startAssistant(any()); } @Test public void testLongPressHome_enabled_withOverride() { mNavButtonController.setAssistantLongPressEnabled(true /*assistantLongPressEnabled*/); - when(mockAssistUtils.tryStartAssistOverride(anyInt())).thenReturn(true); + when(mockContextualSearchInvoker.tryStartAssistOverride(anyInt())).thenReturn(true); mNavButtonController.onButtonLongClick(BUTTON_HOME, mockView); - verify(mockAssistUtils, times(1)).tryStartAssistOverride(anyInt()); + verify(mockContextualSearchInvoker, times(1)).tryStartAssistOverride(anyInt()); verify(mockSystemUiProxy, never()).startAssistant(any()); } @Test public void testLongPressHome_disabled_withoutOverride() { mNavButtonController.setAssistantLongPressEnabled(false /*assistantLongPressEnabled*/); - when(mockAssistUtils.tryStartAssistOverride(anyInt())).thenReturn(false); + when(mockContextualSearchInvoker.tryStartAssistOverride(anyInt())).thenReturn(false); mNavButtonController.onButtonLongClick(BUTTON_HOME, mockView); - verify(mockAssistUtils, never()).tryStartAssistOverride(anyInt()); + verify(mockContextualSearchInvoker, never()).tryStartAssistOverride(anyInt()); verify(mockSystemUiProxy, never()).startAssistant(any()); } @Test public void testLongPressHome_disabled_withOverride() { mNavButtonController.setAssistantLongPressEnabled(false /*assistantLongPressEnabled*/); - when(mockAssistUtils.tryStartAssistOverride(anyInt())).thenReturn(true); + when(mockContextualSearchInvoker.tryStartAssistOverride(anyInt())).thenReturn(true); mNavButtonController.onButtonLongClick(BUTTON_HOME, mockView); - verify(mockAssistUtils, never()).tryStartAssistOverride(anyInt()); + verify(mockContextualSearchInvoker, never()).tryStartAssistOverride(anyInt()); verify(mockSystemUiProxy, never()).startAssistant(any()); } diff --git a/quickstep/tests/multivalentTests/src/com/android/quickstep/inputconsumers/NavHandleLongPressHandlerTest.java b/quickstep/tests/multivalentTests/src/com/android/quickstep/inputconsumers/NavHandleLongPressHandlerTest.java new file mode 100644 index 0000000000..9018775316 --- /dev/null +++ b/quickstep/tests/multivalentTests/src/com/android/quickstep/inputconsumers/NavHandleLongPressHandlerTest.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2024 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.quickstep.inputconsumers; + +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +import android.content.Context; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; +import androidx.test.platform.app.InstrumentationRegistry; + +import com.android.quickstep.DeviceConfigWrapper; +import com.android.quickstep.NavHandle; +import com.android.quickstep.util.TestExtensions; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +@SmallTest +@RunWith(AndroidJUnit4.class) +public class NavHandleLongPressHandlerTest { + + private NavHandleLongPressHandler mLongPressHandler; + @Mock private NavHandle mNavHandle; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); + mLongPressHandler = new NavHandleLongPressHandler(context); + } + + @Test + public void testStartNavBarAnimation_flagDisabled() { + try (AutoCloseable flag = overrideAnimateLPNHFlag(false)) { + mLongPressHandler.startNavBarAnimation(mNavHandle); + verify(mNavHandle, never()) + .animateNavBarLongPress(anyBoolean(), anyBoolean(), anyLong()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testStartNavBarAnimation_flagEnabled() { + try (AutoCloseable flag = overrideAnimateLPNHFlag(true)) { + mLongPressHandler.startNavBarAnimation(mNavHandle); + verify(mNavHandle).animateNavBarLongPress(anyBoolean(), anyBoolean(), anyLong()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private AutoCloseable overrideAnimateLPNHFlag(boolean value) { + return TestExtensions.overrideNavConfigFlag( + "ANIMATE_LPNH", value, () -> DeviceConfigWrapper.get().getAnimateLpnh()); + } +} diff --git a/quickstep/tests/multivalentTests/src/com/android/quickstep/util/ContextualSearchInvokerTest.java b/quickstep/tests/multivalentTests/src/com/android/quickstep/util/ContextualSearchInvokerTest.java new file mode 100644 index 0000000000..543ffe61eb --- /dev/null +++ b/quickstep/tests/multivalentTests/src/com/android/quickstep/util/ContextualSearchInvokerTest.java @@ -0,0 +1,253 @@ +/* + * Copyright (C) 2024 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.quickstep.util; + +import static android.app.contextualsearch.ContextualSearchManager.FEATURE_CONTEXTUAL_SEARCH; + +import static androidx.test.core.app.ApplicationProvider.getApplicationContext; + +import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_ASSISTANT_FAILED_SERVICE_ERROR; +import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_OMNI_ATTEMPTED_OVER_KEYGUARD; +import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_OMNI_ATTEMPTED_OVER_NOTIFICATION_SHADE; +import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_OMNI_ATTEMPTED_SPLITSCREEN; +import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_OMNI_FAILED_NOT_AVAILABLE; +import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_LAUNCH_OMNI_FAILED_SETTING_DISABLED; +import static com.android.quickstep.util.ContextualSearchInvoker.KEYGUARD_SHOWING_SYSUI_FLAGS; +import static com.android.quickstep.util.ContextualSearchInvoker.SHADE_EXPANDED_SYSUI_FLAGS; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.when; + +import android.app.contextualsearch.ContextualSearchManager; +import android.content.Context; +import android.content.pm.PackageManager; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; + +import com.android.launcher3.logging.StatsLogManager; +import com.android.quickstep.DeviceConfigWrapper; +import com.android.quickstep.SystemUiProxy; +import com.android.quickstep.TopTaskTracker; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +/** + * Robolectric unit tests for {@link ContextualSearchInvoker} + */ +@SmallTest +@RunWith(AndroidJUnit4.class) +public class ContextualSearchInvokerTest { + + private static final int CONTEXTUAL_SEARCH_ENTRY_POINT = 123; + + private @Mock PackageManager mMockPackageManager; + private @Mock ContextualSearchStateManager mMockStateManager; + private @Mock TopTaskTracker mMockTopTaskTracker; + private @Mock SystemUiProxy mMockSystemUiProxy; + private @Mock StatsLogManager mMockStatsLogManager; + private @Mock StatsLogManager.StatsLogger mMockStatsLogger; + private @Mock ContextualSearchHapticManager mMockContextualSearchHapticManager; + private @Mock ContextualSearchManager mMockContextualSearchManager; + private ContextualSearchInvoker mContextualSearchInvoker; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(mMockPackageManager.hasSystemFeature(FEATURE_CONTEXTUAL_SEARCH)).thenReturn(true); + Context context = spy(getApplicationContext()); + doReturn(mMockPackageManager).when(context).getPackageManager(); + when(mMockSystemUiProxy.getLastSystemUiStateFlags()).thenReturn(0L); + when(mMockTopTaskTracker.getRunningSplitTaskIds()).thenReturn(new int[]{}); + when(mMockStateManager.isContextualSearchIntentAvailable()).thenReturn(true); + when(mMockStateManager.isContextualSearchSettingEnabled()).thenReturn(true); + when(mMockStatsLogManager.logger()).thenReturn(mMockStatsLogger); + + mContextualSearchInvoker = new ContextualSearchInvoker(context, mMockStateManager, + mMockTopTaskTracker, mMockSystemUiProxy, mMockStatsLogManager, + mMockContextualSearchHapticManager, mMockContextualSearchManager); + } + + @Test + public void runContextualSearchInvocationChecksAndLogFailures_contextualSearchFeatureIsNotAvailable() { + when(mMockPackageManager.hasSystemFeature(FEATURE_CONTEXTUAL_SEARCH)).thenReturn(false); + + assertFalse("Expected invocation to fail when feature is unavailable", + mContextualSearchInvoker.runContextualSearchInvocationChecksAndLogFailures()); + + verify(mMockStatsLogger).log(LAUNCHER_LAUNCH_ASSISTANT_FAILED_SERVICE_ERROR); + } + + @Test + public void runContextualSearchInvocationChecksAndLogFailures_contextualSearchIntentIsAvailable() { + assertTrue("Expected invocation checks to succeed", + mContextualSearchInvoker.runContextualSearchInvocationChecksAndLogFailures()); + + verifyNoMoreInteractions(mMockStatsLogManager); + } + + @Test + public void runContextualSearchInvocationChecksAndLogFailures_contextualSearchIntentIsNotAvailable() { + when(mMockStateManager.isContextualSearchIntentAvailable()).thenReturn(false); + + assertFalse("Expected invocation to fail when feature is unavailable", + mContextualSearchInvoker.runContextualSearchInvocationChecksAndLogFailures()); + + verify(mMockStatsLogger).log(LAUNCHER_LAUNCH_OMNI_FAILED_NOT_AVAILABLE); + } + + @Test + public void runContextualSearchInvocationChecksAndLogFailures_settingDisabled() { + when(mMockStateManager.isContextualSearchSettingEnabled()).thenReturn(false); + + assertFalse("Expected invocation checks to fail when setting is disabled", + mContextualSearchInvoker.runContextualSearchInvocationChecksAndLogFailures()); + + verify(mMockStatsLogger).log(LAUNCHER_LAUNCH_OMNI_FAILED_SETTING_DISABLED); + } + + @Test + public void runContextualSearchInvocationChecksAndLogFailures_notificationShadeIsShowing() { + when(mMockSystemUiProxy.getLastSystemUiStateFlags()).thenReturn(SHADE_EXPANDED_SYSUI_FLAGS); + + assertFalse("Expected invocation checks to fail when notification shade is showing", + mContextualSearchInvoker.runContextualSearchInvocationChecksAndLogFailures()); + + verify(mMockStatsLogger).log(LAUNCHER_LAUNCH_OMNI_ATTEMPTED_OVER_NOTIFICATION_SHADE); + } + + @Test + public void runContextualSearchInvocationChecksAndLogFailures_keyguardIsShowing() { + when(mMockSystemUiProxy.getLastSystemUiStateFlags()).thenReturn( + KEYGUARD_SHOWING_SYSUI_FLAGS); + + assertFalse("Expected invocation checks to fail when keyguard is showing", + mContextualSearchInvoker.runContextualSearchInvocationChecksAndLogFailures()); + + verify(mMockStatsLogger).log(LAUNCHER_LAUNCH_OMNI_ATTEMPTED_OVER_KEYGUARD); + } + + @Test + public void runContextualSearchInvocationChecksAndLogFailures_isInSplitScreen_disallowed() { + when(mMockStateManager.isInvocationAllowedInSplitscreen()).thenReturn(false); + when(mMockTopTaskTracker.getRunningSplitTaskIds()).thenReturn(new int[]{1, 2, 3}); + + assertFalse("Expected invocation checks to fail over split screen", + mContextualSearchInvoker.runContextualSearchInvocationChecksAndLogFailures()); + + // Attempt is logged regardless. + verify(mMockStatsLogger).log(LAUNCHER_LAUNCH_OMNI_ATTEMPTED_SPLITSCREEN); + } + + @Test + public void runContextualSearchInvocationChecksAndLogFailures_isInSplitScreen_allowed() { + when(mMockStateManager.isInvocationAllowedInSplitscreen()).thenReturn(true); + when(mMockTopTaskTracker.getRunningSplitTaskIds()).thenReturn(new int[]{1, 2, 3}); + + assertTrue("Expected invocation checks to succeed over split screen", + mContextualSearchInvoker.runContextualSearchInvocationChecksAndLogFailures()); + + // Attempt is logged regardless. + verify(mMockStatsLogger).log(LAUNCHER_LAUNCH_OMNI_ATTEMPTED_SPLITSCREEN); + } + + @Test + public void invokeContextualSearchUncheckedWithHaptic_cssIsAvailable_commitHapticEnabled() { + try (AutoCloseable flag = overrideSearchHapticCommitFlag(true)) { + assertTrue("Expected invocation unchecked to succeed", + mContextualSearchInvoker.invokeContextualSearchUncheckedWithHaptic( + CONTEXTUAL_SEARCH_ENTRY_POINT)); + verify(mMockContextualSearchHapticManager).vibrateForSearch(); + verify(mMockContextualSearchManager).startContextualSearch( + CONTEXTUAL_SEARCH_ENTRY_POINT); + verifyNoMoreInteractions(mMockStatsLogManager); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void invokeContextualSearchUncheckedWithHaptic_cssIsAvailable_commitHapticDisabled() { + try (AutoCloseable flag = overrideSearchHapticCommitFlag(false)) { + assertTrue("Expected invocation unchecked to succeed", + mContextualSearchInvoker.invokeContextualSearchUncheckedWithHaptic( + CONTEXTUAL_SEARCH_ENTRY_POINT)); + verify(mMockContextualSearchHapticManager, never()).vibrateForSearch(); + verify(mMockContextualSearchManager).startContextualSearch( + CONTEXTUAL_SEARCH_ENTRY_POINT); + verifyNoMoreInteractions(mMockStatsLogManager); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void invokeContextualSearchUncheckedWithHaptic_cssIsNotAvailable_commitHapticEnabled() { + when(mMockStateManager.isContextualSearchIntentAvailable()).thenReturn(false); + + try (AutoCloseable flag = overrideSearchHapticCommitFlag(true)) { + // Still expect true since this method doesn't run the checks. + assertTrue("Expected invocation unchecked to succeed", + mContextualSearchInvoker.invokeContextualSearchUncheckedWithHaptic( + CONTEXTUAL_SEARCH_ENTRY_POINT)); + // Still vibrate based on the flag. + verify(mMockContextualSearchHapticManager).vibrateForSearch(); + verify(mMockContextualSearchManager).startContextualSearch( + CONTEXTUAL_SEARCH_ENTRY_POINT); + verifyNoMoreInteractions(mMockStatsLogManager); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + @Test + public void invokeContextualSearchUncheckedWithHaptic_cssIsNotAvailable_commitHapticDisabled() { + when(mMockStateManager.isContextualSearchIntentAvailable()).thenReturn(false); + + try (AutoCloseable flag = overrideSearchHapticCommitFlag(false)) { + // Still expect true since this method doesn't run the checks. + assertTrue("Expected ContextualSearch invocation unchecked to succeed", + mContextualSearchInvoker.invokeContextualSearchUncheckedWithHaptic( + CONTEXTUAL_SEARCH_ENTRY_POINT)); + // Still don't vibrate based on the flag. + verify(mMockContextualSearchHapticManager, never()).vibrateForSearch(); + verify(mMockContextualSearchManager).startContextualSearch( + CONTEXTUAL_SEARCH_ENTRY_POINT); + verifyNoMoreInteractions(mMockStatsLogManager); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private AutoCloseable overrideSearchHapticCommitFlag(boolean value) { + return TestExtensions.overrideNavConfigFlag( + "ENABLE_SEARCH_HAPTIC_COMMIT", + value, + () -> DeviceConfigWrapper.get().getEnableSearchHapticCommit()); + } +} diff --git a/src/com/android/launcher3/logging/StatsLogManager.java b/src/com/android/launcher3/logging/StatsLogManager.java index fbd24d89f0..06d7a93993 100644 --- a/src/com/android/launcher3/logging/StatsLogManager.java +++ b/src/com/android/launcher3/logging/StatsLogManager.java @@ -798,6 +798,44 @@ public class StatsLogManager implements ResourceBasedOverride { @UiEvent(doc = "User long pressed on the taskbar IME switcher button") LAUNCHER_TASKBAR_IME_SWITCHER_BUTTON_LONGPRESS(1798), + @UiEvent(doc = "Failed to launch assistant due to Google assistant not available") + LAUNCHER_LAUNCH_ASSISTANT_FAILED_NOT_AVAILABLE(1465), + + @UiEvent(doc = "Failed to launch assistant due to service error") + LAUNCHER_LAUNCH_ASSISTANT_FAILED_SERVICE_ERROR(1466), + + @UiEvent(doc = "User launched assistant by long-pressing nav handle") + LAUNCHER_LAUNCH_ASSISTANT_SUCCESSFUL_NAV_HANDLE(1467), + + @UiEvent(doc = "Failed to launch due to Contextual Search not available") + LAUNCHER_LAUNCH_OMNI_FAILED_NOT_AVAILABLE(1471), + + @UiEvent(doc = "Failed to launch due to Contextual Search setting disabled") + LAUNCHER_LAUNCH_OMNI_FAILED_SETTING_DISABLED(1632), + + @UiEvent(doc = "User launched Contextual Search by long-pressing home in 3-button mode") + LAUNCHER_LAUNCH_OMNI_SUCCESSFUL_HOME(1481), + + @UiEvent(doc = "User launched Contextual Search by using accessibility System Action") + LAUNCHER_LAUNCH_OMNI_SUCCESSFUL_SYSTEM_ACTION(1492), + + @UiEvent(doc = "User launched Contextual Search by long pressing the meta key") + LAUNCHER_LAUNCH_OMNI_SUCCESSFUL_META(1606), + + @UiEvent(doc = "Contextual Search invocation was attempted over the notification shade") + LAUNCHER_LAUNCH_OMNI_ATTEMPTED_OVER_NOTIFICATION_SHADE(1485), + + @UiEvent(doc = "The Contextual Search all entrypoints toggle value in Settings") + LAUNCHER_SETTINGS_OMNI_ALL_ENTRYPOINTS_TOGGLE_VALUE(1633), + + @UiEvent(doc = "Contextual Search invocation was attempted over the keyguard") + LAUNCHER_LAUNCH_OMNI_ATTEMPTED_OVER_KEYGUARD(1501), + + @UiEvent(doc = "Contextual Search invocation was attempted while splitscreen is active") + LAUNCHER_LAUNCH_OMNI_ATTEMPTED_SPLITSCREEN(1505), + + @UiEvent(doc = "User long press nav handle and a long press runnable was created.") + LAUNCHER_OMNI_GET_LONG_PRESS_RUNNABLE(1545), // ADD MORE ; @@ -828,6 +866,10 @@ public class StatsLogManager implements ResourceBasedOverride { @UiEvent(doc = "The duration of asynchronous loading workspace") LAUNCHER_LATENCY_STARTUP_WORKSPACE_LOADER_ASYNC(1367), + + @UiEvent(doc = "Time passed between Contextual Search runnable creation and execution. This" + + " ensures that Recent animations have finished before Contextual Search starts.") + LAUNCHER_LATENCY_OMNI_RUNNABLE(1546), ; private final int mId;