am b8c2ac3e: Merge "Watch for voice recognition service changes for all users" into jb-mr1-dev
* commit 'b8c2ac3ecb6ee6319df65a88f42141c070b46163': Watch for voice recognition service changes for all users
This commit is contained in:
@@ -18,13 +18,18 @@ package com.android.server;
|
|||||||
|
|
||||||
import com.android.internal.content.PackageMonitor;
|
import com.android.internal.content.PackageMonitor;
|
||||||
|
|
||||||
|
import android.app.AppGlobals;
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
|
import android.content.pm.IPackageManager;
|
||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
import android.content.pm.ServiceInfo;
|
import android.content.pm.ServiceInfo;
|
||||||
import android.content.pm.PackageManager.NameNotFoundException;
|
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
|
import android.os.RemoteException;
|
||||||
|
import android.os.UserHandle;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.speech.RecognitionService;
|
import android.speech.RecognitionService;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
@@ -34,68 +39,82 @@ import java.util.List;
|
|||||||
|
|
||||||
public class RecognitionManagerService extends Binder {
|
public class RecognitionManagerService extends Binder {
|
||||||
final static String TAG = "RecognitionManagerService";
|
final static String TAG = "RecognitionManagerService";
|
||||||
|
|
||||||
final Context mContext;
|
private final Context mContext;
|
||||||
final MyPackageMonitor mMonitor;
|
private final MyPackageMonitor mMonitor;
|
||||||
|
private final IPackageManager mIPm;
|
||||||
|
|
||||||
|
private static final boolean DEBUG = false;
|
||||||
|
|
||||||
class MyPackageMonitor extends PackageMonitor {
|
class MyPackageMonitor extends PackageMonitor {
|
||||||
public void onSomePackagesChanged() {
|
public void onSomePackagesChanged() {
|
||||||
ComponentName comp = getCurRecognizer();
|
int userHandle = getChangingUserId();
|
||||||
|
if (DEBUG) Slog.i(TAG, "onSomePackagesChanged user=" + userHandle);
|
||||||
|
ComponentName comp = getCurRecognizer(userHandle);
|
||||||
if (comp == null) {
|
if (comp == null) {
|
||||||
if (anyPackagesAppearing()) {
|
if (anyPackagesAppearing()) {
|
||||||
comp = findAvailRecognizer(null);
|
comp = findAvailRecognizer(null, userHandle);
|
||||||
if (comp != null) {
|
if (comp != null) {
|
||||||
setCurRecognizer(comp);
|
setCurRecognizer(comp, userHandle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int change = isPackageDisappearing(comp.getPackageName());
|
int change = isPackageDisappearing(comp.getPackageName());
|
||||||
if (change == PACKAGE_PERMANENT_CHANGE
|
if (change == PACKAGE_PERMANENT_CHANGE
|
||||||
|| change == PACKAGE_TEMPORARY_CHANGE) {
|
|| change == PACKAGE_TEMPORARY_CHANGE) {
|
||||||
setCurRecognizer(findAvailRecognizer(null));
|
setCurRecognizer(findAvailRecognizer(null, userHandle), userHandle);
|
||||||
|
|
||||||
} else if (isPackageModified(comp.getPackageName())) {
|
} else if (isPackageModified(comp.getPackageName())) {
|
||||||
setCurRecognizer(findAvailRecognizer(comp.getPackageName()));
|
setCurRecognizer(findAvailRecognizer(comp.getPackageName(), userHandle),
|
||||||
|
userHandle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RecognitionManagerService(Context context) {
|
RecognitionManagerService(Context context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
mMonitor = new MyPackageMonitor();
|
mMonitor = new MyPackageMonitor();
|
||||||
mMonitor.register(context, null, true);
|
mMonitor.register(context, null, UserHandle.ALL, true);
|
||||||
|
mIPm = AppGlobals.getPackageManager();
|
||||||
|
mContext.registerReceiverAsUser(mBroadcastReceiver, UserHandle.ALL,
|
||||||
|
new IntentFilter(Intent.ACTION_BOOT_COMPLETED), null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void systemReady() {
|
public void systemReady() {
|
||||||
ComponentName comp = getCurRecognizer();
|
initForUser(UserHandle.USER_OWNER);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initForUser(int userHandle) {
|
||||||
|
if (DEBUG) Slog.i(TAG, "initForUser user=" + userHandle);
|
||||||
|
ComponentName comp = getCurRecognizer(userHandle);
|
||||||
if (comp != null) {
|
if (comp != null) {
|
||||||
// See if the current recognizer is no longer available.
|
// See if the current recognizer is no longer available.
|
||||||
try {
|
try {
|
||||||
mContext.getPackageManager().getServiceInfo(comp, 0);
|
mIPm.getServiceInfo(comp, 0, userHandle);
|
||||||
} catch (NameNotFoundException e) {
|
} catch (RemoteException e) {
|
||||||
comp = findAvailRecognizer(null);
|
comp = findAvailRecognizer(null, userHandle);
|
||||||
if (comp != null) {
|
if (comp != null) {
|
||||||
setCurRecognizer(comp);
|
setCurRecognizer(comp, userHandle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
comp = findAvailRecognizer(null);
|
comp = findAvailRecognizer(null, userHandle);
|
||||||
if (comp != null) {
|
if (comp != null) {
|
||||||
setCurRecognizer(comp);
|
setCurRecognizer(comp, userHandle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ComponentName findAvailRecognizer(String prefPackage) {
|
ComponentName findAvailRecognizer(String prefPackage, int userHandle) {
|
||||||
List<ResolveInfo> available =
|
List<ResolveInfo> available =
|
||||||
mContext.getPackageManager().queryIntentServices(
|
mContext.getPackageManager().queryIntentServicesAsUser(
|
||||||
new Intent(RecognitionService.SERVICE_INTERFACE), 0);
|
new Intent(RecognitionService.SERVICE_INTERFACE), 0, userHandle);
|
||||||
int numAvailable = available.size();
|
int numAvailable = available.size();
|
||||||
|
|
||||||
if (numAvailable == 0) {
|
if (numAvailable == 0) {
|
||||||
Slog.w(TAG, "no available voice recognition services found");
|
Slog.w(TAG, "no available voice recognition services found for user " + userHandle);
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
if (prefPackage != null) {
|
if (prefPackage != null) {
|
||||||
@@ -109,25 +128,42 @@ public class RecognitionManagerService extends Binder {
|
|||||||
if (numAvailable > 1) {
|
if (numAvailable > 1) {
|
||||||
Slog.w(TAG, "more than one voice recognition service found, picking first");
|
Slog.w(TAG, "more than one voice recognition service found, picking first");
|
||||||
}
|
}
|
||||||
|
|
||||||
ServiceInfo serviceInfo = available.get(0).serviceInfo;
|
ServiceInfo serviceInfo = available.get(0).serviceInfo;
|
||||||
return new ComponentName(serviceInfo.packageName, serviceInfo.name);
|
return new ComponentName(serviceInfo.packageName, serviceInfo.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ComponentName getCurRecognizer() {
|
ComponentName getCurRecognizer(int userHandle) {
|
||||||
String curRecognizer = Settings.Secure.getString(
|
String curRecognizer = Settings.Secure.getStringForUser(
|
||||||
mContext.getContentResolver(),
|
mContext.getContentResolver(),
|
||||||
Settings.Secure.VOICE_RECOGNITION_SERVICE);
|
Settings.Secure.VOICE_RECOGNITION_SERVICE, userHandle);
|
||||||
if (TextUtils.isEmpty(curRecognizer)) {
|
if (TextUtils.isEmpty(curRecognizer)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if (DEBUG) Slog.i(TAG, "getCurRecognizer curRecognizer=" + curRecognizer
|
||||||
|
+ " user=" + userHandle);
|
||||||
return ComponentName.unflattenFromString(curRecognizer);
|
return ComponentName.unflattenFromString(curRecognizer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setCurRecognizer(ComponentName comp) {
|
void setCurRecognizer(ComponentName comp, int userHandle) {
|
||||||
Settings.Secure.putString(mContext.getContentResolver(),
|
Settings.Secure.putStringForUser(mContext.getContentResolver(),
|
||||||
Settings.Secure.VOICE_RECOGNITION_SERVICE,
|
Settings.Secure.VOICE_RECOGNITION_SERVICE,
|
||||||
comp != null ? comp.flattenToShortString() : "");
|
comp != null ? comp.flattenToShortString() : "", userHandle);
|
||||||
|
if (DEBUG) Slog.i(TAG, "setCurRecognizer comp=" + comp
|
||||||
|
+ " user=" + userHandle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
String action = intent.getAction();
|
||||||
|
if (DEBUG) Slog.i(TAG, "received " + action);
|
||||||
|
if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
|
||||||
|
int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
|
||||||
|
if (userHandle > 0) {
|
||||||
|
initForUser(userHandle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user