Merge "Merge Android 12"
diff --git a/src/Android.bp b/src/Android.bp
index e33bff1..827b4a4 100644
--- a/src/Android.bp
+++ b/src/Android.bp
@@ -227,11 +227,13 @@
     ],
 
     shared_libs: [
+        "framework-permission-aidl-cpp",
         "liblog",
         "libutils",
         "libmedia",
         "libmedia_codeclist",
         "libaudioclient",
+        "libaudioclient_aidl_conversion",
         "libaudiofoundation",
         "libaudiomanager",
         "libbinder",
diff --git a/src/android/AudioPlayer_to_android.cpp b/src/android/AudioPlayer_to_android.cpp
index 10517c9..a324b4a 100644
--- a/src/android/AudioPlayer_to_android.cpp
+++ b/src/android/AudioPlayer_to_android.cpp
@@ -49,9 +49,10 @@
 
 //-----------------------------------------------------------------------------
 // Inline functions to communicate with AudioService through the native AudioManager interface
-inline void audioManagerPlayerEvent(CAudioPlayer* ap, android::player_state_t event) {
+inline void audioManagerPlayerEvent(CAudioPlayer* ap, android::player_state_t event,
+        audio_port_handle_t deviceId) {
     if (ap->mObject.mEngine->mAudioManager != 0) {
-        ap->mObject.mEngine->mAudioManager->playerEvent(ap->mPIId, event);
+        ap->mObject.mEngine->mAudioManager->playerEvent(ap->mPIId, event, deviceId);
     }
 }
 
@@ -878,7 +879,7 @@
         if ((ap->mTrackPlayer->mAudioTrack != 0) && (!ap->mSeek.mLoopEnabled)) {
             ap->mTrackPlayer->mAudioTrack->stop();
         }
-        ap->mTrackPlayer->reportEvent(android::PLAYER_STATE_STOPPED);
+        ap->mTrackPlayer->reportEvent(android::PLAYER_STATE_STOPPED, AUDIO_PORT_HANDLE_NONE);
         }
         break;
 
@@ -1711,7 +1712,7 @@
         }
 
         pAudioPlayer->mTrackPlayer->init(pat, android::PLAYER_TYPE_SLES_AUDIOPLAYER_BUFFERQUEUE,
-                usageForStreamType(pAudioPlayer->mStreamType));
+                usageForStreamType(pAudioPlayer->mStreamType), pAudioPlayer->mSessionId);
 
         // update performance mode according to actual flags granted to AudioTrack
         checkAndSetPerformanceModePost(pAudioPlayer);
@@ -1812,7 +1813,7 @@
             pAudioPlayer->mPIId = pAudioPlayer->mObject.mEngine->mAudioManager->trackPlayer(
                     android::PLAYER_TYPE_SLES_AUDIOPLAYER_URI_FD,
                     usageForStreamType(pAudioPlayer->mStreamType), AUDIO_CONTENT_TYPE_UNKNOWN,
-                    pAudioPlayer->mTrackPlayer);
+                    pAudioPlayer->mTrackPlayer, pAudioPlayer->mSessionId);
         }
         }
         break;
@@ -2232,6 +2233,11 @@
 
     SLuint32 playState = ap->mPlay.mState;
 
