android.speech.RecognitionService Java Examples

The following examples show how to use android.speech.RecognitionService. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RecognitionServiceManager.java    From AlexaAndroid with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return true iff a RecognitionService with the given component name is installed
 */
public static boolean isRecognitionServiceInstalled(PackageManager pm, ComponentName componentName) {
    List<ResolveInfo> services = pm.queryIntentServices(
            new Intent(RecognitionService.SERVICE_INTERFACE), 0);
    for (ResolveInfo ri : services) {
        ServiceInfo si = ri.serviceInfo;
        if (si == null) {
            Log.i("serviceInfo == null");
            continue;
        }
        if (componentName.equals(new ComponentName(si.packageName, si.name))) {
            return true;
        }
    }
    return false;
}
 
Example #2
Source File: RecognitionServiceManager.java    From AlexaAndroid with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return list of currently installed RecognitionService component names flattened to short strings
 */
public List<String> getServices(PackageManager pm) {
    List<String> services = new ArrayList<>();
    int flags = 0;
    //int flags = PackageManager.GET_META_DATA;
    List<ResolveInfo> infos = pm.queryIntentServices(
            new Intent(RecognitionService.SERVICE_INTERFACE), flags);

    for (ResolveInfo ri : infos) {
        ServiceInfo si = ri.serviceInfo;
        if (si == null) {
            Log.i("serviceInfo == null");
            continue;
        }
        String pkg = si.packageName;
        String cls = si.name;
        // TODO: process si.metaData
        String component = (new ComponentName(pkg, cls)).flattenToShortString();
        if (!mCombosExcluded.contains(component)) {
            services.add(component);
        }
    }
    return services;
}
 
Example #3
Source File: RecognitionServiceManager.java    From AlexaAndroid with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return true iff a RecognitionService with the given component name is installed
 */
public static boolean isRecognitionServiceInstalled(PackageManager pm, ComponentName componentName) {
    List<ResolveInfo> services = pm.queryIntentServices(
            new Intent(RecognitionService.SERVICE_INTERFACE), 0);
    for (ResolveInfo ri : services) {
        ServiceInfo si = ri.serviceInfo;
        if (si == null) {
            Log.i("serviceInfo == null");
            continue;
        }
        if (componentName.equals(new ComponentName(si.packageName, si.name))) {
            return true;
        }
    }
    return false;
}
 
Example #4
Source File: RecognitionServiceManager.java    From AlexaAndroid with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return list of currently installed RecognitionService component names flattened to short strings
 */
public List<String> getServices(PackageManager pm) {
    List<String> services = new ArrayList<>();
    int flags = 0;
    //int flags = PackageManager.GET_META_DATA;
    List<ResolveInfo> infos = pm.queryIntentServices(
            new Intent(RecognitionService.SERVICE_INTERFACE), flags);

    for (ResolveInfo ri : infos) {
        ServiceInfo si = ri.serviceInfo;
        if (si == null) {
            Log.i("serviceInfo == null");
            continue;
        }
        String pkg = si.packageName;
        String cls = si.name;
        // TODO: process si.metaData
        String component = (new ComponentName(pkg, cls)).flattenToShortString();
        if (!mCombosExcluded.contains(component)) {
            services.add(component);
        }
    }
    return services;
}
 
Example #5
Source File: RecognitionServiceManager.java    From speechutils with Apache License 2.0 6 votes vote down vote up
/**
 * @return true iff a RecognitionService with the given component name is installed
 */
public static boolean isRecognitionServiceInstalled(PackageManager pm, ComponentName componentName) {
    List<ResolveInfo> services = pm.queryIntentServices(
            new Intent(RecognitionService.SERVICE_INTERFACE), 0);
    for (ResolveInfo ri : services) {
        ServiceInfo si = ri.serviceInfo;
        if (si == null) {
            Log.i("serviceInfo == null");
            continue;
        }
        if (componentName.equals(new ComponentName(si.packageName, si.name))) {
            return true;
        }
    }
    return false;
}
 
Example #6
Source File: RecognitionServiceManager.java    From speechutils with Apache License 2.0 6 votes vote down vote up
/**
 * @return list of currently installed RecognitionService component names flattened to short strings
 */
public List<String> getServices(PackageManager pm) {
    List<String> services = new ArrayList<>();
    int flags = 0;
    //int flags = PackageManager.GET_META_DATA;
    List<ResolveInfo> infos = pm.queryIntentServices(
            new Intent(RecognitionService.SERVICE_INTERFACE), flags);

    for (ResolveInfo ri : infos) {
        ServiceInfo si = ri.serviceInfo;
        if (si == null) {
            Log.i("serviceInfo == null");
            continue;
        }
        String pkg = si.packageName;
        String cls = si.name;
        // TODO: process si.metaData
        String component = (new ComponentName(pkg, cls)).flattenToShortString();
        if (!mCombosExcluded.contains(component)) {
            services.add(component);
        }
    }
    return services;
}
 
