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