Glenn Kasten | b3db213 | 2012-01-19 08:59:58 -0800 | [diff] [blame^] | 1 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2 | ** |
| 3 | ** Copyright 2008, The Android Open Source Project |
| 4 | ** |
| 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 |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 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. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "ToneGenerator" |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <unistd.h> |
| 22 | #include <fcntl.h> |
| 23 | |
| 24 | #include "jni.h" |
| 25 | #include "JNIHelp.h" |
| 26 | #include "android_runtime/AndroidRuntime.h" |
| 27 | |
| 28 | #include "utils/Log.h" |
| 29 | #include "media/AudioSystem.h" |
| 30 | #include "media/ToneGenerator.h" |
| 31 | |
| 32 | // ---------------------------------------------------------------------------- |
| 33 | |
| 34 | using namespace android; |
| 35 | |
| 36 | struct fields_t { |
| 37 | jfieldID context; |
| 38 | }; |
| 39 | static fields_t fields; |
| 40 | |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 41 | static jboolean android_media_ToneGenerator_startTone(JNIEnv *env, jobject thiz, jint toneType, jint durationMs) { |
Glenn Kasten | 0765c44 | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 42 | ALOGV("android_media_ToneGenerator_startTone: %x", (int)thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
| 44 | ToneGenerator *lpToneGen = (ToneGenerator *)env->GetIntField(thiz, |
| 45 | fields.context); |
| 46 | if (lpToneGen == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 47 | jniThrowRuntimeException(env, "Method called after release()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | return false; |
| 49 | } |
| 50 | |
Glenn Kasten | 23f7ad3 | 2012-01-27 15:25:25 -0800 | [diff] [blame] | 51 | return lpToneGen->startTone((ToneGenerator::tone_type) toneType, durationMs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static void android_media_ToneGenerator_stopTone(JNIEnv *env, jobject thiz) { |
Glenn Kasten | 0765c44 | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 55 | ALOGV("android_media_ToneGenerator_stopTone: %x", (int)thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
| 57 | ToneGenerator *lpToneGen = (ToneGenerator *)env->GetIntField(thiz, |
| 58 | fields.context); |
| 59 | |
Glenn Kasten | 0765c44 | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 60 | ALOGV("ToneGenerator lpToneGen: %x", (unsigned int)lpToneGen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | if (lpToneGen == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 62 | jniThrowRuntimeException(env, "Method called after release()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | return; |
| 64 | } |
| 65 | lpToneGen->stopTone(); |
| 66 | } |
| 67 | |
| 68 | static void android_media_ToneGenerator_release(JNIEnv *env, jobject thiz) { |
| 69 | ToneGenerator *lpToneGen = (ToneGenerator *)env->GetIntField(thiz, |
| 70 | fields.context); |
Glenn Kasten | 0765c44 | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 71 | ALOGV("android_media_ToneGenerator_release lpToneGen: %x", (int)lpToneGen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | |
| 73 | env->SetIntField(thiz, fields.context, 0); |
| 74 | |
Glenn Kasten | 1137be1 | 2012-02-08 17:47:58 -0800 | [diff] [blame] | 75 | delete lpToneGen; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | static void android_media_ToneGenerator_native_setup(JNIEnv *env, jobject thiz, |
| 79 | jint streamType, jint volume) { |
Glenn Kasten | bc1d77b | 2012-01-12 16:38:12 -0800 | [diff] [blame] | 80 | ToneGenerator *lpToneGen = new ToneGenerator((audio_stream_type_t) streamType, AudioSystem::linearToLog(volume), true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | |
| 82 | env->SetIntField(thiz, fields.context, 0); |
| 83 | |
Glenn Kasten | 0765c44 | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 84 | ALOGV("android_media_ToneGenerator_native_setup jobject: %x", (int)thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | |
| 86 | if (lpToneGen == NULL) { |
Glenn Kasten | 0765c44 | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 87 | ALOGE("ToneGenerator creation failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 89 | return; |
| 90 | } |
Glenn Kasten | 0765c44 | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 91 | ALOGV("ToneGenerator lpToneGen: %x", (unsigned int)lpToneGen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | |
| 93 | if (!lpToneGen->isInited()) { |
Glenn Kasten | 0765c44 | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 94 | ALOGE("ToneGenerator init failed"); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 95 | jniThrowRuntimeException(env, "Init failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | return; |
| 97 | } |
| 98 | |
| 99 | // Stow our new C++ ToneGenerator in an opaque field in the Java object. |
| 100 | env->SetIntField(thiz, fields.context, (int)lpToneGen); |
| 101 | |
Glenn Kasten | 0765c44 | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 102 | ALOGV("ToneGenerator fields.context: %x", env->GetIntField(thiz, fields.context)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static void android_media_ToneGenerator_native_finalize(JNIEnv *env, |
| 106 | jobject thiz) { |
Glenn Kasten | 0765c44 | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 107 | ALOGV("android_media_ToneGenerator_native_finalize jobject: %x", (int)thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
| 109 | ToneGenerator *lpToneGen = (ToneGenerator *)env->GetIntField(thiz, |
| 110 | fields.context); |
| 111 | |
Glenn Kasten | 0765c44 | 2012-01-27 15:24:38 -0800 | [diff] [blame] | 112 | if (lpToneGen != NULL) { |
| 113 | ALOGV("delete lpToneGen: %p", lpToneGen); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | delete lpToneGen; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // ---------------------------------------------------------------------------- |
| 119 | |
| 120 | static JNINativeMethod gMethods[] = { |
Eric Laurent | 96c08a6 | 2009-09-07 08:38:38 -0700 | [diff] [blame] | 121 | { "startTone", "(II)Z", (void *)android_media_ToneGenerator_startTone }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | { "stopTone", "()V", (void *)android_media_ToneGenerator_stopTone }, |
| 123 | { "release", "()V", (void *)android_media_ToneGenerator_release }, |
| 124 | { "native_setup", "(II)V", (void *)android_media_ToneGenerator_native_setup }, |
| 125 | { "native_finalize", "()V", (void *)android_media_ToneGenerator_native_finalize } |
| 126 | }; |
| 127 | |
| 128 | |
| 129 | int register_android_media_ToneGenerator(JNIEnv *env) { |
| 130 | jclass clazz; |
| 131 | |
| 132 | clazz = env->FindClass("android/media/ToneGenerator"); |
| 133 | if (clazz == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 134 | ALOGE("Can't find %s", "android/media/ToneGenerator"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | fields.context = env->GetFieldID(clazz, "mNativeContext", "I"); |
| 139 | if (fields.context == NULL) { |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 140 | ALOGE("Can't find ToneGenerator.mNativeContext"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | return -1; |
| 142 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 143 | ALOGV("register_android_media_ToneGenerator ToneGenerator fields.context: %x", (unsigned int)fields.context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | |
| 145 | return AndroidRuntime::registerNativeMethods(env, |
| 146 | "android/media/ToneGenerator", gMethods, NELEM(gMethods)); |
| 147 | } |