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