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

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

Merge "Add permission checks and unhide the Hotword recognition APIs" into klp-dev

parents c8fb093a c73c1783
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package android.speech.hotword;

import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;

@@ -47,9 +46,10 @@ public interface HotwordRecognitionListener {

    /**
     * Called back when hotword is detected.
     * The action tells the client what action to take, post hotword-detection.
     *
     * @param intent for the activity to launch, post hotword detection.
     */
    void onHotwordRecognized(PendingIntent intent);
    void onHotwordRecognized(Intent intent);

    /**
     * Called when the HotwordRecognitionService encounters an error.
+29 −8
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.annotation.SdkConstant.SdkConstantType;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
@@ -31,7 +32,6 @@ import android.util.Log;
/**
 * This class provides a base class for hotword detection service implementations.
 * This class should be extended only if you wish to implement a new hotword recognizer.
 * {@hide}
 */
public abstract class HotwordRecognitionService extends Service {
    /**
@@ -45,8 +45,7 @@ public abstract class HotwordRecognitionService extends Service {
    private static final String TAG = "HotwordRecognitionService";

    /** Debugging flag */
    // TODO: Turn off.
    private static final boolean DBG = true;
    private static final boolean DBG = false;

    private static final int MSG_START_RECOGNITION = 1;
    private static final int MSG_STOP_RECOGNITION = 2;
@@ -160,7 +159,7 @@ public abstract class HotwordRecognitionService extends Service {

        public void startHotwordRecognition(IHotwordRecognitionListener listener) {
            if (DBG) Log.d(TAG, "startRecognition called by: " + listener.asBinder());
            if (mInternalService != null) {
            if (mInternalService != null && mInternalService.checkPermissions(listener)) {
                mInternalService.mHandler.sendMessage(
                        Message.obtain(mInternalService.mHandler, MSG_START_RECOGNITION, listener));
            }
@@ -168,7 +167,7 @@ public abstract class HotwordRecognitionService extends Service {

        public void stopHotwordRecognition(IHotwordRecognitionListener listener) {
            if (DBG) Log.d(TAG, "stopRecognition called by: " + listener.asBinder());
            if (mInternalService != null) {
            if (mInternalService != null && mInternalService.checkPermissions(listener)) {
                mInternalService.mHandler.sendMessage(
                        Message.obtain(mInternalService.mHandler, MSG_STOP_RECOGNITION, listener));
            }
@@ -179,6 +178,27 @@ public abstract class HotwordRecognitionService extends Service {
        }
    }

    /**
     * Checks whether the caller has sufficient permissions
     *
     * @param listener to send the error message to in case of error.
     * @return {@code true} if the caller has enough permissions, {@code false} otherwise.
     */
    private boolean checkPermissions(IHotwordRecognitionListener listener) {
        if (DBG) Log.d(TAG, "checkPermissions");
        if (checkCallingOrSelfPermission(android.Manifest.permission.HOTWORD_RECOGNITION) ==
                PackageManager.PERMISSION_GRANTED) {
            return true;
        }
        try {
            Log.e(TAG, "Recognition service called without HOTWORD_RECOGNITION permissions");
            listener.onHotwordError(HotwordRecognizer.ERROR_FAILED);
        } catch (RemoteException e) {
            Log.e(TAG, "onHotwordError(ERROR_FAILED) message failed", e);
        }
        return false;
    }

    /**
     * This class acts passes on the callbacks received from the Hotword service
     * to the listener.
@@ -216,10 +236,11 @@ public abstract class HotwordRecognitionService extends Service {

        /**
         * Called back when hotword is detected.
         * The action tells the client what action to take, post hotword-detection.
         *
         * @param intent for the activity to launch, post hotword detection.
         */
        public void onHotwordRecognized(PendingIntent intent) throws RemoteException {
            mListener.onHotwordRecognized(intent);
        public void onHotwordRecognized(Intent activityIntent) throws RemoteException {
            mListener.onHotwordRecognized(activityIntent);
        }

        /**
+7 −5
Original line number Diff line number Diff line
@@ -45,8 +45,7 @@ import java.util.Queue;
 */
public class HotwordRecognizer {
    /** DEBUG value to enable verbose debug prints */
    // TODO: Turn off.
    private final static boolean DBG = true;
    private final static boolean DBG = false;

    /** Log messages identifier */
    private static final String TAG = "HotwordRecognizer";
@@ -81,6 +80,9 @@ public class HotwordRecognizer {
    /** The service received concurrent start calls */
    public static final int ERROR_SERVICE_ALREADY_STARTED = 6;

    /** Hotword recognition is unavailable on the device */
    public static final int ERROR_UNAVAILABLE = 7;

    /** action codes */
    private static final int MSG_START = 1;
    private static final int MSG_STOP = 2;
@@ -354,7 +356,7 @@ public class HotwordRecognizer {
                        mInternalListener.onHotwordEvent(msg.arg1, (Bundle) msg.obj);
                        break;
                    case MSG_ON_RECOGNIZED:
                        mInternalListener.onHotwordRecognized((PendingIntent) msg.obj);
                        mInternalListener.onHotwordRecognized((Intent) msg.obj);
                        break;
                    case MSG_ON_ERROR:
                        mInternalListener.onHotwordError((Integer) msg.obj);
@@ -380,8 +382,8 @@ public class HotwordRecognizer {
        }

        @Override
        public void onHotwordRecognized(PendingIntent intent) throws RemoteException {
            Message.obtain(mInternalHandler, MSG_ON_RECOGNIZED, intent)
        public void onHotwordRecognized(Intent activityIntent) throws RemoteException {
            Message.obtain(mInternalHandler, MSG_ON_RECOGNIZED, activityIntent)
                    .sendToTarget();
        }

+4 −3
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@

package android.speech.hotword;

import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;

/**
@@ -47,9 +47,10 @@ oneway interface IHotwordRecognitionListener {

    /**
     * Called back when hotword is detected.
     * The action tells the client what action to take, post hotword-detection.
     *
     * @param intent for the activity to launch, post hotword detection.
     */
    void onHotwordRecognized(in PendingIntent intent);
    void onHotwordRecognized(in Intent intent);

    /**
     * Called when the HotwordRecognitionService encounters an error.
+7 −0
Original line number Diff line number Diff line
@@ -2444,6 +2444,13 @@
        android:description="@string/permdesc_accessNetworkConditions"
        android:protectionLevel="signature|system" />

    <!-- Allows an application to request HotwordRecognition.
         @hide This is not a third-party API (intended for system apps). -->
    <permission android:name="android.permission.HOTWORD_RECOGNITION"
        android:label="@string/permlab_hotwordRecognition"
        android:description="@string/permdesc_hotwordRecognition"
        android:protectionLevel="signature|system" />

    <!-- The system process is explicitly the only one allowed to launch the
         confirmation UI for full backup/restore -->
    <uses-permission android:name="android.permission.CONFIRM_FULL_BACKUP"/>
Loading