Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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; |
| 18 | |
| 19 | import com.android.internal.content.PackageMonitor; |
| 20 | |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 21 | import android.app.AppGlobals; |
| 22 | import android.content.BroadcastReceiver; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 26 | import android.content.IntentFilter; |
| 27 | import android.content.pm.IPackageManager; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 28 | import android.content.pm.ResolveInfo; |
| 29 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 30 | import android.os.Binder; |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 31 | import android.os.RemoteException; |
| 32 | import android.os.UserHandle; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 33 | import android.provider.Settings; |
| 34 | import android.speech.RecognitionService; |
| 35 | import android.text.TextUtils; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 36 | import android.util.Slog; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 37 | |
| 38 | import java.util.List; |
| 39 | |
| 40 | public class RecognitionManagerService extends Binder { |
| 41 | final static String TAG = "RecognitionManagerService"; |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 42 | |
| 43 | private final Context mContext; |
| 44 | private final MyPackageMonitor mMonitor; |
| 45 | private final IPackageManager mIPm; |
| 46 | |
| 47 | private static final boolean DEBUG = false; |
| 48 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 49 | class MyPackageMonitor extends PackageMonitor { |
| 50 | public void onSomePackagesChanged() { |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 51 | int userHandle = getChangingUserId(); |
| 52 | if (DEBUG) Slog.i(TAG, "onSomePackagesChanged user=" + userHandle); |
| 53 | ComponentName comp = getCurRecognizer(userHandle); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 54 | if (comp == null) { |
| 55 | if (anyPackagesAppearing()) { |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 56 | comp = findAvailRecognizer(null, userHandle); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 57 | if (comp != null) { |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 58 | setCurRecognizer(comp, userHandle); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | return; |
| 62 | } |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 63 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 64 | int change = isPackageDisappearing(comp.getPackageName()); |
| 65 | if (change == PACKAGE_PERMANENT_CHANGE |
| 66 | || change == PACKAGE_TEMPORARY_CHANGE) { |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 67 | setCurRecognizer(findAvailRecognizer(null, userHandle), userHandle); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 68 | |
| 69 | } else if (isPackageModified(comp.getPackageName())) { |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 70 | setCurRecognizer(findAvailRecognizer(comp.getPackageName(), userHandle), |
| 71 | userHandle); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 75 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 76 | RecognitionManagerService(Context context) { |
| 77 | mContext = context; |
| 78 | mMonitor = new MyPackageMonitor(); |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 79 | mMonitor.register(context, null, UserHandle.ALL, true); |
| 80 | mIPm = AppGlobals.getPackageManager(); |
Dianne Hackborn | d83a096 | 2014-05-02 16:28:33 -0700 | [diff] [blame] | 81 | IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); |
| 82 | filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 83 | mContext.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL, |
Dianne Hackborn | d83a096 | 2014-05-02 16:28:33 -0700 | [diff] [blame] | 84 | filter, null, null); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 85 | } |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 86 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 87 | public void systemReady() { |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 88 | initForUser(UserHandle.USER_OWNER); |
| 89 | } |
| 90 | |
| 91 | private void initForUser(int userHandle) { |
| 92 | if (DEBUG) Slog.i(TAG, "initForUser user=" + userHandle); |
| 93 | ComponentName comp = getCurRecognizer(userHandle); |
Amith Yamasani | 596532d | 2013-01-18 11:04:09 -0800 | [diff] [blame] | 94 | ServiceInfo info = null; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 95 | if (comp != null) { |
Amith Yamasani | 596532d | 2013-01-18 11:04:09 -0800 | [diff] [blame] | 96 | // See if the current recognizer is still available. |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 97 | try { |
Amith Yamasani | 596532d | 2013-01-18 11:04:09 -0800 | [diff] [blame] | 98 | info = mIPm.getServiceInfo(comp, 0, userHandle); |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 99 | } catch (RemoteException e) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 100 | } |
Amith Yamasani | 596532d | 2013-01-18 11:04:09 -0800 | [diff] [blame] | 101 | } |
| 102 | if (info == null) { |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 103 | comp = findAvailRecognizer(null, userHandle); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 104 | if (comp != null) { |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 105 | setCurRecognizer(comp, userHandle); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | } |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 109 | |
| 110 | ComponentName findAvailRecognizer(String prefPackage, int userHandle) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 111 | List<ResolveInfo> available = |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 112 | mContext.getPackageManager().queryIntentServicesAsUser( |
| 113 | new Intent(RecognitionService.SERVICE_INTERFACE), 0, userHandle); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 114 | int numAvailable = available.size(); |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 115 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 116 | if (numAvailable == 0) { |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 117 | Slog.w(TAG, "no available voice recognition services found for user " + userHandle); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 118 | return null; |
| 119 | } else { |
| 120 | if (prefPackage != null) { |
| 121 | for (int i=0; i<numAvailable; i++) { |
| 122 | ServiceInfo serviceInfo = available.get(i).serviceInfo; |
| 123 | if (prefPackage.equals(serviceInfo.packageName)) { |
| 124 | return new ComponentName(serviceInfo.packageName, serviceInfo.name); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | if (numAvailable > 1) { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 129 | Slog.w(TAG, "more than one voice recognition service found, picking first"); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 130 | } |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 131 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 132 | ServiceInfo serviceInfo = available.get(0).serviceInfo; |
| 133 | return new ComponentName(serviceInfo.packageName, serviceInfo.name); |
| 134 | } |
| 135 | } |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 136 | |
| 137 | ComponentName getCurRecognizer(int userHandle) { |
| 138 | String curRecognizer = Settings.Secure.getStringForUser( |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 139 | mContext.getContentResolver(), |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 140 | Settings.Secure.VOICE_RECOGNITION_SERVICE, userHandle); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 141 | if (TextUtils.isEmpty(curRecognizer)) { |
| 142 | return null; |
| 143 | } |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 144 | if (DEBUG) Slog.i(TAG, "getCurRecognizer curRecognizer=" + curRecognizer |
| 145 | + " user=" + userHandle); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 146 | return ComponentName.unflattenFromString(curRecognizer); |
| 147 | } |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 148 | |
| 149 | void setCurRecognizer(ComponentName comp, int userHandle) { |
| 150 | Settings.Secure.putStringForUser(mContext.getContentResolver(), |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 151 | Settings.Secure.VOICE_RECOGNITION_SERVICE, |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 152 | comp != null ? comp.flattenToShortString() : "", userHandle); |
| 153 | if (DEBUG) Slog.i(TAG, "setCurRecognizer comp=" + comp |
| 154 | + " user=" + userHandle); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 155 | } |
Amith Yamasani | f80a9b2 | 2012-09-24 19:38:28 -0700 | [diff] [blame] | 156 | |
| 157 | BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 158 | public void onReceive(Context context, Intent intent) { |
| 159 | String action = intent.getAction(); |
| 160 | if (DEBUG) Slog.i(TAG, "received " + action); |
| 161 | if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { |
| 162 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 163 | if (userHandle > 0) { |
| 164 | initForUser(userHandle); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | }; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 169 | } |