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
|
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.graphics.PixelFormat;
import android.media.AudioAttributes;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.os.Vibrator;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
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 around the assist gesture.
*/
public class AssistGestureManager {
private static final String TAG = "AssistGestureManager";
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 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 */);
}
};
public AssistGestureManager(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));
}
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);
mBar.getNavigationBarView().setDelegateView(mView);
if (visible) {
mView.show(true /* show */, false /* animate */);
}
}
public void onGestureInvoked(boolean vibrate) {
boolean isVoiceInteractorActive = getVoiceInteractorSupportsAssistGesture();
if (!isVoiceInteractorActive && !isAssistantIntentAvailable()) {
return;
}
if (vibrate) {
vibrate();
}
if (!isVoiceInteractorActive || !isVoiceSessionRunning()) {
showOrb();
mView.postDelayed(mHideRunnable, isVoiceInteractorActive
? TIMEOUT_SERVICE
: TIMEOUT_ACTIVITY);
}
startAssist();
}
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 (getVoiceInteractorSupportsAssistGesture()) {
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;
}
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);
}
}
private boolean getVoiceInteractorSupportsAssistGesture() {
try {
return mVoiceInteractionManagerService.activeServiceSupportsAssist();
} catch (RemoteException e) {
Log.w(TAG, "Failed to call activeServiceSupportsAssistGesture", e);
return false;
}
}
private 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.isSessionRunning();
} catch (RemoteException e) {
Log.w(TAG, "Failed to call isSessionRunning", e);
return false;
}
}
public void destroy() {
mWindowManager.removeViewImmediate(mView);
}
private void maybeSwapSearchIcon() {
Intent intent = ((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE))
.getAssistIntent(mContext, false, UserHandle.USER_CURRENT);
ComponentName component = null;
boolean isService = false;
if (getVoiceInteractorSupportsAssistGesture()) {
component = getVoiceInteractorComponentName();
isService = true;
} else if (intent != null) {
component = intent.getComponent();
}
if (component != null) {
replaceDrawable(mView.getOrb().getLogo(), component, ASSIST_ICON_METADATA_NAME,
isService);
} 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() {
if (Settings.System.getIntForUser(mContext.getContentResolver(),
Settings.System.HAPTIC_FEEDBACK_ENABLED, 1, UserHandle.USER_CURRENT) != 0) {
Resources res = mContext.getResources();
Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(res.getInteger(R.integer.config_search_panel_view_vibration_duration),
VIBRATION_ATTRIBUTES);
}
}
public boolean isAssistantIntentAvailable() {
return ((SearchManager) mContext.getSystemService(Context.SEARCH_SERVICE))
.getAssistIntent(mContext, false, UserHandle.USER_CURRENT) != null;
}
}
|