android_hardware_SoundTrigger.cpp revision ed6b9dff563c5e22f040ff37e12c0d771e0478ae
160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent/*
260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent**
360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent** Copyright 2014, The Android Open Source Project
460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent**
560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent** Licensed under the Apache License, Version 2.0 (the "License");
660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent** you may not use this file except in compliance with the License.
760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent** You may obtain a copy of the License at
860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent**
960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent**     http://www.apache.org/licenses/LICENSE-2.0
1060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent**
1160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent** Unless required by applicable law or agreed to in writing, software
1260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent** distributed under the License is distributed on an "AS IS" BASIS,
1360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent** See the License for the specific language governing permissions and
1560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent** limitations under the License.
1660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent*/
1760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
1860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent//#define LOG_NDEBUG 0
1960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent#define LOG_TAG "SoundTrigger-JNI"
2060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent#include <utils/Log.h>
2160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
2260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent#include "jni.h"
2360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent#include "JNIHelp.h"
24ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe#include "core_jni_helpers.h"
2560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent#include <system/sound_trigger.h>
2660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent#include <soundtrigger/SoundTriggerCallback.h>
2760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent#include <soundtrigger/SoundTrigger.h>
2860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent#include <utils/RefBase.h>
2960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent#include <utils/Vector.h>
3060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent#include <binder/IMemory.h>
3160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent#include <binder/MemoryDealer.h>
32d3b8223377b8046280e4c09e728edc600171f941Eric Laurent#include "android_media_AudioFormat.h"
3360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
3460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentusing namespace android;
3560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
3660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jclass gArrayListClass;
3760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic struct {
3860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jmethodID    add;
3960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent} gArrayListMethods;
4060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
41d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic jclass gUUIDClass;
42d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic struct {
43d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jmethodID    toString;
44d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha} gUUIDMethods;
45d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha
4660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic const char* const kSoundTriggerClassPathName = "android/hardware/soundtrigger/SoundTrigger";
4760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jclass gSoundTriggerClass;
4860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
4960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic const char* const kModuleClassPathName = "android/hardware/soundtrigger/SoundTriggerModule";
5060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jclass gModuleClass;
5160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic struct {
5260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jfieldID    mNativeContext;
5360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jfieldID    mId;
5460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent} gModuleFields;
5560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jmethodID   gPostEventFromNative;
5660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
5760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic const char* const kModulePropertiesClassPathName =
5860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                     "android/hardware/soundtrigger/SoundTrigger$ModuleProperties";
5960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jclass gModulePropertiesClass;
6060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jmethodID   gModulePropertiesCstor;
6160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
6260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic const char* const kSoundModelClassPathName =
6360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                     "android/hardware/soundtrigger/SoundTrigger$SoundModel";
6460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jclass gSoundModelClass;
6560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic struct {
66d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jfieldID    uuid;
67d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    jfieldID    vendorUuid;
6860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jfieldID    data;
6960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent} gSoundModelFields;
7060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
71d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic const char* const kKeyphraseClassPathName =
72d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                     "android/hardware/soundtrigger/SoundTrigger$Keyphrase";
73d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic jclass gKeyphraseClass;
7460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic struct {
75d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jfieldID id;
7660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jfieldID recognitionModes;
7760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jfieldID locale;
7860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jfieldID text;
79013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jfieldID users;
80d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha} gKeyphraseFields;
8160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
82d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic const char* const kKeyphraseSoundModelClassPathName =
83d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                 "android/hardware/soundtrigger/SoundTrigger$KeyphraseSoundModel";
84d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic jclass gKeyphraseSoundModelClass;
8560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic struct {
86d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jfieldID    keyphrases;
87d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha} gKeyphraseSoundModelFields;
8860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
89013f66b92db609fceeff9c8171daca13d057cc95Eric Laurentstatic const char* const kRecognitionConfigClassPathName =
90013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                     "android/hardware/soundtrigger/SoundTrigger$RecognitionConfig";
91013f66b92db609fceeff9c8171daca13d057cc95Eric Laurentstatic jclass gRecognitionConfigClass;
92013f66b92db609fceeff9c8171daca13d057cc95Eric Laurentstatic struct {
93013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jfieldID captureRequested;
94013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jfieldID keyphrases;
95013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jfieldID data;
96013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent} gRecognitionConfigFields;
9760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
9860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic const char* const kRecognitionEventClassPathName =
9960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                     "android/hardware/soundtrigger/SoundTrigger$RecognitionEvent";
10060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jclass gRecognitionEventClass;
10160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jmethodID   gRecognitionEventCstor;
10260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
103d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic const char* const kKeyphraseRecognitionEventClassPathName =
104d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                             "android/hardware/soundtrigger/SoundTrigger$KeyphraseRecognitionEvent";
105d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic jclass gKeyphraseRecognitionEventClass;
106d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic jmethodID   gKeyphraseRecognitionEventCstor;
10760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
108d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic const char* const kKeyphraseRecognitionExtraClassPathName =
109d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                             "android/hardware/soundtrigger/SoundTrigger$KeyphraseRecognitionExtra";
110d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic jclass gKeyphraseRecognitionExtraClass;
111d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic jmethodID   gKeyphraseRecognitionExtraCstor;
112013f66b92db609fceeff9c8171daca13d057cc95Eric Laurentstatic struct {
113013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jfieldID id;
114013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jfieldID recognitionModes;
115d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    jfieldID coarseConfidenceLevel;
116013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jfieldID confidenceLevels;
117013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent} gKeyphraseRecognitionExtraFields;
118013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
119013f66b92db609fceeff9c8171daca13d057cc95Eric Laurentstatic const char* const kConfidenceLevelClassPathName =
120013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                             "android/hardware/soundtrigger/SoundTrigger$ConfidenceLevel";
121013f66b92db609fceeff9c8171daca13d057cc95Eric Laurentstatic jclass gConfidenceLevelClass;
122013f66b92db609fceeff9c8171daca13d057cc95Eric Laurentstatic jmethodID   gConfidenceLevelCstor;
123013f66b92db609fceeff9c8171daca13d057cc95Eric Laurentstatic struct {
124013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jfieldID userId;
125013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jfieldID confidenceLevel;
126013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent} gConfidenceLevelFields;
12760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
128d3b8223377b8046280e4c09e728edc600171f941Eric Laurentstatic const char* const kAudioFormatClassPathName =
129d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                             "android/media/AudioFormat";
130d3b8223377b8046280e4c09e728edc600171f941Eric Laurentstatic jclass gAudioFormatClass;
131d3b8223377b8046280e4c09e728edc600171f941Eric Laurentstatic jmethodID gAudioFormatCstor;
132d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
133d3b8223377b8046280e4c09e728edc600171f941Eric Laurentstatic const char* const kSoundModelEventClassPathName =
134d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                     "android/hardware/soundtrigger/SoundTrigger$SoundModelEvent";
135d3b8223377b8046280e4c09e728edc600171f941Eric Laurentstatic jclass gSoundModelEventClass;
136d3b8223377b8046280e4c09e728edc600171f941Eric Laurentstatic jmethodID   gSoundModelEventCstor;
137d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
13860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic Mutex gLock;
13960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
14060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentenum {
14160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_STATUS_OK = 0,
14260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_STATUS_ERROR = INT_MIN,
14360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_PERMISSION_DENIED = -1,
14460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_STATUS_NO_INIT = -19,
14560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_STATUS_BAD_VALUE = -22,
14660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_STATUS_DEAD_OBJECT = -32,
14760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_INVALID_OPERATION = -38,
14860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent};
14960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
15060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentenum  {
15160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_EVENT_RECOGNITION = 1,
15260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_EVENT_SERVICE_DIED = 2,
153d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    SOUNDTRIGGER_EVENT_SOUNDMODEL = 3,
154d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    SOUNDTRIGGER_EVENT_SERVICE_STATE_CHANGE = 4,
15560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent};
15660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
15760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent// ----------------------------------------------------------------------------
15860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent// ref-counted object for callbacks
15960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentclass JNISoundTriggerCallback: public SoundTriggerCallback
16060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
16160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentpublic:
16260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    JNISoundTriggerCallback(JNIEnv* env, jobject thiz, jobject weak_thiz);
16360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ~JNISoundTriggerCallback();
16460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
16560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    virtual void onRecognitionEvent(struct sound_trigger_recognition_event *event);
166d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    virtual void onSoundModelEvent(struct sound_trigger_model_event *event);
167d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    virtual void onServiceStateChange(sound_trigger_service_state_t state);
16860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    virtual void onServiceDied();
16960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
17060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentprivate:
17160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jclass      mClass;     // Reference to SoundTrigger class
17260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jobject     mObject;    // Weak ref to SoundTrigger Java object to call on
17360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent};
17460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
17560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric LaurentJNISoundTriggerCallback::JNISoundTriggerCallback(JNIEnv* env, jobject thiz, jobject weak_thiz)
17660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
17760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
17860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    // Hold onto the SoundTriggerModule class for use in calling the static method
17960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    // that posts events to the application thread.
18060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jclass clazz = env->GetObjectClass(thiz);
18160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (clazz == NULL) {
18260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGE("Can't find class %s", kModuleClassPathName);
18360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return;
18460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
18560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    mClass = (jclass)env->NewGlobalRef(clazz);
18660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
18760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    // We use a weak reference so the SoundTriggerModule object can be garbage collected.
18860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    // The reference is only used as a proxy for callbacks.
18960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    mObject  = env->NewGlobalRef(weak_thiz);
19060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
19160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
19260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric LaurentJNISoundTriggerCallback::~JNISoundTriggerCallback()
19360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
19460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    // remove global references
19560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    JNIEnv *env = AndroidRuntime::getJNIEnv();
19660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    env->DeleteGlobalRef(mObject);
19760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    env->DeleteGlobalRef(mClass);
19860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
19960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
20060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentvoid JNISoundTriggerCallback::onRecognitionEvent(struct sound_trigger_recognition_event *event)
20160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
20260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    JNIEnv *env = AndroidRuntime::getJNIEnv();
203d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    jobject jEvent = NULL;
20460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jbyteArray jData = NULL;
205d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
20660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (event->data_size) {
20760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jData = env->NewByteArray(event->data_size);
20860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jbyte *nData = env->GetByteArrayElements(jData, NULL);
20960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        memcpy(nData, (char *)event + event->data_offset, event->data_size);
21060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->ReleaseByteArrayElements(jData, nData, 0);
21160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
21260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
213d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    jobject jAudioFormat = NULL;
21439fcca0a503658f91a14b4b1d35dfc4ac9fce9d9Eric Laurent    if (event->trigger_in_data || event->capture_available) {
215d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        jAudioFormat = env->NewObject(gAudioFormatClass,
216d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                    gAudioFormatCstor,
217d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                    audioFormatFromNative(event->audio_config.format),
218d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                    event->audio_config.sample_rate,
219d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                    inChannelMaskFromNative(event->audio_config.channel_mask));
220d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
221d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    }
22260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (event->type == SOUND_MODEL_TYPE_KEYPHRASE) {
22360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        struct sound_trigger_phrase_recognition_event *phraseEvent =
22460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                (struct sound_trigger_phrase_recognition_event *)event;
22560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
22660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jobjectArray jExtras = env->NewObjectArray(phraseEvent->num_phrases,
227d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                                  gKeyphraseRecognitionExtraClass, NULL);
22860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (jExtras == NULL) {
22960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            return;
23060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
23160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
23260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        for (size_t i = 0; i < phraseEvent->num_phrases; i++) {
233013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            jobjectArray jConfidenceLevels = env->NewObjectArray(
234013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                        phraseEvent->phrase_extras[i].num_levels,
235013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                        gConfidenceLevelClass, NULL);
236013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
23760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            if (jConfidenceLevels == NULL) {
23860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                return;
23960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            }
240013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            for (size_t j = 0; j < phraseEvent->phrase_extras[i].num_levels; j++) {
241013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                jobject jConfidenceLevel = env->NewObject(gConfidenceLevelClass,
242013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                  gConfidenceLevelCstor,
243013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                  phraseEvent->phrase_extras[i].levels[j].user_id,
244013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                  phraseEvent->phrase_extras[i].levels[j].level);
245013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                env->SetObjectArrayElement(jConfidenceLevels, j, jConfidenceLevel);
246013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                env->DeleteLocalRef(jConfidenceLevel);
247013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            }
248013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
249d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha            jobject jNewExtra = env->NewObject(gKeyphraseRecognitionExtraClass,
250d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                               gKeyphraseRecognitionExtraCstor,
251013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                               phraseEvent->phrase_extras[i].id,
252013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                               phraseEvent->phrase_extras[i].recognition_modes,
253d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                               phraseEvent->phrase_extras[i].confidence_level,
254013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                               jConfidenceLevels);
25560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
25660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            if (jNewExtra == NULL) {
25760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                return;
25860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            }
25960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->SetObjectArrayElement(jExtras, i, jNewExtra);
260013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            env->DeleteLocalRef(jNewExtra);
261013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            env->DeleteLocalRef(jConfidenceLevels);
26260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
263d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha        jEvent = env->NewObject(gKeyphraseRecognitionEventClass, gKeyphraseRecognitionEventCstor,
26460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                event->status, event->model, event->capture_available,
265d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                event->capture_session, event->capture_delay_ms,
266d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                event->capture_preamble_ms, event->trigger_in_data,
267d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                jAudioFormat, jData, jExtras);
268d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->DeleteLocalRef(jAudioFormat);
269d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->DeleteLocalRef(jData);
27060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    } else {
27160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jEvent = env->NewObject(gRecognitionEventClass, gRecognitionEventCstor,
27260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                event->status, event->model, event->capture_available,
273013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                event->capture_session, event->capture_delay_ms,
274d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                event->capture_preamble_ms, event->trigger_in_data,
275d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                jAudioFormat, jData);
276d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->DeleteLocalRef(jAudioFormat);
277d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->DeleteLocalRef(jData);
27860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
27960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
28060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
28160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    env->CallStaticVoidMethod(mClass, gPostEventFromNative, mObject,
28260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                              SOUNDTRIGGER_EVENT_RECOGNITION, 0, 0, jEvent);
283d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
284d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    env->DeleteLocalRef(jEvent);
285d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    if (env->ExceptionCheck()) {
286d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        ALOGW("An exception occurred while notifying an event.");
287d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->ExceptionClear();
288d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    }
289d3b8223377b8046280e4c09e728edc600171f941Eric Laurent}
290d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
291d3b8223377b8046280e4c09e728edc600171f941Eric Laurentvoid JNISoundTriggerCallback::onSoundModelEvent(struct sound_trigger_model_event *event)
292d3b8223377b8046280e4c09e728edc600171f941Eric Laurent{
293d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    JNIEnv *env = AndroidRuntime::getJNIEnv();
294d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    jobject jEvent = NULL;
295d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    jbyteArray jData = NULL;
296d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
297d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    if (event->data_size) {
298d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        jData = env->NewByteArray(event->data_size);
299d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        jbyte *nData = env->GetByteArrayElements(jData, NULL);
300d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        memcpy(nData, (char *)event + event->data_offset, event->data_size);
301d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->ReleaseByteArrayElements(jData, nData, 0);
302d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    }
303d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
304d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    jEvent = env->NewObject(gSoundModelEventClass, gSoundModelEventCstor,
305d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                            event->status, event->model, jData);
306d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
307d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    env->DeleteLocalRef(jData);
308d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    env->CallStaticVoidMethod(mClass, gPostEventFromNative, mObject,
309d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                              SOUNDTRIGGER_EVENT_SOUNDMODEL, 0, 0, jEvent);
310d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    env->DeleteLocalRef(jEvent);
311d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    if (env->ExceptionCheck()) {
312d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        ALOGW("An exception occurred while notifying an event.");
313d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->ExceptionClear();
314d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    }
315d3b8223377b8046280e4c09e728edc600171f941Eric Laurent}
316d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
317d3b8223377b8046280e4c09e728edc600171f941Eric Laurentvoid JNISoundTriggerCallback::onServiceStateChange(sound_trigger_service_state_t state)
318d3b8223377b8046280e4c09e728edc600171f941Eric Laurent{
319d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    JNIEnv *env = AndroidRuntime::getJNIEnv();
320d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    env->CallStaticVoidMethod(mClass, gPostEventFromNative, mObject,
321d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                        SOUNDTRIGGER_EVENT_SERVICE_STATE_CHANGE, state, 0, NULL);
32260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (env->ExceptionCheck()) {
32360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGW("An exception occurred while notifying an event.");
32460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->ExceptionClear();
32560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
32660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
32760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
32860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentvoid JNISoundTriggerCallback::onServiceDied()
32960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
33060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    JNIEnv *env = AndroidRuntime::getJNIEnv();
33160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
33260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    env->CallStaticVoidMethod(mClass, gPostEventFromNative, mObject,
33360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                              SOUNDTRIGGER_EVENT_SERVICE_DIED, 0, 0, NULL);
33460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (env->ExceptionCheck()) {
33560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGW("An exception occurred while notifying an event.");
33660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->ExceptionClear();
33760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
33860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
33960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
34060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent// ----------------------------------------------------------------------------
34160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
34260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic sp<SoundTrigger> getSoundTrigger(JNIEnv* env, jobject thiz)
34360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
34460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    Mutex::Autolock l(gLock);
34560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SoundTrigger* const st = (SoundTrigger*)env->GetLongField(thiz,
34660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                                         gModuleFields.mNativeContext);
34760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return sp<SoundTrigger>(st);
34860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
34960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
35060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic sp<SoundTrigger> setSoundTrigger(JNIEnv* env, jobject thiz, const sp<SoundTrigger>& module)
35160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
35260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    Mutex::Autolock l(gLock);
35360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> old = (SoundTrigger*)env->GetLongField(thiz,
35460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                                         gModuleFields.mNativeContext);
35560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module.get()) {
35660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        module->incStrong((void*)setSoundTrigger);
35760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
35860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (old != 0) {
35960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        old->decStrong((void*)setSoundTrigger);
36060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
36160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    env->SetLongField(thiz, gModuleFields.mNativeContext, (jlong)module.get());
36260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return old;
36360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
36460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
36560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
36660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
36760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_listModules(JNIEnv *env, jobject clazz,
36860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                          jobject jModules)
36960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
37060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("listModules");
37160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
37260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jModules == NULL) {
37360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGE("listModules NULL AudioPatch ArrayList");
37460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_BAD_VALUE;
37560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
37660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (!env->IsInstanceOf(jModules, gArrayListClass)) {
37760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGE("listModules not an arraylist");
37860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_BAD_VALUE;
37960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
38060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
38160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    unsigned int numModules = 0;
38260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    struct sound_trigger_module_descriptor *nModules = NULL;
38360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
38460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status_t status = SoundTrigger::listModules(nModules, &numModules);
38560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (status != NO_ERROR || numModules == 0) {
38660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return (jint)status;
38760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
38860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
38960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nModules = (struct sound_trigger_module_descriptor *)
39060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                            calloc(numModules, sizeof(struct sound_trigger_module_descriptor));
39160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
39260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = SoundTrigger::listModules(nModules, &numModules);
39360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("listModules SoundTrigger::listModules status %d numModules %d", status, numModules);
39460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
39560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (status != NO_ERROR) {
39660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        numModules = 0;
39760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
39860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
39960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    for (size_t i = 0; i < numModules; i++) {
40060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        char str[SOUND_TRIGGER_MAX_STRING_LEN];
40160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
40260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jstring implementor = env->NewStringUTF(nModules[i].properties.implementor);
40360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jstring description = env->NewStringUTF(nModules[i].properties.description);
40460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        SoundTrigger::guidToString(&nModules[i].properties.uuid,
40560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                   str,
40660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                   SOUND_TRIGGER_MAX_STRING_LEN);
40760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jstring uuid = env->NewStringUTF(str);
40860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
409d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        ALOGV("listModules module %zu id %d description %s maxSoundModels %d",
41060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent              i, nModules[i].handle, nModules[i].properties.description,
41160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent              nModules[i].properties.max_sound_models);
41260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
41360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jobject newModuleDesc = env->NewObject(gModulePropertiesClass, gModulePropertiesCstor,
41460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].handle,
41560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               implementor, description, uuid,
41660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.version,
41760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.max_sound_models,
41860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.max_key_phrases,
41960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.max_users,
42060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.recognition_modes,
42160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.capture_transition,
42260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.max_buffer_ms,
42360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.concurrent_capture,
424d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                               nModules[i].properties.power_consumption_mw,
425d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                               nModules[i].properties.trigger_in_event);
42660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
42760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->DeleteLocalRef(implementor);
42860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->DeleteLocalRef(description);
42960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->DeleteLocalRef(uuid);
43060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (newModuleDesc == NULL) {
43160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            status = SOUNDTRIGGER_STATUS_ERROR;
43260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            goto exit;
43360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
43460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->CallBooleanMethod(jModules, gArrayListMethods.add, newModuleDesc);
43560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
43660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
43760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentexit:
43860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    free(nModules);
43960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return (jint) status;
44060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
44160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
44260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic void
44360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_setup(JNIEnv *env, jobject thiz, jobject weak_this)
44460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
44560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("setup");
44660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
44760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<JNISoundTriggerCallback> callback = new JNISoundTriggerCallback(env, thiz, weak_this);
44860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
44960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sound_trigger_module_handle_t handle =
45060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            (sound_trigger_module_handle_t)env->GetIntField(thiz, gModuleFields.mId);
45160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
45260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = SoundTrigger::attach(handle, callback);
45360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == 0) {
45460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return;
45560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
45660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
45760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    setSoundTrigger(env, thiz, module);
45860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
45960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
46060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic void
46160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_detach(JNIEnv *env, jobject thiz)
46260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
46360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("detach");
46460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = setSoundTrigger(env, thiz, 0);
46560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("detach module %p", module.get());
46660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module != 0) {
46760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGV("detach module->detach()");
46860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        module->detach();
46960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
47060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
47160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
47260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic void
47360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_finalize(JNIEnv *env, jobject thiz)
47460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
47560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("finalize");
47660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
47760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module != 0) {
47860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGW("SoundTrigger finalized without being detached");
47960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
48060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    android_hardware_SoundTrigger_detach(env, thiz);
48160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
48260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
48360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
48460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_loadSoundModel(JNIEnv *env, jobject thiz,
48560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                             jobject jSoundModel, jintArray jHandle)
48660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
48760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint status = SOUNDTRIGGER_STATUS_OK;
488013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jbyte *nData = NULL;
48960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    struct sound_trigger_sound_model *nSoundModel;
49060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jbyteArray jData;
49160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<MemoryDealer> memoryDealer;
49260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<IMemory> memory;
49360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    size_t size;
49460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sound_model_handle_t handle;
495d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jobject jUuid;
496d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jstring jUuidString;
497d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    const char *nUuidString;
49860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
49960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("loadSoundModel");
50060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
50160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == NULL) {
50260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
50360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
50460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jHandle == NULL) {
50560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_BAD_VALUE;
50660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
50760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jsize jHandleLen = env->GetArrayLength(jHandle);
50860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jHandleLen == 0) {
50960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_BAD_VALUE;
51060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
51160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint *nHandle = env->GetIntArrayElements(jHandle, NULL);
51260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (nHandle == NULL) {
51360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
51460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
51560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (!env->IsInstanceOf(jSoundModel, gSoundModelClass)) {
51660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_BAD_VALUE;
51760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
51860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
51960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    size_t offset;
52060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sound_trigger_sound_model_type_t type;
521d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    if (env->IsInstanceOf(jSoundModel, gKeyphraseSoundModelClass)) {
52260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        offset = sizeof(struct sound_trigger_phrase_sound_model);
52360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        type = SOUND_MODEL_TYPE_KEYPHRASE;
52460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    } else {
52560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        offset = sizeof(struct sound_trigger_sound_model);
52660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        type = SOUND_MODEL_TYPE_UNKNOWN;
52760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
528d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha
529d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jUuid = env->GetObjectField(jSoundModel, gSoundModelFields.uuid);
530d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jUuidString = (jstring)env->CallObjectMethod(jUuid, gUUIDMethods.toString);
531d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    nUuidString = env->GetStringUTFChars(jUuidString, NULL);
532d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    sound_trigger_uuid_t nUuid;
533d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    SoundTrigger::stringToGuid(nUuidString, &nUuid);
534d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    env->ReleaseStringUTFChars(jUuidString, nUuidString);
535d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    env->DeleteLocalRef(jUuidString);
536d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha
537d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    sound_trigger_uuid_t nVendorUuid;
538d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    jUuid = env->GetObjectField(jSoundModel, gSoundModelFields.vendorUuid);
539d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    if (jUuid != NULL) {
540d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        jUuidString = (jstring)env->CallObjectMethod(jUuid, gUUIDMethods.toString);
541d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        nUuidString = env->GetStringUTFChars(jUuidString, NULL);
542d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        SoundTrigger::stringToGuid(nUuidString, &nVendorUuid);
543d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->ReleaseStringUTFChars(jUuidString, nUuidString);
544d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->DeleteLocalRef(jUuidString);
545d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    } else {
546d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        SoundTrigger::stringToGuid("00000000-0000-0000-0000-000000000000", &nVendorUuid);
547d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    }
548d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
54960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jData = (jbyteArray)env->GetObjectField(jSoundModel, gSoundModelFields.data);
55060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jData == NULL) {
55160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_BAD_VALUE;
55260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
55360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
55460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    size = env->GetArrayLength(jData);
55560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
556013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    nData = env->GetByteArrayElements(jData, NULL);
55760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jData == NULL) {
55860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_ERROR;
55960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
56060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
56160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
56260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    memoryDealer = new MemoryDealer(offset + size, "SoundTrigge-JNI::LoadModel");
56360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (memoryDealer == 0) {
56460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_ERROR;
56560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
56660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
56760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    memory = memoryDealer->allocate(offset + size);
56860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (memory == 0 || memory->pointer() == NULL) {
56960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_ERROR;
57060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
57160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
57260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
57360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nSoundModel = (struct sound_trigger_sound_model *)memory->pointer();
57460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
57560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nSoundModel->type = type;
576d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    nSoundModel->uuid = nUuid;
577d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    nSoundModel->vendor_uuid = nVendorUuid;
57860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nSoundModel->data_size = size;
57960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nSoundModel->data_offset = offset;
58060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    memcpy((char *)nSoundModel + offset, nData, size);
58160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (type == SOUND_MODEL_TYPE_KEYPHRASE) {
58260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        struct sound_trigger_phrase_sound_model *phraseModel =
58360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                (struct sound_trigger_phrase_sound_model *)nSoundModel;
58460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
58560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jobjectArray jPhrases =
586d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha            (jobjectArray)env->GetObjectField(jSoundModel, gKeyphraseSoundModelFields.keyphrases);
58760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (jPhrases == NULL) {
58860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            status = SOUNDTRIGGER_STATUS_BAD_VALUE;
58960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            goto exit;
59060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
59160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
59260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        size_t numPhrases = env->GetArrayLength(jPhrases);
59360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        phraseModel->num_phrases = numPhrases;
594d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        ALOGV("loadSoundModel numPhrases %zu", numPhrases);
59560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        for (size_t i = 0; i < numPhrases; i++) {
59660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            jobject jPhrase = env->GetObjectArrayElement(jPhrases, i);
597d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha            phraseModel->phrases[i].id =
598d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                    env->GetIntField(jPhrase,gKeyphraseFields.id);
59960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            phraseModel->phrases[i].recognition_mode =
600d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                    env->GetIntField(jPhrase,gKeyphraseFields.recognitionModes);
601013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
602013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            jintArray jUsers = (jintArray)env->GetObjectField(jPhrase, gKeyphraseFields.users);
603013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            phraseModel->phrases[i].num_users = env->GetArrayLength(jUsers);
604013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            jint *nUsers = env->GetIntArrayElements(jUsers, NULL);
605013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            memcpy(phraseModel->phrases[i].users,
606013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                   nUsers,
607013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                   phraseModel->phrases[i].num_users * sizeof(int));
608013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            env->ReleaseIntArrayElements(jUsers, nUsers, 0);
609013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            env->DeleteLocalRef(jUsers);
610013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
611d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha            jstring jLocale = (jstring)env->GetObjectField(jPhrase, gKeyphraseFields.locale);
61260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            const char *nLocale = env->GetStringUTFChars(jLocale, NULL);
61360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            strncpy(phraseModel->phrases[i].locale,
61460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                    nLocale,
61560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                    SOUND_TRIGGER_MAX_LOCALE_LEN);
616d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha            jstring jText = (jstring)env->GetObjectField(jPhrase, gKeyphraseFields.text);
61760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            const char *nText = env->GetStringUTFChars(jText, NULL);
61860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            strncpy(phraseModel->phrases[i].text,
61960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                    nText,
62060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                    SOUND_TRIGGER_MAX_STRING_LEN);
62160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
62260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->ReleaseStringUTFChars(jLocale, nLocale);
62360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->DeleteLocalRef(jLocale);
62460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->ReleaseStringUTFChars(jText, nText);
62560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->DeleteLocalRef(jText);
626d3b8223377b8046280e4c09e728edc600171f941Eric Laurent            ALOGV("loadSoundModel phrases %zu text %s locale %s",
62760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                  i, phraseModel->phrases[i].text, phraseModel->phrases[i].locale);
628013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            env->DeleteLocalRef(jPhrase);
62960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
63060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->DeleteLocalRef(jPhrases);
63160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
63260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = module->loadSoundModel(memory, &handle);
63360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("loadSoundModel status %d handle %d", status, handle);
63460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
63560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentexit:
63660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (nHandle != NULL) {
63760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        nHandle[0] = (jint)handle;
63860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->ReleaseIntArrayElements(jHandle, nHandle, NULL);
63960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
64060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (nData != NULL) {
641013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        env->ReleaseByteArrayElements(jData, nData, NULL);
64260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
64360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return status;
64460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
64560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
64660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
64760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_unloadSoundModel(JNIEnv *env, jobject thiz,
64860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               jint jHandle)
64960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
65060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint status = SOUNDTRIGGER_STATUS_OK;
65160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("unloadSoundModel");
65260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
65360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == NULL) {
65460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
65560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
65660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = module->unloadSoundModel((sound_model_handle_t)jHandle);
65760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
65860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return status;
65960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
66060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
66160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
66260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_startRecognition(JNIEnv *env, jobject thiz,
663013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                               jint jHandle, jobject jConfig)
66460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
66560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint status = SOUNDTRIGGER_STATUS_OK;
66660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("startRecognition");
66760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
66860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == NULL) {
66960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
67060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
671013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
672013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    if (!env->IsInstanceOf(jConfig, gRecognitionConfigClass)) {
673013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        return SOUNDTRIGGER_STATUS_BAD_VALUE;
674013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    }
675013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
676013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jbyteArray jData = (jbyteArray)env->GetObjectField(jConfig, gRecognitionConfigFields.data);
67760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jsize dataSize = 0;
678013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jbyte *nData = NULL;
67960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jData != NULL) {
68060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        dataSize = env->GetArrayLength(jData);
68160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (dataSize == 0) {
68260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            return SOUNDTRIGGER_STATUS_BAD_VALUE;
68360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
684013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        nData = env->GetByteArrayElements(jData, NULL);
68560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (nData == NULL) {
68660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            return SOUNDTRIGGER_STATUS_ERROR;
68760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
688013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    }
689013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
690013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    size_t totalSize = sizeof(struct sound_trigger_recognition_config) + dataSize;
691013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    sp<MemoryDealer> memoryDealer =
692013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            new MemoryDealer(totalSize, "SoundTrigge-JNI::StartRecognition");
693013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    if (memoryDealer == 0) {
694013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
695013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    }
696013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    sp<IMemory> memory = memoryDealer->allocate(totalSize);
697013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    if (memory == 0 || memory->pointer() == NULL) {
698013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
699013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    }
700013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    if (dataSize != 0) {
701013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        memcpy((char *)memory->pointer() + sizeof(struct sound_trigger_recognition_config),
702013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                nData,
703013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                dataSize);
704013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        env->ReleaseByteArrayElements(jData, nData, 0);
705013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    }
706013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    env->DeleteLocalRef(jData);
707013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    struct sound_trigger_recognition_config *config =
708013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                    (struct sound_trigger_recognition_config *)memory->pointer();
709013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    config->data_size = dataSize;
710013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    config->data_offset = sizeof(struct sound_trigger_recognition_config);
711013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    config->capture_requested = env->GetIntField(jConfig,
712013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                 gRecognitionConfigFields.captureRequested);
713013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
714013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    config->num_phrases = 0;
715013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jobjectArray jPhrases =
716013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        (jobjectArray)env->GetObjectField(jConfig, gRecognitionConfigFields.keyphrases);
717013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    if (jPhrases != NULL) {
718013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        config->num_phrases = env->GetArrayLength(jPhrases);
719013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    }
720013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    ALOGV("startRecognition num phrases %d", config->num_phrases);
721013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    for (size_t i = 0; i < config->num_phrases; i++) {
722013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        jobject jPhrase = env->GetObjectArrayElement(jPhrases, i);
723013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        config->phrases[i].id = env->GetIntField(jPhrase,
724013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                gKeyphraseRecognitionExtraFields.id);
725013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        config->phrases[i].recognition_modes = env->GetIntField(jPhrase,
726013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                gKeyphraseRecognitionExtraFields.recognitionModes);
727d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        config->phrases[i].confidence_level = env->GetIntField(jPhrase,
728d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                            gKeyphraseRecognitionExtraFields.coarseConfidenceLevel);
729013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        config->phrases[i].num_levels = 0;
730013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        jobjectArray jConfidenceLevels = (jobjectArray)env->GetObjectField(jPhrase,
731013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                gKeyphraseRecognitionExtraFields.confidenceLevels);
732013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        if (jConfidenceLevels != NULL) {
733013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            config->phrases[i].num_levels = env->GetArrayLength(jConfidenceLevels);
73460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
735d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        ALOGV("startRecognition phrase %zu num_levels %d", i, config->phrases[i].num_levels);
736013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        for (size_t j = 0; j < config->phrases[i].num_levels; j++) {
737013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            jobject jConfidenceLevel = env->GetObjectArrayElement(jConfidenceLevels, j);
738013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            config->phrases[i].levels[j].user_id = env->GetIntField(jConfidenceLevel,
739013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                                    gConfidenceLevelFields.userId);
740013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            config->phrases[i].levels[j].level = env->GetIntField(jConfidenceLevel,
741013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                          gConfidenceLevelFields.confidenceLevel);
742013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            env->DeleteLocalRef(jConfidenceLevel);
74360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
744d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        ALOGV("startRecognition phrases %zu", i);
745013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        env->DeleteLocalRef(jConfidenceLevels);
746013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        env->DeleteLocalRef(jPhrase);
74760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
748013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    env->DeleteLocalRef(jPhrases);
74960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
75060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = module->startRecognition(jHandle, memory);
75160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return status;
75260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
75360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
75460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
75560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_stopRecognition(JNIEnv *env, jobject thiz,
75660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               jint jHandle)
75760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
75860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint status = SOUNDTRIGGER_STATUS_OK;
75960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("stopRecognition");
76060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
76160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == NULL) {
76260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
76360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
76460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = module->stopRecognition(jHandle);
76560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return status;
76660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
76760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
76860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic JNINativeMethod gMethods[] = {
76960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"listModules",
77060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(Ljava/util/ArrayList;)I",
77160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_listModules},
77260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent};
77360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
77460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
77560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic JNINativeMethod gModuleMethods[] = {
77660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"native_setup",
77760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(Ljava/lang/Object;)V",
77860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_setup},
77960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"native_finalize",
78060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "()V",
78160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_finalize},
78260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"detach",
78360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "()V",
78460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_detach},
78560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"loadSoundModel",
78660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(Landroid/hardware/soundtrigger/SoundTrigger$SoundModel;[I)I",
78760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_loadSoundModel},
78860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"unloadSoundModel",
78960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(I)I",
79060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_unloadSoundModel},
79160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"startRecognition",
792013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        "(ILandroid/hardware/soundtrigger/SoundTrigger$RecognitionConfig;)I",
79360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_startRecognition},
79460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"stopRecognition",
79560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(I)I",
79660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_stopRecognition},
79760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent};
79860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
79960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentint register_android_hardware_SoundTrigger(JNIEnv *env)
80060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
801ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass arrayListClass = FindClassOrDie(env, "java/util/ArrayList");
802ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gArrayListClass = MakeGlobalRefOrDie(env, arrayListClass);
803ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gArrayListMethods.add = GetMethodIDOrDie(env, arrayListClass, "add", "(Ljava/lang/Object;)Z");
804ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
805ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass uuidClass = FindClassOrDie(env, "java/util/UUID");
806ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gUUIDClass = MakeGlobalRefOrDie(env, uuidClass);
807ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gUUIDMethods.toString = GetMethodIDOrDie(env, uuidClass, "toString", "()Ljava/lang/String;");
808ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
809ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass lClass = FindClassOrDie(env, kSoundTriggerClassPathName);
810ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gSoundTriggerClass = MakeGlobalRefOrDie(env, lClass);
811ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
812ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass moduleClass = FindClassOrDie(env, kModuleClassPathName);
813ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gModuleClass = MakeGlobalRefOrDie(env, moduleClass);
814ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gPostEventFromNative = GetStaticMethodIDOrDie(env, moduleClass, "postEventFromNative",
815ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                                  "(Ljava/lang/Object;IIILjava/lang/Object;)V");
816ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gModuleFields.mNativeContext = GetFieldIDOrDie(env, moduleClass, "mNativeContext", "J");
817ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gModuleFields.mId = GetFieldIDOrDie(env, moduleClass, "mId", "I");
818ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
819ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass modulePropertiesClass = FindClassOrDie(env, kModulePropertiesClassPathName);
820ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gModulePropertiesClass = MakeGlobalRefOrDie(env, modulePropertiesClass);
821ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gModulePropertiesCstor = GetMethodIDOrDie(env, modulePropertiesClass, "<init>",
822ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe            "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;IIIIIZIZIZ)V");
823ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
824ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass soundModelClass = FindClassOrDie(env, kSoundModelClassPathName);
825ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gSoundModelClass = MakeGlobalRefOrDie(env, soundModelClass);
826ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gSoundModelFields.uuid = GetFieldIDOrDie(env, soundModelClass, "uuid", "Ljava/util/UUID;");
827ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gSoundModelFields.vendorUuid = GetFieldIDOrDie(env, soundModelClass, "vendorUuid",
828ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                                   "Ljava/util/UUID;");
829ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gSoundModelFields.data = GetFieldIDOrDie(env, soundModelClass, "data", "[B");
830ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
831ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass keyphraseClass = FindClassOrDie(env, kKeyphraseClassPathName);
832ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseClass = MakeGlobalRefOrDie(env, keyphraseClass);
833ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseFields.id = GetFieldIDOrDie(env, keyphraseClass, "id", "I");
834ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseFields.recognitionModes = GetFieldIDOrDie(env, keyphraseClass, "recognitionModes",
835ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                                        "I");
836ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseFields.locale = GetFieldIDOrDie(env, keyphraseClass, "locale", "Ljava/lang/String;");
837ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseFields.text = GetFieldIDOrDie(env, keyphraseClass, "text", "Ljava/lang/String;");
838ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseFields.users = GetFieldIDOrDie(env, keyphraseClass, "users", "[I");
839ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
840ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass keyphraseSoundModelClass = FindClassOrDie(env, kKeyphraseSoundModelClassPathName);
841ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseSoundModelClass = MakeGlobalRefOrDie(env, keyphraseSoundModelClass);
842ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseSoundModelFields.keyphrases = GetFieldIDOrDie(env, keyphraseSoundModelClass,
843d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                         "keyphrases",
844d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                         "[Landroid/hardware/soundtrigger/SoundTrigger$Keyphrase;");
84560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
846ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass recognitionEventClass = FindClassOrDie(env, kRecognitionEventClassPathName);
847ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gRecognitionEventClass = MakeGlobalRefOrDie(env, recognitionEventClass);
848ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gRecognitionEventCstor = GetMethodIDOrDie(env, recognitionEventClass, "<init>",
849d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                              "(IIZIIIZLandroid/media/AudioFormat;[B)V");
85060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
851ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass keyphraseRecognitionEventClass = FindClassOrDie(env,
852ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                                           kKeyphraseRecognitionEventClassPathName);
853ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionEventClass = MakeGlobalRefOrDie(env, keyphraseRecognitionEventClass);
854ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionEventCstor = GetMethodIDOrDie(env, keyphraseRecognitionEventClass, "<init>",
855d3b8223377b8046280e4c09e728edc600171f941Eric Laurent              "(IIZIIIZLandroid/media/AudioFormat;[B[Landroid/hardware/soundtrigger/SoundTrigger$KeyphraseRecognitionExtra;)V");
856013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
85760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
858ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass keyRecognitionConfigClass = FindClassOrDie(env, kRecognitionConfigClassPathName);
859ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gRecognitionConfigClass = MakeGlobalRefOrDie(env, keyRecognitionConfigClass);
860ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gRecognitionConfigFields.captureRequested = GetFieldIDOrDie(env, keyRecognitionConfigClass,
861ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                                                "captureRequested", "Z");
862ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gRecognitionConfigFields.keyphrases = GetFieldIDOrDie(env, keyRecognitionConfigClass,
863ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe           "keyphrases", "[Landroid/hardware/soundtrigger/SoundTrigger$KeyphraseRecognitionExtra;");
864ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gRecognitionConfigFields.data = GetFieldIDOrDie(env, keyRecognitionConfigClass, "data", "[B");
865ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
866ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass keyphraseRecognitionExtraClass = FindClassOrDie(env,
867ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                                           kKeyphraseRecognitionExtraClassPathName);
868ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionExtraClass = MakeGlobalRefOrDie(env, keyphraseRecognitionExtraClass);
869ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionExtraCstor = GetMethodIDOrDie(env, keyphraseRecognitionExtraClass,
870ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe            "<init>", "(III[Landroid/hardware/soundtrigger/SoundTrigger$ConfidenceLevel;)V");
871ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionExtraFields.id = GetFieldIDOrDie(env, gKeyphraseRecognitionExtraClass,
872ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                                          "id", "I");
873ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionExtraFields.recognitionModes = GetFieldIDOrDie(env,
874ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe            gKeyphraseRecognitionExtraClass, "recognitionModes", "I");
875ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionExtraFields.coarseConfidenceLevel = GetFieldIDOrDie(env,
876ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe            gKeyphraseRecognitionExtraClass, "coarseConfidenceLevel", "I");
877ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionExtraFields.confidenceLevels = GetFieldIDOrDie(env,
878ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe            gKeyphraseRecognitionExtraClass, "confidenceLevels",
879ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe            "[Landroid/hardware/soundtrigger/SoundTrigger$ConfidenceLevel;");
880ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
881ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass confidenceLevelClass = FindClassOrDie(env, kConfidenceLevelClassPathName);
882ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gConfidenceLevelClass = MakeGlobalRefOrDie(env, confidenceLevelClass);
883ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gConfidenceLevelCstor = GetMethodIDOrDie(env, confidenceLevelClass, "<init>", "(II)V");
884ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gConfidenceLevelFields.userId = GetFieldIDOrDie(env, confidenceLevelClass, "userId", "I");
885ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gConfidenceLevelFields.confidenceLevel = GetFieldIDOrDie(env, confidenceLevelClass,
886013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                             "confidenceLevel", "I");
88760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
888ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass audioFormatClass = FindClassOrDie(env, kAudioFormatClassPathName);
889ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gAudioFormatClass = MakeGlobalRefOrDie(env, audioFormatClass);
890ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gAudioFormatCstor = GetMethodIDOrDie(env, audioFormatClass, "<init>", "(III)V");
891d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
892ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass soundModelEventClass = FindClassOrDie(env, kSoundModelEventClassPathName);
893ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gSoundModelEventClass = MakeGlobalRefOrDie(env, soundModelEventClass);
894ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gSoundModelEventCstor = GetMethodIDOrDie(env, soundModelEventClass, "<init>", "(II[B)V");
89560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
896013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
897ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    RegisterMethodsOrDie(env, kSoundTriggerClassPathName, gMethods, NELEM(gMethods));
898ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    return RegisterMethodsOrDie(env, kModuleClassPathName, gModuleMethods, NELEM(gModuleMethods));
89960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
900