blob: 9a5cd9b814c42cd9a43617eab79b926005c3442a [file] [log] [blame]
Sandeepd7018202014-07-10 15:15:39 -07001/**
Eric Laurente48188c2014-04-18 17:44:11 -07002 * 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
17package android.hardware.soundtrigger;
18
Eric Laurente48188c2014-04-18 17:44:11 -070019import android.os.Handler;
Sandeep Siddhartha05589722014-07-17 16:21:54 -070020import android.os.Parcel;
21import android.os.Parcelable;
Eric Laurente48188c2014-04-18 17:44:11 -070022
23import java.util.ArrayList;
Sandeep Siddhartha05589722014-07-17 16:21:54 -070024import java.util.Arrays;
Eric Laurente48188c2014-04-18 17:44:11 -070025import java.util.UUID;
26
27/**
28 * The SoundTrigger class provides access via JNI to the native service managing
29 * the sound trigger HAL.
30 *
31 * @hide
32 */
33public class SoundTrigger {
34
35 public static final int STATUS_OK = 0;
36 public static final int STATUS_ERROR = Integer.MIN_VALUE;
37 public static final int STATUS_PERMISSION_DENIED = -1;
38 public static final int STATUS_NO_INIT = -19;
39 public static final int STATUS_BAD_VALUE = -22;
40 public static final int STATUS_DEAD_OBJECT = -32;
41 public static final int STATUS_INVALID_OPERATION = -38;
42
43 /*****************************************************************************
44 * A ModuleProperties describes a given sound trigger hardware module
45 * managed by the native sound trigger service. Each module has a unique
46 * ID used to target any API call to this paricular module. Module
47 * properties are returned by listModules() method.
48 ****************************************************************************/
Sandeep Siddhartha05589722014-07-17 16:21:54 -070049 public static class ModuleProperties implements Parcelable {
Eric Laurente48188c2014-04-18 17:44:11 -070050 /** Unique module ID provided by the native service */
51 public final int id;
52
53 /** human readable voice detection engine implementor */
54 public final String implementor;
55
56 /** human readable voice detection engine description */
57 public final String description;
58
59 /** Unique voice engine Id (changes with each version) */
60 public final UUID uuid;
61
62 /** Voice detection engine version */
63 public final int version;
64
65 /** Maximum number of active sound models */
66 public final int maxSoundModels;
67
68 /** Maximum number of key phrases */
Sandeep Siddharthad4233c62014-06-12 18:31:19 -070069 public final int maxKeyphrases;
Eric Laurente48188c2014-04-18 17:44:11 -070070
71 /** Maximum number of users per key phrase */
72 public final int maxUsers;
73
74 /** Supported recognition modes (bit field, RECOGNITION_MODE_VOICE_TRIGGER ...) */
75 public final int recognitionModes;
76
77 /** Supports seamless transition to capture mode after recognition */
78 public final boolean supportsCaptureTransition;
79
80 /** Maximum buffering capacity in ms if supportsCaptureTransition() is true */
81 public final int maxBufferMs;
82
83 /** Supports capture by other use cases while detection is active */
84 public final boolean supportsConcurrentCapture;
85
86 /** Rated power consumption when detection is active with TDB silence/sound/speech ratio */
87 public final int powerConsumptionMw;
88
89 ModuleProperties(int id, String implementor, String description,
Sandeep Siddharthad4233c62014-06-12 18:31:19 -070090 String uuid, int version, int maxSoundModels, int maxKeyphrases,
Eric Laurente48188c2014-04-18 17:44:11 -070091 int maxUsers, int recognitionModes, boolean supportsCaptureTransition,
92 int maxBufferMs, boolean supportsConcurrentCapture,
93 int powerConsumptionMw) {
94 this.id = id;
95 this.implementor = implementor;
96 this.description = description;
97 this.uuid = UUID.fromString(uuid);
98 this.version = version;
99 this.maxSoundModels = maxSoundModels;
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700100 this.maxKeyphrases = maxKeyphrases;
Eric Laurente48188c2014-04-18 17:44:11 -0700101 this.maxUsers = maxUsers;
102 this.recognitionModes = recognitionModes;
103 this.supportsCaptureTransition = supportsCaptureTransition;
104 this.maxBufferMs = maxBufferMs;
105 this.supportsConcurrentCapture = supportsConcurrentCapture;
106 this.powerConsumptionMw = powerConsumptionMw;
107 }
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700108
109 public static final Parcelable.Creator<ModuleProperties> CREATOR
110 = new Parcelable.Creator<ModuleProperties>() {
111 public ModuleProperties createFromParcel(Parcel in) {
112 return ModuleProperties.fromParcel(in);
113 }
114
115 public ModuleProperties[] newArray(int size) {
116 return new ModuleProperties[size];
117 }
118 };
119
120 private static ModuleProperties fromParcel(Parcel in) {
121 int id = in.readInt();
122 String implementor = in.readString();
123 String description = in.readString();
124 String uuid = in.readString();
125 int version = in.readInt();
126 int maxSoundModels = in.readInt();
127 int maxKeyphrases = in.readInt();
128 int maxUsers = in.readInt();
129 int recognitionModes = in.readInt();
130 boolean supportsCaptureTransition = in.readByte() == 1;
131 int maxBufferMs = in.readInt();
132 boolean supportsConcurrentCapture = in.readByte() == 1;
133 int powerConsumptionMw = in.readInt();
134 return new ModuleProperties(id, implementor, description, uuid, version,
135 maxSoundModels, maxKeyphrases, maxUsers, recognitionModes,
136 supportsCaptureTransition, maxBufferMs, supportsConcurrentCapture,
137 powerConsumptionMw);
138 }
139
140 @Override
141 public void writeToParcel(Parcel dest, int flags) {
142 dest.writeInt(id);
143 dest.writeString(implementor);
144 dest.writeString(description);
145 dest.writeString(uuid.toString());
146 dest.writeInt(version);
147 dest.writeInt(maxSoundModels);
148 dest.writeInt(maxKeyphrases);
149 dest.writeInt(maxUsers);
150 dest.writeInt(recognitionModes);
151 dest.writeByte((byte) (supportsCaptureTransition ? 1 : 0));
152 dest.writeInt(maxBufferMs);
153 dest.writeByte((byte) (supportsConcurrentCapture ? 1 : 0));
154 dest.writeInt(powerConsumptionMw);
155 }
156
157 @Override
158 public int describeContents() {
159 return 0;
160 }
161
162 @Override
163 public String toString() {
164 return "ModuleProperties [id=" + id + ", implementor=" + implementor + ", description="
165 + description + ", uuid=" + uuid + ", version=" + version + ", maxSoundModels="
166 + maxSoundModels + ", maxKeyphrases=" + maxKeyphrases + ", maxUsers="
167 + maxUsers + ", recognitionModes=" + recognitionModes
168 + ", supportsCaptureTransition=" + supportsCaptureTransition + ", maxBufferMs="
169 + maxBufferMs + ", supportsConcurrentCapture=" + supportsConcurrentCapture
170 + ", powerConsumptionMw=" + powerConsumptionMw + "]";
171 }
Eric Laurente48188c2014-04-18 17:44:11 -0700172 }
173
174 /*****************************************************************************
175 * A SoundModel describes the attributes and contains the binary data used by the hardware
176 * implementation to detect a particular sound pattern.
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700177 * A specialized version {@link KeyphraseSoundModel} is defined for key phrase
Eric Laurente48188c2014-04-18 17:44:11 -0700178 * sound models.
179 ****************************************************************************/
180 public static class SoundModel {
181 /** Undefined sound model type */
182 public static final int TYPE_UNKNOWN = -1;
183
184 /** Keyphrase sound model */
185 public static final int TYPE_KEYPHRASE = 0;
186
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700187 /** Unique sound model identifier */
188 public final UUID uuid;
189
Eric Laurente48188c2014-04-18 17:44:11 -0700190 /** Sound model type (e.g. TYPE_KEYPHRASE); */
191 public final int type;
192
193 /** Opaque data. For use by vendor implementation and enrollment application */
194 public final byte[] data;
195
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700196 public SoundModel(UUID uuid, int type, byte[] data) {
197 this.uuid = uuid;
Eric Laurente48188c2014-04-18 17:44:11 -0700198 this.type = type;
199 this.data = data;
200 }
201 }
202
203 /*****************************************************************************
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700204 * A Keyphrase describes a key phrase that can be detected by a
205 * {@link KeyphraseSoundModel}
Eric Laurente48188c2014-04-18 17:44:11 -0700206 ****************************************************************************/
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700207 public static class Keyphrase implements Parcelable {
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700208 /** Unique identifier for this keyphrase */
209 public final int id;
210
Eric Laurente48188c2014-04-18 17:44:11 -0700211 /** Recognition modes supported for this key phrase in the model */
212 public final int recognitionModes;
213
214 /** Locale of the keyphrase. JAVA Locale string e.g en_US */
215 public final String locale;
216
217 /** Key phrase text */
218 public final String text;
219
Eric Laurent013f66b2014-07-06 16:35:00 -0700220 /** Users this key phrase has been trained for. countains sound trigger specific user IDs
221 * derived from system user IDs {@link android.os.UserHandle#getIdentifier()}. */
222 public final int[] users;
Eric Laurente48188c2014-04-18 17:44:11 -0700223
Eric Laurent013f66b2014-07-06 16:35:00 -0700224 public Keyphrase(int id, int recognitionModes, String locale, String text, int[] users) {
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700225 this.id = id;
Eric Laurente48188c2014-04-18 17:44:11 -0700226 this.recognitionModes = recognitionModes;
227 this.locale = locale;
228 this.text = text;
Eric Laurent013f66b2014-07-06 16:35:00 -0700229 this.users = users;
Eric Laurente48188c2014-04-18 17:44:11 -0700230 }
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700231
232 public static final Parcelable.Creator<Keyphrase> CREATOR
233 = new Parcelable.Creator<Keyphrase>() {
234 public Keyphrase createFromParcel(Parcel in) {
235 return Keyphrase.fromParcel(in);
236 }
237
238 public Keyphrase[] newArray(int size) {
239 return new Keyphrase[size];
240 }
241 };
242
243 private static Keyphrase fromParcel(Parcel in) {
244 int id = in.readInt();
245 int recognitionModes = in.readInt();
246 String locale = in.readString();
247 String text = in.readString();
248 int[] users = null;
249 int numUsers = in.readInt();
Sandeep Siddhartha110f5692014-07-20 12:22:56 -0700250 if (numUsers >= 0) {
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700251 users = new int[numUsers];
252 in.readIntArray(users);
253 }
254 return new Keyphrase(id, recognitionModes, locale, text, users);
255 }
256
257 @Override
258 public void writeToParcel(Parcel dest, int flags) {
259 dest.writeInt(id);
260 dest.writeInt(recognitionModes);
261 dest.writeString(locale);
262 dest.writeString(text);
263 if (users != null) {
264 dest.writeInt(users.length);
265 dest.writeIntArray(users);
266 } else {
Sandeep Siddhartha110f5692014-07-20 12:22:56 -0700267 dest.writeInt(-1);
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700268 }
269 }
270
271 @Override
272 public int describeContents() {
273 return 0;
274 }
275
276 @Override
277 public int hashCode() {
278 final int prime = 31;
279 int result = 1;
280 result = prime * result + ((text == null) ? 0 : text.hashCode());
281 result = prime * result + id;
282 result = prime * result + ((locale == null) ? 0 : locale.hashCode());
283 result = prime * result + recognitionModes;
284 result = prime * result + Arrays.hashCode(users);
285 return result;
286 }
287
288 @Override
289 public boolean equals(Object obj) {
290 if (this == obj)
291 return true;
292 if (obj == null)
293 return false;
294 if (getClass() != obj.getClass())
295 return false;
296 Keyphrase other = (Keyphrase) obj;
297 if (text == null) {
298 if (other.text != null)
299 return false;
300 } else if (!text.equals(other.text))
301 return false;
302 if (id != other.id)
303 return false;
304 if (locale == null) {
305 if (other.locale != null)
306 return false;
307 } else if (!locale.equals(other.locale))
308 return false;
309 if (recognitionModes != other.recognitionModes)
310 return false;
311 if (!Arrays.equals(users, other.users))
312 return false;
313 return true;
314 }
315
316 @Override
317 public String toString() {
318 return "Keyphrase [id=" + id + ", recognitionModes=" + recognitionModes + ", locale="
319 + locale + ", text=" + text + ", users=" + Arrays.toString(users) + "]";
320 }
Eric Laurente48188c2014-04-18 17:44:11 -0700321 }
322
323 /*****************************************************************************
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700324 * A KeyphraseSoundModel is a specialized {@link SoundModel} for key phrases.
Eric Laurente48188c2014-04-18 17:44:11 -0700325 * It contains data needed by the hardware to detect a certain number of key phrases
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700326 * and the list of corresponding {@link Keyphrase} descriptors.
Eric Laurente48188c2014-04-18 17:44:11 -0700327 ****************************************************************************/
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700328 public static class KeyphraseSoundModel extends SoundModel implements Parcelable {
Eric Laurente48188c2014-04-18 17:44:11 -0700329 /** Key phrases in this sound model */
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700330 public final Keyphrase[] keyphrases; // keyword phrases in model
Eric Laurente48188c2014-04-18 17:44:11 -0700331
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700332 public KeyphraseSoundModel(UUID id, byte[] data, Keyphrase[] keyphrases) {
333 super(id, TYPE_KEYPHRASE, data);
334 this.keyphrases = keyphrases;
Eric Laurente48188c2014-04-18 17:44:11 -0700335 }
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700336
337 public static final Parcelable.Creator<KeyphraseSoundModel> CREATOR
338 = new Parcelable.Creator<KeyphraseSoundModel>() {
339 public KeyphraseSoundModel createFromParcel(Parcel in) {
340 return KeyphraseSoundModel.fromParcel(in);
341 }
342
343 public KeyphraseSoundModel[] newArray(int size) {
344 return new KeyphraseSoundModel[size];
345 }
346 };
347
348 private static KeyphraseSoundModel fromParcel(Parcel in) {
349 UUID uuid = UUID.fromString(in.readString());
350 byte[] data = null;
351 int dataLength = in.readInt();
Sandeep Siddhartha110f5692014-07-20 12:22:56 -0700352 if (dataLength >= 0) {
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700353 data = new byte[dataLength];
354 in.readByteArray(data);
355 }
356 Keyphrase[] keyphrases = in.createTypedArray(Keyphrase.CREATOR);
357 return new KeyphraseSoundModel(uuid, data, keyphrases);
358 }
359
360 @Override
361 public int describeContents() {
362 return 0;
363 }
364
365 @Override
366 public void writeToParcel(Parcel dest, int flags) {
367 dest.writeString(uuid.toString());
368 if (data != null) {
369 dest.writeInt(data.length);
370 dest.writeByteArray(data);
371 } else {
Sandeep Siddhartha110f5692014-07-20 12:22:56 -0700372 dest.writeInt(-1);
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700373 }
374 dest.writeTypedArray(keyphrases, 0);
375 }
Sandeep Siddhartha110f5692014-07-20 12:22:56 -0700376
377 @Override
378 public String toString() {
379 return "KeyphraseSoundModel [keyphrases=" + Arrays.toString(keyphrases) + ", uuid="
380 + uuid + ", type=" + type + ", data? " + (data != null) + "]";
381 }
Eric Laurente48188c2014-04-18 17:44:11 -0700382 }
383
384 /**
385 * Modes for key phrase recognition
386 */
387 /** Simple recognition of the key phrase */
388 public static final int RECOGNITION_MODE_VOICE_TRIGGER = 0x1;
389 /** Trigger only if one user is identified */
390 public static final int RECOGNITION_MODE_USER_IDENTIFICATION = 0x2;
391 /** Trigger only if one user is authenticated */
392 public static final int RECOGNITION_MODE_USER_AUTHENTICATION = 0x4;
393
394 /**
395 * Status codes for {@link RecognitionEvent}
396 */
397 /** Recognition success */
398 public static final int RECOGNITION_STATUS_SUCCESS = 0;
399 /** Recognition aborted (e.g. capture preempted by anotehr use case */
400 public static final int RECOGNITION_STATUS_ABORT = 1;
401 /** Recognition failure */
402 public static final int RECOGNITION_STATUS_FAILURE = 2;
403
404 /**
405 * A RecognitionEvent is provided by the
406 * {@link StatusListener#onRecognition(RecognitionEvent)}
407 * callback upon recognition success or failure.
408 */
409 public static class RecognitionEvent {
410 /** Recognition status e.g {@link #RECOGNITION_STATUS_SUCCESS} */
411 public final int status;
412 /** Sound Model corresponding to this event callback */
413 public final int soundModelHandle;
414 /** True if it is possible to capture audio from this utterance buffered by the hardware */
415 public final boolean captureAvailable;
416 /** Audio session ID to be used when capturing the utterance with an AudioRecord
417 * if captureAvailable() is true. */
418 public final int captureSession;
419 /** Delay in ms between end of model detection and start of audio available for capture.
420 * A negative value is possible (e.g. if keyphrase is also available for capture) */
421 public final int captureDelayMs;
Eric Laurent013f66b2014-07-06 16:35:00 -0700422 /** Duration in ms of audio captured before the start of the trigger. 0 if none. */
423 public final int capturePreambleMs;
Eric Laurente48188c2014-04-18 17:44:11 -0700424 /** Opaque data for use by system applications who know about voice engine internals,
425 * typically during enrollment. */
426 public final byte[] data;
427
428 RecognitionEvent(int status, int soundModelHandle, boolean captureAvailable,
Eric Laurent013f66b2014-07-06 16:35:00 -0700429 int captureSession, int captureDelayMs, int capturePreambleMs, byte[] data) {
Eric Laurente48188c2014-04-18 17:44:11 -0700430 this.status = status;
431 this.soundModelHandle = soundModelHandle;
432 this.captureAvailable = captureAvailable;
433 this.captureSession = captureSession;
434 this.captureDelayMs = captureDelayMs;
Eric Laurent013f66b2014-07-06 16:35:00 -0700435 this.capturePreambleMs = capturePreambleMs;
Eric Laurente48188c2014-04-18 17:44:11 -0700436 this.data = data;
437 }
438 }
439
440 /**
Eric Laurent013f66b2014-07-06 16:35:00 -0700441 * A RecognitionConfig is provided to
442 * {@link SoundTriggerModule#startRecognition(int, RecognitionConfig)} to configure the
443 * recognition request.
444 */
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700445 public static class RecognitionConfig implements Parcelable {
Eric Laurent013f66b2014-07-06 16:35:00 -0700446 /** True if the DSP should capture the trigger sound and make it available for further
447 * capture. */
448 public final boolean captureRequested;
449 /** List of all keyphrases in the sound model for which recognition should be performed with
450 * options for each keyphrase. */
451 public final KeyphraseRecognitionExtra keyphrases[];
452 /** Opaque data for use by system applications who know about voice engine internals,
453 * typically during enrollment. */
454 public final byte[] data;
455
456 public RecognitionConfig(boolean captureRequested,
457 KeyphraseRecognitionExtra keyphrases[], byte[] data) {
458 this.captureRequested = captureRequested;
459 this.keyphrases = keyphrases;
460 this.data = data;
461 }
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700462
463 public static final Parcelable.Creator<RecognitionConfig> CREATOR
464 = new Parcelable.Creator<RecognitionConfig>() {
465 public RecognitionConfig createFromParcel(Parcel in) {
466 return RecognitionConfig.fromParcel(in);
467 }
468
469 public RecognitionConfig[] newArray(int size) {
470 return new RecognitionConfig[size];
471 }
472 };
473
474 private static RecognitionConfig fromParcel(Parcel in) {
475 boolean captureRequested = in.readByte() == 1;
476 KeyphraseRecognitionExtra[] keyphrases =
477 in.createTypedArray(KeyphraseRecognitionExtra.CREATOR);
478 byte[] data = null;
479 int dataLength = in.readInt();
Sandeep Siddhartha110f5692014-07-20 12:22:56 -0700480 if (dataLength >= 0) {
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700481 data = new byte[dataLength];
482 in.readByteArray(data);
483 }
484 return new RecognitionConfig(captureRequested, keyphrases, data);
485 }
486
487 @Override
488 public void writeToParcel(Parcel dest, int flags) {
489 dest.writeByte((byte) (captureRequested ? 1 : 0));
490 dest.writeTypedArray(keyphrases, 0);
491 if (data != null) {
492 dest.writeInt(data.length);
493 dest.writeByteArray(data);
494 } else {
Sandeep Siddhartha110f5692014-07-20 12:22:56 -0700495 dest.writeInt(-1);
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700496 }
497 }
498
499 @Override
500 public int describeContents() {
501 return 0;
502 }
Sandeep Siddhartha110f5692014-07-20 12:22:56 -0700503
504 @Override
505 public String toString() {
506 return "RecognitionConfig [captureRequested=" + captureRequested + ", keyphrases="
507 + Arrays.toString(keyphrases) + ", data? " + (data != null) + "]";
508 }
Eric Laurent013f66b2014-07-06 16:35:00 -0700509 }
510
511 /**
512 * Confidence level for users defined in a keyphrase.
513 * - The confidence level is expressed in percent (0% -100%).
514 * When used in a {@link KeyphraseRecognitionEvent} it indicates the detected confidence level
515 * When used in a {@link RecognitionConfig} it indicates the minimum confidence level that
516 * should trigger a recognition.
517 * - The user ID is derived from the system ID {@link android.os.UserHandle#getIdentifier()}.
518 */
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700519 public static class ConfidenceLevel implements Parcelable {
Eric Laurent013f66b2014-07-06 16:35:00 -0700520 public final int userId;
521 public final int confidenceLevel;
522
523 public ConfidenceLevel(int userId, int confidenceLevel) {
524 this.userId = userId;
525 this.confidenceLevel = confidenceLevel;
526 }
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700527
528 public static final Parcelable.Creator<ConfidenceLevel> CREATOR
529 = new Parcelable.Creator<ConfidenceLevel>() {
530 public ConfidenceLevel createFromParcel(Parcel in) {
531 return ConfidenceLevel.fromParcel(in);
532 }
533
534 public ConfidenceLevel[] newArray(int size) {
535 return new ConfidenceLevel[size];
536 }
537 };
538
539 private static ConfidenceLevel fromParcel(Parcel in) {
540 int userId = in.readInt();
541 int confidenceLevel = in.readInt();
542 return new ConfidenceLevel(userId, confidenceLevel);
543 }
544
545 @Override
546 public void writeToParcel(Parcel dest, int flags) {
547 dest.writeInt(userId);
548 dest.writeInt(confidenceLevel);
549 }
550
551 @Override
552 public int describeContents() {
553 return 0;
554 }
Eric Laurent013f66b2014-07-06 16:35:00 -0700555 }
556
557 /**
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700558 * Additional data conveyed by a {@link KeyphraseRecognitionEvent}
Eric Laurente48188c2014-04-18 17:44:11 -0700559 * for a key phrase detection.
560 */
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700561 public static class KeyphraseRecognitionExtra implements Parcelable {
562 /** The keyphrase ID */
Eric Laurent013f66b2014-07-06 16:35:00 -0700563 public final int id;
Eric Laurente48188c2014-04-18 17:44:11 -0700564
565 /** Recognition modes matched for this event */
566 public final int recognitionModes;
567
Eric Laurent013f66b2014-07-06 16:35:00 -0700568 /** Confidence levels for all users recognized (KeyphraseRecognitionEvent) or to
569 * be recognized (RecognitionConfig) */
570 public final ConfidenceLevel[] confidenceLevels;
571
572 public KeyphraseRecognitionExtra(int id, int recognitionModes,
573 ConfidenceLevel[] confidenceLevels) {
574 this.id = id;
Eric Laurente48188c2014-04-18 17:44:11 -0700575 this.recognitionModes = recognitionModes;
Eric Laurent013f66b2014-07-06 16:35:00 -0700576 this.confidenceLevels = confidenceLevels;
Eric Laurente48188c2014-04-18 17:44:11 -0700577 }
Sandeep Siddhartha05589722014-07-17 16:21:54 -0700578
579 public static final Parcelable.Creator<KeyphraseRecognitionExtra> CREATOR
580 = new Parcelable.Creator<KeyphraseRecognitionExtra>() {
581 public KeyphraseRecognitionExtra createFromParcel(Parcel in) {
582 return KeyphraseRecognitionExtra.fromParcel(in);
583 }
584
585 public KeyphraseRecognitionExtra[] newArray(int size) {
586 return new KeyphraseRecognitionExtra[size];
587 }
588 };
589
590 private static KeyphraseRecognitionExtra fromParcel(Parcel in) {
591 int id = in.readInt();
592 int recognitionModes = in.readInt();
593 ConfidenceLevel[] confidenceLevels = in.createTypedArray(ConfidenceLevel.CREATOR);
594 return new KeyphraseRecognitionExtra(id, recognitionModes, confidenceLevels);
595 }
596
597 @Override
598 public void writeToParcel(Parcel dest, int flags) {
599 dest.writeInt(id);
600 dest.writeInt(recognitionModes);
601 dest.writeTypedArray(confidenceLevels, 0);
602 }
603
604 @Override
605 public int describeContents() {
606 return 0;
607 }
Eric Laurente48188c2014-04-18 17:44:11 -0700608 }
609
610 /**
611 * Specialized {@link RecognitionEvent} for a key phrase detection.
612 */
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700613 public static class KeyphraseRecognitionEvent extends RecognitionEvent {
Eric Laurente48188c2014-04-18 17:44:11 -0700614 /** Indicates if the key phrase is present in the buffered audio available for capture */
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700615 public final KeyphraseRecognitionExtra[] keyphraseExtras;
Eric Laurente48188c2014-04-18 17:44:11 -0700616
617 /** Additional data available for each recognized key phrases in the model */
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700618 public final boolean keyphraseInCapture;
Eric Laurente48188c2014-04-18 17:44:11 -0700619
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700620 KeyphraseRecognitionEvent(int status, int soundModelHandle, boolean captureAvailable,
Eric Laurent013f66b2014-07-06 16:35:00 -0700621 int captureSession, int captureDelayMs, int capturePreambleMs, byte[] data,
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700622 boolean keyphraseInCapture, KeyphraseRecognitionExtra[] keyphraseExtras) {
Eric Laurent013f66b2014-07-06 16:35:00 -0700623 super(status, soundModelHandle, captureAvailable, captureSession, captureDelayMs,
624 capturePreambleMs, data);
Sandeep Siddharthad4233c62014-06-12 18:31:19 -0700625 this.keyphraseInCapture = keyphraseInCapture;
626 this.keyphraseExtras = keyphraseExtras;
Eric Laurente48188c2014-04-18 17:44:11 -0700627 }
628 }
629
630 /**
631 * Returns a list of descriptors for all harware modules loaded.
632 * @param modules A ModuleProperties array where the list will be returned.
633 * @return - {@link #STATUS_OK} in case of success
634 * - {@link #STATUS_ERROR} in case of unspecified error
635 * - {@link #STATUS_PERMISSION_DENIED} if the caller does not have system permission
636 * - {@link #STATUS_NO_INIT} if the native service cannot be reached
637 * - {@link #STATUS_BAD_VALUE} if modules is null
638 * - {@link #STATUS_DEAD_OBJECT} if the binder transaction to the native service fails
639 */
640 public static native int listModules(ArrayList <ModuleProperties> modules);
641
642 /**
643 * Get an interface on a hardware module to control sound models and recognition on
644 * this module.
645 * @param moduleId Sound module system identifier {@link ModuleProperties#id}. mandatory.
646 * @param listener {@link StatusListener} interface. Mandatory.
647 * @param handler the Handler that will receive the callabcks. Can be null if default handler
648 * is OK.
649 * @return a valid sound module in case of success or null in case of error.
650 */
651 public static SoundTriggerModule attachModule(int moduleId,
652 StatusListener listener,
653 Handler handler) {
654 if (listener == null) {
655 return null;
656 }
657 SoundTriggerModule module = new SoundTriggerModule(moduleId, listener, handler);
658 return module;
659 }
660
661 /**
662 * Interface provided by the client application when attaching to a {@link SoundTriggerModule}
663 * to received recognition and error notifications.
664 */
665 public static interface StatusListener {
666 /**
667 * Called when recognition succeeds of fails
668 */
669 public abstract void onRecognition(RecognitionEvent event);
670
671 /**
672 * Called when the sound trigger native service dies
673 */
674 public abstract void onServiceDied();
675 }
676}