diff options
author | 2024-12-10 23:19:57 +0000 | |
---|---|---|
committer | 2024-12-10 23:19:57 +0000 | |
commit | ff841e4c80442e618e1ebca2455f69b22e182f6b (patch) | |
tree | b9067f887ed1e410928809c3d5706d1c1d36201e | |
parent | 4acfb8f36066345744fbc065d9d1018ece8b15cb (diff) | |
parent | 7b526fce10e5ffec13218dc9dab1d2da0c5035c8 (diff) |
Merge "[Media Quality] Move away from Guava" into main
3 files changed, 130 insertions, 15 deletions
diff --git a/services/core/Android.bp b/services/core/Android.bp index b3d85f8aac48..cbbb285ff1d0 100644 --- a/services/core/Android.bp +++ b/services/core/Android.bp @@ -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", diff --git a/services/core/java/com/android/server/media/quality/BiMap.java b/services/core/java/com/android/server/media/quality/BiMap.java new file mode 100644 index 000000000000..82b82847a29f --- /dev/null +++ b/services/core/java/com/android/server/media/quality/BiMap.java @@ -0,0 +1,119 @@ +/* + * 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(); + } +} 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 f363d5852354..3e488bf2207f 100644 --- a/services/core/java/com/android/server/media/quality/MediaQualityService.java +++ b/services/core/java/com/android/server/media/quality/MediaQualityService.java @@ -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; @@ -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); @@ -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(), @@ -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 + " = ?"; @@ -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(), @@ -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 + " = ?"; @@ -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; } @@ -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) { |