2 * Copyright (C) 2014 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.server.voiceinteraction;
19 import android.app.ActivityManager;
20 import android.app.ActivityManagerInternal;
21 import android.app.ActivityManagerNative;
22 import android.app.IActivityManager;
23 import android.content.BroadcastReceiver;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.content.ServiceConnection;
29 import android.content.pm.PackageManager;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.os.IBinder;
33 import android.os.RemoteException;
34 import android.os.ServiceManager;
35 import android.os.UserHandle;
36 import android.service.voice.IVoiceInteractionService;
37 import android.service.voice.IVoiceInteractionSession;
38 import android.service.voice.VoiceInteractionService;
39 import android.service.voice.VoiceInteractionServiceInfo;
40 import android.util.PrintWriterPrinter;
41 import android.util.Slog;
42 import android.view.IWindowManager;
44 import com.android.internal.app.IVoiceInteractionSessionListener;
45 import com.android.internal.app.IVoiceInteractionSessionShowCallback;
46 import com.android.internal.app.IVoiceInteractor;
47 import com.android.server.LocalServices;
49 import java.io.FileDescriptor;
50 import java.io.PrintWriter;
51 import java.util.List;
53 class VoiceInteractionManagerServiceImpl implements VoiceInteractionSessionConnection.Callback {
54 final static String TAG = "VoiceInteractionServiceManager";
56 final static String CLOSE_REASON_VOICE_INTERACTION = "voiceinteraction";
60 final Context mContext;
61 final Handler mHandler;
62 final VoiceInteractionManagerService.VoiceInteractionManagerServiceStub mServiceStub;
64 final ComponentName mComponent;
65 final IActivityManager mAm;
66 final VoiceInteractionServiceInfo mInfo;
67 final ComponentName mSessionComponentName;
68 final IWindowManager mIWindowManager;
69 boolean mBound = false;
70 IVoiceInteractionService mService;
72 VoiceInteractionSessionConnection mActiveSession;
73 int mDisabledShowContext;
75 final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
77 public void onReceive(Context context, Intent intent) {
78 if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {
79 String reason = intent.getStringExtra("reason");
80 if (!CLOSE_REASON_VOICE_INTERACTION.equals(reason) && !"dream".equals(reason)) {
81 synchronized (mServiceStub) {
82 if (mActiveSession != null && mActiveSession.mSession != null) {
84 mActiveSession.mSession.closeSystemDialogs();
85 } catch (RemoteException e) {
94 final ServiceConnection mConnection = new ServiceConnection() {
96 public void onServiceConnected(ComponentName name, IBinder service) {
97 synchronized (mServiceStub) {
98 mService = IVoiceInteractionService.Stub.asInterface(service);
101 } catch (RemoteException e) {
107 public void onServiceDisconnected(ComponentName name) {
112 VoiceInteractionManagerServiceImpl(Context context, Handler handler,
113 VoiceInteractionManagerService.VoiceInteractionManagerServiceStub stub,
114 int userHandle, ComponentName service) {
119 mComponent = service;
120 mAm = ActivityManagerNative.getDefault();
121 VoiceInteractionServiceInfo info;
123 info = new VoiceInteractionServiceInfo(context.getPackageManager(), service, mUser);
124 } catch (PackageManager.NameNotFoundException e) {
125 Slog.w(TAG, "Voice interaction service not found: " + service, e);
127 mSessionComponentName = null;
128 mIWindowManager = null;
133 if (mInfo.getParseError() != null) {
134 Slog.w(TAG, "Bad voice interaction service: " + mInfo.getParseError());
135 mSessionComponentName = null;
136 mIWindowManager = null;
141 mSessionComponentName = new ComponentName(service.getPackageName(),
142 mInfo.getSessionService());
143 mIWindowManager = IWindowManager.Stub.asInterface(
144 ServiceManager.getService(Context.WINDOW_SERVICE));
145 IntentFilter filter = new IntentFilter();
146 filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
147 mContext.registerReceiver(mBroadcastReceiver, filter, null, handler);
150 public boolean showSessionLocked(Bundle args, int flags,
151 IVoiceInteractionSessionShowCallback showCallback, IBinder activityToken) {
152 if (mActiveSession == null) {
153 mActiveSession = new VoiceInteractionSessionConnection(mServiceStub,
154 mSessionComponentName, mUser, mContext, this,
155 mInfo.getServiceInfo().applicationInfo.uid, mHandler);
157 List<IBinder> activityTokens = null;
158 if (activityToken == null) {
159 // Let's get top activities from all visible stacks
160 activityTokens = LocalServices.getService(ActivityManagerInternal.class)
161 .getTopVisibleActivities();
163 return mActiveSession.showLocked(args, flags, mDisabledShowContext, showCallback,
164 activityToken, activityTokens);
167 public boolean hideSessionLocked() {
168 if (mActiveSession != null) {
169 return mActiveSession.hideLocked();
174 public boolean deliverNewSessionLocked(IBinder token,
175 IVoiceInteractionSession session, IVoiceInteractor interactor) {
176 if (mActiveSession == null || token != mActiveSession.mToken) {
177 Slog.w(TAG, "deliverNewSession does not match active session");
180 mActiveSession.deliverNewSessionLocked(session, interactor);
184 public int startVoiceActivityLocked(int callingPid, int callingUid, IBinder token,
185 Intent intent, String resolvedType) {
187 if (mActiveSession == null || token != mActiveSession.mToken) {
188 Slog.w(TAG, "startVoiceActivity does not match active session");
189 return ActivityManager.START_VOICE_NOT_ACTIVE_SESSION;
191 if (!mActiveSession.mShown) {
192 Slog.w(TAG, "startVoiceActivity not allowed on hidden session");
193 return ActivityManager.START_VOICE_HIDDEN_SESSION;
195 intent = new Intent(intent);
196 intent.addCategory(Intent.CATEGORY_VOICE);
197 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
198 return mAm.startVoiceActivity(mComponent.getPackageName(), callingPid, callingUid,
199 intent, resolvedType, mActiveSession.mSession, mActiveSession.mInteractor,
200 0, null, null, mUser);
201 } catch (RemoteException e) {
202 throw new IllegalStateException("Unexpected remote error", e);
206 public void setKeepAwakeLocked(IBinder token, boolean keepAwake) {
208 if (mActiveSession == null || token != mActiveSession.mToken) {
209 Slog.w(TAG, "setKeepAwake does not match active session");
212 mAm.setVoiceKeepAwake(mActiveSession.mSession, keepAwake);
213 } catch (RemoteException e) {
214 throw new IllegalStateException("Unexpected remote error", e);
218 public void closeSystemDialogsLocked(IBinder token) {
220 if (mActiveSession == null || token != mActiveSession.mToken) {
221 Slog.w(TAG, "closeSystemDialogs does not match active session");
224 mAm.closeSystemDialogs(CLOSE_REASON_VOICE_INTERACTION);
225 } catch (RemoteException e) {
226 throw new IllegalStateException("Unexpected remote error", e);
230 public void finishLocked(IBinder token, boolean finishTask) {
231 if (mActiveSession == null || (!finishTask && token != mActiveSession.mToken)) {
232 Slog.w(TAG, "finish does not match active session");
235 mActiveSession.cancelLocked(finishTask);
236 mActiveSession = null;
239 public void setDisabledShowContextLocked(int callingUid, int flags) {
240 int activeUid = mInfo.getServiceInfo().applicationInfo.uid;
241 if (callingUid != activeUid) {
242 throw new SecurityException("Calling uid " + callingUid
243 + " does not match active uid " + activeUid);
245 mDisabledShowContext = flags;
248 public int getDisabledShowContextLocked(int callingUid) {
249 int activeUid = mInfo.getServiceInfo().applicationInfo.uid;
250 if (callingUid != activeUid) {
251 throw new SecurityException("Calling uid " + callingUid
252 + " does not match active uid " + activeUid);
254 return mDisabledShowContext;
257 public int getUserDisabledShowContextLocked(int callingUid) {
258 int activeUid = mInfo.getServiceInfo().applicationInfo.uid;
259 if (callingUid != activeUid) {
260 throw new SecurityException("Calling uid " + callingUid
261 + " does not match active uid " + activeUid);
263 return mActiveSession != null ? mActiveSession.getUserDisabledShowContextLocked() : 0;
266 public boolean supportsLocalVoiceInteraction() {
267 return mInfo.getSupportsLocalInteraction();
270 public void dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args) {
272 pw.print(" NOT VALID: ");
274 pw.println("no info");
276 pw.println(mInfo.getParseError());
280 pw.print(" mUser="); pw.println(mUser);
281 pw.print(" mComponent="); pw.println(mComponent.flattenToShortString());
282 pw.print(" Session service="); pw.println(mInfo.getSessionService());
283 pw.println(" Service info:");
284 mInfo.getServiceInfo().dump(new PrintWriterPrinter(pw), " ");
285 pw.print(" Recognition service="); pw.println(mInfo.getRecognitionService());
286 pw.print(" Settings activity="); pw.println(mInfo.getSettingsActivity());
287 pw.print(" Supports assist="); pw.println(mInfo.getSupportsAssist());
288 pw.print(" Supports launch from keyguard=");
289 pw.println(mInfo.getSupportsLaunchFromKeyguard());
290 if (mDisabledShowContext != 0) {
291 pw.print(" mDisabledShowContext=");
292 pw.println(Integer.toHexString(mDisabledShowContext));
294 pw.print(" mBound="); pw.print(mBound); pw.print(" mService="); pw.println(mService);
295 if (mActiveSession != null) {
296 pw.println(" Active session:");
297 mActiveSession.dump(" ", pw);
302 Intent intent = new Intent(VoiceInteractionService.SERVICE_INTERFACE);
303 intent.setComponent(mComponent);
304 mBound = mContext.bindServiceAsUser(intent, mConnection,
305 Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, new UserHandle(mUser));
307 Slog.w(TAG, "Failed binding to voice interaction service " + mComponent);
311 public void launchVoiceAssistFromKeyguard() {
312 if (mService == null) {
313 Slog.w(TAG, "Not bound to voice interaction service " + mComponent);
317 mService.launchVoiceAssistFromKeyguard();
318 } catch (RemoteException e) {
319 Slog.w(TAG, "RemoteException while calling launchVoiceAssistFromKeyguard", e);
323 void shutdownLocked() {
324 // If there is an active session, cancel it to allow it to clean up its window and other
326 if (mActiveSession != null) {
327 mActiveSession.cancelLocked(false);
328 mActiveSession = null;
331 if (mService != null) {
334 } catch (RemoteException e) {
335 Slog.w(TAG, "RemoteException in shutdown", e);
339 mContext.unbindService(mConnection);
343 mContext.unregisterReceiver(mBroadcastReceiver);
347 void notifySoundModelsChangedLocked() {
348 if (mService == null) {
349 Slog.w(TAG, "Not bound to voice interaction service " + mComponent);
353 mService.soundModelsChanged();
354 } catch (RemoteException e) {
355 Slog.w(TAG, "RemoteException while calling soundModelsChanged", e);
360 public void sessionConnectionGone(VoiceInteractionSessionConnection connection) {
361 synchronized (mServiceStub) {
362 finishLocked(connection.mToken, false);
367 public void onSessionShown(VoiceInteractionSessionConnection connection) {
368 mServiceStub.onSessionShown();
372 public void onSessionHidden(VoiceInteractionSessionConnection connection) {
373 mServiceStub.onSessionHidden();