Example #7
Source File: RecognizerChecker.java    From dialogflow-android-client with Apache License 2.0 6 votes vote down vote up
private static ComponentName findRecognizerByPackage(final Context context, final String prefPackage) {
    final PackageManager pm = context.getPackageManager();
    final List<ResolveInfo> available = pm != null ? pm.queryIntentServices(new Intent(RecognitionService.SERVICE_INTERFACE), 0) : new LinkedList<ResolveInfo>();
    final int numAvailable = available.size();

    if (numAvailable == 0) {
        // no available voice recognition services found
        return null;
    } else {
        if (prefPackage != null) {
            for (final ResolveInfo anAvailable : available) {
                final ServiceInfo serviceInfo = anAvailable.serviceInfo;

                if (serviceInfo != null && prefPackage.equals(serviceInfo.packageName)) {
                    return new ComponentName(serviceInfo.packageName, serviceInfo.name);
                }
            }
        }
        // Do not pick up first available, but use default one
        return null;
    }
}
 
Example #8
Source File: RecognitionServiceManager.java    From speechutils with Apache License 2.0 5 votes vote down vote up
public static String getSettingsActivity(Context context, ServiceInfo si)
        throws XmlPullParserException, IOException {
    PackageManager pm = context.getPackageManager();
    XmlResourceParser parser = null;
    try {
        parser = si.loadXmlMetaData(pm, RecognitionService.SERVICE_META_DATA);
        if (parser == null) {
            throw new XmlPullParserException("No " + RecognitionService.SERVICE_META_DATA + " meta-data");
        }

        int type;
        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
                && type != XmlPullParser.START_TAG) {
        }

        String nodeName = parser.getName();
        if (!"recognition-service".equals(nodeName)) {
            throw new XmlPullParserException(
                    "Meta-data does not start with recognition-service tag");
        }

        return parser.getAttributeValue("http://schemas.android.com/apk/res/android",
                "settingsActivity");
    } finally {
        if (parser != null) parser.close();
    }
}
 
Example #9
Source File: AbstractRecognitionService.java    From speechutils with Apache License 2.0 5 votes vote down vote up
/**
 * Stops the recording and closes the connection to the server.
 */
@Override
protected void onCancel(RecognitionService.Callback listener) {
    Log.i("onCancel");
    disconnectAndStopRecording();
    // Send empty results if recognition is cancelled
    // TEST: if it works with Google Translate and Slide IT
    onResults(new Bundle());
}
 
Example #10
Source File: SpeechRecognition.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * This method must be called before any instance of SpeechRecognition can be created. It will
 * query Android's package manager to find a suitable speech recognition provider that supports
 * continuous recognition.
 */
// TODO(crbug.com/635567): Fix this properly.
@SuppressLint("WrongConstant")
public static boolean initialize(Context context) {
    if (!SpeechRecognizer.isRecognitionAvailable(context)) return false;

    PackageManager pm = context.getPackageManager();
    Intent intent = new Intent(RecognitionService.SERVICE_INTERFACE);
    final List<ResolveInfo> list = pm.queryIntentServices(intent, PackageManager.GET_SERVICES);

    for (ResolveInfo resolve : list) {
        ServiceInfo service = resolve.serviceInfo;

        if (!service.packageName.equals(PROVIDER_PACKAGE_NAME)) continue;

        if (PackageUtils.getPackageVersion(context, service.packageName)
                < PROVIDER_MIN_VERSION) {
            continue;
        }

        sRecognitionProvider = new ComponentName(service.packageName, service.name);

        return true;
    }

    // If we reach this point, we failed to find a suitable recognition provider.
    return false;
}
 
Example #11
Source File: SpeechRecognition.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean initialize(Context context) {
    if (!SpeechRecognizer.isRecognitionAvailable(context))
        return false;

    PackageManager pm = context.getPackageManager();
    Intent intent = new Intent(RecognitionService.SERVICE_INTERFACE);
    final List<ResolveInfo> list = pm.queryIntentServices(intent, PackageManager.GET_SERVICES);

    for (ResolveInfo resolve : list) {
        ServiceInfo service = resolve.serviceInfo;

        if (!service.packageName.equals(PROVIDER_PACKAGE_NAME))
            continue;

        int versionCode;
        try {
            versionCode = pm.getPackageInfo(service.packageName, 0).versionCode;
        } catch (NameNotFoundException e) {
            continue;
        }

        if (versionCode < PROVIDER_MIN_VERSION)
            continue;

        mRecognitionProvider = new ComponentName(service.packageName, service.name);

        return true;
    }

    // If we reach this point, we failed to find a suitable recognition provider.
    return false;
}
 
