1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
package com.android.systemui.assist;
import android.app.ActivityManager;
import android.app.ActivityOptions;
import android.app.SearchManager;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.ContentObserver;
import android.graphics.PixelFormat;
import android.media.AudioAttributes;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import com.android.internal.app.IVoiceInteractionManagerService;
import com.android.internal.app.IVoiceInteractionSessionShowCallback;
import com.android.systemui.R;
import com.android.systemui.statusbar.CommandQueue;
import com.android.systemui.statusbar.phone.PhoneStatusBar;
/**
* Class to manage everything related to assist in SystemUI.
*/
public class AssistManager {
private static final String TAG = "AssistManager";
private static final String ASSIST_ICON_METADATA_NAME =
"com.android.systemui.action_assist_icon";
private static final AudioAttributes VIBRATION_ATTRIBUTES = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
.build();
private static final long TIMEOUT_SERVICE = 2500;
private static final long TIMEOUT_ACTIVITY = 1000;
private final Context mContext;
private final WindowManager mWindowManager;
private AssistOrbContainer mView;
private final PhoneStatusBar mBar;
private final IVoiceInteractionManagerService mVoiceInteractionManagerService;
private ComponentName mAssistComponent;
private IVoiceInteractionSessionShowCallback mShowCallback =
new IVoiceInteractionSessionShowCallback.Stub() {
@Override
public void onFailed() throws RemoteException {
mView.post(mHideRunnable);
}
@Override
public void onShown() throws RemoteException {
mView.post(mHideRunnable);
}
};
private Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
mView.removeCallbacks(this);
mView.show(false /* show */, true /* animate */);
}
};
private final ContentObserver mAssistSettingsObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
updateAssistInfo();
}
};
public AssistManager(PhoneStatusBar bar, Context context) {
mContext = context;
mBar = bar;
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
mVoiceInteractionManagerService = IVoiceInteractionManagerService.Stub.asInterface(
ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE));
mContext.getContentResolver().registerContentObserver(
Settings.Secure.getUriFor(Settings.Secure.ASSISTANT), false,
mAssistSettingsObserver);
mAssistSettingsObserver.onChange(false);
}
public void onConfigurationChanged() {
boolean visible = false;
if (mView != null) {
visible = mView.isShowing();
mWindowManager.removeView(mView);
}
mView = (AssistOrbContainer) LayoutInflater.from(mContext).inflate(
R.layout.assist_orb, null);
mView.setVisibility(View.GONE);
mView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
WindowManager.LayoutParams lp = getLayoutParams();
mWindowManager.addView(mView, lp);
if (visible) {
mView.show(true /* show */, false /* animate */);
}
}
public void onGestureInvoked(boolean vibrate) {
if (mAssistComponent == null) {
return;
}
if (vibrate) {
vibrate();
}
final boolean isService = isAssistantService();
if (isService || !isVoiceSessionRunning()) {
showOrb();
mView.postDelayed(mHideRunnable, isService
? TIMEOUT_SERVICE
: TIMEOUT_ACTIVITY);
}
startAssist();
}
public void hideAssist() {
try {
mVoiceInteractionManagerService.hideCurrentSession();
} catch (RemoteException e) {
Log.w(TAG, "Failed to call hideCurrentSession", e);
}
}
private WindowManager.LayoutParams getLayoutParams() {
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
mContext.getResources().getDimensionPixelSize(R.dimen.assist_orb_scrim_height),
WindowManager.LayoutParams.TYPE_VOICE_INTERACTION_STARTING,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
if (ActivityManager.isHighEndGfx()) {
lp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
}
lp.gravity = Gravity.BOTTOM | Gravity.START;
lp.setTitle("AssistPreviewPanel");
lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED
| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING;
return lp;
}
private void showOrb() {
maybeSwapSearchIcon();
mView.show(true /* show */, true /* animate */);
}
private void startAssist() {
if (mAssistComponent != null) {
if (isAssistantService()) {
startVoiceInteractor();
} else {
startAssistActivity();
}
}
}
private void startAssistActivity() {
if (!mBar.isDeviceProvisioned()) {
return;
}
// Close Recent Apps if needed
mBar.animateCollapsePanels(CommandQueue.FLAG_EXCLUDE_SEARCH_PANEL |
CommandQueue.FLAG_EXCLUDE_RECENTS_PANEL);
final Intent intent = ((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE))
.getAssistIntent(mContext, true, UserHandle.USER_CURRENT);
if (intent == null) {
return;
}
if (mAssistComponent != null) {
intent.setComponent(mAssistComponent);
}
try {
final ActivityOptions opts = ActivityOptions.makeCustomAnimation(mContext,
R.anim.search_launch_enter, R.anim.search_launch_exit);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
AsyncTask.execute(new Runnable() {
@Override
public void run() {
mContext.startActivityAsUser(intent, opts.toBundle(),
new UserHandle(UserHandle.USER_CURRENT));
}
});
} catch (ActivityNotFoundException e) {
Log.w(TAG, "Activity not found for " + intent.getAction());
}
}
private void startVoiceInteractor() {
try {
mVoiceInteractionManagerService.showSessionForActiveService(mShowCallback);
} catch (RemoteException e) {
Log.w(TAG, "Failed to call showSessionForActiveService", e);
}
}
public void launchVoiceAssistFromKeyguard() {
try {
mVoiceInteractionManagerService.launchVoiceAssistFromKeyguard();
} catch (RemoteException e) {
Log.w(TAG, "Failed to call launchVoiceAssistFromKeyguard", e);
}
}
private boolean getVoiceInteractorSupportsAssistGesture() {
try {
return mVoiceInteractionManagerService != null
&& mVoiceInteractionManagerService.activeServiceSupportsAssist();
} catch (RemoteException e) {
Log.w(TAG, "Failed to call activeServiceSupportsAssistGesture", e);
return false;
}
}
public boolean canVoiceAssistBeLaunchedFromKeyguard() {
try {
return mVoiceInteractionManagerService != null
&& mVoiceInteractionManagerService.activeServiceSupportsLaunchFromKeyguard();
} catch (RemoteException e) {
Log.w(TAG, "Failed to call activeServiceSupportsLaunchFromKeyguard", e);
return false;
}
}
public ComponentName getVoiceInteractorComponentName() {
try {
return mVoiceInteractionManagerService.getActiveServiceComponentName();
} catch (RemoteException e) {
Log.w(TAG, "Failed to call getActiveServiceComponentName", e);
return null;
}
}
private boolean isVoiceSessionRunning() {
try {
return mVoiceInteractionManagerService != null
&& mVoiceInteractionManagerService.isSessionRunning();
} catch (RemoteException e) {
Log.w(TAG, "Failed to call isSessionRunning", e);
return false;
}
}
public void destroy() {
mWindowManager.removeViewImmediate(mView);
}
private void maybeSwapSearchIcon() {
if (mAssistComponent != null) {
replaceDrawable(mView.getOrb().getLogo(), mAssistComponent, ASSIST_ICON_METADATA_NAME,
isAssistantService());
} else {
mView.getOrb().getLogo().setImageDrawable(null);
}
}
public void replaceDrawable(ImageView v, ComponentName component, String name,
boolean isService) {
if (component != null) {
try {
PackageManager packageManager = mContext.getPackageManager();
// Look for the search icon specified in the activity meta-data
Bundle metaData = isService
? packageManager.getServiceInfo(
component, PackageManager.GET_META_DATA).metaData
: packageManager.getActivityInfo(
component, PackageManager.GET_META_DATA).metaData;
if (metaData != null) {
int iconResId = metaData.getInt(name);
if (iconResId != 0) {
Resources res = packageManager.getResourcesForApplication(
component.getPackageName());
v.setImageDrawable(res.getDrawable(iconResId));
return;
}
}
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, "Failed to swap drawable; "
+ component.flattenToShortString() + " not found", e);
} catch (Resources.NotFoundException nfe) {
Log.w(TAG, "Failed to swap drawable from "
+ component.flattenToShortString(), nfe);
}
}
v.setImageDrawable(null);
}
private void vibrate() {
mView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
}
private boolean isAssistantService() {
return mAssistComponent == null ?
false : mAssistComponent.equals(getVoiceInteractorComponentName());
}
private void updateAssistInfo() {
final String setting = Settings.Secure.getStringForUser(mContext.getContentResolver(),
Settings.Secure.ASSISTANT, UserHandle.USER_CURRENT);
if (setting != null) {
mAssistComponent = ComponentName.unflattenFromString(setting);
return;
}
// Fallback to keep backward compatible behavior when there is no user setting.
if (getVoiceInteractorSupportsAssistGesture()) {
mAssistComponent = getVoiceInteractorComponentName();
return;
}
Intent intent = ((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE))
.getAssistIntent(mContext, false, UserHandle.USER_CURRENT);
if (intent != null) {
mAssistComponent = intent.getComponent();
return;
}
mAssistComponent = null;
}
}
|