From c978736155c945e5b173dadee5ad4ad3e2b6fb40 Mon Sep 17 00:00:00 2001 From: DingYong Date: Fri, 11 Nov 2022 21:30:54 +0800 Subject: [PATCH] Call a search activity when using KEYCODE_SEARCH This change adds the ability to change the behavior of the search key to open a search activity instead of attempting to start the search activity of the currently opened app. The new behavior remains the same by default, but now it can be changed to either do nothing or open a configured activity; in this mode, if no activity has been configured, the `ACTION_WEB_SEARCH` Intent will be launched. Test: manual Bug: 240865724 Signed-off-by: LiZhihong DingYong Change-Id: I3037d255b693ade3f45e170746e0c2b2de5db6fb --- core/res/res/values/config.xml | 19 ++++++-- core/res/res/values/symbols.xml | 2 + .../server/policy/PhoneWindowManager.java | 44 ++++++++++++++++++- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml index 47e71fbd75877..9582c9e8eadc6 100644 --- a/core/res/res/values/config.xml +++ b/core/res/res/values/config.xml @@ -1096,14 +1096,25 @@ 0 0 + + 0 + + + + 3500 diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 49d4199596a75..fa1150cb36745 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -451,6 +451,8 @@ + + diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java index 28169e56635f8..50197c9ea1ec5 100644 --- a/services/core/java/com/android/server/policy/PhoneWindowManager.java +++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java @@ -322,6 +322,10 @@ public class PhoneWindowManager implements WindowManagerPolicy { static final int TRIPLE_PRESS_PRIMARY_NOTHING = 0; static final int TRIPLE_PRESS_PRIMARY_TOGGLE_ACCESSIBILITY = 1; + // Must match: config_searchKeyBehavior in config.xml + static final int SEARCH_BEHAVIOR_DEFAULT_SEARCH = 0; + static final int SEARCH_BEHAVIOR_TARGET_ACTIVITY = 1; + static public final String SYSTEM_DIALOG_REASON_KEY = "reason"; static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions"; static public final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"; @@ -525,6 +529,8 @@ public class PhoneWindowManager implements WindowManagerPolicy { boolean mWakeOnAssistKeyPress; boolean mWakeOnBackKeyPress; long mWakeUpToLastStateTimeout; + int mSearchKeyBehavior; + ComponentName mSearchKeyTargetActivity; private boolean mHandleVolumeKeysInWM; @@ -2039,6 +2045,12 @@ public class PhoneWindowManager implements WindowManagerPolicy { mWakeUpToLastStateTimeout = mContext.getResources().getInteger( com.android.internal.R.integer.config_wakeUpToLastStateTimeoutMillis); + mSearchKeyBehavior = mContext.getResources().getInteger( + com.android.internal.R.integer.config_searchKeyBehavior); + + mSearchKeyTargetActivity = ComponentName.unflattenFromString( + mContext.getResources().getString( + com.android.internal.R.string.config_searchKeyTargetActivity)); readConfigurationDependentBehaviors(); mDisplayFoldController = DisplayFoldController.create(context, DEFAULT_DISPLAY); @@ -2942,7 +2954,19 @@ public class PhoneWindowManager implements WindowManagerPolicy { toggleNotificationPanel(); } return key_consumed; - + case KeyEvent.KEYCODE_SEARCH: + if (down && repeatCount == 0 && !keyguardOn()) { + switch(mSearchKeyBehavior) { + case SEARCH_BEHAVIOR_TARGET_ACTIVITY: { + launchTargetSearchActivity(); + return key_consumed; + } + case SEARCH_BEHAVIOR_DEFAULT_SEARCH: + default: + break; + } + } + break; case KeyEvent.KEYCODE_SPACE: // Handle keyboard layout switching. (META + SPACE) if ((metaState & KeyEvent.META_META_MASK) == 0) { @@ -6003,4 +6027,22 @@ public class PhoneWindowManager implements WindowManagerPolicy { } } + private void launchTargetSearchActivity() { + Intent intent; + if (mSearchKeyTargetActivity != null) { + intent = new Intent(); + intent.setComponent(mSearchKeyTargetActivity); + } else { + intent = new Intent(Intent.ACTION_WEB_SEARCH); + } + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK + | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); + try { + startActivityAsUser(intent, UserHandle.CURRENT_OR_SELF); + } catch (ActivityNotFoundException ignore) { + Slog.e(TAG, "Could not resolve activity with : " + + intent.getComponent().flattenToString() + + " name."); + } + } }