android_hardware_SoundTrigger.cpp revision d4233c68fc17f0909e9e36494db85a634f8e2665
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"
2460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent#include "android_runtime/AndroidRuntime.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>
3260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
3360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentusing namespace android;
3460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
3560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jclass gArrayListClass;
3660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic struct {
3760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jmethodID    add;
3860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent} gArrayListMethods;
3960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
40d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic jclass gUUIDClass;
41d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic struct {
42d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jmethodID    toString;
43d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha} gUUIDMethods;
44d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha
4560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic const char* const kSoundTriggerClassPathName = "android/hardware/soundtrigger/SoundTrigger";
4660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jclass gSoundTriggerClass;
4760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
4860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic const char* const kModuleClassPathName = "android/hardware/soundtrigger/SoundTriggerModule";
4960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jclass gModuleClass;
5060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic struct {
5160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jfieldID    mNativeContext;
5260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jfieldID    mId;
5360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent} gModuleFields;
5460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jmethodID   gPostEventFromNative;
5560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
5660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic const char* const kModulePropertiesClassPathName =
5760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                     "android/hardware/soundtrigger/SoundTrigger$ModuleProperties";
5860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jclass gModulePropertiesClass;
5960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jmethodID   gModulePropertiesCstor;
6060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
6160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic const char* const kSoundModelClassPathName =
6260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                     "android/hardware/soundtrigger/SoundTrigger$SoundModel";
6360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jclass gSoundModelClass;
6460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic struct {
65d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jfieldID    uuid;
6660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jfieldID    data;
6760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent} gSoundModelFields;
6860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
69d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic const char* const kKeyphraseClassPathName =
70d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                     "android/hardware/soundtrigger/SoundTrigger$Keyphrase";
71d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic jclass gKeyphraseClass;
7260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic struct {
73d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jfieldID id;
7460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jfieldID recognitionModes;
7560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jfieldID locale;
7660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jfieldID text;
7760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jfieldID numUsers;
78d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha} gKeyphraseFields;
7960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
80d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic const char* const kKeyphraseSoundModelClassPathName =
81d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                 "android/hardware/soundtrigger/SoundTrigger$KeyphraseSoundModel";
82d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic jclass gKeyphraseSoundModelClass;
8360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic struct {
84d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jfieldID    keyphrases;
85d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha} gKeyphraseSoundModelFields;
8660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
8760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
8860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic const char* const kRecognitionEventClassPathName =
8960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                     "android/hardware/soundtrigger/SoundTrigger$RecognitionEvent";
9060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jclass gRecognitionEventClass;
9160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jmethodID   gRecognitionEventCstor;
9260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
93d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic const char* const kKeyphraseRecognitionEventClassPathName =
94d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                             "android/hardware/soundtrigger/SoundTrigger$KeyphraseRecognitionEvent";
95d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic jclass gKeyphraseRecognitionEventClass;
96d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic jmethodID   gKeyphraseRecognitionEventCstor;
9760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
98d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic const char* const kKeyphraseRecognitionExtraClassPathName =
99d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                             "android/hardware/soundtrigger/SoundTrigger$KeyphraseRecognitionExtra";
100d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic jclass gKeyphraseRecognitionExtraClass;
101d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddharthastatic jmethodID   gKeyphraseRecognitionExtraCstor;
10260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
10360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic Mutex gLock;
10460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
10560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentenum {
10660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_STATUS_OK = 0,
10760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_STATUS_ERROR = INT_MIN,
10860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_PERMISSION_DENIED = -1,
10960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_STATUS_NO_INIT = -19,
11060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_STATUS_BAD_VALUE = -22,
11160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_STATUS_DEAD_OBJECT = -32,
11260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_INVALID_OPERATION = -38,
11360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent};
11460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
11560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentenum  {
11660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_EVENT_RECOGNITION = 1,
11760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SOUNDTRIGGER_EVENT_SERVICE_DIED = 2,
11860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent};
11960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
12060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent// ----------------------------------------------------------------------------
12160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent// ref-counted object for callbacks
12260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentclass JNISoundTriggerCallback: public SoundTriggerCallback
12360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
12460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentpublic:
12560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    JNISoundTriggerCallback(JNIEnv* env, jobject thiz, jobject weak_thiz);
12660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ~JNISoundTriggerCallback();
12760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
12860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    virtual void onRecognitionEvent(struct sound_trigger_recognition_event *event);
12960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    virtual void onServiceDied();
13060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
13160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentprivate:
13260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jclass      mClass;     // Reference to SoundTrigger class
13360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jobject     mObject;    // Weak ref to SoundTrigger Java object to call on
13460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent};
13560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
13660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric LaurentJNISoundTriggerCallback::JNISoundTriggerCallback(JNIEnv* env, jobject thiz, jobject weak_thiz)
13760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
13860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
13960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    // Hold onto the SoundTriggerModule class for use in calling the static method
14060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    // that posts events to the application thread.
14160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jclass clazz = env->GetObjectClass(thiz);
14260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (clazz == NULL) {
14360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGE("Can't find class %s", kModuleClassPathName);
14460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return;
14560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
14660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    mClass = (jclass)env->NewGlobalRef(clazz);
14760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
14860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    // We use a weak reference so the SoundTriggerModule object can be garbage collected.
14960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    // The reference is only used as a proxy for callbacks.
15060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    mObject  = env->NewGlobalRef(weak_thiz);
15160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
15260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
15360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric LaurentJNISoundTriggerCallback::~JNISoundTriggerCallback()
15460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
15560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    // remove global references
15660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    JNIEnv *env = AndroidRuntime::getJNIEnv();
15760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    env->DeleteGlobalRef(mObject);
15860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    env->DeleteGlobalRef(mClass);
15960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
16060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
16160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentvoid JNISoundTriggerCallback::onRecognitionEvent(struct sound_trigger_recognition_event *event)
16260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
16360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    JNIEnv *env = AndroidRuntime::getJNIEnv();
16460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
16560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jobject jEvent;
16660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
16760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jbyteArray jData = NULL;
16860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (event->data_size) {
16960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jData = env->NewByteArray(event->data_size);
17060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jbyte *nData = env->GetByteArrayElements(jData, NULL);
17160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        memcpy(nData, (char *)event + event->data_offset, event->data_size);
17260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->ReleaseByteArrayElements(jData, nData, 0);
17360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
17460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
17560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (event->type == SOUND_MODEL_TYPE_KEYPHRASE) {
17660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        struct sound_trigger_phrase_recognition_event *phraseEvent =
17760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                (struct sound_trigger_phrase_recognition_event *)event;
17860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
17960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jobjectArray jExtras = env->NewObjectArray(phraseEvent->num_phrases,
180d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                                  gKeyphraseRecognitionExtraClass, NULL);
18160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (jExtras == NULL) {
18260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            return;
18360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
18460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
18560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        for (size_t i = 0; i < phraseEvent->num_phrases; i++) {
18660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            jintArray jConfidenceLevels = env->NewIntArray(phraseEvent->phrase_extras[i].num_users);
18760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            if (jConfidenceLevels == NULL) {
18860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                return;
18960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            }
19060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            jint *nConfidenceLevels = env->GetIntArrayElements(jConfidenceLevels, NULL);
19160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            memcpy(nConfidenceLevels,
19260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                   phraseEvent->phrase_extras[i].confidence_levels,
19360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                   phraseEvent->phrase_extras[i].num_users * sizeof(int));
19460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->ReleaseIntArrayElements(jConfidenceLevels, nConfidenceLevels, 0);
195d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha            jobject jNewExtra = env->NewObject(gKeyphraseRecognitionExtraClass,
196d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                               gKeyphraseRecognitionExtraCstor,
19760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               jConfidenceLevels,
19860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               phraseEvent->phrase_extras[i].recognition_modes);
19960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
20060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            if (jNewExtra == NULL) {
20160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                return;
20260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            }
20360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->SetObjectArrayElement(jExtras, i, jNewExtra);
20460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
20560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
206d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha        jEvent = env->NewObject(gKeyphraseRecognitionEventClass, gKeyphraseRecognitionEventCstor,
20760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                event->status, event->model, event->capture_available,
20860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                               event->capture_session, event->capture_delay_ms, jData,
20960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                               phraseEvent->key_phrase_in_capture, jExtras);
21060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    } else {
21160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jEvent = env->NewObject(gRecognitionEventClass, gRecognitionEventCstor,
21260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                event->status, event->model, event->capture_available,
21360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                event->capture_session, event->capture_delay_ms, jData);
21460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
21560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
21660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
21760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    env->CallStaticVoidMethod(mClass, gPostEventFromNative, mObject,
21860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                              SOUNDTRIGGER_EVENT_RECOGNITION, 0, 0, jEvent);
21960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (env->ExceptionCheck()) {
22060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGW("An exception occurred while notifying an event.");
22160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->ExceptionClear();
22260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
22360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
22460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
22560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentvoid JNISoundTriggerCallback::onServiceDied()
22660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
22760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    JNIEnv *env = AndroidRuntime::getJNIEnv();
22860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
22960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    env->CallStaticVoidMethod(mClass, gPostEventFromNative, mObject,
23060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                              SOUNDTRIGGER_EVENT_SERVICE_DIED, 0, 0, NULL);
23160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (env->ExceptionCheck()) {
23260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGW("An exception occurred while notifying an event.");
23360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->ExceptionClear();
23460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
23560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
23660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
23760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent// ----------------------------------------------------------------------------
23860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
23960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic sp<SoundTrigger> getSoundTrigger(JNIEnv* env, jobject thiz)
24060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
24160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    Mutex::Autolock l(gLock);
24260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    SoundTrigger* const st = (SoundTrigger*)env->GetLongField(thiz,
24360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                                         gModuleFields.mNativeContext);
24460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return sp<SoundTrigger>(st);
24560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
24660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
24760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic sp<SoundTrigger> setSoundTrigger(JNIEnv* env, jobject thiz, const sp<SoundTrigger>& module)
24860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
24960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    Mutex::Autolock l(gLock);
25060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> old = (SoundTrigger*)env->GetLongField(thiz,
25160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                                         gModuleFields.mNativeContext);
25260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module.get()) {
25360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        module->incStrong((void*)setSoundTrigger);
25460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
25560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (old != 0) {
25660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        old->decStrong((void*)setSoundTrigger);
25760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
25860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    env->SetLongField(thiz, gModuleFields.mNativeContext, (jlong)module.get());
25960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return old;
26060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
26160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
26260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
26360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
26460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_listModules(JNIEnv *env, jobject clazz,
26560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                          jobject jModules)
26660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
26760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("listModules");
26860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
26960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jModules == NULL) {
27060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGE("listModules NULL AudioPatch ArrayList");
27160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_BAD_VALUE;
27260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
27360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (!env->IsInstanceOf(jModules, gArrayListClass)) {
27460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGE("listModules not an arraylist");
27560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_BAD_VALUE;
27660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
27760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
27860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    unsigned int numModules = 0;
27960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    struct sound_trigger_module_descriptor *nModules = NULL;
28060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
28160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status_t status = SoundTrigger::listModules(nModules, &numModules);
28260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (status != NO_ERROR || numModules == 0) {
28360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return (jint)status;
28460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
28560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
28660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nModules = (struct sound_trigger_module_descriptor *)
28760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                            calloc(numModules, sizeof(struct sound_trigger_module_descriptor));
28860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
28960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = SoundTrigger::listModules(nModules, &numModules);
29060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("listModules SoundTrigger::listModules status %d numModules %d", status, numModules);
29160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
29260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (status != NO_ERROR) {
29360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        numModules = 0;
29460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
29560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
29660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    for (size_t i = 0; i < numModules; i++) {
29760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        char str[SOUND_TRIGGER_MAX_STRING_LEN];
29860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
29960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jstring implementor = env->NewStringUTF(nModules[i].properties.implementor);
30060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jstring description = env->NewStringUTF(nModules[i].properties.description);
30160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        SoundTrigger::guidToString(&nModules[i].properties.uuid,
30260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                   str,
30360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                   SOUND_TRIGGER_MAX_STRING_LEN);
30460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jstring uuid = env->NewStringUTF(str);
30560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
30660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGV("listModules module %d id %d description %s maxSoundModels %d",
30760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent              i, nModules[i].handle, nModules[i].properties.description,
30860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent              nModules[i].properties.max_sound_models);
30960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
31060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jobject newModuleDesc = env->NewObject(gModulePropertiesClass, gModulePropertiesCstor,
31160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].handle,
31260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               implementor, description, uuid,
31360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.version,
31460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.max_sound_models,
31560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.max_key_phrases,
31660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.max_users,
31760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.recognition_modes,
31860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.capture_transition,
31960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.max_buffer_ms,
32060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.concurrent_capture,
32160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               nModules[i].properties.power_consumption_mw);
32260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
32360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->DeleteLocalRef(implementor);
32460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->DeleteLocalRef(description);
32560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->DeleteLocalRef(uuid);
32660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (newModuleDesc == NULL) {
32760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            status = SOUNDTRIGGER_STATUS_ERROR;
32860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            goto exit;
32960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
33060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->CallBooleanMethod(jModules, gArrayListMethods.add, newModuleDesc);
33160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
33260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
33360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentexit:
33460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    free(nModules);
33560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return (jint) status;
33660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
33760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
33860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic void
33960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_setup(JNIEnv *env, jobject thiz, jobject weak_this)
34060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
34160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("setup");
34260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
34360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<JNISoundTriggerCallback> callback = new JNISoundTriggerCallback(env, thiz, weak_this);
34460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
34560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sound_trigger_module_handle_t handle =
34660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            (sound_trigger_module_handle_t)env->GetIntField(thiz, gModuleFields.mId);
34760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
34860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = SoundTrigger::attach(handle, callback);
34960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == 0) {
35060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return;
35160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
35260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
35360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    setSoundTrigger(env, thiz, module);
35460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
35560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
35660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic void
35760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_detach(JNIEnv *env, jobject thiz)
35860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
35960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("detach");
36060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = setSoundTrigger(env, thiz, 0);
36160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("detach module %p", module.get());
36260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module != 0) {
36360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGV("detach module->detach()");
36460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        module->detach();
36560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
36660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
36760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
36860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic void
36960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_finalize(JNIEnv *env, jobject thiz)
37060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
37160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("finalize");
37260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
37360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module != 0) {
37460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGW("SoundTrigger finalized without being detached");
37560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
37660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    android_hardware_SoundTrigger_detach(env, thiz);
37760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
37860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
37960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
38060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_loadSoundModel(JNIEnv *env, jobject thiz,
38160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                             jobject jSoundModel, jintArray jHandle)
38260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
38360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint status = SOUNDTRIGGER_STATUS_OK;
38460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    char *nData = NULL;
38560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    struct sound_trigger_sound_model *nSoundModel;
38660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jbyteArray jData;
38760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<MemoryDealer> memoryDealer;
38860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<IMemory> memory;
38960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    size_t size;
39060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sound_model_handle_t handle;
391d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jobject jUuid;
392d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jstring jUuidString;
393d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    const char *nUuidString;
39460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
39560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("loadSoundModel");
39660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
39760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == NULL) {
39860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
39960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
40060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jHandle == NULL) {
40160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_BAD_VALUE;
40260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
40360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jsize jHandleLen = env->GetArrayLength(jHandle);
40460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jHandleLen == 0) {
40560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_BAD_VALUE;
40660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
40760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint *nHandle = env->GetIntArrayElements(jHandle, NULL);
40860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (nHandle == NULL) {
40960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
41060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
41160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (!env->IsInstanceOf(jSoundModel, gSoundModelClass)) {
41260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_BAD_VALUE;
41360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
41460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
41560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    size_t offset;
41660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sound_trigger_sound_model_type_t type;
417d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    if (env->IsInstanceOf(jSoundModel, gKeyphraseSoundModelClass)) {
41860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        offset = sizeof(struct sound_trigger_phrase_sound_model);
41960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        type = SOUND_MODEL_TYPE_KEYPHRASE;
42060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    } else {
42160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        offset = sizeof(struct sound_trigger_sound_model);
42260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        type = SOUND_MODEL_TYPE_UNKNOWN;
42360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
424d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha
425d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jUuid = env->GetObjectField(jSoundModel, gSoundModelFields.uuid);
426d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jUuidString = (jstring)env->CallObjectMethod(jUuid, gUUIDMethods.toString);
427d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    nUuidString = env->GetStringUTFChars(jUuidString, NULL);
428d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    sound_trigger_uuid_t nUuid;
429d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    SoundTrigger::stringToGuid(nUuidString, &nUuid);
430d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    env->ReleaseStringUTFChars(jUuidString, nUuidString);
431d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    env->DeleteLocalRef(jUuidString);
432d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha
43360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jData = (jbyteArray)env->GetObjectField(jSoundModel, gSoundModelFields.data);
43460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jData == NULL) {
43560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_BAD_VALUE;
43660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
43760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
43860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    size = env->GetArrayLength(jData);
43960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
44060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nData = (char *)env->GetByteArrayElements(jData, NULL);
44160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jData == NULL) {
44260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_ERROR;
44360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
44460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
44560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
44660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    memoryDealer = new MemoryDealer(offset + size, "SoundTrigge-JNI::LoadModel");
44760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (memoryDealer == 0) {
44860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_ERROR;
44960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
45060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
45160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    memory = memoryDealer->allocate(offset + size);
45260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (memory == 0 || memory->pointer() == NULL) {
45360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = SOUNDTRIGGER_STATUS_ERROR;
45460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        goto exit;
45560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
45660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
45760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nSoundModel = (struct sound_trigger_sound_model *)memory->pointer();
45860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
45960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nSoundModel->type = type;
460d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    nSoundModel->uuid = nUuid;
46160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nSoundModel->data_size = size;
46260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    nSoundModel->data_offset = offset;
46360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    memcpy((char *)nSoundModel + offset, nData, size);
46460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (type == SOUND_MODEL_TYPE_KEYPHRASE) {
46560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        struct sound_trigger_phrase_sound_model *phraseModel =
46660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                (struct sound_trigger_phrase_sound_model *)nSoundModel;
46760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
46860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        jobjectArray jPhrases =
469d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha            (jobjectArray)env->GetObjectField(jSoundModel, gKeyphraseSoundModelFields.keyphrases);
47060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (jPhrases == NULL) {
47160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            status = SOUNDTRIGGER_STATUS_BAD_VALUE;
47260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            goto exit;
47360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
47460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
47560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        size_t numPhrases = env->GetArrayLength(jPhrases);
47660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        phraseModel->num_phrases = numPhrases;
47760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        ALOGV("loadSoundModel numPhrases %d", numPhrases);
47860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        for (size_t i = 0; i < numPhrases; i++) {
47960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            jobject jPhrase = env->GetObjectArrayElement(jPhrases, i);
480d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha            phraseModel->phrases[i].id =
481d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                    env->GetIntField(jPhrase,gKeyphraseFields.id);
48260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            phraseModel->phrases[i].recognition_mode =
483d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                    env->GetIntField(jPhrase,gKeyphraseFields.recognitionModes);
48460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            phraseModel->phrases[i].num_users =
485d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                    env->GetIntField(jPhrase, gKeyphraseFields.numUsers);
486d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha            jstring jLocale = (jstring)env->GetObjectField(jPhrase, gKeyphraseFields.locale);
48760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            const char *nLocale = env->GetStringUTFChars(jLocale, NULL);
48860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            strncpy(phraseModel->phrases[i].locale,
48960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                    nLocale,
49060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                    SOUND_TRIGGER_MAX_LOCALE_LEN);
491d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha            jstring jText = (jstring)env->GetObjectField(jPhrase, gKeyphraseFields.text);
49260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            const char *nText = env->GetStringUTFChars(jText, NULL);
49360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            strncpy(phraseModel->phrases[i].text,
49460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                    nText,
49560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                    SOUND_TRIGGER_MAX_STRING_LEN);
49660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
49760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->ReleaseStringUTFChars(jLocale, nLocale);
49860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->DeleteLocalRef(jLocale);
49960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->ReleaseStringUTFChars(jText, nText);
50060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            env->DeleteLocalRef(jText);
50160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            ALOGV("loadSoundModel phrases %d text %s locale %s",
50260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                  i, phraseModel->phrases[i].text, phraseModel->phrases[i].locale);
50360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
50460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->DeleteLocalRef(jPhrases);
50560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
50660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = module->loadSoundModel(memory, &handle);
50760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("loadSoundModel status %d handle %d", status, handle);
50860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
50960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentexit:
51060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (nHandle != NULL) {
51160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        nHandle[0] = (jint)handle;
51260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->ReleaseIntArrayElements(jHandle, nHandle, NULL);
51360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
51460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (nData != NULL) {
51560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        env->ReleaseByteArrayElements(jData, (jbyte *)nData, NULL);
51660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
51760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return status;
51860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
51960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
52060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
52160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_unloadSoundModel(JNIEnv *env, jobject thiz,
52260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               jint jHandle)
52360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
52460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint status = SOUNDTRIGGER_STATUS_OK;
52560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("unloadSoundModel");
52660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
52760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == NULL) {
52860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
52960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
53060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = module->unloadSoundModel((sound_model_handle_t)jHandle);
53160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
53260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return status;
53360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
53460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
53560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
53660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_startRecognition(JNIEnv *env, jobject thiz,
53760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               jint jHandle, jbyteArray jData)
53860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
53960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint status = SOUNDTRIGGER_STATUS_OK;
54060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("startRecognition");
54160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
54260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == NULL) {
54360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
54460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
54560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jsize dataSize = 0;
54660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    char *nData = NULL;
54760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<IMemory> memory;
54860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (jData != NULL) {
54960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        dataSize = env->GetArrayLength(jData);
55060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (dataSize == 0) {
55160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            return SOUNDTRIGGER_STATUS_BAD_VALUE;
55260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
55360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        nData = (char *)env->GetByteArrayElements(jData, NULL);
55460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (nData == NULL) {
55560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            return SOUNDTRIGGER_STATUS_ERROR;
55660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
55760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        sp<MemoryDealer> memoryDealer =
55860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                new MemoryDealer(dataSize, "SoundTrigge-JNI::StartRecognition");
55960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (memoryDealer == 0) {
56060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            return SOUNDTRIGGER_STATUS_ERROR;
56160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
56260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        memory = memoryDealer->allocate(dataSize);
56360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        if (memory == 0 || memory->pointer() == NULL) {
56460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent            return SOUNDTRIGGER_STATUS_ERROR;
56560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        }
56660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        memcpy(memory->pointer(), nData, dataSize);
56760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
56860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
56960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = module->startRecognition(jHandle, memory);
57060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return status;
57160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
57260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
57360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic jint
57460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentandroid_hardware_SoundTrigger_stopRecognition(JNIEnv *env, jobject thiz,
57560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                               jint jHandle)
57660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
57760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jint status = SOUNDTRIGGER_STATUS_OK;
57860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    ALOGV("stopRecognition");
57960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    sp<SoundTrigger> module = getSoundTrigger(env, thiz);
58060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (module == NULL) {
58160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        return SOUNDTRIGGER_STATUS_ERROR;
58260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
58360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    status = module->stopRecognition(jHandle);
58460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return status;
58560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
58660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
58760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic JNINativeMethod gMethods[] = {
58860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"listModules",
58960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(Ljava/util/ArrayList;)I",
59060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_listModules},
59160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent};
59260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
59360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
59460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentstatic JNINativeMethod gModuleMethods[] = {
59560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"native_setup",
59660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(Ljava/lang/Object;)V",
59760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_setup},
59860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"native_finalize",
59960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "()V",
60060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_finalize},
60160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"detach",
60260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "()V",
60360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_detach},
60460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"loadSoundModel",
60560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(Landroid/hardware/soundtrigger/SoundTrigger$SoundModel;[I)I",
60660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_loadSoundModel},
60760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"unloadSoundModel",
60860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(I)I",
60960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_unloadSoundModel},
61060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"startRecognition",
61160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(I[B)I",
61260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_startRecognition},
61360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    {"stopRecognition",
61460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        "(I)I",
61560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        (void *)android_hardware_SoundTrigger_stopRecognition},
61660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent};
61760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
61860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurentint register_android_hardware_SoundTrigger(JNIEnv *env)
61960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent{
62060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jclass arrayListClass = env->FindClass("java/util/ArrayList");
62160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    gArrayListClass = (jclass) env->NewGlobalRef(arrayListClass);
62260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    gArrayListMethods.add = env->GetMethodID(arrayListClass, "add", "(Ljava/lang/Object;)Z");
62360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
624d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jclass uuidClass = env->FindClass("java/util/UUID");
625d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gUUIDClass = (jclass) env->NewGlobalRef(uuidClass);
626d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gUUIDMethods.toString = env->GetMethodID(uuidClass, "toString", "()Ljava/lang/String;");
627d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha
62860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jclass lClass = env->FindClass(kSoundTriggerClassPathName);
62960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    gSoundTriggerClass = (jclass) env->NewGlobalRef(lClass);
63060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
63160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jclass moduleClass = env->FindClass(kModuleClassPathName);
63260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    gModuleClass = (jclass) env->NewGlobalRef(moduleClass);
63360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    gPostEventFromNative = env->GetStaticMethodID(moduleClass, "postEventFromNative",
63460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                            "(Ljava/lang/Object;IIILjava/lang/Object;)V");
63560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    gModuleFields.mNativeContext = env->GetFieldID(moduleClass, "mNativeContext", "J");
63660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    gModuleFields.mId = env->GetFieldID(moduleClass, "mId", "I");
63760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
63860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
63960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jclass modulePropertiesClass = env->FindClass(kModulePropertiesClassPathName);
64060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    gModulePropertiesClass = (jclass) env->NewGlobalRef(modulePropertiesClass);
64160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    gModulePropertiesCstor = env->GetMethodID(modulePropertiesClass, "<init>",
64260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                              "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;IIIIIZIZI)V");
64360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
64460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jclass soundModelClass = env->FindClass(kSoundModelClassPathName);
64560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    gSoundModelClass = (jclass) env->NewGlobalRef(soundModelClass);
646d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gSoundModelFields.uuid = env->GetFieldID(soundModelClass, "uuid", "Ljava/util/UUID;");
64760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    gSoundModelFields.data = env->GetFieldID(soundModelClass, "data", "[B");
64860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
649d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jclass keyphraseClass = env->FindClass(kKeyphraseClassPathName);
650d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gKeyphraseClass = (jclass) env->NewGlobalRef(keyphraseClass);
651d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gKeyphraseFields.id = env->GetFieldID(keyphraseClass, "id", "I");
652d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gKeyphraseFields.recognitionModes = env->GetFieldID(keyphraseClass, "recognitionModes", "I");
653d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gKeyphraseFields.locale = env->GetFieldID(keyphraseClass, "locale", "Ljava/lang/String;");
654d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gKeyphraseFields.text = env->GetFieldID(keyphraseClass, "text", "Ljava/lang/String;");
655d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gKeyphraseFields.numUsers = env->GetFieldID(keyphraseClass, "numUsers", "I");
65660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
657d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jclass keyphraseSoundModelClass = env->FindClass(kKeyphraseSoundModelClassPathName);
658d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gKeyphraseSoundModelClass = (jclass) env->NewGlobalRef(keyphraseSoundModelClass);
659d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gKeyphraseSoundModelFields.keyphrases = env->GetFieldID(keyphraseSoundModelClass,
660d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                         "keyphrases",
661d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha                                         "[Landroid/hardware/soundtrigger/SoundTrigger$Keyphrase;");
66260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
66360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
66460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    jclass recognitionEventClass = env->FindClass(kRecognitionEventClassPathName);
66560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    gRecognitionEventClass = (jclass) env->NewGlobalRef(recognitionEventClass);
66660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    gRecognitionEventCstor = env->GetMethodID(recognitionEventClass, "<init>",
66760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                              "(IIZII[B)V");
66860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
669d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jclass keyphraseRecognitionEventClass = env->FindClass(kKeyphraseRecognitionEventClassPathName);
670d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gKeyphraseRecognitionEventClass = (jclass) env->NewGlobalRef(keyphraseRecognitionEventClass);
671d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gKeyphraseRecognitionEventCstor = env->GetMethodID(keyphraseRecognitionEventClass, "<init>",
672d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha              "(IIZII[BZ[Landroid/hardware/soundtrigger/SoundTrigger$KeyphraseRecognitionExtra;)V");
67360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
67460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
675d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    jclass keyphraseRecognitionExtraClass = env->FindClass(kKeyphraseRecognitionExtraClassPathName);
676d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gKeyphraseRecognitionExtraClass = (jclass) env->NewGlobalRef(keyphraseRecognitionExtraClass);
677d4233c68fc17f0909e9e36494db85a634f8e2665Sandeep Siddhartha    gKeyphraseRecognitionExtraCstor = env->GetMethodID(keyphraseRecognitionExtraClass, "<init>",
67860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                                              "([II)V");
67960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
68060b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    int status = AndroidRuntime::registerNativeMethods(env,
68160b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                kSoundTriggerClassPathName, gMethods, NELEM(gMethods));
68260b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
68360b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    if (status == 0) {
68460b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent        status = AndroidRuntime::registerNativeMethods(env,
68560b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent                kModuleClassPathName, gModuleMethods, NELEM(gModuleMethods));
68660b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    }
68760b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent
68860b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent    return status;
68960b62bc5c11c0bfcdf84ca8f5b2053e5747f86bcEric Laurent}
690