1/*
2 * Copyright 2018 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#ifndef ANDROID_JAUDIOATTRIBUTES_H
18#define ANDROID_JAUDIOATTRIBUTES_H
19
20#include <jni.h>
21#include <system/audio.h>
22
23namespace android {
24
25class JAudioAttributes {
26public:
27    /* Creates a Java AudioAttributes object. */
28    static jobject createAudioAttributesObj(JNIEnv *env,
29                                            const audio_attributes_t* pAttributes,
30                                            audio_stream_type_t streamType) {
31
32        jclass jBuilderCls = env->FindClass("android/media/AudioAttributes$Builder");
33        jmethodID jBuilderCtor = env->GetMethodID(jBuilderCls, "<init>", "()V");
34        jobject jBuilderObj = env->NewObject(jBuilderCls, jBuilderCtor);
35
36        if (pAttributes != NULL) {
37            // If pAttributes is not null, streamType is ignored.
38            jmethodID jSetUsage = env->GetMethodID(
39                    jBuilderCls, "setUsage", "(I)Landroid/media/AudioAttributes$Builder;");
40            jBuilderObj = env->CallObjectMethod(jBuilderObj, jSetUsage, pAttributes->usage);
41
42            jmethodID jSetContentType = env->GetMethodID(jBuilderCls, "setContentType",
43                    "(I)Landroid/media/AudioAttributes$Builder;");
44            jBuilderObj = env->CallObjectMethod(jBuilderObj, jSetContentType,
45                    pAttributes->content_type);
46
47            // TODO: Java AudioAttributes.Builder.setCapturePreset() is systemApi and hidden.
48            // Can we use this method?
49//            jmethodID jSetCapturePreset = env->GetMethodID(jBuilderCls, "setCapturePreset",
50//                    "(I)Landroid/media/AudioAttributes$Builder;");
51//            jBuilderObj = env->CallObjectMethod(jBuilderObj, jSetCapturePreset,
52//                    pAttributes->source);
53
54            jmethodID jSetFlags = env->GetMethodID(jBuilderCls, "setFlags",
55                    "(I)Landroid/media/AudioAttributes$Builder;");
56            jBuilderObj = env->CallObjectMethod(jBuilderObj, jSetFlags, pAttributes->flags);
57
58            // TODO: Handle the 'tags' (char[] to HashSet<String>).
59            // How to parse the char[]? Is there any example of it?
60            // Also, the addTags() method is hidden.
61        } else {
62            // Call AudioAttributes.Builder.setLegacyStreamType().build()
63            jmethodID jSetLegacyStreamType = env->GetMethodID(jBuilderCls, "setLegacyStreamType",
64                    "(I)Landroid/media/AudioAttributes$Builder;");
65            jBuilderObj = env->CallObjectMethod(jBuilderObj, jSetLegacyStreamType, streamType);
66        }
67
68        jmethodID jBuild = env->GetMethodID(jBuilderCls, "build",
69                "()Landroid/media/AudioAttributes;");
70        return env->CallObjectMethod(jBuilderObj, jBuild);
71    }
72
73};
74
75} // namespace android
76
77#endif // ANDROID_JAUDIOATTRIBUTES_H
78