[Media Quality] Implement Sound Profile get related APIs

Test: cts and logs
Flag: android.media.tv.flags.media_quality_fw
Bug: 378708165

Change-Id: Ic7399b728988a74b8f724ff1a12af903ad2c4e7b
diff --git a/services/core/java/com/android/server/media/quality/MediaQualityService.java b/services/core/java/com/android/server/media/quality/MediaQualityService.java
index c7e00d3..a8ea5b6 100644
--- a/services/core/java/com/android/server/media/quality/MediaQualityService.java
+++ b/services/core/java/com/android/server/media/quality/MediaQualityService.java
@@ -25,7 +25,7 @@
 import android.media.quality.IMediaQualityManager;
 import android.media.quality.IPictureProfileCallback;
 import android.media.quality.ISoundProfileCallback;
-import android.media.quality.MediaQualityContract;
+import android.media.quality.MediaQualityContract.BaseParameters;
 import android.media.quality.ParamCapability;
 import android.media.quality.PictureProfile;
 import android.media.quality.PictureProfileHandle;
@@ -42,6 +42,7 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
+import java.util.stream.Collectors;
 
 /**
  * This service manage picture profile and sound profile for TV setting. Also communicates with the
@@ -75,10 +76,10 @@
             SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase();
 
             ContentValues values = new ContentValues();
-            values.put(MediaQualityContract.BaseParameters.PARAMETER_TYPE, pp.getProfileType());
-            values.put(MediaQualityContract.BaseParameters.PARAMETER_NAME, pp.getName());
-            values.put(MediaQualityContract.BaseParameters.PARAMETER_PACKAGE, pp.getPackageName());
-            values.put(MediaQualityContract.BaseParameters.PARAMETER_INPUT_ID, pp.getInputId());
+            values.put(BaseParameters.PARAMETER_TYPE, pp.getProfileType());
+            values.put(BaseParameters.PARAMETER_NAME, pp.getName());
+            values.put(BaseParameters.PARAMETER_PACKAGE, pp.getPackageName());
+            values.put(BaseParameters.PARAMETER_INPUT_ID, pp.getInputId());
             values.put(mMediaQualityDbHelper.SETTINGS, bundleToJson(pp.getParameters()));
 
             // id is auto-generated by SQLite upon successful insertion of row
@@ -90,6 +91,7 @@
         public void updatePictureProfile(String id, PictureProfile pp) {
             // TODO: implement
         }
+
         @Override
         public void removePictureProfile(String id) {
             // TODO: implement
@@ -99,8 +101,8 @@
         public PictureProfile getPictureProfile(int type, String name) {
             SQLiteDatabase db = mMediaQualityDbHelper.getReadableDatabase();
 
-            String selection = MediaQualityContract.BaseParameters.PARAMETER_TYPE + " = ? AND "
-                    + MediaQualityContract.BaseParameters.PARAMETER_NAME + " = ?";
+            String selection = BaseParameters.PARAMETER_TYPE + " = ? AND "
+                    + BaseParameters.PARAMETER_NAME + " = ?";
             String[] selectionArguments = {Integer.toString(type), name};
 
             try (
@@ -176,26 +178,26 @@
 
         private String[] getAllPictureProfileColumns() {
             return new String[]{
-                    MediaQualityContract.BaseParameters.PARAMETER_ID,
-                    MediaQualityContract.BaseParameters.PARAMETER_TYPE,
-                    MediaQualityContract.BaseParameters.PARAMETER_NAME,
-                    MediaQualityContract.BaseParameters.PARAMETER_INPUT_ID,
-                    MediaQualityContract.BaseParameters.PARAMETER_PACKAGE,
+                    BaseParameters.PARAMETER_ID,
+                    BaseParameters.PARAMETER_TYPE,
+                    BaseParameters.PARAMETER_NAME,
+                    BaseParameters.PARAMETER_INPUT_ID,
+                    BaseParameters.PARAMETER_PACKAGE,
                     mMediaQualityDbHelper.SETTINGS
             };
         }
 
         private PictureProfile getPictureProfileFromCursor(Cursor cursor) {
             String returnId = cursor.getString(cursor.getColumnIndexOrThrow(
-                    MediaQualityContract.BaseParameters.PARAMETER_ID));
+                    BaseParameters.PARAMETER_ID));
             int type = cursor.getInt(cursor.getColumnIndexOrThrow(
-                    MediaQualityContract.BaseParameters.PARAMETER_TYPE));
+                    BaseParameters.PARAMETER_TYPE));
             String name = cursor.getString(cursor.getColumnIndexOrThrow(
-                    MediaQualityContract.BaseParameters.PARAMETER_NAME));
+                    BaseParameters.PARAMETER_NAME));
             String inputId = cursor.getString(cursor.getColumnIndexOrThrow(
-                    MediaQualityContract.BaseParameters.PARAMETER_INPUT_ID));
+                    BaseParameters.PARAMETER_INPUT_ID));
             String packageName = cursor.getString(cursor.getColumnIndexOrThrow(
-                    MediaQualityContract.BaseParameters.PARAMETER_PACKAGE));
+                    BaseParameters.PARAMETER_PACKAGE));
             String settings = cursor.getString(
                     cursor.getColumnIndexOrThrow(mMediaQualityDbHelper.SETTINGS));
             return new PictureProfile(returnId, type, name, inputId,
@@ -204,7 +206,7 @@
 
         @Override
         public List<PictureProfile> getPictureProfilesByPackage(String packageName) {
-            String selection = MediaQualityContract.BaseParameters.PARAMETER_PACKAGE + " = ?";
+            String selection = BaseParameters.PARAMETER_PACKAGE + " = ?";
             String[] selectionArguments = {packageName};
             return getPictureProfilesBasedOnConditions(getAllPictureProfileColumns(), selection,
                     selectionArguments);
@@ -217,14 +219,12 @@
 
         @Override
         public List<String> getPictureProfilePackageNames() {
-            String [] column = {MediaQualityContract.BaseParameters.PARAMETER_NAME};
+            String [] column = {BaseParameters.PARAMETER_NAME};
             List<PictureProfile> pictureProfiles = getPictureProfilesBasedOnConditions(column,
                     null, null);
-            List<String> packageNames = new ArrayList<>();
-            for (PictureProfile pictureProfile: pictureProfiles) {
-                packageNames.add(pictureProfile.getName());
-            }
-            return packageNames;
+            return pictureProfiles.stream()
+                    .map(PictureProfile::getName)
+                    .collect(Collectors.toList());
         }
 
         private List<PictureProfile> getPictureProfilesBasedOnConditions(String[] columns,
@@ -267,23 +267,110 @@
         public void removeSoundProfile(String id) {
             // TODO: implement
         }
+
         @Override
         public SoundProfile getSoundProfile(int type, String id) {
-            return null;
+            SQLiteDatabase db = mMediaQualityDbHelper.getReadableDatabase();
+
+            String selection = BaseParameters.PARAMETER_ID + " = ?";
+            String[] selectionArguments = {id};
+
+            try (
+                    Cursor cursor = db.query(
+                            mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME,
+                            getAllSoundProfileColumns(),
+                            selection,
+                            selectionArguments,
+                            /*groupBy=*/ null,
+                            /*having=*/ null,
+                            /*orderBy=*/ null)
+            ) {
+                int count = cursor.getCount();
+                if (count == 0) {
+                    return null;
+                }
+                if (count > 1) {
+                    Log.wtf(TAG, String.format(Locale.US, "%d entries found for id=%s"
+                                    + " in %s. Should only ever be 0 or 1.", count, id,
+                            mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME));
+                    return null;
+                }
+                cursor.moveToFirst();
+                return getSoundProfileFromCursor(cursor);
+            }
         }
