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

Commit 33397475 authored by Wenyu Zhang's avatar Wenyu Zhang Committed by Android (Google) Code Review
Browse files

Merge "Small refactor AudioDeviceVolumeManager in AudioService" into main

parents ed9508ed 44eb5aa1
Loading
Loading
Loading
Loading
+10 −11
Original line number Diff line number Diff line
@@ -493,7 +493,7 @@ public class AudioService extends IAudioService.Stub
    private static final int MSG_INIT_ADI_DEVICE_STATES = 103;
    private static final int MSG_INIT_INPUT_GAINS = 104;
    private static final int MSG_SET_INPUT_GAIN_INDEX = 105;
    private static final int MSG_APPLY_INPUT_GAIN_INDEX = 105;
    private static final int MSG_PERSIST_INPUT_GAIN_INDEX = 106;
    // end of messages handled under wakelock
@@ -1626,7 +1626,6 @@ public class AudioService extends IAudioService.Stub
                new InputDeviceVolumeHelper(
                        mSettings,
                        mContentResolver,
                        mSettingsLock,
                        System.INPUT_GAIN_INDEX_SETTINGS);
    }
@@ -5804,7 +5803,7 @@ public class AudioService extends IAudioService.Stub
            // to persist).
            sendMsg(
                    mAudioHandler,
                    MSG_SET_INPUT_GAIN_INDEX,
                    MSG_APPLY_INPUT_GAIN_INDEX,
                    SENDMSG_QUEUE,
                    /*arg1*/ index,
                    /*arg2*/ 0,
@@ -5813,22 +5812,22 @@ public class AudioService extends IAudioService.Stub
        }
    }
    private void setInputGainIndexInt(@NonNull AudioDeviceAttributes ada, int index) {
    private void onApplyInputGainIndex(@NonNull AudioDeviceAttributes ada, int index) {
        // TODO(b/364923030): call AudioSystem to apply input gain in native layer.
        // Post a persist input gain msg.
        sendMsg(
                mAudioHandler,
                MSG_PERSIST_INPUT_GAIN_INDEX,
                SENDMSG_QUEUE,
                /*arg1*/ index,
                SENDMSG_REPLACE,
                /*arg1*/ 0,
                /*arg2*/ 0,
                /*obj*/ ada,
                PERSIST_DELAY);
    }
    private void persistInputGainIndex(@NonNull AudioDeviceAttributes ada, int index) {
        mInputDeviceVolumeHelper.persistInputGainIndex(ada, index);
    private void onPersistInputGainIndex(@NonNull AudioDeviceAttributes ada) {
        mInputDeviceVolumeHelper.persistInputGainIndex(ada);
    }
    /**
@@ -10213,12 +10212,12 @@ public class AudioService extends IAudioService.Stub
                    vgs.persistVolumeGroup(msg.arg1);
                    break;
                case MSG_SET_INPUT_GAIN_INDEX:
                    setInputGainIndexInt((AudioDeviceAttributes) msg.obj, msg.arg1);
                case MSG_APPLY_INPUT_GAIN_INDEX:
                    onApplyInputGainIndex((AudioDeviceAttributes) msg.obj, msg.arg1);
                    break;
                case MSG_PERSIST_INPUT_GAIN_INDEX:
                    persistInputGainIndex((AudioDeviceAttributes) msg.obj, msg.arg1);
                    onPersistInputGainIndex((AudioDeviceAttributes) msg.obj);
                    break;
                case MSG_PERSIST_RINGER_MODE:
+23 −35
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 The Android Open Source Project
 * 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.
@@ -43,7 +43,6 @@ import java.util.Set;

    private final SettingsAdapter mSettings;
    private final ContentResolver mContentResolver;
    private final Object mSettingsLock;
    private final String mInputGainIndexSettingsName;

    // A map between device internal type (e.g. AudioSystem.DEVICE_IN_BUILTIN_MIC) to its input gain
@@ -54,11 +53,9 @@ import java.util.Set;
    InputDeviceVolumeHelper(
            SettingsAdapter settings,
            ContentResolver contentResolver,
            Object settingsLock,
            String settingsName) {
        mSettings = settings;
        mContentResolver = contentResolver;
        mSettingsLock = settingsLock;
        mInputGainIndexSettingsName = settingsName;

        IntArray internalDeviceTypes = new IntArray();
@@ -82,34 +79,27 @@ import java.util.Set;
        readSettings();
    }

    public void readSettings() {
    private void readSettings() {
        synchronized (InputDeviceVolumeHelper.class) {
            for (int inputDeviceType : mSupportedDeviceTypes) {
                // Retrieve current input gain for device. If no input gain stored for current
                // device, use default input gain.
                int index;
                if (!hasValidSettingsName()) {
                    index = INDEX_DEFAULT;
                } else {
                String name = getSettingNameForDevice(inputDeviceType);
                    index =
                            mSettings.getSystemIntForUser(
                int index = name == null
                        ? INDEX_DEFAULT
                        : mSettings.getSystemIntForUser(
                                mContentResolver, name, INDEX_DEFAULT, UserHandle.USER_CURRENT);
                }

                mInputGainIndexMap.put(inputDeviceType, getValidIndex(index));
            }
        }
    }

    public boolean hasValidSettingsName() {
        return mInputGainIndexSettingsName != null && !mInputGainIndexSettingsName.isEmpty();
    }

    public @Nullable String getSettingNameForDevice(int inputDeviceType) {
        if (!hasValidSettingsName()) {
    private @Nullable String getSettingNameForDevice(int inputDeviceType) {
        if (mInputGainIndexSettingsName == null || mInputGainIndexSettingsName.isEmpty()) {
            return null;
        }

        final String suffix = AudioSystem.getInputDeviceName(inputDeviceType);
        if (suffix.isEmpty()) {
            return mInputGainIndexSettingsName;
@@ -158,7 +148,6 @@ import java.util.Set;
        ensureValidInputDeviceType(inputDeviceType);

        int oldIndex;
        synchronized (mSettingsLock) {
        synchronized (InputDeviceVolumeHelper.class) {
            oldIndex = getInputGainIndex(ada);
            index = getValidIndex(index);
@@ -171,16 +160,15 @@ import java.util.Set;
            return true;
        }
    }
    }

    public void persistInputGainIndex(@NonNull AudioDeviceAttributes ada, int index) {
    public void persistInputGainIndex(@NonNull AudioDeviceAttributes ada) {
        int inputDeviceType = AudioDeviceInfo.convertDeviceTypeToInternalInputDevice(ada.getType());
        ensureValidInputDeviceType(inputDeviceType);

        if (hasValidSettingsName()) {
        String name = getSettingNameForDevice(inputDeviceType);
        if (name != null) {
            int index = getInputGainIndex(ada);
            mSettings.putSystemIntForUser(
                    mContentResolver,
                    getSettingNameForDevice(inputDeviceType),
                    name,
                    index,
                    UserHandle.USER_CURRENT);
        }