Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.voiceinteraction; |
| 18 | |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 20 | import android.app.ActivityManagerNative; |
| 21 | import android.app.IActivityManager; |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 26 | import android.content.IntentFilter; |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 27 | import android.content.ServiceConnection; |
| 28 | import android.content.pm.PackageManager; |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 29 | import android.os.Bundle; |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 30 | import android.os.Handler; |
| 31 | import android.os.IBinder; |
| 32 | import android.os.RemoteException; |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 33 | import android.os.ServiceManager; |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 34 | import android.os.UserHandle; |
| 35 | import android.service.voice.IVoiceInteractionService; |
| 36 | import android.service.voice.IVoiceInteractionSession; |
| 37 | import android.service.voice.VoiceInteractionService; |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 38 | import android.service.voice.VoiceInteractionServiceInfo; |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 39 | import android.util.Slog; |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 40 | import android.view.IWindowManager; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 41 | |
Jorim Jaggi | 225d3b5 | 2015-04-01 11:18:57 -0700 | [diff] [blame^] | 42 | import com.android.internal.app.IVoiceInteractionSessionShowCallback; |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 43 | import com.android.internal.app.IVoiceInteractor; |
| 44 | |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 45 | import java.io.FileDescriptor; |
| 46 | import java.io.PrintWriter; |
| 47 | |
Dianne Hackborn | ffeecb1 | 2015-02-25 11:08:11 -0800 | [diff] [blame] | 48 | class VoiceInteractionManagerServiceImpl implements VoiceInteractionSessionConnection.Callback { |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 49 | final static String TAG = "VoiceInteractionServiceManager"; |
| 50 | |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 51 | final boolean mValid; |
| 52 | |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 53 | final Context mContext; |
| 54 | final Handler mHandler; |
| 55 | final Object mLock; |
| 56 | final int mUser; |
| 57 | final ComponentName mComponent; |
| 58 | final IActivityManager mAm; |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 59 | final VoiceInteractionServiceInfo mInfo; |
| 60 | final ComponentName mSessionComponentName; |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 61 | final IWindowManager mIWindowManager; |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 62 | boolean mBound = false; |
| 63 | IVoiceInteractionService mService; |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 64 | |
Dianne Hackborn | ffeecb1 | 2015-02-25 11:08:11 -0800 | [diff] [blame] | 65 | VoiceInteractionSessionConnection mActiveSession; |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 66 | |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 67 | final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 68 | @Override |
| 69 | public void onReceive(Context context, Intent intent) { |
| 70 | if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) { |
| 71 | synchronized (mLock) { |
| 72 | if (mActiveSession != null && mActiveSession.mSession != null) { |
| 73 | try { |
| 74 | mActiveSession.mSession.closeSystemDialogs(); |
| 75 | } catch (RemoteException e) { |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | }; |
| 82 | |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 83 | final ServiceConnection mConnection = new ServiceConnection() { |
| 84 | @Override |
| 85 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 86 | synchronized (mLock) { |
| 87 | mService = IVoiceInteractionService.Stub.asInterface(service); |
Dianne Hackborn | fee756f | 2014-07-16 17:31:10 -0700 | [diff] [blame] | 88 | try { |
| 89 | mService.ready(); |
| 90 | } catch (RemoteException e) { |
| 91 | } |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void onServiceDisconnected(ComponentName name) { |
| 97 | mService = null; |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | VoiceInteractionManagerServiceImpl(Context context, Handler handler, Object lock, |
| 102 | int userHandle, ComponentName service) { |
| 103 | mContext = context; |
| 104 | mHandler = handler; |
| 105 | mLock = lock; |
| 106 | mUser = userHandle; |
| 107 | mComponent = service; |
| 108 | mAm = ActivityManagerNative.getDefault(); |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 109 | VoiceInteractionServiceInfo info; |
| 110 | try { |
| 111 | info = new VoiceInteractionServiceInfo(context.getPackageManager(), service); |
| 112 | } catch (PackageManager.NameNotFoundException e) { |
| 113 | Slog.w(TAG, "Voice interaction service not found: " + service); |
| 114 | mInfo = null; |
| 115 | mSessionComponentName = null; |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 116 | mIWindowManager = null; |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 117 | mValid = false; |
| 118 | return; |
| 119 | } |
| 120 | mInfo = info; |
| 121 | if (mInfo.getParseError() != null) { |
| 122 | Slog.w(TAG, "Bad voice interaction service: " + mInfo.getParseError()); |
| 123 | mSessionComponentName = null; |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 124 | mIWindowManager = null; |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 125 | mValid = false; |
| 126 | return; |
| 127 | } |
| 128 | mValid = true; |
| 129 | mSessionComponentName = new ComponentName(service.getPackageName(), |
| 130 | mInfo.getSessionService()); |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 131 | mIWindowManager = IWindowManager.Stub.asInterface( |
| 132 | ServiceManager.getService(Context.WINDOW_SERVICE)); |
| 133 | IntentFilter filter = new IntentFilter(); |
| 134 | filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
| 135 | mContext.registerReceiver(mBroadcastReceiver, filter, null, handler); |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Jorim Jaggi | 225d3b5 | 2015-04-01 11:18:57 -0700 | [diff] [blame^] | 138 | public boolean showSessionLocked(int callingPid, int callingUid, Bundle args, int flags, |
| 139 | IVoiceInteractionSessionShowCallback showCallback) { |
Dianne Hackborn | ffeecb1 | 2015-02-25 11:08:11 -0800 | [diff] [blame] | 140 | if (mActiveSession == null) { |
| 141 | mActiveSession = new VoiceInteractionSessionConnection(mLock, mSessionComponentName, |
| 142 | mUser, mContext, this, callingPid, callingUid); |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 143 | } |
Jorim Jaggi | 225d3b5 | 2015-04-01 11:18:57 -0700 | [diff] [blame^] | 144 | return mActiveSession.showLocked(args, flags, showCallback); |
Dianne Hackborn | ffeecb1 | 2015-02-25 11:08:11 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | public boolean hideSessionLocked(int callingPid, int callingUid) { |
| 148 | return mActiveSession.hideLocked(); |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 151 | public boolean deliverNewSessionLocked(int callingPid, int callingUid, IBinder token, |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 152 | IVoiceInteractionSession session, IVoiceInteractor interactor) { |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 153 | if (mActiveSession == null || token != mActiveSession.mToken) { |
| 154 | Slog.w(TAG, "deliverNewSession does not match active session"); |
| 155 | return false; |
| 156 | } |
Dianne Hackborn | ffeecb1 | 2015-02-25 11:08:11 -0800 | [diff] [blame] | 157 | mActiveSession.deliverNewSessionLocked(session, interactor); |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 158 | return true; |
| 159 | } |
| 160 | |
| 161 | public int startVoiceActivityLocked(int callingPid, int callingUid, IBinder token, |
| 162 | Intent intent, String resolvedType) { |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 163 | try { |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 164 | if (mActiveSession == null || token != mActiveSession.mToken) { |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 165 | Slog.w(TAG, "startVoiceActivity does not match active session"); |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 166 | return ActivityManager.START_CANCELED; |
| 167 | } |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 168 | intent = new Intent(intent); |
| 169 | intent.addCategory(Intent.CATEGORY_VOICE); |
| 170 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 171 | return mAm.startVoiceActivity(mComponent.getPackageName(), callingPid, callingUid, |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 172 | intent, resolvedType, mActiveSession.mSession, mActiveSession.mInteractor, |
Jeff Hao | 1b012d3 | 2014-08-20 10:35:34 -0700 | [diff] [blame] | 173 | 0, null, null, mUser); |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 174 | } catch (RemoteException e) { |
| 175 | throw new IllegalStateException("Unexpected remote error", e); |
| 176 | } |
| 177 | } |
| 178 | |
Dianne Hackborn | 3d07c94 | 2015-03-13 18:02:54 -0700 | [diff] [blame] | 179 | public void setKeepAwakeLocked(int callingPid, int callingUid, IBinder token, |
| 180 | boolean keepAwake) { |
| 181 | try { |
| 182 | if (mActiveSession == null || token != mActiveSession.mToken) { |
| 183 | Slog.w(TAG, "setKeepAwake does not match active session"); |
| 184 | return; |
| 185 | } |
| 186 | mAm.setVoiceKeepAwake(mActiveSession.mSession, keepAwake); |
| 187 | } catch (RemoteException e) { |
| 188 | throw new IllegalStateException("Unexpected remote error", e); |
| 189 | } |
| 190 | } |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 191 | |
| 192 | public void finishLocked(int callingPid, int callingUid, IBinder token) { |
| 193 | if (mActiveSession == null || token != mActiveSession.mToken) { |
| 194 | Slog.w(TAG, "finish does not match active session"); |
| 195 | return; |
| 196 | } |
| 197 | mActiveSession.cancel(); |
| 198 | mActiveSession = null; |
| 199 | } |
| 200 | |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 201 | public void dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 202 | if (!mValid) { |
| 203 | pw.print(" NOT VALID: "); |
| 204 | if (mInfo == null) { |
| 205 | pw.println("no info"); |
| 206 | } else { |
| 207 | pw.println(mInfo.getParseError()); |
| 208 | } |
| 209 | return; |
| 210 | } |
| 211 | pw.print(" mComponent="); pw.println(mComponent.flattenToShortString()); |
| 212 | pw.print(" Session service="); pw.println(mInfo.getSessionService()); |
| 213 | pw.print(" Settings activity="); pw.println(mInfo.getSettingsActivity()); |
| 214 | pw.print(" mBound="); pw.print(mBound); pw.print(" mService="); pw.println(mService); |
| 215 | if (mActiveSession != null) { |
| 216 | pw.println(" Active session:"); |
| 217 | mActiveSession.dump(" ", pw); |
| 218 | } |
| 219 | } |
| 220 | |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 221 | void startLocked() { |
| 222 | Intent intent = new Intent(VoiceInteractionService.SERVICE_INTERFACE); |
| 223 | intent.setComponent(mComponent); |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 224 | mBound = mContext.bindServiceAsUser(intent, mConnection, |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 225 | Context.BIND_AUTO_CREATE, new UserHandle(mUser)); |
Dianne Hackborn | 18f0d35 | 2014-04-25 17:06:18 -0700 | [diff] [blame] | 226 | if (!mBound) { |
| 227 | Slog.w(TAG, "Failed binding to voice interaction service " + mComponent); |
| 228 | } |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | void shutdownLocked() { |
Sandeep Siddhartha | 8ef360f | 2014-07-28 16:40:11 -0700 | [diff] [blame] | 232 | try { |
| 233 | if (mService != null) { |
| 234 | mService.shutdown(); |
| 235 | } |
| 236 | } catch (RemoteException e) { |
| 237 | Slog.w(TAG, "RemoteException in shutdown", e); |
| 238 | } |
| 239 | |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 240 | if (mBound) { |
| 241 | mContext.unbindService(mConnection); |
| 242 | mBound = false; |
| 243 | } |
Dianne Hackborn | c03c916 | 2014-05-02 10:45:59 -0700 | [diff] [blame] | 244 | if (mValid) { |
| 245 | mContext.unregisterReceiver(mBroadcastReceiver); |
| 246 | } |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 247 | } |
Sandeep Siddhartha | 6daae96 | 2014-07-21 10:31:34 -0700 | [diff] [blame] | 248 | |
| 249 | void notifySoundModelsChangedLocked() { |
| 250 | if (mService == null) { |
| 251 | Slog.w(TAG, "Not bound to voice interaction service " + mComponent); |
| 252 | } |
| 253 | try { |
| 254 | mService.soundModelsChanged(); |
| 255 | } catch (RemoteException e) { |
| 256 | Slog.w(TAG, "RemoteException while calling soundModelsChanged", e); |
| 257 | } |
| 258 | } |
Dianne Hackborn | ffeecb1 | 2015-02-25 11:08:11 -0800 | [diff] [blame] | 259 | |
| 260 | @Override |
| 261 | public void sessionConnectionGone(VoiceInteractionSessionConnection connection) { |
| 262 | synchronized (mLock) { |
| 263 | finishLocked(connection.mCallingPid, connection.mCallingUid, connection.mToken); |
| 264 | } |
| 265 | } |
Dianne Hackborn | 91097de | 2014-04-04 18:02:06 -0700 | [diff] [blame] | 266 | } |