Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4af3c7cf authored by Sandeep Siddhartha's avatar Sandeep Siddhartha Committed by Android (Google) Code Review
Browse files

Merge "Add capability APIs to VoiceInteractionService"

parents 5833bbe2 e912ac01
Loading
Loading
Loading
Loading
+56 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.service.voice;

import java.util.UUID;

/**
 * Properties of the DSP hardware on the device.
 * @hide
 */
public class DspInfo {
    /**
     * Unique voice engine Id (changes with each version).
     */
    public final UUID voiceEngineId;

    /**
     * Human readable voice detection engine implementor.
     */
    public final String voiceEngineImplementor;
    /**
     * Human readable voice detection engine description.
     */
    public final String voiceEngineDescription;
    /**
     * Human readable voice detection engine version
     */
    public final String voiceEngineVersion;
    /**
     * Rated power consumption when detection is active.
     */
    public final int powerConsumptionMw;

    public DspInfo(UUID voiceEngineId, String voiceEngineImplementor,
            String voiceEngineDescription, String voiceEngineVersion, int powerConsumptionMw) {
        this.voiceEngineId = voiceEngineId;
        this.voiceEngineImplementor = voiceEngineImplementor;
        this.voiceEngineDescription = voiceEngineDescription;
        this.voiceEngineVersion = voiceEngineVersion;
        this.powerConsumptionMw = powerConsumptionMw;
    }
}
+70 −1
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ public class KeyphraseEnrollmentInfo {
     * voice-enrollment-application}&gt;</code> tag.
     */
    private static final String VOICE_KEYPHRASE_META_DATA = "android.voice_enrollment";

    /**
     * Activity Action: Show activity for managing the keyphrases for hotword detection.
     * This needs to be defined by an activity that supports enrolling users for hotword/keyphrase
@@ -53,8 +52,24 @@ public class KeyphraseEnrollmentInfo {
     */
    public static final String ACTION_MANAGE_VOICE_KEYPHRASES =
            "com.android.intent.action.MANAGE_VOICE_KEYPHRASES";
    /**
     * Intent extra: The intent extra for un-enrolling a user for a particular keyphrase.
     */
    public static final String EXTRA_VOICE_KEYPHRASE_UNENROLL =
            "com.android.intent.extra.VOICE_KEYPHRASE_UNENROLL";
    /**
     * Intent extra: The hint text to be shown on the voice keyphrase management UI.
     */
    public static final String EXTRA_VOICE_KEYPHRASE_HINT_TEXT =
            "com.android.intent.extra.VOICE_KEYPHRASE_HINT_TEXT";
    /**
     * Intent extra: The voice locale to use while managing the keyphrase.
     */
    public static final String EXTRA_VOICE_KEYPHRASE_LOCALE =
            "com.android.intent.extra.VOICE_KEYPHRASE_LOCALE";

    private KeyphraseInfo[] mKeyphrases;
    private String mEnrollmentPackage;
    private String mParseError;

    public KeyphraseEnrollmentInfo(PackageManager pm) {
@@ -85,6 +100,7 @@ public class KeyphraseEnrollmentInfo {
                    // require the MANAGE_VOICE_KEYPHRASES permission.
                    continue;
                }
                mEnrollmentPackage = ai.packageName;
                found = true;
                break;
            } catch (PackageManager.NameNotFoundException e) {
@@ -93,6 +109,7 @@ public class KeyphraseEnrollmentInfo {
        }

        if (!found) {
            mKeyphrases = null;
            mParseError = "No suitable enrollment application found";
            return;
        }
@@ -164,4 +181,56 @@ public class KeyphraseEnrollmentInfo {
    public String getParseError() {
        return mParseError;
    }

    /**
     * Returns an intent to launch an activity that manages the given keyphrase
     * for the locale.
     *
     * @param enroll Indicates if the intent should enroll the user or un-enroll them.
     * @param keyphrase The keyphrase that the user needs to be enrolled to.
     * @param locale The locale for which the enrollment needs to be performed.
     * @return An {@link Intent} to manage the keyphrase. This can be null if managing the
     *         given keyphrase/locale combination isn't possible.
     */
    public Intent getManageKeyphraseIntent(boolean enroll, String keyphrase, String locale) {
        if (mEnrollmentPackage == null || mEnrollmentPackage.isEmpty()) {
            Log.w(TAG, "No enrollment application exists");
            return null;
        }

        if (isKeyphraseEnrollmentSupported(keyphrase, locale)) {
            Intent intent = new Intent(ACTION_MANAGE_VOICE_KEYPHRASES)
                    .setPackage(mEnrollmentPackage)
                    .putExtra(EXTRA_VOICE_KEYPHRASE_HINT_TEXT, keyphrase)
                    .putExtra(EXTRA_VOICE_KEYPHRASE_LOCALE, locale);
            if (!enroll) intent.putExtra(EXTRA_VOICE_KEYPHRASE_UNENROLL, true);
            return intent;
        }
        return null;
    }

    /**
     * Indicates if enrollment is supported for the given keyphrase & locale.
     *
     * @param keyphrase The keyphrase that the user needs to be enrolled to.
     * @param locale The locale for which the enrollment needs to be performed.
     * @return true, if an enrollment client supports the given keyphrase and the given locale.
     */
    public boolean isKeyphraseEnrollmentSupported(String keyphrase, String locale) {
        if (mKeyphrases == null || mKeyphrases.length == 0) {
            Log.w(TAG, "Enrollment application doesn't support keyphrases");
            return false;
        }
        for (KeyphraseInfo keyphraseInfo : mKeyphrases) {
            // Check if the given keyphrase is supported in the locale provided by
            // the enrollment application.
            String supportedKeyphrase = keyphraseInfo.keyphrase;
            if (supportedKeyphrase.equalsIgnoreCase(keyphrase)
                    && keyphraseInfo.supportedLocales.contains(locale)) {
                return true;
            }
        }
        Log.w(TAG, "Enrollment application doesn't support the given keyphrase");
        return false;
    }
}
+4 −1
Original line number Diff line number Diff line
@@ -2,7 +2,10 @@ package android.service.voice;

import android.util.ArraySet;

/** @hide */
/**
 * A Voice Keyphrase.
 * @hide
 */
public class KeyphraseInfo {
    public final int id;
    public final String keyphrase;
+23 −1
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package android.service.voice;

import android.annotation.SdkConstant;
import android.app.Instrumentation;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
@@ -65,6 +64,9 @@ public class VoiceInteractionService extends Service {

    IVoiceInteractionManagerService mSystemService;

    private DspInfo mDspInfo;
    private KeyphraseEnrollmentInfo mKeyphraseEnrollmentInfo;

    public void startSession(Bundle args) {
        try {
            mSystemService.startSession(mInterface, args);
@@ -77,6 +79,8 @@ public class VoiceInteractionService extends Service {
        super.onCreate();
        mSystemService = IVoiceInteractionManagerService.Stub.asInterface(
                ServiceManager.getService(Context.VOICE_INTERACTION_MANAGER_SERVICE));
        mKeyphraseEnrollmentInfo = new KeyphraseEnrollmentInfo(getPackageManager());
        // TODO(sansid): Read mDspInfo from the SoundTriggerModel API.
    }

    @Override
@@ -86,4 +90,22 @@ public class VoiceInteractionService extends Service {
        }
        return null;
    }

    /**
     * Indicates if always-on hotword detection is available for the given keyphrase and locale
     * on this system.
     * Availability implies that the hardware on this system is capable of listening for
     * the given keyphrase or not.
     * @param keyphrase The keyphrase whose availability is being checked.
     * @param locale The locale for which the availability is being checked.
     * @return Indicates if always-on hotword detection is available for the given keyphrase.
     * TODO(sansid): Unhide this.
     * @hide
     */
    public final boolean isAlwaysOnHotwordAvailable(String keyphrase, String locale) {
        // The available keyphrases is a combination of DSP availability and
        // the keyphrases that have an enrollment application for them.
        return mDspInfo != null
                && mKeyphraseEnrollmentInfo.isKeyphraseEnrollmentSupported(keyphrase, locale);
    }
}