android_hardware_SoundTrigger.cpp revision a9470c19a9dfe362a201c569f14b669b7aa69ee4
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);
2688f89feba39be8e705c6b9f550c5af141a9e189daEric Laurent        env->DeleteLocalRef(jExtras);
26960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    } else {
27060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jEvent = env->NewObject(gRecognitionEventClass, gRecognitionEventCstor,
27160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                event->status, event->model, event->capture_available,
272013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                event->capture_session, event->capture_delay_ms,
273d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                event->capture_preamble_ms, event->trigger_in_data,
274d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                jAudioFormat, jData);
2758f89feba39be8e705c6b9f550c5af141a9e189daEric Laurent    }
2768f89feba39be8e705c6b9f550c5af141a9e189daEric Laurent
2778f89feba39be8e705c6b9f550c5af141a9e189daEric Laurent    if (jAudioFormat != NULL) {
278d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->DeleteLocalRef(jAudioFormat);
2798f89feba39be8e705c6b9f550c5af141a9e189daEric Laurent    }
2808f89feba39be8e705c6b9f550c5af141a9e189daEric Laurent    if (jData != NULL) {
281d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->DeleteLocalRef(jData);
28260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
28360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
28460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    env->CallStaticVoidMethod(mClass, gPostEventFromNative, mObject,
28560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                              SOUNDTRIGGER_EVENT_RECOGNITION, 0, 0, jEvent);
286d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
287d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    env->DeleteLocalRef(jEvent);
288d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    if (env->ExceptionCheck()) {
289d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        ALOGW("An exception occurred while notifying an event.");
290d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->ExceptionClear();
291d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    }
292d3b8223377b8046280e4c09e728edc600171f941Eric Laurent}
293d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
294d3b8223377b8046280e4c09e728edc600171f941Eric Laurentvoid JNISoundTriggerCallback::onSoundModelEvent(struct sound_trigger_model_event *event)
295d3b8223377b8046280e4c09e728edc600171f941Eric Laurent{
296d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    JNIEnv *env = AndroidRuntime::getJNIEnv();
297d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    jobject jEvent = NULL;
298d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    jbyteArray jData = NULL;
299d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
300d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    if (event->data_size) {
301d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        jData = env->NewByteArray(event->data_size);
302d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        jbyte *nData = env->GetByteArrayElements(jData, NULL);
303d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        memcpy(nData, (char *)event + event->data_offset, event->data_size);
304d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->ReleaseByteArrayElements(jData, nData, 0);
305d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    }
306d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
307d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    jEvent = env->NewObject(gSoundModelEventClass, gSoundModelEventCstor,
308d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                            event->status, event->model, jData);
309d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
310d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    env->DeleteLocalRef(jData);
311d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    env->CallStaticVoidMethod(mClass, gPostEventFromNative, mObject,
312d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                              SOUNDTRIGGER_EVENT_SOUNDMODEL, 0, 0, jEvent);
313d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    env->DeleteLocalRef(jEvent);
314d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    if (env->ExceptionCheck()) {
315d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        ALOGW("An exception occurred while notifying an event.");
316d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->ExceptionClear();
317d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    }
318d3b8223377b8046280e4c09e728edc600171f941Eric Laurent}
319d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
320d3b8223377b8046280e4c09e728edc600171f941Eric Laurentvoid JNISoundTriggerCallback::onServiceStateChange(sound_trigger_service_state_t state)
321d3b8223377b8046280e4c09e728edc600171f941Eric Laurent{
322d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    JNIEnv *env = AndroidRuntime::getJNIEnv();
323d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    env->CallStaticVoidMethod(mClass, gPostEventFromNative, mObject,
324d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                        SOUNDTRIGGER_EVENT_SERVICE_STATE_CHANGE, state, 0, NULL);
32560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (env->ExceptionCheck()) {
32660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGW("An exception occurred while notifying an event.");
32760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->ExceptionClear();
32860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
32960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
33060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
33160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentvoid JNISoundTriggerCallback::onServiceDied()
33260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
33360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    JNIEnv *env = AndroidRuntime::getJNIEnv();
33460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
33560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    env->CallStaticVoidMethod(mClass, gPostEventFromNative, mObject,
33660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                              SOUNDTRIGGER_EVENT_SERVICE_DIED, 0, 0, NULL);
33760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (env->ExceptionCheck()) {
33860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGW("An exception occurred while notifying an event.");
33960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->ExceptionClear();
34060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
34160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
34260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
34360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent// ----------------------------------------------------------------------------
34460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
34560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic sp<SoundTrigger> getSoundTrigger(JNIEnv* env, jobject thiz)
34660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
34760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    Mutex::Autolock l(gLock);
34860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SoundTrigger* const st = (SoundTrigger*)env->GetLongField(thiz,
34960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                                         gModuleFields.mNativeContext);
35060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return sp<SoundTrigger>(st);
35160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
35260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
35360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic sp<SoundTrigger> setSoundTrigger(JNIEnv* env, jobject thiz, const sp<SoundTrigger>& module)
35460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
35560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    Mutex::Autolock l(gLock);
35660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> old = (SoundTrigger*)env->GetLongField(thiz,
35760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                                         gModuleFields.mNativeContext);
35860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module.get()) {
35960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        module->incStrong((void*)setSoundTrigger);
36060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
36160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (old != 0) {
36260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        old->decStrong((void*)setSoundTrigger);
36360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
36460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    env->SetLongField(thiz, gModuleFields.mNativeContext, (jlong)module.get());
36560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return old;
36660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
36760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
36860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
36960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
37060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_listModules(JNIEnv *env, jobject clazz,
37160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                          jobject jModules)
37260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
37360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("listModules");
37460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
37560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jModules == NULL) {
37660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGE("listModules NULL AudioPatch ArrayList");
37760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_BAD_VALUE;
37860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
37960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (!env->IsInstanceOf(jModules, gArrayListClass)) {
38060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGE("listModules not an arraylist");
38160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_BAD_VALUE;
38260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
38360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
38460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    unsigned int numModules = 0;
38560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    struct sound_trigger_module_descriptor *nModules = NULL;
38660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
38760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status_t status = SoundTrigger::listModules(nModules, &numModules);
38860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (status != NO_ERROR || numModules == 0) {
38960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return (jint)status;
39060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
39160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
39260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nModules = (struct sound_trigger_module_descriptor *)
39360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                            calloc(numModules, sizeof(struct sound_trigger_module_descriptor));
39460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
39560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = SoundTrigger::listModules(nModules, &numModules);
39660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("listModules SoundTrigger::listModules status %d numModules %d", status, numModules);
39760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
39860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (status != NO_ERROR) {
39960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        numModules = 0;
40060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
40160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
40260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    for (size_t i = 0; i < numModules; i++) {
40360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        char str[SOUND_TRIGGER_MAX_STRING_LEN];
40460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
40560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jstring implementor = env->NewStringUTF(nModules[i].properties.implementor);
40660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jstring description = env->NewStringUTF(nModules[i].properties.description);
40760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        SoundTrigger::guidToString(&nModules[i].properties.uuid,
40860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                   str,
40960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                   SOUND_TRIGGER_MAX_STRING_LEN);
41060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jstring uuid = env->NewStringUTF(str);
41160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
412d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        ALOGV("listModules module %zu id %d description %s maxSoundModels %d",
41360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent              i, nModules[i].handle, nModules[i].properties.description,
41460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent              nModules[i].properties.max_sound_models);
41560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
41660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jobject newModuleDesc = env->NewObject(gModulePropertiesClass, gModulePropertiesCstor,
41760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].handle,
41860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               implementor, description, uuid,
41960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.version,
42060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.max_sound_models,
42160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.max_key_phrases,
42260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.max_users,
42360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.recognition_modes,
42460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.capture_transition,
42560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.max_buffer_ms,
42660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.concurrent_capture,
427d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                               nModules[i].properties.power_consumption_mw,
428d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                               nModules[i].properties.trigger_in_event);
42960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
43060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->DeleteLocalRef(implementor);
43160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->DeleteLocalRef(description);
43260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->DeleteLocalRef(uuid);
43360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (newModuleDesc == NULL) {
43460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            status = SOUNDTRIGGER_STATUS_ERROR;
43560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            goto exit;
43660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
43760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->CallBooleanMethod(jModules, gArrayListMethods.add, newModuleDesc);
43860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
43960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
44060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentexit:
44160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    free(nModules);
44260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return (jint) status;
44360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
44460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
44560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic void
44660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_setup(JNIEnv *env, jobject thiz, jobject weak_this)
44760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
44860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("setup");
44960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
45060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<JNISoundTriggerCallback> callback = new JNISoundTriggerCallback(env, thiz, weak_this);
45160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
45260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sound_trigger_module_handle_t handle =
45360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            (sound_trigger_module_handle_t)env->GetIntField(thiz, gModuleFields.mId);
45460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
45560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = SoundTrigger::attach(handle, callback);
45660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == 0) {
45760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return;
45860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
45960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
46060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    setSoundTrigger(env, thiz, module);
46160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
46260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
46360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic void
46460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_detach(JNIEnv *env, jobject thiz)
46560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
46660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("detach");
46760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = setSoundTrigger(env, thiz, 0);
46860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("detach module %p", module.get());
46960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module != 0) {
47060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGV("detach module->detach()");
47160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        module->detach();
47260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
47360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
47460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
47560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic void
47660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_finalize(JNIEnv *env, jobject thiz)
47760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
47860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("finalize");
47960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
48060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module != 0) {
48160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGW("SoundTrigger finalized without being detached");
48260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
48360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    android_hardware_SoundTrigger_detach(env, thiz);
48460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
48560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
48660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
48760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_loadSoundModel(JNIEnv *env, jobject thiz,
48860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                             jobject jSoundModel, jintArray jHandle)
48960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
49060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint status = SOUNDTRIGGER_STATUS_OK;
491013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jbyte *nData = NULL;
49260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    struct sound_trigger_sound_model *nSoundModel;
49360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jbyteArray jData;
49460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<MemoryDealer> memoryDealer;
49560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<IMemory> memory;
49660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    size_t size;
49760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sound_model_handle_t handle;
498d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jobject jUuid;
499d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jstring jUuidString;
500d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    const char *nUuidString;
50160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
50260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("loadSoundModel");
50360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
50460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == NULL) {
50560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
50660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
50760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jHandle == NULL) {
50860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_BAD_VALUE;
50960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
51060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jsize jHandleLen = env->GetArrayLength(jHandle);
51160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jHandleLen == 0) {
51260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_BAD_VALUE;
51360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
51460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint *nHandle = env->GetIntArrayElements(jHandle, NULL);
51560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (nHandle == NULL) {
51660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
51760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
51860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (!env->IsInstanceOf(jSoundModel, gSoundModelClass)) {
51960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_BAD_VALUE;
52060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
52160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
52260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    size_t offset;
52360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sound_trigger_sound_model_type_t type;
524d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    if (env->IsInstanceOf(jSoundModel, gKeyphraseSoundModelClass)) {
52560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        offset = sizeof(struct sound_trigger_phrase_sound_model);
52660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        type = SOUND_MODEL_TYPE_KEYPHRASE;
52760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    } else {
52860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        offset = sizeof(struct sound_trigger_sound_model);
52960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        type = SOUND_MODEL_TYPE_UNKNOWN;
53060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
531d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha
532d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jUuid = env->GetObjectField(jSoundModel, gSoundModelFields.uuid);
533d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jUuidString = (jstring)env->CallObjectMethod(jUuid, gUUIDMethods.toString);
534d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    nUuidString = env->GetStringUTFChars(jUuidString, NULL);
535d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    sound_trigger_uuid_t nUuid;
536d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    SoundTrigger::stringToGuid(nUuidString, &nUuid);
537d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    env->ReleaseStringUTFChars(jUuidString, nUuidString);
538d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    env->DeleteLocalRef(jUuidString);
539d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha
540d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    sound_trigger_uuid_t nVendorUuid;
541d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    jUuid = env->GetObjectField(jSoundModel, gSoundModelFields.vendorUuid);
542d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    if (jUuid != NULL) {
543d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        jUuidString = (jstring)env->CallObjectMethod(jUuid, gUUIDMethods.toString);
544d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        nUuidString = env->GetStringUTFChars(jUuidString, NULL);
545d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        SoundTrigger::stringToGuid(nUuidString, &nVendorUuid);
546d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->ReleaseStringUTFChars(jUuidString, nUuidString);
547d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        env->DeleteLocalRef(jUuidString);
548d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    } else {
549d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        SoundTrigger::stringToGuid("00000000-0000-0000-0000-000000000000", &nVendorUuid);
550d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    }
551d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
55260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jData = (jbyteArray)env->GetObjectField(jSoundModel, gSoundModelFields.data);
55360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jData == NULL) {
55460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_BAD_VALUE;
55560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
55660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
55760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    size = env->GetArrayLength(jData);
55860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
559013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    nData = env->GetByteArrayElements(jData, NULL);
56060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jData == NULL) {
56160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_ERROR;
56260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
56360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
56460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
56560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    memoryDealer = new MemoryDealer(offset + size, "SoundTrigge-JNI::LoadModel");
56660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (memoryDealer == 0) {
56760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_ERROR;
56860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
56960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
57060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    memory = memoryDealer->allocate(offset + size);
57160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (memory == 0 || memory->pointer() == NULL) {
57260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_ERROR;
57360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
57460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
57560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
57660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nSoundModel = (struct sound_trigger_sound_model *)memory->pointer();
57760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
57860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nSoundModel->type = type;
579d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    nSoundModel->uuid = nUuid;
580d3b8223377b8046280e4c09e728edc600171f941Eric Laurent    nSoundModel->vendor_uuid = nVendorUuid;
58160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nSoundModel->data_size = size;
58260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nSoundModel->data_offset = offset;
58360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    memcpy((char *)nSoundModel + offset, nData, size);
58460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (type == SOUND_MODEL_TYPE_KEYPHRASE) {
58560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        struct sound_trigger_phrase_sound_model *phraseModel =
58660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                (struct sound_trigger_phrase_sound_model *)nSoundModel;
58760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
58860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jobjectArray jPhrases =
589d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha            (jobjectArray)env->GetObjectField(jSoundModel, gKeyphraseSoundModelFields.keyphrases);
59060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (jPhrases == NULL) {
59160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            status = SOUNDTRIGGER_STATUS_BAD_VALUE;
59260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            goto exit;
59360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
59460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
59560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        size_t numPhrases = env->GetArrayLength(jPhrases);
59660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        phraseModel->num_phrases = numPhrases;
597d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        ALOGV("loadSoundModel numPhrases %zu", numPhrases);
59860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        for (size_t i = 0; i < numPhrases; i++) {
59960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            jobject jPhrase = env->GetObjectArrayElement(jPhrases, i);
600d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha            phraseModel->phrases[i].id =
601d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                    env->GetIntField(jPhrase,gKeyphraseFields.id);
60260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            phraseModel->phrases[i].recognition_mode =
603d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                    env->GetIntField(jPhrase,gKeyphraseFields.recognitionModes);
604013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
605013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            jintArray jUsers = (jintArray)env->GetObjectField(jPhrase, gKeyphraseFields.users);
606013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            phraseModel->phrases[i].num_users = env->GetArrayLength(jUsers);
607013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            jint *nUsers = env->GetIntArrayElements(jUsers, NULL);
608013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            memcpy(phraseModel->phrases[i].users,
609013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                   nUsers,
610013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                   phraseModel->phrases[i].num_users * sizeof(int));
611013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            env->ReleaseIntArrayElements(jUsers, nUsers, 0);
612013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            env->DeleteLocalRef(jUsers);
613013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
614d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha            jstring jLocale = (jstring)env->GetObjectField(jPhrase, gKeyphraseFields.locale);
61560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            const char *nLocale = env->GetStringUTFChars(jLocale, NULL);
61660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            strncpy(phraseModel->phrases[i].locale,
61760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                    nLocale,
61860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                    SOUND_TRIGGER_MAX_LOCALE_LEN);
619d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha            jstring jText = (jstring)env->GetObjectField(jPhrase, gKeyphraseFields.text);
62060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            const char *nText = env->GetStringUTFChars(jText, NULL);
62160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            strncpy(phraseModel->phrases[i].text,
62260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                    nText,
62360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                    SOUND_TRIGGER_MAX_STRING_LEN);
62460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
62560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->ReleaseStringUTFChars(jLocale, nLocale);
62660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->DeleteLocalRef(jLocale);
62760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->ReleaseStringUTFChars(jText, nText);
62860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->DeleteLocalRef(jText);
629d3b8223377b8046280e4c09e728edc600171f941Eric Laurent            ALOGV("loadSoundModel phrases %zu text %s locale %s",
63060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                  i, phraseModel->phrases[i].text, phraseModel->phrases[i].locale);
631013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            env->DeleteLocalRef(jPhrase);
63260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
63360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->DeleteLocalRef(jPhrases);
63460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
63560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = module->loadSoundModel(memory, &handle);
63660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("loadSoundModel status %d handle %d", status, handle);
63760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
63860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentexit:
63960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (nHandle != NULL) {
64060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        nHandle[0] = (jint)handle;
64160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->ReleaseIntArrayElements(jHandle, nHandle, NULL);
64260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
64360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (nData != NULL) {
644013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        env->ReleaseByteArrayElements(jData, nData, NULL);
64560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
64660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return status;
64760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
64860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
64960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
65060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_unloadSoundModel(JNIEnv *env, jobject thiz,
65160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               jint jHandle)
65260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
65360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint status = SOUNDTRIGGER_STATUS_OK;
65460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("unloadSoundModel");
65560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
65660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == NULL) {
65760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
65860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
65960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = module->unloadSoundModel((sound_model_handle_t)jHandle);
66060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
66160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return status;
66260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
66360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
66460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
66560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_startRecognition(JNIEnv *env, jobject thiz,
666013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                               jint jHandle, jobject jConfig)
66760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
66860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint status = SOUNDTRIGGER_STATUS_OK;
66960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("startRecognition");
67060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
67160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == NULL) {
67260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
67360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
674013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
675013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    if (!env->IsInstanceOf(jConfig, gRecognitionConfigClass)) {
676013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        return SOUNDTRIGGER_STATUS_BAD_VALUE;
677013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    }
678013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
679013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jbyteArray jData = (jbyteArray)env->GetObjectField(jConfig, gRecognitionConfigFields.data);
68060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jsize dataSize = 0;
681013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jbyte *nData = NULL;
68260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jData != NULL) {
68360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        dataSize = env->GetArrayLength(jData);
68460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (dataSize == 0) {
68560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            return SOUNDTRIGGER_STATUS_BAD_VALUE;
68660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
687013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        nData = env->GetByteArrayElements(jData, NULL);
68860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (nData == NULL) {
68960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            return SOUNDTRIGGER_STATUS_ERROR;
69060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
691013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    }
692013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
693013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    size_t totalSize = sizeof(struct sound_trigger_recognition_config) + dataSize;
694013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    sp<MemoryDealer> memoryDealer =
695013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            new MemoryDealer(totalSize, "SoundTrigge-JNI::StartRecognition");
696013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    if (memoryDealer == 0) {
697013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
698013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    }
699013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    sp<IMemory> memory = memoryDealer->allocate(totalSize);
700013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    if (memory == 0 || memory->pointer() == NULL) {
701013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
702013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    }
703013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    if (dataSize != 0) {
704013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        memcpy((char *)memory->pointer() + sizeof(struct sound_trigger_recognition_config),
705013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                nData,
706013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                dataSize);
707013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        env->ReleaseByteArrayElements(jData, nData, 0);
708013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    }
709013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    env->DeleteLocalRef(jData);
710013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    struct sound_trigger_recognition_config *config =
711013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                    (struct sound_trigger_recognition_config *)memory->pointer();
712013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    config->data_size = dataSize;
713013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    config->data_offset = sizeof(struct sound_trigger_recognition_config);
714013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    config->capture_requested = env->GetIntField(jConfig,
715013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                 gRecognitionConfigFields.captureRequested);
716013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
717013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    config->num_phrases = 0;
718013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    jobjectArray jPhrases =
719013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        (jobjectArray)env->GetObjectField(jConfig, gRecognitionConfigFields.keyphrases);
720013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    if (jPhrases != NULL) {
721013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        config->num_phrases = env->GetArrayLength(jPhrases);
722013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    }
723013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    ALOGV("startRecognition num phrases %d", config->num_phrases);
724013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    for (size_t i = 0; i < config->num_phrases; i++) {
725013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        jobject jPhrase = env->GetObjectArrayElement(jPhrases, i);
726013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        config->phrases[i].id = env->GetIntField(jPhrase,
727013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                gKeyphraseRecognitionExtraFields.id);
728013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        config->phrases[i].recognition_modes = env->GetIntField(jPhrase,
729013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                gKeyphraseRecognitionExtraFields.recognitionModes);
730d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        config->phrases[i].confidence_level = env->GetIntField(jPhrase,
731d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                            gKeyphraseRecognitionExtraFields.coarseConfidenceLevel);
732013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        config->phrases[i].num_levels = 0;
733013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        jobjectArray jConfidenceLevels = (jobjectArray)env->GetObjectField(jPhrase,
734013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                gKeyphraseRecognitionExtraFields.confidenceLevels);
735013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        if (jConfidenceLevels != NULL) {
736013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            config->phrases[i].num_levels = env->GetArrayLength(jConfidenceLevels);
73760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
738d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        ALOGV("startRecognition phrase %zu num_levels %d", i, config->phrases[i].num_levels);
739013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        for (size_t j = 0; j < config->phrases[i].num_levels; j++) {
740013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            jobject jConfidenceLevel = env->GetObjectArrayElement(jConfidenceLevels, j);
741013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            config->phrases[i].levels[j].user_id = env->GetIntField(jConfidenceLevel,
742013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                                    gConfidenceLevelFields.userId);
743013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            config->phrases[i].levels[j].level = env->GetIntField(jConfidenceLevel,
744013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                          gConfidenceLevelFields.confidenceLevel);
745013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent            env->DeleteLocalRef(jConfidenceLevel);
74660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
747d3b8223377b8046280e4c09e728edc600171f941Eric Laurent        ALOGV("startRecognition phrases %zu", i);
748013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        env->DeleteLocalRef(jConfidenceLevels);
749013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        env->DeleteLocalRef(jPhrase);
75060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
751013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent    env->DeleteLocalRef(jPhrases);
75260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
75360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = module->startRecognition(jHandle, memory);
75460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return status;
75560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
75660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
75760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
75860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_stopRecognition(JNIEnv *env, jobject thiz,
75960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               jint jHandle)
76060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
76160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint status = SOUNDTRIGGER_STATUS_OK;
76260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("stopRecognition");
76360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
76460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == NULL) {
76560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
76660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
76760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = module->stopRecognition(jHandle);
76860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return status;
76960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
77060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
77160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic JNINativeMethod gMethods[] = {
77260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"listModules",
77360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(Ljava/util/ArrayList;)I",
77460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_listModules},
77560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent};
77660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
77760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
77860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic JNINativeMethod gModuleMethods[] = {
77960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"native_setup",
78060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(Ljava/lang/Object;)V",
78160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_setup},
78260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"native_finalize",
78360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "()V",
78460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_finalize},
78560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"detach",
78660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "()V",
78760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_detach},
78860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"loadSoundModel",
78960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(Landroid/hardware/soundtrigger/SoundTrigger$SoundModel;[I)I",
79060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_loadSoundModel},
79160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"unloadSoundModel",
79260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(I)I",
79360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_unloadSoundModel},
79460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"startRecognition",
795013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent        "(ILandroid/hardware/soundtrigger/SoundTrigger$RecognitionConfig;)I",
79660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_startRecognition},
79760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"stopRecognition",
79860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(I)I",
79960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_stopRecognition},
80060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent};
80160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
80260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentint register_android_hardware_SoundTrigger(JNIEnv *env)
80360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
804ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass arrayListClass = FindClassOrDie(env, "java/util/ArrayList");
805ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gArrayListClass = MakeGlobalRefOrDie(env, arrayListClass);
806ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gArrayListMethods.add = GetMethodIDOrDie(env, arrayListClass, "add", "(Ljava/lang/Object;)Z");
807ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
808ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass uuidClass = FindClassOrDie(env, "java/util/UUID");
809ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gUUIDClass = MakeGlobalRefOrDie(env, uuidClass);
810ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gUUIDMethods.toString = GetMethodIDOrDie(env, uuidClass, "toString", "()Ljava/lang/String;");
811ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
812ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass lClass = FindClassOrDie(env, kSoundTriggerClassPathName);
813ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gSoundTriggerClass = MakeGlobalRefOrDie(env, lClass);
814ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
815ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass moduleClass = FindClassOrDie(env, kModuleClassPathName);
816ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gModuleClass = MakeGlobalRefOrDie(env, moduleClass);
817ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gPostEventFromNative = GetStaticMethodIDOrDie(env, moduleClass, "postEventFromNative",
818ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                                  "(Ljava/lang/Object;IIILjava/lang/Object;)V");
819ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gModuleFields.mNativeContext = GetFieldIDOrDie(env, moduleClass, "mNativeContext", "J");
820ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gModuleFields.mId = GetFieldIDOrDie(env, moduleClass, "mId", "I");
821ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
822ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass modulePropertiesClass = FindClassOrDie(env, kModulePropertiesClassPathName);
823ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gModulePropertiesClass = MakeGlobalRefOrDie(env, modulePropertiesClass);
824ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gModulePropertiesCstor = GetMethodIDOrDie(env, modulePropertiesClass, "<init>",
825ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe            "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;IIIIIZIZIZ)V");
826ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
827ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass soundModelClass = FindClassOrDie(env, kSoundModelClassPathName);
828ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gSoundModelClass = MakeGlobalRefOrDie(env, soundModelClass);
829ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gSoundModelFields.uuid = GetFieldIDOrDie(env, soundModelClass, "uuid", "Ljava/util/UUID;");
830ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gSoundModelFields.vendorUuid = GetFieldIDOrDie(env, soundModelClass, "vendorUuid",
831ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                                   "Ljava/util/UUID;");
832ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gSoundModelFields.data = GetFieldIDOrDie(env, soundModelClass, "data", "[B");
833ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
834ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass keyphraseClass = FindClassOrDie(env, kKeyphraseClassPathName);
835ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseClass = MakeGlobalRefOrDie(env, keyphraseClass);
836ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseFields.id = GetFieldIDOrDie(env, keyphraseClass, "id", "I");
837ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseFields.recognitionModes = GetFieldIDOrDie(env, keyphraseClass, "recognitionModes",
838ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                                        "I");
839ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseFields.locale = GetFieldIDOrDie(env, keyphraseClass, "locale", "Ljava/lang/String;");
840ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseFields.text = GetFieldIDOrDie(env, keyphraseClass, "text", "Ljava/lang/String;");
841ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseFields.users = GetFieldIDOrDie(env, keyphraseClass, "users", "[I");
842ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
843ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass keyphraseSoundModelClass = FindClassOrDie(env, kKeyphraseSoundModelClassPathName);
844ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseSoundModelClass = MakeGlobalRefOrDie(env, keyphraseSoundModelClass);
845ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseSoundModelFields.keyphrases = GetFieldIDOrDie(env, keyphraseSoundModelClass,
846d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                         "keyphrases",
847d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                         "[Landroid/hardware/soundtrigger/SoundTrigger$Keyphrase;");
84860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
849ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass recognitionEventClass = FindClassOrDie(env, kRecognitionEventClassPathName);
850ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gRecognitionEventClass = MakeGlobalRefOrDie(env, recognitionEventClass);
851ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gRecognitionEventCstor = GetMethodIDOrDie(env, recognitionEventClass, "<init>",
852d3b8223377b8046280e4c09e728edc600171f941Eric Laurent                                              "(IIZIIIZLandroid/media/AudioFormat;[B)V");
85360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
854ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass keyphraseRecognitionEventClass = FindClassOrDie(env,
855ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                                           kKeyphraseRecognitionEventClassPathName);
856ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionEventClass = MakeGlobalRefOrDie(env, keyphraseRecognitionEventClass);
857ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionEventCstor = GetMethodIDOrDie(env, keyphraseRecognitionEventClass, "<init>",
858d3b8223377b8046280e4c09e728edc600171f941Eric Laurent              "(IIZIIIZLandroid/media/AudioFormat;[B[Landroid/hardware/soundtrigger/SoundTrigger$KeyphraseRecognitionExtra;)V");
859013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
86060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
861ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass keyRecognitionConfigClass = FindClassOrDie(env, kRecognitionConfigClassPathName);
862ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gRecognitionConfigClass = MakeGlobalRefOrDie(env, keyRecognitionConfigClass);
863ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gRecognitionConfigFields.captureRequested = GetFieldIDOrDie(env, keyRecognitionConfigClass,
864ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                                                "captureRequested", "Z");
865ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gRecognitionConfigFields.keyphrases = GetFieldIDOrDie(env, keyRecognitionConfigClass,
866ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe           "keyphrases", "[Landroid/hardware/soundtrigger/SoundTrigger$KeyphraseRecognitionExtra;");
867ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gRecognitionConfigFields.data = GetFieldIDOrDie(env, keyRecognitionConfigClass, "data", "[B");
868ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
869ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass keyphraseRecognitionExtraClass = FindClassOrDie(env,
870ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                                           kKeyphraseRecognitionExtraClassPathName);
871ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionExtraClass = MakeGlobalRefOrDie(env, keyphraseRecognitionExtraClass);
872ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionExtraCstor = GetMethodIDOrDie(env, keyphraseRecognitionExtraClass,
873ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe            "<init>", "(III[Landroid/hardware/soundtrigger/SoundTrigger$ConfidenceLevel;)V");
874ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionExtraFields.id = GetFieldIDOrDie(env, gKeyphraseRecognitionExtraClass,
875ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe                                                          "id", "I");
876ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionExtraFields.recognitionModes = GetFieldIDOrDie(env,
877ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe            gKeyphraseRecognitionExtraClass, "recognitionModes", "I");
878ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionExtraFields.coarseConfidenceLevel = GetFieldIDOrDie(env,
879ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe            gKeyphraseRecognitionExtraClass, "coarseConfidenceLevel", "I");
880ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gKeyphraseRecognitionExtraFields.confidenceLevels = GetFieldIDOrDie(env,
881ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe            gKeyphraseRecognitionExtraClass, "confidenceLevels",
882ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe            "[Landroid/hardware/soundtrigger/SoundTrigger$ConfidenceLevel;");
883ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe
884ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass confidenceLevelClass = FindClassOrDie(env, kConfidenceLevelClassPathName);
885ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gConfidenceLevelClass = MakeGlobalRefOrDie(env, confidenceLevelClass);
886ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gConfidenceLevelCstor = GetMethodIDOrDie(env, confidenceLevelClass, "<init>", "(II)V");
887ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gConfidenceLevelFields.userId = GetFieldIDOrDie(env, confidenceLevelClass, "userId", "I");
888ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gConfidenceLevelFields.confidenceLevel = GetFieldIDOrDie(env, confidenceLevelClass,
889013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent                                                             "confidenceLevel", "I");
89060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
891ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass audioFormatClass = FindClassOrDie(env, kAudioFormatClassPathName);
892ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gAudioFormatClass = MakeGlobalRefOrDie(env, audioFormatClass);
893a9470c19a9dfe362a201c569f14b669b7aa69ee4Andy Hung    gAudioFormatCstor = GetMethodIDOrDie(env, audioFormatClass, "<init>", "(IIII)V");
894d3b8223377b8046280e4c09e728edc600171f941Eric Laurent
895ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    jclass soundModelEventClass = FindClassOrDie(env, kSoundModelEventClassPathName);
896ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gSoundModelEventClass = MakeGlobalRefOrDie(env, soundModelEventClass);
897ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    gSoundModelEventCstor = GetMethodIDOrDie(env, soundModelEventClass, "<init>", "(II[B)V");
89860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
899013f66b92db609fceeff9c8171daca13d057cc95Eric Laurent
900ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    RegisterMethodsOrDie(env, kSoundTriggerClassPathName, gMethods, NELEM(gMethods));
901ed6b9dff563c5e22f040ff37e12c0d771e0478aeAndreas Gampe    return RegisterMethodsOrDie(env, kModuleClassPathName, gModuleMethods, NELEM(gModuleMethods));
90260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
903