blob: 3567cfc7a22d8b06a1606e558bbc8fae6c136afa [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
21import android.content.ComponentName;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.ResolveInfo;
25import android.content.pm.ServiceInfo;
26import android.content.pm.PackageManager.NameNotFoundException;
27import android.os.Binder;
28import android.provider.Settings;
29import android.speech.RecognitionService;
30import android.text.TextUtils;
Joe Onorato8a9b2202010-02-26 18:56:32 -080031import android.util.Slog;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080032
33import java.util.List;
34
35public class RecognitionManagerService extends Binder {
36 final static String TAG = "RecognitionManagerService";
37
38 final Context mContext;
39 final MyPackageMonitor mMonitor;
40
41 class MyPackageMonitor extends PackageMonitor {
42 public void onSomePackagesChanged() {
43 ComponentName comp = getCurRecognizer();
44 if (comp == null) {
45 if (anyPackagesAppearing()) {
46 comp = findAvailRecognizer(null);
47 if (comp != null) {
48 setCurRecognizer(comp);
49 }
50 }
51 return;
52 }
53
54 int change = isPackageDisappearing(comp.getPackageName());
55 if (change == PACKAGE_PERMANENT_CHANGE
56 || change == PACKAGE_TEMPORARY_CHANGE) {
57 setCurRecognizer(findAvailRecognizer(null));
58
59 } else if (isPackageModified(comp.getPackageName())) {
60 setCurRecognizer(findAvailRecognizer(comp.getPackageName()));
61 }
62 }
63 }
64
65 RecognitionManagerService(Context context) {
66 mContext = context;
67 mMonitor = new MyPackageMonitor();
Dianne Hackbornd0d75032012-04-19 23:12:09 -070068 mMonitor.register(context, null, true);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080069 }
70
71 public void systemReady() {
72 ComponentName comp = getCurRecognizer();
73 if (comp != null) {
74 // See if the current recognizer is no longer available.
75 try {
76 mContext.getPackageManager().getServiceInfo(comp, 0);
77 } catch (NameNotFoundException e) {
Bjorn Bringertddf32292012-04-16 18:16:37 +010078 comp = findAvailRecognizer(null);
79 if (comp != null) {
80 setCurRecognizer(comp);
81 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080082 }
83 } else {
84 comp = findAvailRecognizer(null);
85 if (comp != null) {
86 setCurRecognizer(comp);
87 }
88 }
89 }
90
91 ComponentName findAvailRecognizer(String prefPackage) {
92 List<ResolveInfo> available =
93 mContext.getPackageManager().queryIntentServices(
94 new Intent(RecognitionService.SERVICE_INTERFACE), 0);
95 int numAvailable = available.size();
96
97 if (numAvailable == 0) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080098 Slog.w(TAG, "no available voice recognition services found");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080099 return null;
100 } else {
101 if (prefPackage != null) {
102 for (int i=0; i<numAvailable; i++) {
103 ServiceInfo serviceInfo = available.get(i).serviceInfo;
104 if (prefPackage.equals(serviceInfo.packageName)) {
105 return new ComponentName(serviceInfo.packageName, serviceInfo.name);
106 }
107 }
108 }
109 if (numAvailable > 1) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800110 Slog.w(TAG, "more than one voice recognition service found, picking first");
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800111 }
112
113 ServiceInfo serviceInfo = available.get(0).serviceInfo;
114 return new ComponentName(serviceInfo.packageName, serviceInfo.name);
115 }
116 }
117
118 ComponentName getCurRecognizer() {
119 String curRecognizer = Settings.Secure.getString(
120 mContext.getContentResolver(),
121 Settings.Secure.VOICE_RECOGNITION_SERVICE);
122 if (TextUtils.isEmpty(curRecognizer)) {
123 return null;
124 }
125 return ComponentName.unflattenFromString(curRecognizer);
126 }
127
128 void setCurRecognizer(ComponentName comp) {
129 Settings.Secure.putString(mContext.getContentResolver(),
130 Settings.Secure.VOICE_RECOGNITION_SERVICE,
131 comp != null ? comp.flattenToShortString() : "");
132 }
133}