Loading services/core/Android.bp +0 −1 Original line number Diff line number Diff line Loading @@ -222,7 +222,6 @@ java_library_static { "securebox", "apache-commons-math", "battery_saver_flag_lib", "guava", "notification_flags_lib", "power_hint_flags_lib", "biometrics_flags_lib", Loading services/core/java/com/android/server/media/quality/BiMap.java 0 → 100644 +119 −0 Original line number Diff line number Diff line /* * Copyright (C) 2024 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.android.server.media.quality; import android.util.ArrayMap; import java.util.Collection; import java.util.Map; /** * A very basic bidirectional map. * * @param <K> data type of Key * @param <V> data type of Value */ public class BiMap<K, V> { private Map<K, V> mPrimaryMap = new ArrayMap<>(); private Map<V, K> mSecondaryMap = new ArrayMap<>(); /** * Add key and associated value to the map * * @param key key to add * @param value value to add * @return true if successfully added, false otherwise */ public boolean put(K key, V value) { if (key == null || value == null || mPrimaryMap.containsKey(key) || mSecondaryMap.containsKey(value)) { return false; } mPrimaryMap.put(key, value); mSecondaryMap.put(value, key); return true; } /** * Remove key and associated value from the map * * @param key key to remove * @return true if removed, false otherwise */ public boolean remove(K key) { if (key == null) { return false; } if (mPrimaryMap.containsKey(key)) { V value = getValue(key); mPrimaryMap.remove(key); mSecondaryMap.remove(value); return true; } return false; } /** * Remove value and associated key from the map * * @param value value to remove * @return true if removed, false otherwise */ public boolean removeValue(V value) { if (value == null) { return false; } return remove(getKey(value)); } /** * Get the value * * @param key key for which to get value * @return V */ public V getValue(K key) { return mPrimaryMap.get(key); } /** * Get the key * * @param value value for which to get key * @return K */ public K getKey(V value) { return mSecondaryMap.get(value); } /** * Get the values of the map. * @return Collection */ public Collection<V> getValues() { return mPrimaryMap.values(); } /** * Clear the map */ public void clear() { mPrimaryMap.clear(); mSecondaryMap.clear(); } } services/core/java/com/android/server/media/quality/MediaQualityService.java +11 −14 Original line number Diff line number Diff line Loading @@ -38,9 +38,6 @@ import android.util.Log; import com.android.server.SystemService; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import org.json.JSONException; import org.json.JSONObject; Loading Loading @@ -68,8 +65,8 @@ public class MediaQualityService extends SystemService { public MediaQualityService(Context context) { super(context); mContext = context; mPictureProfileTempIdMap = HashBiMap.create(); mSoundProfileTempIdMap = HashBiMap.create(); mPictureProfileTempIdMap = new BiMap<>(); mSoundProfileTempIdMap = new BiMap<>(); mMediaQualityDbHelper = new MediaQualityDbHelper(mContext); mMediaQualityDbHelper.setWriteAheadLoggingEnabled(true); mMediaQualityDbHelper.setIdleConnectionTimeout(30); Loading Loading @@ -98,13 +95,13 @@ public class MediaQualityService extends SystemService { Long id = db.insert(mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME, null, values); populateTempIdMap(mPictureProfileTempIdMap, id); pp.setProfileId(mPictureProfileTempIdMap.get(id)); pp.setProfileId(mPictureProfileTempIdMap.getValue(id)); return pp; } @Override public void updatePictureProfile(String id, PictureProfile pp, UserHandle user) { Long intId = mPictureProfileTempIdMap.inverse().get(id); Long intId = mPictureProfileTempIdMap.getKey(id); ContentValues values = getContentValues(intId, pp.getProfileType(), Loading @@ -120,7 +117,7 @@ public class MediaQualityService extends SystemService { @Override public void removePictureProfile(String id, UserHandle user) { Long intId = mPictureProfileTempIdMap.inverse().get(id); Long intId = mPictureProfileTempIdMap.getKey(id); if (intId != null) { SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); String selection = BaseParameters.PARAMETER_ID + " = ?"; Loading Loading @@ -220,13 +217,13 @@ public class MediaQualityService extends SystemService { Long id = db.insert(mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME, null, values); populateTempIdMap(mSoundProfileTempIdMap, id); sp.setProfileId(mSoundProfileTempIdMap.get(id)); sp.setProfileId(mSoundProfileTempIdMap.getValue(id)); return sp; } @Override public void updateSoundProfile(String id, SoundProfile sp, UserHandle user) { Long intId = mSoundProfileTempIdMap.inverse().get(id); Long intId = mSoundProfileTempIdMap.getKey(id); ContentValues values = getContentValues(intId, sp.getProfileType(), Loading @@ -241,7 +238,7 @@ public class MediaQualityService extends SystemService { @Override public void removeSoundProfile(String id, UserHandle user) { Long intId = mSoundProfileTempIdMap.inverse().get(id); Long intId = mSoundProfileTempIdMap.getKey(id); if (intId != null) { SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); String selection = BaseParameters.PARAMETER_ID + " = ?"; Loading Loading @@ -317,12 +314,12 @@ public class MediaQualityService extends SystemService { } private void populateTempIdMap(BiMap<Long, String> map, Long id) { if (id != null && map.get(id) == null) { if (id != null && map.getValue(id) == null) { String uuid; int attempts = 0; while (attempts < MAX_UUID_GENERATION_ATTEMPTS) { uuid = UUID.randomUUID().toString(); if (!map.inverse().containsKey(uuid)) { if (map.getKey(uuid) == null) { map.put(id, uuid); return; } Loading Loading @@ -448,7 +445,7 @@ public class MediaQualityService extends SystemService { int colIndex = cursor.getColumnIndex(BaseParameters.PARAMETER_ID); Long dbId = colIndex != -1 ? cursor.getLong(colIndex) : null; populateTempIdMap(map, dbId); return map.get(dbId); return map.getValue(dbId); } private int getType(Cursor cursor) { Loading Loading
services/core/Android.bp +0 −1 Original line number Diff line number Diff line Loading @@ -222,7 +222,6 @@ java_library_static { "securebox", "apache-commons-math", "battery_saver_flag_lib", "guava", "notification_flags_lib", "power_hint_flags_lib", "biometrics_flags_lib", Loading
services/core/java/com/android/server/media/quality/BiMap.java 0 → 100644 +119 −0 Original line number Diff line number Diff line /* * Copyright (C) 2024 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.android.server.media.quality; import android.util.ArrayMap; import java.util.Collection; import java.util.Map; /** * A very basic bidirectional map. * * @param <K> data type of Key * @param <V> data type of Value */ public class BiMap<K, V> { private Map<K, V> mPrimaryMap = new ArrayMap<>(); private Map<V, K> mSecondaryMap = new ArrayMap<>(); /** * Add key and associated value to the map * * @param key key to add * @param value value to add * @return true if successfully added, false otherwise */ public boolean put(K key, V value) { if (key == null || value == null || mPrimaryMap.containsKey(key) || mSecondaryMap.containsKey(value)) { return false; } mPrimaryMap.put(key, value); mSecondaryMap.put(value, key); return true; } /** * Remove key and associated value from the map * * @param key key to remove * @return true if removed, false otherwise */ public boolean remove(K key) { if (key == null) { return false; } if (mPrimaryMap.containsKey(key)) { V value = getValue(key); mPrimaryMap.remove(key); mSecondaryMap.remove(value); return true; } return false; } /** * Remove value and associated key from the map * * @param value value to remove * @return true if removed, false otherwise */ public boolean removeValue(V value) { if (value == null) { return false; } return remove(getKey(value)); } /** * Get the value * * @param key key for which to get value * @return V */ public V getValue(K key) { return mPrimaryMap.get(key); } /** * Get the key * * @param value value for which to get key * @return K */ public K getKey(V value) { return mSecondaryMap.get(value); } /** * Get the values of the map. * @return Collection */ public Collection<V> getValues() { return mPrimaryMap.values(); } /** * Clear the map */ public void clear() { mPrimaryMap.clear(); mSecondaryMap.clear(); } }
services/core/java/com/android/server/media/quality/MediaQualityService.java +11 −14 Original line number Diff line number Diff line Loading @@ -38,9 +38,6 @@ import android.util.Log; import com.android.server.SystemService; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import org.json.JSONException; import org.json.JSONObject; Loading Loading @@ -68,8 +65,8 @@ public class MediaQualityService extends SystemService { public MediaQualityService(Context context) { super(context); mContext = context; mPictureProfileTempIdMap = HashBiMap.create(); mSoundProfileTempIdMap = HashBiMap.create(); mPictureProfileTempIdMap = new BiMap<>(); mSoundProfileTempIdMap = new BiMap<>(); mMediaQualityDbHelper = new MediaQualityDbHelper(mContext); mMediaQualityDbHelper.setWriteAheadLoggingEnabled(true); mMediaQualityDbHelper.setIdleConnectionTimeout(30); Loading Loading @@ -98,13 +95,13 @@ public class MediaQualityService extends SystemService { Long id = db.insert(mMediaQualityDbHelper.PICTURE_QUALITY_TABLE_NAME, null, values); populateTempIdMap(mPictureProfileTempIdMap, id); pp.setProfileId(mPictureProfileTempIdMap.get(id)); pp.setProfileId(mPictureProfileTempIdMap.getValue(id)); return pp; } @Override public void updatePictureProfile(String id, PictureProfile pp, UserHandle user) { Long intId = mPictureProfileTempIdMap.inverse().get(id); Long intId = mPictureProfileTempIdMap.getKey(id); ContentValues values = getContentValues(intId, pp.getProfileType(), Loading @@ -120,7 +117,7 @@ public class MediaQualityService extends SystemService { @Override public void removePictureProfile(String id, UserHandle user) { Long intId = mPictureProfileTempIdMap.inverse().get(id); Long intId = mPictureProfileTempIdMap.getKey(id); if (intId != null) { SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); String selection = BaseParameters.PARAMETER_ID + " = ?"; Loading Loading @@ -220,13 +217,13 @@ public class MediaQualityService extends SystemService { Long id = db.insert(mMediaQualityDbHelper.SOUND_QUALITY_TABLE_NAME, null, values); populateTempIdMap(mSoundProfileTempIdMap, id); sp.setProfileId(mSoundProfileTempIdMap.get(id)); sp.setProfileId(mSoundProfileTempIdMap.getValue(id)); return sp; } @Override public void updateSoundProfile(String id, SoundProfile sp, UserHandle user) { Long intId = mSoundProfileTempIdMap.inverse().get(id); Long intId = mSoundProfileTempIdMap.getKey(id); ContentValues values = getContentValues(intId, sp.getProfileType(), Loading @@ -241,7 +238,7 @@ public class MediaQualityService extends SystemService { @Override public void removeSoundProfile(String id, UserHandle user) { Long intId = mSoundProfileTempIdMap.inverse().get(id); Long intId = mSoundProfileTempIdMap.getKey(id); if (intId != null) { SQLiteDatabase db = mMediaQualityDbHelper.getWritableDatabase(); String selection = BaseParameters.PARAMETER_ID + " = ?"; Loading Loading @@ -317,12 +314,12 @@ public class MediaQualityService extends SystemService { } private void populateTempIdMap(BiMap<Long, String> map, Long id) { if (id != null && map.get(id) == null) { if (id != null && map.getValue(id) == null) { String uuid; int attempts = 0; while (attempts < MAX_UUID_GENERATION_ATTEMPTS) { uuid = UUID.randomUUID().toString(); if (!map.inverse().containsKey(uuid)) { if (map.getKey(uuid) == null) { map.put(id, uuid); return; } Loading Loading @@ -448,7 +445,7 @@ public class MediaQualityService extends SystemService { int colIndex = cursor.getColumnIndex(BaseParameters.PARAMETER_ID); Long dbId = colIndex != -1 ? cursor.getLong(colIndex) : null; populateTempIdMap(map, dbId); return map.get(dbId); return map.getValue(dbId); } private int getType(Cursor cursor) { Loading