/services/java/com/android/server/RecognitionManagerService.java

https://github.com/aizuzi/platform_frameworks_base · Java · 167 lines · 132 code · 19 blank · 16 comment · 34 complexity · b398a7c3e08f6e872b9027d5ddb35cf1 MD5 · raw file

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