blob: 60d38ae1c050b5bbd7244cf01982b83ac1a1739b [file] [log] [blame]
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001/*
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
17package com.android.server;
18
19import com.android.internal.content.PackageMonitor;
20
Amith Yamasanif80a9b22012-09-24 19:38:28 -070021import android.app.AppGlobals;
22import android.content.BroadcastReceiver;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080023import android.content.ComponentName;
24import android.content.Context;
25import android.content.Intent;
Amith Yamasanif80a9b22012-09-24 19:38:28 -070026import android.content.IntentFilter;
27import android.content.pm.IPackageManager;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080028import android.content.pm.ResolveInfo;
29import android.content.pm.ServiceInfo;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080030import android.os.Binder;
Amith Yamasanif80a9b22012-09-24 19:38:28 -070031import android.os.RemoteException;
32import android.os.UserHandle;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080033import android.provider.Settings;
34import android.speech.RecognitionService;
35import android.text.TextUtils;
Joe Onorato8a9b2202010-02-26 18:56:32 -080036import android.util.Slog;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080037
38import java.util.List;
39
40public class RecognitionManagerService extends Binder {
41 final static String TAG = "RecognitionManagerService";
Amith Yamasanif80a9b22012-09-24 19:38:28 -070042
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 Hackborn21f1bd12010-02-19 17:02:21 -080049 class MyPackageMonitor extends PackageMonitor {
50 public void onSomePackagesChanged() {
Amith Yamasanif80a9b22012-09-24 19:38:28 -070051 int userHandle = getChangingUserId();
52 if (DEBUG) Slog.i(TAG, "onSomePackagesChanged user=" + userHandle);
53 ComponentName comp = getCurRecognizer(userHandle);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080054 if (comp == null) {
55 if (anyPackagesAppearing()) {
Amith Yamasanif80a9b22012-09-24 19:38:28 -070056 comp = findAvailRecognizer(null, userHandle);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080057 if (comp != null) {
Amith Yamasanif80a9b22012-09-24 19:38:28 -070058 setCurRecognizer(comp, userHandle);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080059 }
60 }
61 return;
62 }
Amith Yamasanif80a9b22012-09-24 19:38:28 -070063
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080064 int change = isPackageDisappearing(comp.getPackageName());
65 if (change == PACKAGE_PERMANENT_CHANGE
66 || change == PACKAGE_TEMPORARY_CHANGE) {
Amith Yamasanif80a9b22012-09-24 19:38:28 -070067 setCurRecognizer(findAvailRecognizer(null, userHandle), userHandle);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080068
69 } else if (isPackageModified(comp.getPackageName())) {
Amith Yamasanif80a9b22012-09-24 19:38:28 -070070 setCurRecognizer(findAvailRecognizer(comp.getPackageName(), userHandle),
71 userHandle);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080072 }
73 }
74 }
Amith Yamasanif80a9b22012-09-24 19:38:28 -070075
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080076 RecognitionManagerService(Context context) {
77 mContext = context;
78 mMonitor = new MyPackageMonitor();
Amith Yamasanif80a9b22012-09-24 19:38:28 -070079 mMonitor.register(context, null, UserHandle.ALL, true);
80 mIPm = AppGlobals.getPackageManager();
Dianne Hackbornd83a0962014-05-02 16:28:33 -070081 IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
82 filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
Amith Yamasanif80a9b22012-09-24 19:38:28 -070083 mContext.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL,
Dianne Hackbornd83a0962014-05-02 16:28:33 -070084 filter, null, null);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080085 }
Amith Yamasanif80a9b22012-09-24 19:38:28 -070086
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080087 public void systemReady() {
Amith Yamasanif80a9b22012-09-24 19:38:28 -070088 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 Yamasani596532d2013-01-18 11:04:09 -080094 ServiceInfo info = null;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080095 if (comp != null) {
Amith Yamasani596532d2013-01-18 11:04:09 -080096 // See if the current recognizer is still available.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080097 try {
Amith Yamasani596532d2013-01-18 11:04:09 -080098 info = mIPm.getServiceInfo(comp, 0, userHandle);
Amith Yamasanif80a9b22012-09-24 19:38:28 -070099 } catch (RemoteException e) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800100 }
Amith Yamasani596532d2013-01-18 11:04:09 -0800101 }
102 if (info == null) {
Amith Yamasanif80a9b22012-09-24 19:38:28 -0700103 comp = findAvailRecognizer(null, userHandle);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800104 if (comp != null) {
Amith Yamasanif80a9b22012-09-24 19:38:28 -0700105 setCurRecognizer(comp, userHandle);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800106 }
107 }
108 }
Amith Yamasanif80a9b22012-09-24 19:38:28 -0700109
110 ComponentName findAvailRecognizer(String prefPackage, int userHandle) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800111 List<ResolveInfo> available =
Amith Yamasanif80a9b22012-09-24 19:38:28 -0700112 mContext.getPackageManager().queryIntentServicesAsUser(
113 new Intent(RecognitionService.SERVICE_INTERFACE), 0, userHandle);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800114 int numAvailable = available.size();
Amith Yamasanif80a9b22012-09-24 19:38:28 -0700115
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800116 if (numAvailable == 0) {
Amith Yamasanif80a9b22012-09-24 19:38:28 -0700117 Slog.w(TAG, "no available voice recognition services found for user " + userHandle);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800118 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 Onorato8a9b2202010-02-26 18:56:32 -0800129 Slog.w(TAG, "more than one voice recognition service found, picking first");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800130 }
Amith Yamasanif80a9b22012-09-24 19:38:28 -0700131
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800132 ServiceInfo serviceInfo = available.get(0).serviceInfo;
133 return new ComponentName(serviceInfo.packageName, serviceInfo.name);
134 }
135 }
Amith Yamasanif80a9b22012-09-24 19:38:28 -0700136
137 ComponentName getCurRecognizer(int userHandle) {
138 String curRecognizer = Settings.Secure.getStringForUser(
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800139 mContext.getContentResolver(),
Amith Yamasanif80a9b22012-09-24 19:38:28 -0700140 Settings.Secure.VOICE_RECOGNITION_SERVICE, userHandle);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800141 if (TextUtils.isEmpty(curRecognizer)) {
142 return null;
143 }
Amith Yamasanif80a9b22012-09-24 19:38:28 -0700144 if (DEBUG) Slog.i(TAG, "getCurRecognizer curRecognizer=" + curRecognizer
145 + " user=" + userHandle);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800146 return ComponentName.unflattenFromString(curRecognizer);
147 }
Amith Yamasanif80a9b22012-09-24 19:38:28 -0700148
149 void setCurRecognizer(ComponentName comp, int userHandle) {
150 Settings.Secure.putStringForUser(mContext.getContentResolver(),
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800151 Settings.Secure.VOICE_RECOGNITION_SERVICE,
Amith Yamasanif80a9b22012-09-24 19:38:28 -0700152 comp != null ? comp.flattenToShortString() : "", userHandle);
153 if (DEBUG) Slog.i(TAG, "setCurRecognizer comp=" + comp
154 + " user=" + userHandle);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800155 }
Amith Yamasanif80a9b22012-09-24 19:38:28 -0700156
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 Hackborn21f1bd12010-02-19 17:02:21 -0800169}