Example #12
Source File: SpeechRecognition.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static boolean initialize(Context context) {
    if (!SpeechRecognizer.isRecognitionAvailable(context))
        return false;

    PackageManager pm = context.getPackageManager();
    Intent intent = new Intent(RecognitionService.SERVICE_INTERFACE);
    final List<ResolveInfo> list = pm.queryIntentServices(intent, PackageManager.GET_SERVICES);

    for (ResolveInfo resolve : list) {
        ServiceInfo service = resolve.serviceInfo;

        if (!service.packageName.equals(PROVIDER_PACKAGE_NAME))
            continue;

        int versionCode;
        try {
            versionCode = pm.getPackageInfo(service.packageName, 0).versionCode;
        } catch (NameNotFoundException e) {
            continue;
        }

        if (versionCode < PROVIDER_MIN_VERSION)
            continue;

        mRecognitionProvider = new ComponentName(service.packageName, service.name);

        return true;
    }

    // If we reach this point, we failed to find a suitable recognition provider.
    return false;
}
 
Example #13
Source File: SelfAwareConditions.java    From Saiy-PS with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Utility method to construct the {@link SpeechRecognizer} instance
 *
 * @param recognitionListener the {@link SaiyRecognitionListener}
 * @return the {@link Pair} containing the {@link SpeechRecognizer} and Intent with extras
 */
public Pair<SpeechRecognizer, Intent> getNativeRecognition(
        @NonNull final SaiyRecognitionListener recognitionListener) {

    final long then = System.nanoTime();

    SpeechRecognizer recognizer = null;

    final List<ResolveInfo> recognitionServices = mContext.getPackageManager().queryIntentServices(
            new Intent(RecognitionService.SERVICE_INTERFACE), 0);

    if (UtilsList.notNaked(recognitionServices)) {

        String packageName;
        String serviceName;
        ServiceInfo serviceInfo;

        for (final ResolveInfo info : recognitionServices) {

            serviceInfo = info.serviceInfo;
            packageName = serviceInfo.packageName;
            serviceName = serviceInfo.name;

            if (packageName != null && serviceName != null) {
                if (DEBUG) {
                    MyLog.i(CLS_NAME, "getNativeRecognition: Recognizer: " + packageName + " : " + serviceName);
                }

                if (packageName.startsWith(PACKAGE_NAME_GOOGLE)) {
                    recognizer = SpeechRecognizer.createSpeechRecognizer(mContext,
                            new ComponentName(packageName, serviceName));
                    break;
                }
            }
        }

    } else {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getNativeRecognition: recognitionServices: naked");
        }

        return null;
    }

    if (recognizer == null) {
        if (DEBUG) {
            MyLog.w(CLS_NAME, "getNativeRecognition: recognizer: null");
        }

        return null;
    }

    recognizer.setRecognitionListener(recognitionListener);

    if (DEBUG) {
        MyLog.getElapsed(CLS_NAME, "getNativeRecognition", then);
    }

    return new Pair<>(recognizer, getNativeIntent());
}
 
Example #14
Source File: AbstractRecognitionService.java    From speechutils with Apache License 2.0 4 votes vote down vote up
/**
 * Stops the recording and informs the server that no more packages are coming.
 */
@Override
protected void onStopListening(RecognitionService.Callback listener) {
    Log.i("onStopListening");
    onEndOfSpeech();
}
 
Example #15
Source File: IntentUtils.java    From speechutils with Apache License 2.0 3 votes vote down vote up
/**
 * Checks whether a speech recognition service is available on the system. If this method
 * returns {@code false}, {@link SpeechRecognizer#createSpeechRecognizer(Context, ComponentName)}
 * will fail.
 * Similar to {@link SpeechRecognizer#isRecognitionAvailable(Context)} but supports
 * restricting the intent query by component name.
 * <p/>
 * TODO: propose to add this to SpeechRecognizer
 * TODO: clarify what does "will fail" mean
 *
 * @param context       with which {@code SpeechRecognizer} will be created
 * @param componentName of the recognition service
 * @return {@code true} if recognition is available, {@code false} otherwise
 */
public static boolean isRecognitionAvailable(final Context context, ComponentName componentName) {
    Intent intent = new Intent(RecognitionService.SERVICE_INTERFACE);
    intent.setComponent(componentName);
    final List<ResolveInfo> list = context.getPackageManager().queryIntentServices(intent, 0);
    return list.size() != 0;
}
 
Example #16
Source File: DeviceInfo.java    From Saiy-PS with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Get the default voice recognition provider
 *
 * @param ctx the application context
 * @return the default or an empty string if one is not present
 */
public static String getDefaultVRProvider(@NonNull final Context ctx) {
    final List<ResolveInfo> services = ctx.getPackageManager().queryIntentServices(
            new Intent(RecognitionService.SERVICE_INTERFACE), 0);
    return UtilsList.notNaked(services) ? services.get(0).serviceInfo.packageName : "";
}