1 /* //device/libs/android_runtime/android_media_AudioSystem.cpp
3 ** Copyright 2008, The Android Open Source Project
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
9 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
18 #define LOG_TAG "ToneGenerator"
26 #include "android_runtime/AndroidRuntime.h"
28 #include "utils/Log.h"
29 #include "media/AudioSystem.h"
30 #include "media/ToneGenerator.h"
32 // ----------------------------------------------------------------------------
34 using namespace android;
39 static fields_t fields;
41 static jboolean android_media_ToneGenerator_startTone(JNIEnv *env, jobject thiz, jint toneType, jint durationMs) {
42 LOGV("android_media_ToneGenerator_startTone: %x\n", (int)thiz);
44 ToneGenerator *lpToneGen = (ToneGenerator *)env->GetIntField(thiz,
46 if (lpToneGen == NULL) {
47 jniThrowException(env, "java/lang/RuntimeException", "Method called after release()");
51 return lpToneGen->startTone(toneType, durationMs);
54 static void android_media_ToneGenerator_stopTone(JNIEnv *env, jobject thiz) {
55 LOGV("android_media_ToneGenerator_stopTone: %x\n", (int)thiz);
57 ToneGenerator *lpToneGen = (ToneGenerator *)env->GetIntField(thiz,
60 LOGV("ToneGenerator lpToneGen: %x\n", (unsigned int)lpToneGen);
61 if (lpToneGen == NULL) {
62 jniThrowException(env, "java/lang/RuntimeException", "Method called after release()");
65 lpToneGen->stopTone();
68 static void android_media_ToneGenerator_release(JNIEnv *env, jobject thiz) {
69 ToneGenerator *lpToneGen = (ToneGenerator *)env->GetIntField(thiz,
71 LOGV("android_media_ToneGenerator_release lpToneGen: %x\n", (int)lpToneGen);
73 env->SetIntField(thiz, fields.context, 0);
80 static void android_media_ToneGenerator_native_setup(JNIEnv *env, jobject thiz,
81 jint streamType, jint volume) {
82 ToneGenerator *lpToneGen = new ToneGenerator(streamType, AudioSystem::linearToLog(volume), true);
84 env->SetIntField(thiz, fields.context, 0);
86 LOGV("android_media_ToneGenerator_native_setup jobject: %x\n", (int)thiz);
88 if (lpToneGen == NULL) {
89 LOGE("ToneGenerator creation failed \n");
90 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
93 LOGV("ToneGenerator lpToneGen: %x\n", (unsigned int)lpToneGen);
95 if (!lpToneGen->isInited()) {
96 LOGE("ToneGenerator init failed \n");
97 jniThrowException(env, "java/lang/RuntimeException", "Init failed");
101 // Stow our new C++ ToneGenerator in an opaque field in the Java object.
102 env->SetIntField(thiz, fields.context, (int)lpToneGen);
104 LOGV("ToneGenerator fields.context: %x\n", env->GetIntField(thiz, fields.context));
107 static void android_media_ToneGenerator_native_finalize(JNIEnv *env,
109 LOGV("android_media_ToneGenerator_native_finalize jobject: %x\n", (int)thiz);
111 ToneGenerator *lpToneGen = (ToneGenerator *)env->GetIntField(thiz,
115 LOGV("delete lpToneGen: %x\n", (int)lpToneGen);
120 // ----------------------------------------------------------------------------
122 static JNINativeMethod gMethods[] = {
123 { "startTone", "(II)Z", (void *)android_media_ToneGenerator_startTone },
124 { "stopTone", "()V", (void *)android_media_ToneGenerator_stopTone },
125 { "release", "()V", (void *)android_media_ToneGenerator_release },
126 { "native_setup", "(II)V", (void *)android_media_ToneGenerator_native_setup },
127 { "native_finalize", "()V", (void *)android_media_ToneGenerator_native_finalize }
131 int register_android_media_ToneGenerator(JNIEnv *env) {
134 clazz = env->FindClass("android/media/ToneGenerator");
136 LOGE("Can't find %s", "android/media/ToneGenerator");
140 fields.context = env->GetFieldID(clazz, "mNativeContext", "I");
141 if (fields.context == NULL) {
142 LOGE("Can't find ToneGenerator.mNativeContext");
145 LOGV("register_android_media_ToneGenerator ToneGenerator fields.context: %x", (unsigned int)fields.context);
147 return AndroidRuntime::registerNativeMethods(env,
148 "android/media/ToneGenerator", gMethods, NELEM(gMethods));