1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "android_media_AudioDeviceAttributes.h"
18 #include "android_media_AudioErrors.h"
19 #include "core_jni_helpers.h"
20 
21 #include <media/AudioDeviceTypeAddr.h>
22 
23 using namespace android;
24 
25 static jclass gAudioDeviceAttributesClass;
26 static jmethodID gAudioDeviceAttributesCstor;
27 static struct {
28     jfieldID mAddress;
29     jfieldID mNativeType;
30     // other fields unused by JNI
31 } gAudioDeviceAttributesFields;
32 
33 namespace android {
34 
createAudioDeviceAttributesFromNative(JNIEnv * env,jobject * jAudioDeviceAttributes,const AudioDeviceTypeAddr * devTypeAddr)35 jint createAudioDeviceAttributesFromNative(JNIEnv *env, jobject *jAudioDeviceAttributes,
36                                  const AudioDeviceTypeAddr *devTypeAddr) {
37     jint jStatus = (jint)AUDIO_JAVA_SUCCESS;
38     jint jNativeType = (jint)devTypeAddr->mType;
39     ScopedLocalRef<jstring> jAddress(env, env->NewStringUTF(devTypeAddr->getAddress()));
40 
41     *jAudioDeviceAttributes =
42         env->NewObject(gAudioDeviceAttributesClass, gAudioDeviceAttributesCstor,
43                        jNativeType, jAddress.get());
44 
45     return jStatus;
46 }
47 
createAudioDeviceTypeAddrFromJava(JNIEnv * env,AudioDeviceTypeAddr * devTypeAddr,const jobject jAudioDeviceAttributes)48 jint createAudioDeviceTypeAddrFromJava(JNIEnv *env, AudioDeviceTypeAddr *devTypeAddr,
49                                        const jobject jAudioDeviceAttributes) {
50     devTypeAddr->mType = (audio_devices_t)env->GetIntField(jAudioDeviceAttributes,
51                          gAudioDeviceAttributesFields.mNativeType);
52 
53     jstring jAddress = (jstring)env->GetObjectField(jAudioDeviceAttributes,
54                        gAudioDeviceAttributesFields.mAddress);
55     devTypeAddr->setAddress(ScopedUtfChars(env, jAddress).c_str());
56 
57     return AUDIO_JAVA_SUCCESS;
58 }
59 
60 } // namespace android
61 
register_android_media_AudioDeviceAttributes(JNIEnv * env)62 int register_android_media_AudioDeviceAttributes(JNIEnv *env) {
63     jclass audioDeviceTypeAddressClass =
64             FindClassOrDie(env, "android/media/AudioDeviceAttributes");
65     gAudioDeviceAttributesClass = MakeGlobalRefOrDie(env, audioDeviceTypeAddressClass);
66     gAudioDeviceAttributesCstor =
67             GetMethodIDOrDie(env, audioDeviceTypeAddressClass, "<init>", "(ILjava/lang/String;)V");
68 
69     gAudioDeviceAttributesFields.mNativeType =
70             GetFieldIDOrDie(env, gAudioDeviceAttributesClass, "mNativeType", "I");
71     gAudioDeviceAttributesFields.mAddress =
72             GetFieldIDOrDie(env, gAudioDeviceAttributesClass, "mAddress", "Ljava/lang/String;");
73 
74     return 0;
75 }
76