Added transient players to auto KS.

Added transient sounds player to the audio fragment in auto kitchen
sink. The player allowes for playback of the transient sounds for longer
duration than it is currently possible.

Bug: 188933555
Test: m -j, and play audio in KS.
Change-Id: Ic73f1f2794e0681dddf3d744482f208e37a2b576
diff --git a/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/audio/AudioTestFragment.java b/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/audio/AudioTestFragment.java
index 9f847e7..5ce906a 100644
--- a/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/audio/AudioTestFragment.java
+++ b/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/audio/AudioTestFragment.java
@@ -641,9 +641,11 @@
         mViewPager = view.findViewById(R.id.zones_player_view_pager);
         mAudioPlayerZoneAdapter = new CarAudioZoneTabAdapter(getChildFragmentManager());
         mViewPager.setAdapter(mAudioPlayerZoneAdapter);
-        mAudioPlayerZoneAdapter
-                .addFragment(new AudioSystemPlayerFragment(mCarAudioManager, mAudioManager),
-                        "System Players");
+        mAudioPlayerZoneAdapter.addFragment(
+                new AudioSystemPlayerFragment(mCarAudioManager, mAudioManager),
+                "System Players");
+        mAudioPlayerZoneAdapter.addFragment(new AudioTransientPlayersFragment(),
+                "Transient Sound Players");
         mAudioPlayerZoneAdapter.notifyDataSetChanged();
         mPlayerTabLayout.setupWithViewPager(mViewPager);
     }
diff --git a/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/audio/AudioTransientPlayersFragment.java b/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/audio/AudioTransientPlayersFragment.java
new file mode 100644
index 0000000..2cd68ad
--- /dev/null
+++ b/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/audio/AudioTransientPlayersFragment.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.android.car.kitchensink.audio;
+
+import static android.media.AudioAttributes.USAGE_ALARM;
+import static android.media.AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE;
+import static android.media.AudioAttributes.USAGE_ASSISTANT;
+import static android.media.AudioAttributes.USAGE_NOTIFICATION;
+import static android.media.AudioAttributes.USAGE_NOTIFICATION_RINGTONE;
+import static android.media.AudioAttributes.USAGE_VOICE_COMMUNICATION;
+
+import static com.google.android.car.kitchensink.R.raw.one2six;
+import static com.google.android.car.kitchensink.R.raw.turnright;
+
+import android.media.AudioAttributes;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+
+import androidx.fragment.app.Fragment;
+
+import com.android.internal.widget.LinearLayoutManager;
+import com.android.internal.widget.RecyclerView;
+
+import com.google.android.car.kitchensink.R;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public final class AudioTransientPlayersFragment extends Fragment {
+
+    private static final List<Integer> TRANSIENT_SOUND_USAGES = new ArrayList<>(Arrays.asList(
+            USAGE_ASSISTANCE_NAVIGATION_GUIDANCE, USAGE_ASSISTANT, USAGE_NOTIFICATION_RINGTONE,
+            USAGE_VOICE_COMMUNICATION, USAGE_ALARM, USAGE_NOTIFICATION));
+
+    private final List<AudioPlayer> mSystemPlayers =
+            new ArrayList<>(TRANSIENT_SOUND_USAGES.size());
+
+    private RecyclerView mRecyclerView;
+    private PlayerAdapter mSystemPlayerAdapter;
+
+    AudioTransientPlayersFragment() {
+    }
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container,
+            Bundle savedInstanceState) {
+        View view = inflater.inflate(R.layout.audio_player_tab, container, false);
+        initPlayers();
+
+        mRecyclerView = view.findViewById(R.id.players_view);
+
+        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
+
+        mSystemPlayerAdapter = new PlayerAdapter(mSystemPlayers);
+        mRecyclerView.setAdapter(mSystemPlayerAdapter);
+        mRecyclerView.scrollToPosition(0);
+
+        return view;
+    }
+
+    @Override
+    public void onDestroyView() {
+        super.onDestroyView();
+        for (int index = 0; index < mSystemPlayers.size(); index++) {
+            mSystemPlayers.get(index).stop();
+        }
+        mSystemPlayers.clear();
+    }
+
+    private void initPlayers() {
+        for (int index = 0; index < TRANSIENT_SOUND_USAGES.size(); index++) {
+            int usage = TRANSIENT_SOUND_USAGES.get(index);
+            mSystemPlayers.add(getCarSoundsPlayer(usage));
+        }
+    }
+
+    private AudioPlayer getCarSoundsPlayer(int usage) {
+        AudioAttributes attributes = new AudioAttributes.Builder().setUsage(usage).build();
+        return new AudioPlayer(getContext(), getPlayerResource(usage), attributes);
+    }
+
+    private int getPlayerResource(int usage) {
+        if (usage == USAGE_ASSISTANCE_NAVIGATION_GUIDANCE) {
+            return turnright;
+        }
+        return one2six;
+    }
+}
diff --git a/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/audio/PlayerAdapter.java b/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/audio/PlayerAdapter.java
index 9c2cd63..e61cf55 100644
--- a/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/audio/PlayerAdapter.java
+++ b/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink/audio/PlayerAdapter.java
@@ -16,7 +16,7 @@
 
 package com.google.android.car.kitchensink.audio;
 
-import static android.media.AudioManager.AUDIOFOCUS_GAIN_TRANSIENT;
+import static android.media.AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK;
 
 import static com.google.android.car.kitchensink.audio.AudioPlayer.PLAYER_STATE_COMPLETED;
 import static com.google.android.car.kitchensink.audio.AudioPlayer.PLAYER_STATE_DELAYED;
@@ -75,7 +75,7 @@
         }
 
         private void startPlayer() {
-            mPlayer.start(true, true, AUDIOFOCUS_GAIN_TRANSIENT, state -> {
+            mPlayer.start(true, true, AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK, state -> {
                 handleStateChange(state);
             });
         }