1 /**
2  * Copyright (C) 2014 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 
17 package com.android.server.soundtrigger;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.hardware.soundtrigger.IRecognitionStatusCallback;
22 import android.hardware.soundtrigger.ModelParams;
23 import android.hardware.soundtrigger.SoundTrigger;
24 import android.hardware.soundtrigger.SoundTrigger.Keyphrase;
25 import android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel;
26 import android.hardware.soundtrigger.SoundTrigger.ModelParamRange;
27 import android.hardware.soundtrigger.SoundTrigger.ModuleProperties;
28 import android.hardware.soundtrigger.SoundTrigger.RecognitionConfig;
29 import android.os.IBinder;
30 
31 import com.android.server.voiceinteraction.VoiceInteractionManagerService;
32 
33 import java.io.FileDescriptor;
34 import java.io.PrintWriter;
35 
36 /**
37  * Provides a local service for managing voice-related recoginition models. This is primarily used
38  * by the {@link VoiceInteractionManagerService}.
39  */
40 public interface SoundTriggerInternal {
41     /**
42      * Return codes for {@link Session#startRecognition(int, KeyphraseSoundModel,
43      *      IRecognitionStatusCallback, RecognitionConfig)},
44      * {@link Session#stopRecognition(int, IRecognitionStatusCallback)}
45      */
46     int STATUS_ERROR = SoundTrigger.STATUS_ERROR;
47     int STATUS_OK = SoundTrigger.STATUS_OK;
48 
attach(@onNull IBinder client)49     Session attach(@NonNull IBinder client);
50 
51     /**
52      * Dumps service-wide information.
53      */
dump(FileDescriptor fd, PrintWriter pw, String[] args)54     void dump(FileDescriptor fd, PrintWriter pw, String[] args);
55 
56     interface Session {
57         /**
58          * Starts recognition for the given keyphraseId.
59          *
60          * @param keyphraseId The identifier of the keyphrase for which
61          *                    the recognition is to be started.
62          * @param soundModel  The sound model to use for recognition.
63          * @param listener    The listener for the recognition events related to the given
64          *                    keyphrase.
65          * @param runInBatterySaverMode flag that indicates whether the recognition should continue
66          *                              after battery saver mode is enabled.
67          * @return One of {@link #STATUS_ERROR} or {@link #STATUS_OK}.
68          */
startRecognition(int keyphraseId, KeyphraseSoundModel soundModel, IRecognitionStatusCallback listener, RecognitionConfig recognitionConfig, boolean runInBatterySaverMode)69         int startRecognition(int keyphraseId, KeyphraseSoundModel soundModel,
70                 IRecognitionStatusCallback listener, RecognitionConfig recognitionConfig,
71                 boolean runInBatterySaverMode);
72 
73         /**
74          * Stops recognition for the given {@link Keyphrase} if a recognition is
75          * currently active.
76          *
77          * @param keyphraseId The identifier of the keyphrase for which
78          *                    the recognition is to be stopped.
79          * @param listener    The listener for the recognition events related to the given
80          *                    keyphrase.
81          * @return One of {@link #STATUS_ERROR} or {@link #STATUS_OK}.
82          */
stopRecognition(int keyphraseId, IRecognitionStatusCallback listener)83         int stopRecognition(int keyphraseId, IRecognitionStatusCallback listener);
84 
getModuleProperties()85         ModuleProperties getModuleProperties();
86 
87         /**
88          * Set a model specific {@link ModelParams} with the given value. This
89          * parameter will keep its value for the duration the model is loaded regardless of starting
90          * and
91          * stopping recognition. Once the model is unloaded, the value will be lost.
92          * {@link #queryParameter} should be checked first before calling this
93          * method.
94          *
95          * @param keyphraseId The identifier of the keyphrase for which
96          *                    to modify model parameters
97          * @param modelParam  {@link ModelParams}
98          * @param value       Value to set
99          * @return - {@link SoundTrigger#STATUS_OK} in case of success
100          * - {@link SoundTrigger#STATUS_NO_INIT} if the native service cannot be reached
101          * - {@link SoundTrigger#STATUS_BAD_VALUE} invalid input parameter
102          * - {@link SoundTrigger#STATUS_INVALID_OPERATION} if the call is out of sequence or
103          * if API is not supported by HAL
104          */
setParameter(int keyphraseId, @ModelParams int modelParam, int value)105         int setParameter(int keyphraseId, @ModelParams int modelParam, int value);
106 
107         /**
108          * Get a model specific {@link ModelParams}. This parameter will keep its value
109          * for the duration the model is loaded regardless of starting and stopping recognition.
110          * Once the model is unloaded, the value will be lost. If the value is not set, a default
111          * value is returned. See ModelParams for parameter default values.
112          * {{@link #queryParameter}} should be checked first before calling this
113          * method.
114          *
115          * @param keyphraseId The identifier of the keyphrase for which
116          *                    to modify model parameters
117          * @param modelParam  {@link ModelParams}
118          * @return value of parameter
119          * @throws UnsupportedOperationException if hal or model do not support this API.
120          *                                       queryParameter should be checked first.
121          * @throws IllegalArgumentException      if invalid model handle or parameter is passed.
122          *                                       queryParameter should be checked first.
123          */
getParameter(int keyphraseId, @ModelParams int modelParam)124         int getParameter(int keyphraseId, @ModelParams int modelParam);
125 
126         /**
127          * Determine if parameter control is supported for the given model handle.
128          * This method should be checked prior to calling {@link #setParameter}
129          * or {@link #getParameter}.
130          *
131          * @param keyphraseId The identifier of the keyphrase for which
132          *                    to modify model parameters
133          * @param modelParam  {@link ModelParams}
134          * @return supported range of parameter, null if not supported
135          */
136         @Nullable
queryParameter(int keyphraseId, @ModelParams int modelParam)137         ModelParamRange queryParameter(int keyphraseId,
138                 @ModelParams int modelParam);
139 
140         /**
141          * Unloads (and stops if running) the given keyphraseId
142          */
unloadKeyphraseModel(int keyphaseId)143         int unloadKeyphraseModel(int keyphaseId);
144 
145         /**
146          * Dumps session-wide information.
147          */
dump(FileDescriptor fd, PrintWriter pw, String[] args)148         void dump(FileDescriptor fd, PrintWriter pw, String[] args);
149     }
150 }
151