Merge "Merge Android 12"
diff --git a/src/java/android/net/rtp/AudioGroup.java b/src/java/android/net/rtp/AudioGroup.java
index fd5992f..3a85bbc 100644
--- a/src/java/android/net/rtp/AudioGroup.java
+++ b/src/java/android/net/rtp/AudioGroup.java
@@ -17,9 +17,10 @@
 package android.net.rtp;
 
 import android.annotation.NonNull;
-import android.app.ActivityThread;
+import android.content.AttributionSource;
 import android.content.Context;
 import android.media.AudioManager;
+import android.os.Parcel;
 
 import java.util.HashMap;
 import java.util.Locale;
@@ -117,7 +118,7 @@
 
     /**
      * Creates an empty AudioGroup.
-     * @param context Context used to get package name
+     * @param context Context the group belongs to
      */
     public AudioGroup(@NonNull Context context) {
         mContext = context;
@@ -167,11 +168,15 @@
                 AudioCodec codec = stream.getCodec();
                 String codecSpec = String.format(Locale.US, "%d %s %s", codec.type,
                         codec.rtpmap, codec.fmtp);
-                long id = nativeAdd(stream.getMode(), stream.getSocket(),
-                        stream.getRemoteAddress().getHostAddress(),
-                        stream.getRemotePort(), codecSpec, stream.getDtmfType(),
-                        mContext != null ? mContext.getOpPackageName()
-                                : ActivityThread.currentOpPackageName());
+
+                final long id;
+                try (AttributionSource.ScopedParcelState attributionSourceState = mContext
+                        .getAttributionSource().asScopedParcelState()) {
+                    id = nativeAdd(stream.getMode(), stream.getSocket(),
+                            stream.getRemoteAddress().getHostAddress(),
+                            stream.getRemotePort(), codecSpec, stream.getDtmfType(),
+                            attributionSourceState.getParcel());
+                }
                 mStreams.put(stream, id);
             } catch (NullPointerException e) {
                 throw new IllegalStateException(e);
@@ -179,8 +184,8 @@
         }
     }
 
-    private native long nativeAdd(int mode, int socket, String remoteAddress,
-            int remotePort, String codecSpec, int dtmfType, String opPackageName);
+    private native long nativeAdd(int mode, int socket, String remoteAddress, int remotePort,
+            String codecSpec, int dtmfType, Parcel attributionSource);
 
     // Package-private method used by AudioStream.join().
     synchronized void remove(AudioStream stream) {
diff --git a/src/jni/rtp/Android.bp b/src/jni/rtp/Android.bp
index a1497f9..325d6b9 100644
--- a/src/jni/rtp/Android.bp
+++ b/src/jni/rtp/Android.bp
@@ -36,8 +36,11 @@
     ],
 
     shared_libs: [
+        "framework-permission-aidl-cpp",
+        "libandroid_runtime",
         "libaudioclient",
         "libaudiofoundation",
+        "libbinder",
         "libcutils",
         "liblog",
         "libnativehelper",
@@ -46,6 +49,7 @@
     ],
     static_libs: [
         "libgsm",
+        "framework-permission-aidl-cpp",
         "libstagefright_amrnbdec",
         "libstagefright_amrnbenc",
     ],
diff --git a/src/jni/rtp/AudioGroup.cpp b/src/jni/rtp/AudioGroup.cpp
index 9efd11a..e92e799 100644
--- a/src/jni/rtp/AudioGroup.cpp
+++ b/src/jni/rtp/AudioGroup.cpp
@@ -44,6 +44,8 @@
 #include <system/audio.h>
 
 #include <nativehelper/ScopedUtfChars.h>
+#include <android/content/AttributionSourceState.h>
+#include <android_os_Parcel.h>
 
 #include "jni.h"
 #include <nativehelper/JNIHelp.h>
@@ -57,6 +59,8 @@
 
 using namespace android;
 
+using android::content::AttributionSourceState;
+
 int gRandom = -1;
 
 // We use a circular array to implement jitter buffer. The simplest way is doing
