Add package name when initializing ToneGenerator.

Bug: 175595599
Test: ToneGeneratorTest
Change-Id: Iab250564b3af6901f08775f279ba88b1b0eade19
This commit is contained in:
jiabin
2020-12-16 11:10:11 -08:00
parent 1599b64849
commit f40041458e
2 changed files with 25 additions and 15 deletions

View File

@@ -23,6 +23,7 @@
#include <jni.h>
#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedUtfChars.h>
#include "core_jni_helpers.h"
#include <utils/Log.h>
@@ -85,9 +86,12 @@ static void android_media_ToneGenerator_release(JNIEnv *env, jobject thiz) {
delete lpToneGen;
}
static void android_media_ToneGenerator_native_setup(JNIEnv *env, jobject thiz,
jint streamType, jint volume) {
ToneGenerator *lpToneGen = new ToneGenerator((audio_stream_type_t) streamType, AudioSystem::linearToLog(volume), true);
static void android_media_ToneGenerator_native_setup(JNIEnv *env, jobject thiz, jint streamType,
jint volume, jstring opPackageName) {
ScopedUtfChars opPackageNameStr(env, opPackageName);
ToneGenerator *lpToneGen =
new ToneGenerator((audio_stream_type_t)streamType, AudioSystem::linearToLog(volume),
true /*threadCanCallJava*/, opPackageNameStr.c_str());
env->SetLongField(thiz, fields.context, 0);
@@ -123,15 +127,14 @@ static void android_media_ToneGenerator_native_finalize(JNIEnv *env,
// ----------------------------------------------------------------------------
static const JNINativeMethod gMethods[] = {
{ "startTone", "(II)Z", (void *)android_media_ToneGenerator_startTone },
{ "stopTone", "()V", (void *)android_media_ToneGenerator_stopTone },
{ "getAudioSessionId", "()I", (void *)android_media_ToneGenerator_getAudioSessionId},
{ "release", "()V", (void *)android_media_ToneGenerator_release },
{ "native_setup", "(II)V", (void *)android_media_ToneGenerator_native_setup },
{ "native_finalize", "()V", (void *)android_media_ToneGenerator_native_finalize }
};
static const JNINativeMethod gMethods[] =
{{"startTone", "(II)Z", (void *)android_media_ToneGenerator_startTone},
{"stopTone", "()V", (void *)android_media_ToneGenerator_stopTone},
{"getAudioSessionId", "()I", (void *)android_media_ToneGenerator_getAudioSessionId},
{"release", "()V", (void *)android_media_ToneGenerator_release},
{"native_setup", "(IILjava/lang/String;)V",
(void *)android_media_ToneGenerator_native_setup},
{"native_finalize", "()V", (void *)android_media_ToneGenerator_native_finalize}};
int register_android_media_ToneGenerator(JNIEnv *env) {
jclass clazz = FindClassOrDie(env, "android/media/ToneGenerator");

View File

@@ -16,9 +16,11 @@
package android.media;
import android.annotation.NonNull;
import android.app.ActivityThread;
import android.compat.annotation.UnsupportedAppUsage;
import android.os.Build;
import android.text.TextUtils;
/**
@@ -746,7 +748,7 @@ public class ToneGenerator
*
*/
public ToneGenerator(int streamType, int volume) {
native_setup(streamType, volume);
native_setup(streamType, volume, getCurrentOpPackageName());
}
/**
@@ -880,7 +882,8 @@ public class ToneGenerator
*/
public native void release();
private native final void native_setup(int streamType, int volume);
private native void native_setup(
int streamType, int volume, @NonNull String opPackageName);
private native final void native_finalize();
@@ -895,6 +898,10 @@ public class ToneGenerator
@Override
protected void finalize() { native_finalize(); }
private String getCurrentOpPackageName() {
return TextUtils.emptyIfNull(ActivityThread.currentOpPackageName());
}
@SuppressWarnings("unused")
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
private long mNativeContext; // accessed by native methods