+
         @Override
         public List<SoundProfile> getSoundProfilesByPackage(String packageName) {
-            return new ArrayList<>();
+            String selection = BaseParameters.PARAMETER_PACKAGE + " = ?";
+            String[] selectionArguments = {packageName};
+            return getSoundProfilesBasedOnConditions(getAllSoundProfileColumns(), selection,
+                    selectionArguments);
         }
+
         @Override
         public List<SoundProfile> getAvailableSoundProfiles() {
             return new ArrayList<>();
         }
+
         @Override
         public List<String> getSoundProfilePackageNames() {
-            return new ArrayList<>();
+            String [] column = {BaseParameters.PARAMETER_NAME};
+            List<SoundProfile> soundProfiles = getSoundProfilesBasedOnConditions(column,
+                    null, null);
+            return soundProfiles.stream()
+                    .map(SoundProfile::getName)
+                    .collect(Collectors.toList());
         }
 
+        private String[] getAllSoundProfileColumns() {
+            return new String[]{
+                    BaseParameters.PARAMETER_ID,
+                    BaseParameters.PARAMETER_NAME,
+                    BaseParameters.PARAMETER_INPUT_ID,
+                    BaseParameters.PARAMETER_PACKAGE,
+                    mMediaQualityDbHelper.SETTINGS
+            };
+        }
+
+        private SoundProfile getSoundProfileFromCursor(Cursor cursor) {
+            String returnId = cursor.getString(
+                    cursor.getColumnIndexOrThrow(BaseParameters.PARAMETER_ID));
+            int type = cursor.getInt(
+                    cursor.getColumnIndexOrThrow(BaseParameters.PARAMETER_TYPE));
+            String name = cursor.getString(
+                    cursor.getColumnIndexOrThrow(BaseParameters.PARAMETER_NAME));
+            String inputId = cursor.getString(
+                    cursor.getColumnIndexOrThrow(BaseParameters.PARAMETER_INPUT_ID));
+            String packageName = cursor.getString(
+                    cursor.getColumnIndexOrThrow(BaseParameters.PARAMETER_PACKAGE));
+            String settings = cursor.getString(
+                    cursor.getColumnIndexOrThrow(mMediaQualityDbHelper.SETTINGS));
+            return new SoundProfile(returnId, type, name, inputId, packageName,
+                    jsonToBundle(settings));
+        }
+
+        private List<SoundProfile> getSoundProfilesBasedOnConditions(String[] columns,
+                String selection, String[] selectionArguments) {
+            SQLiteDatabase db = mMediaQualityDbHelper.getReadableDatabase();
+
+            try (
+                    Cursor cursor = db.query(
+                            mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME,
+                            columns,
+                            selection,
+                            selectionArguments,
+                            /*groupBy=*/ null,
+                            /*having=*/ null,
+                            /*orderBy=*/ null)
+            ) {
+                List<SoundProfile> soundProfiles = new ArrayList<>();
+                while (cursor.moveToNext()) {
+                    soundProfiles.add(getSoundProfileFromCursor(cursor));
+                }
+                return soundProfiles;
+            }
+        }
 
         @Override
         public void registerPictureProfileCallback(final IPictureProfileCallback callback) {