@@ -480,7 +484,7 @@
 class AudioGroup
 {
 public:
-    explicit AudioGroup(const String16 &opPackageName);
+    explicit AudioGroup(const AttributionSourceState &attributionSource);
     ~AudioGroup();
     bool set(int sampleRate, int sampleCount);
 
@@ -505,7 +509,7 @@
     int mEventQueue;
     volatile int mDtmfEvent;
 
-    String16 mOpPackageName;
+    const AttributionSourceState mAttributionSource;
 
     int mMode;
     int mSampleRate;
@@ -554,9 +558,9 @@
     sp<DeviceThread> mDeviceThread;
 };
 
-AudioGroup::AudioGroup(const String16 &opPackageName)
+AudioGroup::AudioGroup(const AttributionSourceState &attributionSource)
+        : mAttributionSource(attributionSource)
 {
-    mOpPackageName = opPackageName;
     mMode = ON_HOLD;
     mChain = NULL;
     mEventQueue = -1;
@@ -818,7 +822,7 @@
 
     // Initialize AudioTrack and AudioRecord.
     sp<AudioTrack> track = new AudioTrack();
-    sp<AudioRecord> record = new AudioRecord(mGroup->mOpPackageName);
+    sp<AudioRecord> record = new AudioRecord(mGroup->mAttributionSource);
     // Set caller name so it can be logged in destructor.
     // MediaMetricsConstants.h: AMEDIAMETRICS_PROP_CALLERNAME_VALUE_RTP
     track->setCallerName("rtp");
@@ -851,7 +855,7 @@
     sp<AudioEffect> aec;
     if (mode == ECHO_SUPPRESSION) {
         if (mGroup->platformHasAec()) {
-            aec = new AudioEffect(mGroup->mOpPackageName);
+            aec = new AudioEffect(mGroup->mAttributionSource);
             aec->set(FX_IID_AEC,
                      NULL,
                      0,
@@ -953,7 +957,7 @@
 
 jlong add(JNIEnv *env, jobject thiz, jint mode,
     jint socket, jstring jRemoteAddress, jint remotePort,
-    jstring jCodecSpec, jint dtmfType, jstring opPackageNameStr)
+    jstring jCodecSpec, jint dtmfType, jobject jAttributionSource)
 {
     AudioCodec *codec = NULL;
     AudioStream *stream = NULL;
@@ -981,7 +985,9 @@
         return 0;
     }
 
-    ScopedUtfChars opPackageName(env, opPackageNameStr);
+    Parcel* parcel = parcelForJavaObject(env, jAttributionSource);
+    AttributionSourceState attributionSource;
+    attributionSource.readFromParcel(parcel);
 
     // Create audio codec.
     int codecType = -1;
@@ -1012,7 +1018,7 @@
     group = (AudioGroup *)env->GetLongField(thiz, gNative);
     if (!group) {
         int mode = env->GetIntField(thiz, gMode);
-        group = new AudioGroup(String16(opPackageName.c_str()));
+        group = new AudioGroup(attributionSource);
         if (!group->set(8000, 256) || !group->setMode(mode)) {
             jniThrowException(env, "java/lang/IllegalStateException",
                 "cannot initialize audio group");
@@ -1068,7 +1074,7 @@
 }
 
 JNINativeMethod gMethods[] = {
-    {"nativeAdd", "(IILjava/lang/String;ILjava/lang/String;ILjava/lang/String;)J", (void *)add},
+    {"nativeAdd", "(IILjava/lang/String;ILjava/lang/String;ILandroid/os/Parcel;)J", (void *)add},
     {"nativeRemove", "(J)V", (void *)remove},
     {"nativeSetMode", "(I)V", (void *)setMode},
     {"nativeSendDtmf", "(I)V", (void *)sendDtmf},
@@ -1092,5 +1098,6 @@
         ALOGE("JNI registration failed");
         return -1;
     }
+
     return 0;
 }