Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f6276953 authored by Xin Li's avatar Xin Li Committed by Automerger Merge Worker
Browse files

Merge "[Media Quality] Move away from Guava" into main am: e95acbef

parents daa57be1 e95acbef
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -226,7 +226,6 @@ java_library_static {
        "securebox",
        "apache-commons-math",
        "battery_saver_flag_lib",
        "guava",
        "notification_flags_lib",
        "power_hint_flags_lib",
        "biometrics_flags_lib",
+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();
    }
}
+9 −12
Original line number Diff line number Diff line
@@ -37,9 +37,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;

@@ -66,8 +63,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);
@@ -96,7 +93,7 @@ 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;
        }

@@ -107,7 +104,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 + " = ?";
@@ -202,7 +199,7 @@ 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;
        }

@@ -213,7 +210,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 + " = ?";
@@ -284,9 +281,9 @@ 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 = UUID.randomUUID().toString();
                while (map.inverse().containsKey(uuid)) {
                while (map.getKey(uuid) != null) {
                    uuid = UUID.randomUUID().toString();
                }
                map.put(id, uuid);
@@ -386,7 +383,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) {