+    audio_port_handle_t deviceId = AUDIO_PORT_HANDLE_NONE;
+    if (ap->mTrackPlayer != 0 && ap->mTrackPlayer->mAudioTrack != 0) {
+        deviceId = ap->mTrackPlayer->mAudioTrack->getRoutedDeviceId();
+    }
+
     switch (ap->mAndroidObjType) {
     case AUDIOPLAYER_FROM_PCM_BUFFERQUEUE:
         switch (playState) {
@@ -2249,7 +2255,7 @@
                 // instead of ap->mTrackPlayer->mAudioTrack->start();
                 if (!ap->mDeferredStart) {
                     // state change
-                    ap->mTrackPlayer->reportEvent(android::PLAYER_STATE_STARTED);
+                    ap->mTrackPlayer->reportEvent(android::PLAYER_STATE_STARTED, deviceId);
                 }
                 ap->mDeferredStart = true;
             }
@@ -2264,14 +2270,14 @@
         switch (playState) {
         case SL_PLAYSTATE_STOPPED:
             aplayer_setPlayState(ap->mAPlayer, playState, &ap->mAndroidObjState);
-            audioManagerPlayerEvent(ap, android::PLAYER_STATE_STOPPED);
+            audioManagerPlayerEvent(ap, android::PLAYER_STATE_STOPPED, AUDIO_PORT_HANDLE_NONE);
             break;
         case SL_PLAYSTATE_PAUSED:
             aplayer_setPlayState(ap->mAPlayer, playState, &ap->mAndroidObjState);
-            audioManagerPlayerEvent(ap, android::PLAYER_STATE_PAUSED);
+            audioManagerPlayerEvent(ap, android::PLAYER_STATE_PAUSED, AUDIO_PORT_HANDLE_NONE);
             break;
         case SL_PLAYSTATE_PLAYING:
-            audioManagerPlayerEvent(ap, android::PLAYER_STATE_STARTED);
+            audioManagerPlayerEvent(ap, android::PLAYER_STATE_STARTED, deviceId);
             aplayer_setPlayState(ap->mAPlayer, playState, &ap->mAndroidObjState);
             break;
         }
@@ -2481,7 +2487,8 @@
     // queue was stopped when the queue become empty, we restart as soon as a new buffer
     // has been enqueued since we're in playing state
     if (ap->mTrackPlayer->mAudioTrack != 0) {
-        ap->mTrackPlayer->reportEvent(android::PLAYER_STATE_STARTED);
+        ap->mTrackPlayer->reportEvent(android::PLAYER_STATE_STARTED,
+                            ap->mTrackPlayer->mAudioTrack->getRoutedDeviceId());
         // instead of ap->mTrackPlayer->mAudioTrack->start();
         ap->mDeferredStart = true;
     }
diff --git a/src/android/AudioRecorder_to_android.cpp b/src/android/AudioRecorder_to_android.cpp
index b1fd5ef..411beff 100644
--- a/src/android/AudioRecorder_to_android.cpp
+++ b/src/android/AudioRecorder_to_android.cpp
@@ -29,6 +29,8 @@
 #define KEY_RECORDING_PRESET_PARAMSIZE  sizeof(SLuint32)
 #define KEY_PERFORMANCE_MODE_PARAMSIZE  sizeof(SLuint32)
 
+using android::content::AttributionSourceState;
+
 //-----------------------------------------------------------------------------
 // Internal utility functions
 //----------------------------
@@ -687,13 +689,19 @@
     SL_LOGV("SLES channel mask %#x converted to Android mask %#x", df_pcm->channelMask,
             channelMask);
 
+    // TODO b/182392769: use attribution source util
+    AttributionSourceState attributionSource;
+    attributionSource.uid = VALUE_OR_FATAL(android::legacy2aidl_uid_t_int32_t(getuid()));
+    attributionSource.pid = VALUE_OR_FATAL(android::legacy2aidl_pid_t_int32_t(getpid()));
+    attributionSource.token = android::sp<android::BBinder>::make();
+
     // initialize platform-specific CAudioRecorder fields
     ar->mAudioRecord = new android::AudioRecord(
             ar->mRecordSource,     // source
             sampleRate,            // sample rate in Hertz
             sles_to_android_sampleFormat(df_pcm),               // format
             channelMask,           // channel mask
-            android::String16(),   // app ops
+            attributionSource,
             0,                     // frameCount
             audioRecorder_callback,// callback_t
             (void*)ar,             // user, callback data, here the AudioRecorder
diff --git a/src/android/android_Effect.cpp b/src/android/android_Effect.cpp
index 9341d04..2b5582f 100644
--- a/src/android/android_Effect.cpp
+++ b/src/android/android_Effect.cpp
@@ -32,6 +32,8 @@
 
 #include <system/audio.h>
 
+using android::content::AttributionSourceState;
+
 static const int EQUALIZER_PARAM_SIZE_MAX = sizeof(effect_param_t) + 2 * sizeof(int32_t)
         + EFFECT_STRING_LEN_MAX;
 
@@ -665,7 +667,14 @@
         const effect_uuid_t *type) {
     //SL_LOGV("android_fx_initEffectObj on session %d", sessionId);
 
-    effect = new android::AudioEffect(android::String16());
+    // TODO b/182392769: use attribution source util
+    AttributionSourceState attributionSource;
+    attributionSource.uid = VALUE_OR_FATAL(android::legacy2aidl_uid_t_int32_t(getuid()));
+    attributionSource.pid = VALUE_OR_FATAL(android::legacy2aidl_pid_t_int32_t(getpid()));
+    attributionSource.token = android::sp<android::BBinder>::make();
+
+    effect = android::sp<android::AudioEffect>::make(attributionSource);
+
     effect->set(type, EFFECT_UUID_NULL,
             0,// priority
             0,// effect callback
@@ -806,7 +815,14 @@
     }
 
     // create new effect
-    android::sp<android::AudioEffect> pFx = new android::AudioEffect(android::String16());
+    // TODO b/182392769: use attribution source util
+    AttributionSourceState attributionSource;
+    attributionSource.uid = VALUE_OR_FATAL(android::legacy2aidl_uid_t_int32_t(getuid()));
+    attributionSource.pid = VALUE_OR_FATAL(android::legacy2aidl_pid_t_int32_t(getpid()));
+    attributionSource.token = android::sp<android::BBinder>::make();
+
+    const auto pFx = android::sp<android::AudioEffect>::make(attributionSource);
+
     pFx->set(NULL, // not using type to create effect
             (const effect_uuid_t*)pUuid,
             0,// priority