Re-add media storage logic

Save ordered list of last used sources, allow default to be set

Bug: 124451543
Test: Manual
(cherry picked from commit 299c01dbead0143a1786173ba5252f566cccf365)
Change-Id: Ia24532ea4f98756138de2befeca630e53356a8e3
diff --git a/service/res/values/strings.xml b/service/res/values/strings.xml
index e3c4895..0855727 100644
--- a/service/res/values/strings.xml
+++ b/service/res/values/strings.xml
@@ -237,4 +237,7 @@
     <string name="car_permission_label_car_power">Car Power</string>
     <!-- Permission text: apps read information of car's power state [CHAR LIMIT=NONE] -->
     <string name="car_permission_desc_car_power">Access car power state</string>
+
+    <!-- The package name of the media application that will be selected as the default [CHAR LIMIT=NONE] -->
+    <string name="default_media_application" translatable="false">com.android.bluetooth</string>
 </resources>
diff --git a/service/src/com/android/car/CarMediaService.java b/service/src/com/android/car/CarMediaService.java
index bcf8c99..bf08674 100644
--- a/service/src/com/android/car/CarMediaService.java
+++ b/service/src/com/android/car/CarMediaService.java
@@ -37,16 +37,21 @@
 import android.text.TextUtils;
 import android.util.Log;
 
+import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 
 import com.android.car.user.CarUserService;
 
 import java.io.PrintWriter;
+import java.util.ArrayDeque;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
+import java.util.Deque;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * CarMediaService manages the currently active media source for car apps. This is different from
@@ -61,6 +66,7 @@
 
     private static final String SOURCE_KEY = "media_source";
     private static final String SHARED_PREF = "com.android.car.media.car_media_service";
+    private static final String PACKAGE_NAME_SEPARATOR = ",";
 
     private final Context mContext;
     private final MediaSessionManager mMediaSessionManager;
@@ -127,7 +133,7 @@
     public void init() {
         CarLocalServices.getService(CarUserService.class).runOnUser0Unlock(() -> {
             mSharedPrefs = mContext.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
-            mPrimaryMediaPackage = mSharedPrefs.getString(SOURCE_KEY, null);
+            mPrimaryMediaPackage = getLastMediaPackage();
         });
     }
 
@@ -272,7 +278,7 @@
 
         if (mSharedPrefs != null) {
             if (!TextUtils.isEmpty(mPrimaryMediaPackage)) {
-                mSharedPrefs.edit().putString(SOURCE_KEY, mPrimaryMediaPackage).apply();
+                saveLastMediaPackage(mPrimaryMediaPackage);
                 mRemovedMediaSourcePackage = null;
             }
         } else {
@@ -317,7 +323,6 @@
         }
     }
 
-
     private boolean isMediaService(String packageName) {
         PackageManager packageManager = mContext.getPackageManager();
         Intent mediaIntent = new Intent();
@@ -328,4 +333,43 @@
                 PackageManager.GET_RESOLVED_FILTER);
         return mediaServices.size() > 0;
     }
+
+    private void saveLastMediaPackage(@NonNull String packageName) {
+        String serialized = mSharedPrefs.getString(SOURCE_KEY, null);
+        if (serialized == null) {
+            mSharedPrefs.edit().putString(SOURCE_KEY, packageName).apply();
+        } else {
+            Deque<String> packageNames = getPackageNameList(serialized);
+            packageNames.remove(packageName);
+            packageNames.addFirst(packageName);
+            mSharedPrefs.edit().putString(SOURCE_KEY, serializePackageNameList(packageNames))
+                    .apply();
+        }
+    }
+
+    private String getLastMediaPackage() {
+        String serialized = mSharedPrefs.getString(SOURCE_KEY, null);
+        if (!TextUtils.isEmpty(serialized)) {
+            for (String packageName : getPackageNameList(serialized)) {
+                if (isMediaService(packageName)) {
+                    return packageName;
+                }
+            }
+        }
+
+        String defaultSourcePackage = mContext.getString(R.string.default_media_application);
+        if (isMediaService(defaultSourcePackage)) {
+            return defaultSourcePackage;
+        }
+        return null;
+    }
+
+    private String serializePackageNameList(Deque<String> packageNames) {
+        return packageNames.stream().collect(Collectors.joining(PACKAGE_NAME_SEPARATOR));
+    }
+
+    private Deque<String> getPackageNameList(String serialized) {
+        String[] packageNames = serialized.split(PACKAGE_NAME_SEPARATOR);
+        return new ArrayDeque(Arrays.asList(packageNames));
+    }
 }