Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 1 | package com.android.systemui.assist; |
| 2 | |
Yohei Yukawa | 528e468 | 2015-09-10 22:21:04 -0700 | [diff] [blame] | 3 | import android.annotation.NonNull; |
| 4 | import android.annotation.Nullable; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 5 | import android.app.ActivityManager; |
| 6 | import android.app.ActivityOptions; |
| 7 | import android.app.SearchManager; |
| 8 | import android.content.ActivityNotFoundException; |
| 9 | import android.content.ComponentName; |
| 10 | import android.content.Context; |
| 11 | import android.content.Intent; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 12 | import android.content.pm.ActivityInfo; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 13 | import android.content.pm.PackageManager; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 14 | import android.content.res.Configuration; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 15 | import android.content.res.Resources; |
| 16 | import android.graphics.PixelFormat; |
Steven Wu | 11da574 | 2019-04-25 16:12:06 -0400 | [diff] [blame] | 17 | import android.metrics.LogMaker; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 18 | import android.os.AsyncTask; |
Wale Ogunwale | 6ce0fb8 | 2016-12-13 14:24:00 -0800 | [diff] [blame] | 19 | import android.os.Binder; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 20 | import android.os.Bundle; |
Xiyuan Xia | e6de8b9 | 2015-06-06 22:18:05 -0700 | [diff] [blame] | 21 | import android.os.Handler; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 22 | import android.os.RemoteException; |
Govinda Wasserman | 27aa92b | 2019-04-16 14:45:54 -0400 | [diff] [blame] | 23 | import android.os.SystemClock; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 24 | import android.os.UserHandle; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 25 | import android.provider.Settings; |
Dianne Hackborn | 17f6935 | 2015-07-17 18:04:14 -0700 | [diff] [blame] | 26 | import android.service.voice.VoiceInteractionSession; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | import android.view.Gravity; |
| 29 | import android.view.LayoutInflater; |
| 30 | import android.view.View; |
| 31 | import android.view.ViewGroup; |
| 32 | import android.view.WindowManager; |
| 33 | import android.widget.ImageView; |
| 34 | |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 35 | import com.android.internal.app.AssistUtils; |
Annie Chin | ecb9f3e | 2016-06-27 16:01:52 -0700 | [diff] [blame] | 36 | import com.android.internal.app.IVoiceInteractionSessionListener; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 37 | import com.android.internal.app.IVoiceInteractionSessionShowCallback; |
Steven Wu | 11da574 | 2019-04-25 16:12:06 -0400 | [diff] [blame] | 38 | import com.android.internal.logging.MetricsLogger; |
| 39 | import com.android.internal.logging.nano.MetricsProto.MetricsEvent; |
Kaori Katou | 70554e0 | 2016-08-04 17:11:07 +0900 | [diff] [blame] | 40 | import com.android.keyguard.KeyguardUpdateMonitor; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 41 | import com.android.settingslib.applications.InterestingConfigChanges; |
| 42 | import com.android.systemui.ConfigurationChangedReceiver; |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 43 | import com.android.systemui.Dependency; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 44 | import com.android.systemui.R; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 45 | import com.android.systemui.SysUiServiceProvider; |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 46 | import com.android.systemui.assist.ui.DefaultUiController; |
| 47 | import com.android.systemui.recents.OverviewProxyService; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 48 | import com.android.systemui.statusbar.CommandQueue; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 49 | import com.android.systemui.statusbar.policy.DeviceProvisionedController; |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 50 | |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 51 | /** |
Selim Cinek | e70d653 | 2015-04-24 16:46:13 -0700 | [diff] [blame] | 52 | * Class to manage everything related to assist in SystemUI. |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 53 | */ |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 54 | public class AssistManager implements ConfigurationChangedReceiver { |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 55 | |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 56 | /** |
| 57 | * Controls the UI for showing Assistant invocation progress. |
| 58 | */ |
| 59 | public interface UiController { |
| 60 | /** |
| 61 | * Updates the invocation progress. |
| 62 | * |
| 63 | * @param type one of INVOCATION_TYPE_GESTURE, INVOCATION_TYPE_ACTIVE_EDGE, |
| 64 | * INVOCATION_TYPE_VOICE, INVOCATION_TYPE_QUICK_SEARCH_BAR, |
| 65 | * INVOCATION_HOME_BUTTON_LONG_PRESS |
| 66 | * @param progress a float between 0 and 1 inclusive. 0 represents the beginning of the |
| 67 | * gesture; 1 represents the end. |
| 68 | */ |
| 69 | void onInvocationProgress(int type, float progress); |
| 70 | |
| 71 | /** |
| 72 | * Called when an invocation gesture completes. |
| 73 | * |
| 74 | * @param velocity the speed of the invocation gesture, in pixels per millisecond. For |
| 75 | * drags, this is 0. |
| 76 | */ |
| 77 | void onGestureCompletion(float velocity); |
| 78 | |
| 79 | /** |
| 80 | * Called with the Bundle from VoiceInteractionSessionListener.onSetUiHints. |
| 81 | */ |
| 82 | void processBundle(Bundle hints); |
| 83 | |
| 84 | /** |
James O'Leary | 7b92b12 | 2019-07-09 12:43:45 -0400 | [diff] [blame] | 85 | * Hides any SysUI for the assistant, but _does not_ close the assistant itself. |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 86 | */ |
| 87 | void hide(); |
| 88 | } |
| 89 | |
Selim Cinek | e70d653 | 2015-04-24 16:46:13 -0700 | [diff] [blame] | 90 | private static final String TAG = "AssistManager"; |
Matt Casey | 4cff16f | 2019-01-14 14:56:06 -0500 | [diff] [blame] | 91 | |
| 92 | // Note that VERBOSE logging may leak PII (e.g. transcription contents). |
| 93 | private static final boolean VERBOSE = false; |
| 94 | |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 95 | private static final String ASSIST_ICON_METADATA_NAME = |
| 96 | "com.android.systemui.action_assist_icon"; |
Govinda Wasserman | 27aa92b | 2019-04-16 14:45:54 -0400 | [diff] [blame] | 97 | private static final String INVOCATION_TIME_MS_KEY = "invocation_time_ms"; |
Govinda Wasserman | a2c88760 | 2019-05-08 08:02:47 -0400 | [diff] [blame] | 98 | private static final String INVOCATION_PHONE_STATE_KEY = "invocation_phone_state"; |
Govinda Wasserman | 27aa92b | 2019-04-16 14:45:54 -0400 | [diff] [blame] | 99 | public static final String INVOCATION_TYPE_KEY = "invocation_type"; |
| 100 | |
| 101 | public static final int INVOCATION_TYPE_GESTURE = 1; |
| 102 | public static final int INVOCATION_TYPE_ACTIVE_EDGE = 2; |
| 103 | public static final int INVOCATION_TYPE_VOICE = 3; |
| 104 | public static final int INVOCATION_TYPE_QUICK_SEARCH_BAR = 4; |
| 105 | public static final int INVOCATION_HOME_BUTTON_LONG_PRESS = 5; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 106 | |
Govinda Wasserman | b7cd162 | 2019-05-31 11:59:19 -0400 | [diff] [blame] | 107 | public static final int DISMISS_REASON_INVOCATION_CANCELLED = 1; |
| 108 | public static final int DISMISS_REASON_TAP = 2; |
| 109 | public static final int DISMISS_REASON_BACK = 3; |
| 110 | public static final int DISMISS_REASON_TIMEOUT = 4; |
| 111 | |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 112 | private static final long TIMEOUT_SERVICE = 2500; |
| 113 | private static final long TIMEOUT_ACTIVITY = 1000; |
| 114 | |
Annie Chin | 4c249ff | 2016-07-08 14:52:00 -0700 | [diff] [blame] | 115 | protected final Context mContext; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 116 | private final WindowManager mWindowManager; |
Adrian Roos | 4f43dc0 | 2015-06-17 16:43:38 -0700 | [diff] [blame] | 117 | private final AssistDisclosure mAssistDisclosure; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 118 | private final InterestingConfigChanges mInterestingConfigChanges; |
Govinda Wasserman | a2c88760 | 2019-05-08 08:02:47 -0400 | [diff] [blame] | 119 | private final PhoneStateMonitor mPhoneStateMonitor; |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 120 | private final AssistHandleBehaviorController mHandleController; |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 121 | private final UiController mUiController; |
Adrian Roos | 4f43dc0 | 2015-06-17 16:43:38 -0700 | [diff] [blame] | 122 | |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 123 | private AssistOrbContainer mView; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 124 | private final DeviceProvisionedController mDeviceProvisionedController; |
Annie Chin | ecb9f3e | 2016-06-27 16:01:52 -0700 | [diff] [blame] | 125 | protected final AssistUtils mAssistUtils; |
Rajeev Kumar | 3e08430 | 2017-09-25 19:08:12 -0700 | [diff] [blame] | 126 | private final boolean mShouldEnableOrb; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 127 | |
| 128 | private IVoiceInteractionSessionShowCallback mShowCallback = |
| 129 | new IVoiceInteractionSessionShowCallback.Stub() { |
| 130 | |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 131 | @Override |
| 132 | public void onFailed() throws RemoteException { |
| 133 | mView.post(mHideRunnable); |
| 134 | } |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 135 | |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 136 | @Override |
| 137 | public void onShown() throws RemoteException { |
| 138 | mView.post(mHideRunnable); |
| 139 | } |
| 140 | }; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 141 | |
| 142 | private Runnable mHideRunnable = new Runnable() { |
| 143 | @Override |
| 144 | public void run() { |
| 145 | mView.removeCallbacks(this); |
| 146 | mView.show(false /* show */, true /* animate */); |
| 147 | } |
| 148 | }; |
| 149 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 150 | public AssistManager(DeviceProvisionedController controller, Context context) { |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 151 | mContext = context; |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 152 | mDeviceProvisionedController = controller; |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 153 | mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 154 | mAssistUtils = new AssistUtils(context); |
Adrian Roos | 4f43dc0 | 2015-06-17 16:43:38 -0700 | [diff] [blame] | 155 | mAssistDisclosure = new AssistDisclosure(context, new Handler()); |
Govinda Wasserman | a2c88760 | 2019-05-08 08:02:47 -0400 | [diff] [blame] | 156 | mPhoneStateMonitor = new PhoneStateMonitor(context); |
Govinda Wasserman | 5dfe965 | 2019-05-30 13:24:24 -0400 | [diff] [blame] | 157 | mHandleController = |
| 158 | new AssistHandleBehaviorController(context, mAssistUtils, new Handler()); |
Annie Chin | ecb9f3e | 2016-06-27 16:01:52 -0700 | [diff] [blame] | 159 | |
| 160 | registerVoiceInteractionSessionListener(); |
Jason Monk | 3edad31 | 2017-06-05 11:33:48 -0400 | [diff] [blame] | 161 | mInterestingConfigChanges = new InterestingConfigChanges(ActivityInfo.CONFIG_ORIENTATION |
| 162 | | ActivityInfo.CONFIG_LOCALE | ActivityInfo.CONFIG_UI_MODE |
| 163 | | ActivityInfo.CONFIG_SCREEN_LAYOUT | ActivityInfo.CONFIG_ASSETS_PATHS); |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 164 | onConfigurationChanged(context.getResources().getConfiguration()); |
Rajeev Kumar | 3e08430 | 2017-09-25 19:08:12 -0700 | [diff] [blame] | 165 | mShouldEnableOrb = !ActivityManager.isLowRamDeviceStatic(); |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 166 | |
| 167 | mUiController = new DefaultUiController(mContext); |
| 168 | |
| 169 | OverviewProxyService overviewProxy = Dependency.get(OverviewProxyService.class); |
| 170 | overviewProxy.addCallback(new OverviewProxyService.OverviewProxyListener() { |
| 171 | @Override |
| 172 | public void onAssistantProgress(float progress) { |
| 173 | // Progress goes from 0 to 1 to indicate how close the assist gesture is to |
| 174 | // completion. |
| 175 | onInvocationProgress(INVOCATION_TYPE_GESTURE, progress); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void onAssistantGestureCompletion(float velocity) { |
| 180 | onGestureCompletion(velocity); |
| 181 | } |
| 182 | }); |
Annie Chin | ecb9f3e | 2016-06-27 16:01:52 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | protected void registerVoiceInteractionSessionListener() { |
| 186 | mAssistUtils.registerVoiceInteractionSessionListener( |
| 187 | new IVoiceInteractionSessionListener.Stub() { |
Matt Casey | 4cff16f | 2019-01-14 14:56:06 -0500 | [diff] [blame] | 188 | @Override |
| 189 | public void onVoiceSessionShown() throws RemoteException { |
| 190 | if (VERBOSE) { |
| 191 | Log.v(TAG, "Voice open"); |
| 192 | } |
| 193 | } |
Annie Chin | ecb9f3e | 2016-06-27 16:01:52 -0700 | [diff] [blame] | 194 | |
Matt Casey | 4cff16f | 2019-01-14 14:56:06 -0500 | [diff] [blame] | 195 | @Override |
| 196 | public void onVoiceSessionHidden() throws RemoteException { |
| 197 | if (VERBOSE) { |
| 198 | Log.v(TAG, "Voice closed"); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | @Override |
James O'Leary | 9c9dd98 | 2019-02-08 15:49:06 -0500 | [diff] [blame] | 203 | public void onSetUiHints(Bundle hints) { |
Matt Casey | 4cff16f | 2019-01-14 14:56:06 -0500 | [diff] [blame] | 204 | if (VERBOSE) { |
James O'Leary | 9c9dd98 | 2019-02-08 15:49:06 -0500 | [diff] [blame] | 205 | Log.v(TAG, "UI hints received"); |
Matt Casey | 4cff16f | 2019-01-14 14:56:06 -0500 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | }); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 211 | public void onConfigurationChanged(Configuration newConfiguration) { |
| 212 | if (!mInterestingConfigChanges.applyNewConfig(mContext.getResources())) { |
| 213 | return; |
| 214 | } |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 215 | boolean visible = false; |
| 216 | if (mView != null) { |
| 217 | visible = mView.isShowing(); |
| 218 | mWindowManager.removeView(mView); |
| 219 | } |
| 220 | |
| 221 | mView = (AssistOrbContainer) LayoutInflater.from(mContext).inflate( |
| 222 | R.layout.assist_orb, null); |
| 223 | mView.setVisibility(View.GONE); |
| 224 | mView.setSystemUiVisibility( |
| 225 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
| 226 | | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); |
| 227 | WindowManager.LayoutParams lp = getLayoutParams(); |
| 228 | mWindowManager.addView(mView, lp); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 229 | if (visible) { |
| 230 | mView.show(true /* show */, false /* animate */); |
| 231 | } |
| 232 | } |
| 233 | |
Annie Chin | 937a991 | 2016-05-31 10:14:23 -0700 | [diff] [blame] | 234 | protected boolean shouldShowOrb() { |
jschung | fc5e15c | 2019-05-08 11:11:59 +0900 | [diff] [blame] | 235 | return false; |
Annie Chin | 937a991 | 2016-05-31 10:14:23 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Jorim Jaggi | 165ce06 | 2015-07-06 16:18:11 -0700 | [diff] [blame] | 238 | public void startAssist(Bundle args) { |
Yohei Yukawa | 528e468 | 2015-09-10 22:21:04 -0700 | [diff] [blame] | 239 | final ComponentName assistComponent = getAssistInfo(); |
| 240 | if (assistComponent == null) { |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 241 | return; |
| 242 | } |
Xiyuan Xia | e6de8b9 | 2015-06-06 22:18:05 -0700 | [diff] [blame] | 243 | |
Yohei Yukawa | 528e468 | 2015-09-10 22:21:04 -0700 | [diff] [blame] | 244 | final boolean isService = assistComponent.equals(getVoiceInteractorComponentName()); |
Annie Chin | 937a991 | 2016-05-31 10:14:23 -0700 | [diff] [blame] | 245 | if (!isService || (!isVoiceSessionRunning() && shouldShowOrb())) { |
Yohei Yukawa | 528e468 | 2015-09-10 22:21:04 -0700 | [diff] [blame] | 246 | showOrb(assistComponent, isService); |
Xiyuan Xia | e6de8b9 | 2015-06-06 22:18:05 -0700 | [diff] [blame] | 247 | mView.postDelayed(mHideRunnable, isService |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 248 | ? TIMEOUT_SERVICE |
| 249 | : TIMEOUT_ACTIVITY); |
| 250 | } |
Dan Sandler | 62bdce5 | 2019-04-23 19:18:21 -0400 | [diff] [blame] | 251 | |
| 252 | if (args == null) { |
| 253 | args = new Bundle(); |
| 254 | } |
Govinda Wasserman | 1f606b0 | 2019-05-29 09:16:02 -0400 | [diff] [blame] | 255 | int invocationType = args.getInt(INVOCATION_TYPE_KEY, 0); |
| 256 | if (invocationType == INVOCATION_TYPE_GESTURE) { |
| 257 | mHandleController.onAssistantGesturePerformed(); |
| 258 | } |
Govinda Wasserman | b7cd162 | 2019-05-31 11:59:19 -0400 | [diff] [blame] | 259 | int phoneState = mPhoneStateMonitor.getPhoneState(); |
| 260 | args.putInt(INVOCATION_PHONE_STATE_KEY, phoneState); |
Govinda Wasserman | 6445e68 | 2019-06-04 09:37:32 -0400 | [diff] [blame] | 261 | args.putLong(INVOCATION_TIME_MS_KEY, SystemClock.elapsedRealtime()); |
Govinda Wasserman | 6418e97 | 2019-06-03 12:37:43 -0400 | [diff] [blame] | 262 | logStartAssist(invocationType, phoneState); |
Yohei Yukawa | 528e468 | 2015-09-10 22:21:04 -0700 | [diff] [blame] | 263 | startAssistInternal(args, assistComponent, isService); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Govinda Wasserman | b130e94 | 2019-04-15 10:07:37 -0400 | [diff] [blame] | 266 | /** Called when the user is performing an assistant invocation action (e.g. Active Edge) */ |
| 267 | public void onInvocationProgress(int type, float progress) { |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 268 | mUiController.onInvocationProgress(type, progress); |
Govinda Wasserman | b130e94 | 2019-04-15 10:07:37 -0400 | [diff] [blame] | 269 | } |
| 270 | |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 271 | /** |
| 272 | * Called when the user has invoked the assistant with the incoming velocity, in pixels per |
Miranda Kephart | 4be2690 | 2019-05-17 13:55:37 -0400 | [diff] [blame] | 273 | * millisecond. For invocations without a velocity (e.g. slow drag), the velocity is set to |
| 274 | * zero. |
| 275 | */ |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 276 | public void onGestureCompletion(float velocity) { |
| 277 | mUiController.onGestureCompletion(velocity); |
Miranda Kephart | 4be2690 | 2019-05-17 13:55:37 -0400 | [diff] [blame] | 278 | } |
| 279 | |
Jorim Jaggi | b835dd7 | 2015-06-08 12:28:42 -0700 | [diff] [blame] | 280 | public void hideAssist() { |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 281 | mAssistUtils.hideCurrentSession(); |
Jorim Jaggi | b835dd7 | 2015-06-08 12:28:42 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 284 | private WindowManager.LayoutParams getLayoutParams() { |
| 285 | WindowManager.LayoutParams lp = new WindowManager.LayoutParams( |
| 286 | ViewGroup.LayoutParams.MATCH_PARENT, |
| 287 | mContext.getResources().getDimensionPixelSize(R.dimen.assist_orb_scrim_height), |
| 288 | WindowManager.LayoutParams.TYPE_VOICE_INTERACTION_STARTING, |
| 289 | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
| 290 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
| 291 | | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, |
| 292 | PixelFormat.TRANSLUCENT); |
Wale Ogunwale | 6ce0fb8 | 2016-12-13 14:24:00 -0800 | [diff] [blame] | 293 | lp.token = new Binder(); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 294 | lp.gravity = Gravity.BOTTOM | Gravity.START; |
| 295 | lp.setTitle("AssistPreviewPanel"); |
| 296 | lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED |
| 297 | | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING; |
| 298 | return lp; |
| 299 | } |
| 300 | |
Yohei Yukawa | 528e468 | 2015-09-10 22:21:04 -0700 | [diff] [blame] | 301 | private void showOrb(@NonNull ComponentName assistComponent, boolean isService) { |
| 302 | maybeSwapSearchIcon(assistComponent, isService); |
Rajeev Kumar | 3e08430 | 2017-09-25 19:08:12 -0700 | [diff] [blame] | 303 | if (mShouldEnableOrb) { |
| 304 | mView.show(true /* show */, true /* animate */); |
| 305 | } |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Yohei Yukawa | 528e468 | 2015-09-10 22:21:04 -0700 | [diff] [blame] | 308 | private void startAssistInternal(Bundle args, @NonNull ComponentName assistComponent, |
| 309 | boolean isService) { |
| 310 | if (isService) { |
| 311 | startVoiceInteractor(args); |
| 312 | } else { |
| 313 | startAssistActivity(args, assistComponent); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
Yohei Yukawa | 528e468 | 2015-09-10 22:21:04 -0700 | [diff] [blame] | 317 | private void startAssistActivity(Bundle args, @NonNull ComponentName assistComponent) { |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 318 | if (!mDeviceProvisionedController.isDeviceProvisioned()) { |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 319 | return; |
| 320 | } |
| 321 | |
| 322 | // Close Recent Apps if needed |
Jason Monk | 9c7844c | 2017-01-18 15:21:53 -0500 | [diff] [blame] | 323 | SysUiServiceProvider.getComponent(mContext, CommandQueue.class).animateCollapsePanels( |
Jason Monk | 297c04e | 2018-08-23 17:16:59 -0400 | [diff] [blame] | 324 | CommandQueue.FLAG_EXCLUDE_SEARCH_PANEL | CommandQueue.FLAG_EXCLUDE_RECENTS_PANEL, |
| 325 | false /* force */); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 326 | |
Adrian Roos | 4f43dc0 | 2015-06-17 16:43:38 -0700 | [diff] [blame] | 327 | boolean structureEnabled = Settings.Secure.getIntForUser(mContext.getContentResolver(), |
| 328 | Settings.Secure.ASSIST_STRUCTURE_ENABLED, 1, UserHandle.USER_CURRENT) != 0; |
| 329 | |
Steven Wu | 2b71d79 | 2018-05-08 11:03:17 -0400 | [diff] [blame] | 330 | final SearchManager searchManager = |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 331 | (SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE); |
Steven Wu | 2b71d79 | 2018-05-08 11:03:17 -0400 | [diff] [blame] | 332 | if (searchManager == null) { |
| 333 | return; |
| 334 | } |
| 335 | final Intent intent = searchManager.getAssistIntent(structureEnabled); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 336 | if (intent == null) { |
| 337 | return; |
| 338 | } |
Yohei Yukawa | 528e468 | 2015-09-10 22:21:04 -0700 | [diff] [blame] | 339 | intent.setComponent(assistComponent); |
Jorim Jaggi | 165ce06 | 2015-07-06 16:18:11 -0700 | [diff] [blame] | 340 | intent.putExtras(args); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 341 | |
Rajeev Kumar | cd497ef | 2019-08-06 12:02:47 -0700 | [diff] [blame] | 342 | if (structureEnabled && AssistUtils.isDisclosureEnabled(mContext)) { |
Adrian Roos | 4f43dc0 | 2015-06-17 16:43:38 -0700 | [diff] [blame] | 343 | showDisclosure(); |
| 344 | } |
| 345 | |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 346 | try { |
| 347 | final ActivityOptions opts = ActivityOptions.makeCustomAnimation(mContext, |
| 348 | R.anim.search_launch_enter, R.anim.search_launch_exit); |
| 349 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 350 | AsyncTask.execute(new Runnable() { |
| 351 | @Override |
| 352 | public void run() { |
| 353 | mContext.startActivityAsUser(intent, opts.toBundle(), |
| 354 | new UserHandle(UserHandle.USER_CURRENT)); |
| 355 | } |
| 356 | }); |
| 357 | } catch (ActivityNotFoundException e) { |
| 358 | Log.w(TAG, "Activity not found for " + intent.getAction()); |
| 359 | } |
| 360 | } |
| 361 | |
Jorim Jaggi | 165ce06 | 2015-07-06 16:18:11 -0700 | [diff] [blame] | 362 | private void startVoiceInteractor(Bundle args) { |
Dianne Hackborn | 17f6935 | 2015-07-17 18:04:14 -0700 | [diff] [blame] | 363 | mAssistUtils.showSessionForActiveService(args, |
| 364 | VoiceInteractionSession.SHOW_SOURCE_ASSIST_GESTURE, mShowCallback, null); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Selim Cinek | e70d653 | 2015-04-24 16:46:13 -0700 | [diff] [blame] | 367 | public void launchVoiceAssistFromKeyguard() { |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 368 | mAssistUtils.launchVoiceAssistFromKeyguard(); |
Selim Cinek | e70d653 | 2015-04-24 16:46:13 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Selim Cinek | e70d653 | 2015-04-24 16:46:13 -0700 | [diff] [blame] | 371 | public boolean canVoiceAssistBeLaunchedFromKeyguard() { |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 372 | return mAssistUtils.activeServiceSupportsLaunchFromKeyguard(); |
Selim Cinek | e70d653 | 2015-04-24 16:46:13 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | public ComponentName getVoiceInteractorComponentName() { |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 376 | return mAssistUtils.getActiveServiceComponentName(); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | private boolean isVoiceSessionRunning() { |
Adrian Roos | e917508 | 2015-06-15 13:23:14 -0700 | [diff] [blame] | 380 | return mAssistUtils.isSessionRunning(); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Yohei Yukawa | 528e468 | 2015-09-10 22:21:04 -0700 | [diff] [blame] | 383 | private void maybeSwapSearchIcon(@NonNull ComponentName assistComponent, boolean isService) { |
| 384 | replaceDrawable(mView.getOrb().getLogo(), assistComponent, ASSIST_ICON_METADATA_NAME, |
| 385 | isService); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | public void replaceDrawable(ImageView v, ComponentName component, String name, |
| 389 | boolean isService) { |
| 390 | if (component != null) { |
| 391 | try { |
| 392 | PackageManager packageManager = mContext.getPackageManager(); |
| 393 | // Look for the search icon specified in the activity meta-data |
| 394 | Bundle metaData = isService |
| 395 | ? packageManager.getServiceInfo( |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 396 | component, PackageManager.GET_META_DATA).metaData |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 397 | : packageManager.getActivityInfo( |
| 398 | component, PackageManager.GET_META_DATA).metaData; |
| 399 | if (metaData != null) { |
| 400 | int iconResId = metaData.getInt(name); |
| 401 | if (iconResId != 0) { |
| 402 | Resources res = packageManager.getResourcesForApplication( |
| 403 | component.getPackageName()); |
| 404 | v.setImageDrawable(res.getDrawable(iconResId)); |
| 405 | return; |
| 406 | } |
| 407 | } |
| 408 | } catch (PackageManager.NameNotFoundException e) { |
Matt Casey | 4cff16f | 2019-01-14 14:56:06 -0500 | [diff] [blame] | 409 | if (VERBOSE) { |
| 410 | Log.v(TAG, "Assistant component " |
| 411 | + component.flattenToShortString() + " not found"); |
| 412 | } |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 413 | } catch (Resources.NotFoundException nfe) { |
| 414 | Log.w(TAG, "Failed to swap drawable from " |
| 415 | + component.flattenToShortString(), nfe); |
| 416 | } |
| 417 | } |
| 418 | v.setImageDrawable(null); |
| 419 | } |
| 420 | |
Govinda Wasserman | c7495cd | 2019-05-20 14:43:28 -0400 | [diff] [blame] | 421 | protected AssistHandleBehaviorController getHandleBehaviorController() { |
| 422 | return mHandleController; |
| 423 | } |
| 424 | |
Yohei Yukawa | 528e468 | 2015-09-10 22:21:04 -0700 | [diff] [blame] | 425 | @Nullable |
Matthew Ng | add4c39 | 2019-03-01 16:02:31 -0800 | [diff] [blame] | 426 | public ComponentName getAssistInfoForUser(int userId) { |
| 427 | return mAssistUtils.getAssistComponentForUser(userId); |
| 428 | } |
| 429 | |
| 430 | @Nullable |
Yohei Yukawa | 528e468 | 2015-09-10 22:21:04 -0700 | [diff] [blame] | 431 | private ComponentName getAssistInfo() { |
Matthew Ng | add4c39 | 2019-03-01 16:02:31 -0800 | [diff] [blame] | 432 | return getAssistInfoForUser(KeyguardUpdateMonitor.getCurrentUser()); |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 433 | } |
Adrian Roos | 4f43dc0 | 2015-06-17 16:43:38 -0700 | [diff] [blame] | 434 | |
| 435 | public void showDisclosure() { |
| 436 | mAssistDisclosure.postShow(); |
| 437 | } |
Selim Cinek | 9a63499 | 2015-06-23 01:10:07 -0400 | [diff] [blame] | 438 | |
Jorim Jaggi | 19695d9 | 2015-07-20 15:51:40 -0700 | [diff] [blame] | 439 | public void onLockscreenShown() { |
| 440 | mAssistUtils.onLockscreenShown(); |
| 441 | } |
Govinda Wasserman | b7cd162 | 2019-05-31 11:59:19 -0400 | [diff] [blame] | 442 | |
Govinda Wasserman | e9ff1ff | 2019-07-31 15:26:33 -0400 | [diff] [blame] | 443 | public long getAssistHandleShowAndGoRemainingDurationMs() { |
| 444 | return mHandleController.getShowAndGoRemainingTimeMs(); |
| 445 | } |
| 446 | |
Govinda Wasserman | b7cd162 | 2019-05-31 11:59:19 -0400 | [diff] [blame] | 447 | /** Returns the logging flags for the given Assistant invocation type. */ |
| 448 | public int toLoggingSubType(int invocationType) { |
| 449 | return toLoggingSubType(invocationType, mPhoneStateMonitor.getPhoneState()); |
| 450 | } |
| 451 | |
Govinda Wasserman | 6418e97 | 2019-06-03 12:37:43 -0400 | [diff] [blame] | 452 | protected void logStartAssist(int invocationType, int phoneState) { |
| 453 | MetricsLogger.action( |
| 454 | new LogMaker(MetricsEvent.ASSISTANT) |
| 455 | .setType(MetricsEvent.TYPE_OPEN) |
| 456 | .setSubtype(toLoggingSubType(invocationType, phoneState))); |
| 457 | } |
| 458 | |
| 459 | protected final int toLoggingSubType(int invocationType, int phoneState) { |
Govinda Wasserman | b7cd162 | 2019-05-31 11:59:19 -0400 | [diff] [blame] | 460 | // Note that this logic will break if the number of Assistant invocation types exceeds 7. |
| 461 | // There are currently 5 invocation types, but we will be migrating to the new logging |
| 462 | // framework in the next update. |
| 463 | int subType = mHandleController.areHandlesShowing() ? 0 : 1; |
| 464 | subType |= invocationType << 1; |
| 465 | subType |= phoneState << 4; |
| 466 | return subType; |
| 467 | } |
Jorim Jaggi | 2fdeeab | 2015-04-01 15:13:03 -0700 | [diff] [blame] | 468 | } |