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

Commit 4de1b200 authored by Eric Laurent's avatar Eric Laurent
Browse files

RESTRICT AUTOMERGE - Revert "Refactor the SADeviceState to AdiDeviceState"

Revert submission 25286114-anon_bt_address_sc_dev

Reason for revert: b/329515274
Reverted changes: /q/submissionid:25286114-anon_bt_address_sc_dev

Change-Id: I808f111cbc0c493edde92906e7b7700b48b05c5f
Merged-In: I97a97fea0952227a9663e7001c9f031fb3cc725e
parent 602efc4c
Loading
Loading
Loading
Loading
+0 −7
Original line number Diff line number Diff line
@@ -8963,13 +8963,6 @@ public final class Settings {
        @Readable
        public static final String UNSAFE_VOLUME_MUSIC_ACTIVE_MS = "unsafe_volume_music_active_ms";
        /**
         * Internal collection of audio device inventory items
         * The device item stored are {@link com.android.server.audio.AdiDeviceState}
         * @hide
         */
        public static final String AUDIO_DEVICE_INVENTORY = "audio_device_inventory";
        /**
         * Indicates whether notification display on the lock screen is enabled.
         * <p>
+0 −63
Original line number Diff line number Diff line
@@ -1119,9 +1119,6 @@ public class AudioSystem
    public static final Set<Integer> DEVICE_IN_ALL_SCO_SET;
    /** @hide */
    public static final Set<Integer> DEVICE_IN_ALL_USB_SET;
    /** @hide */
    public static final Set<Integer> DEVICE_IN_ALL_BLE_SET;

    static {
        DEVICE_IN_ALL_SET = new HashSet<>();
        DEVICE_IN_ALL_SET.add(DEVICE_IN_COMMUNICATION);
@@ -1161,66 +1158,6 @@ public class AudioSystem
        DEVICE_IN_ALL_USB_SET.add(DEVICE_IN_USB_ACCESSORY);
        DEVICE_IN_ALL_USB_SET.add(DEVICE_IN_USB_DEVICE);
        DEVICE_IN_ALL_USB_SET.add(DEVICE_IN_USB_HEADSET);

        DEVICE_IN_ALL_BLE_SET = new HashSet<>();
        DEVICE_IN_ALL_BLE_SET.add(DEVICE_IN_BLE_HEADSET);
    }

    /** @hide */
    public static boolean isBluetoothDevice(int deviceType) {
        return isBluetoothA2dpOutDevice(deviceType)
                || isBluetoothScoDevice(deviceType)
                || isBluetoothLeDevice(deviceType);
    }

    /** @hide */
    public static boolean isBluetoothOutDevice(int deviceType) {
        return isBluetoothA2dpOutDevice(deviceType)
                || isBluetoothScoOutDevice(deviceType)
                || isBluetoothLeOutDevice(deviceType);
    }

    /** @hide */
    public static boolean isBluetoothInDevice(int deviceType) {
        return isBluetoothScoInDevice(deviceType)
                || isBluetoothLeInDevice(deviceType);
    }

    /** @hide */
    public static boolean isBluetoothA2dpOutDevice(int deviceType) {
        return DEVICE_OUT_ALL_A2DP_SET.contains(deviceType);
    }

    /** @hide */
    public static boolean isBluetoothScoOutDevice(int deviceType) {
        return DEVICE_OUT_ALL_SCO_SET.contains(deviceType);
    }

    /** @hide */
    public static boolean isBluetoothScoInDevice(int deviceType) {
        return DEVICE_IN_ALL_SCO_SET.contains(deviceType);
    }

    /** @hide */
    public static boolean isBluetoothScoDevice(int deviceType) {
        return isBluetoothScoOutDevice(deviceType)
                || isBluetoothScoInDevice(deviceType);
    }

    /** @hide */
    public static boolean isBluetoothLeOutDevice(int deviceType) {
        return DEVICE_OUT_ALL_BLE_SET.contains(deviceType);
    }

    /** @hide */
    public static boolean isBluetoothLeInDevice(int deviceType) {
        return DEVICE_IN_ALL_BLE_SET.contains(deviceType);
    }

    /** @hide */
    public static boolean isBluetoothLeDevice(int deviceType) {
        return isBluetoothLeOutDevice(deviceType)
                || isBluetoothLeInDevice(deviceType);
    }

    /** @hide */
+0 −1
Original line number Diff line number Diff line
@@ -615,7 +615,6 @@ public class SettingsBackupTest {
                 Settings.Secure.ASSIST_SCREENSHOT_ENABLED,
                 Settings.Secure.ASSIST_STRUCTURE_ENABLED,
                 Settings.Secure.ATTENTIVE_TIMEOUT,
                 Settings.Secure.AUDIO_DEVICE_INVENTORY, // setting not controllable by user
                 Settings.Secure.AUTOFILL_FEATURE_FIELD_CLASSIFICATION,
                 Settings.Secure.AUTOFILL_USER_DATA_MAX_CATEGORY_COUNT,
                 Settings.Secure.AUTOFILL_USER_DATA_MAX_FIELD_CLASSIFICATION_IDS_SIZE,
+0 −206
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.audio;

import static android.media.AudioSystem.DEVICE_NONE;
import static android.media.AudioSystem.isBluetoothDevice;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.media.AudioDeviceAttributes;
import android.media.AudioDeviceInfo;
import android.text.TextUtils;
import android.util.Log;

import java.util.Objects;

/**
 * Class representing all devices that were previously or are currently connected. Data is
 * persisted in {@link android.provider.Settings.Secure}
 */
/*package*/ final class AdiDeviceState {
    private static final String TAG = "AS.AdiDeviceState";

    private static final String SETTING_FIELD_SEPARATOR = ",";

    @AudioDeviceInfo.AudioDeviceType
    private final int mDeviceType;

    private final int mInternalDeviceType;
    @NonNull
    private final String mDeviceAddress;
    private boolean mSAEnabled;
    private boolean mHasHeadTracker = false;
    private boolean mHeadTrackerEnabled;

    /**
     * Constructor
     *
     * @param deviceType external audio device type
     * @param internalDeviceType if not set pass {@link DEVICE_NONE}, in this case the
     *                           default conversion of the external type will be used
     * @param address must be non-null for wireless devices
     * @throws NullPointerException if a null address is passed for a wireless device
     */
    AdiDeviceState(@AudioDeviceInfo.AudioDeviceType int deviceType,
                        int internalDeviceType,
                        @Nullable String address) {
        mDeviceType = deviceType;
        if (internalDeviceType != DEVICE_NONE) {
            mInternalDeviceType = internalDeviceType;
        } else {
            mInternalDeviceType = AudioDeviceInfo.convertDeviceTypeToInternalDevice(deviceType);

        }
        mDeviceAddress = isBluetoothDevice(mInternalDeviceType) ? Objects.requireNonNull(
                address) : "";
    }



    @AudioDeviceInfo.AudioDeviceType
    public int getDeviceType() {
        return mDeviceType;
    }

    public int getInternalDeviceType() {
        return mInternalDeviceType;
    }

    @NonNull
    public String getDeviceAddress() {
        return mDeviceAddress;
    }

    public void setSAEnabled(boolean sAEnabled) {
        mSAEnabled = sAEnabled;
    }

    public boolean isSAEnabled() {
        return mSAEnabled;
    }

    public void setHeadTrackerEnabled(boolean headTrackerEnabled) {
        mHeadTrackerEnabled = headTrackerEnabled;
    }

    public boolean isHeadTrackerEnabled() {
        return mHeadTrackerEnabled;
    }

    public void setHasHeadTracker(boolean hasHeadTracker) {
        mHasHeadTracker = hasHeadTracker;
    }


    public boolean hasHeadTracker() {
        return mHasHeadTracker;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        // type check and cast
        if (getClass() != obj.getClass()) {
            return false;
        }
        final AdiDeviceState sads = (AdiDeviceState) obj;
        return mDeviceType == sads.mDeviceType
                && mInternalDeviceType == sads.mInternalDeviceType
                && mDeviceAddress.equals(sads.mDeviceAddress)  // NonNull
                && mSAEnabled == sads.mSAEnabled
                && mHasHeadTracker == sads.mHasHeadTracker
                && mHeadTrackerEnabled == sads.mHeadTrackerEnabled;
    }

    @Override
    public int hashCode() {
        return Objects.hash(mDeviceType, mInternalDeviceType, mDeviceAddress, mSAEnabled,
                mHasHeadTracker, mHeadTrackerEnabled);
    }

    @Override
    public String toString() {
        return "type: " + mDeviceType + "internal type: " + mInternalDeviceType
                + " addr: " + mDeviceAddress + " enabled: " + mSAEnabled
                + " HT: " + mHasHeadTracker + " HTenabled: " + mHeadTrackerEnabled;
    }

    public String toPersistableString() {
        return (new StringBuilder().append(mDeviceType)
                .append(SETTING_FIELD_SEPARATOR).append(mDeviceAddress)
                .append(SETTING_FIELD_SEPARATOR).append(mSAEnabled ? "1" : "0")
                .append(SETTING_FIELD_SEPARATOR).append(mHasHeadTracker ? "1" : "0")
                .append(SETTING_FIELD_SEPARATOR).append(mHeadTrackerEnabled ? "1" : "0")
                .append(SETTING_FIELD_SEPARATOR).append(mInternalDeviceType)
                .toString());
    }

    /**
     * Gets the max size (including separators) when persisting the elements with
     * {@link AdiDeviceState#toPersistableString()}.
     */
    public static int getPeristedMaxSize() {
        return 36;  /* (mDeviceType)2 + (mDeviceAddresss)17 + (mInternalDeviceType)9 + (mSAEnabled)1
                           + (mHasHeadTracker)1 + (mHasHeadTrackerEnabled)1
                           + (SETTINGS_FIELD_SEPARATOR)5 */
    }

    @Nullable
    public static AdiDeviceState fromPersistedString(@Nullable String persistedString) {
        if (persistedString == null) {
            return null;
        }
        if (persistedString.isEmpty()) {
            return null;
        }
        String[] fields = TextUtils.split(persistedString, SETTING_FIELD_SEPARATOR);
        // we may have 5 fields for the legacy AdiDeviceState and 6 containing the internal
        // device type
        if (fields.length != 5 && fields.length != 6) {
            // expecting all fields, fewer may mean corruption, ignore those settings
            return null;
        }
        try {
            final int deviceType = Integer.parseInt(fields[0]);
            int internalDeviceType = -1;
            if (fields.length == 6) {
                internalDeviceType = Integer.parseInt(fields[5]);
            }
            final AdiDeviceState deviceState = new AdiDeviceState(deviceType,
                    internalDeviceType, fields[1]);
            deviceState.setHasHeadTracker(Integer.parseInt(fields[2]) == 1);
            deviceState.setHasHeadTracker(Integer.parseInt(fields[3]) == 1);
            deviceState.setHeadTrackerEnabled(Integer.parseInt(fields[4]) == 1);
            return deviceState;
        } catch (NumberFormatException e) {
            Log.e(TAG, "unable to parse setting for AdiDeviceState: " + persistedString, e);
            return null;
        }
    }

    public AudioDeviceAttributes getAudioDeviceAttributes() {
        return new AudioDeviceAttributes(AudioDeviceAttributes.ROLE_OUTPUT,
                mDeviceType, mDeviceAddress);
    }

}
+2 −72
Original line number Diff line number Diff line
@@ -47,7 +47,6 @@ import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.util.PrintWriterPrinter;
@@ -64,11 +63,8 @@ import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;


/**
 * @hide
 * (non final for mocking/spying)
 */
public class AudioDeviceBroker {
/** @hide */
/*package*/ final class AudioDeviceBroker {

    private static final String TAG = "AS.AudioDeviceBroker";

@@ -1419,9 +1415,6 @@ public class AudioDeviceBroker {
                    final int capturePreset = msg.arg1;
                    mDeviceInventory.onSaveClearPreferredDevicesForCapturePreset(capturePreset);
                } break;
                case MSG_PERSIST_AUDIO_DEVICE_SETTINGS:
                    onPersistAudioDeviceSettings();
                    break;
                default:
                    Log.wtf(TAG, "Invalid message " + msg.what);
            }
@@ -1503,8 +1496,6 @@ public class AudioDeviceBroker {
    private static final int MSG_IL_SET_PREF_DEVICES_FOR_STRATEGY = 40;
    private static final int MSG_I_REMOVE_PREF_DEVICES_FOR_STRATEGY = 41;

    private static final int MSG_PERSIST_AUDIO_DEVICE_SETTINGS = 54;

    private static boolean isMessageHandledUnderWakelock(int msgId) {
        switch(msgId) {
            case MSG_L_SET_WIRED_DEVICE_CONNECTION_STATE:
@@ -1851,65 +1842,4 @@ public class AudioDeviceBroker {
        }
        return null;
    }

    /**
     * post a message to persist the audio device settings.
     * Message is delayed by 1s on purpose in case of successive changes in quick succession (at
     * init time for instance)
     * Note this method is made public to work around a Mockito bug where it needs to be public
     * in order to be mocked by a test a the same package
     * (see https://code.google.com/archive/p/mockito/issues/127)
     */
    public void persistAudioDeviceSettings() {
        sendMsg(MSG_PERSIST_AUDIO_DEVICE_SETTINGS, SENDMSG_REPLACE, /*delay*/ 1000);
    }

    void onPersistAudioDeviceSettings() {
        final String deviceSettings = mDeviceInventory.getDeviceSettings();
        Log.v(TAG, "saving audio device settings: " + deviceSettings);
        boolean res = Settings.Secure.putStringForUser(
                mAudioService.getContentResolver(), Settings.Secure.AUDIO_DEVICE_INVENTORY,
                deviceSettings, UserHandle.USER_CURRENT);

        if (!res) {
            Log.e(TAG, "error saving audio device settings: " + deviceSettings);
        }
    }

    void onReadAudioDeviceSettings() {
        final ContentResolver contentResolver = mAudioService.getContentResolver();
        String settings = Settings.Secure.getStringForUser(contentResolver,
                Settings.Secure.AUDIO_DEVICE_INVENTORY, UserHandle.USER_CURRENT);
        if (settings != null && !settings.equals("")) {
            setDeviceSettings(settings);
        }
    }

    void setDeviceSettings(String settings) {
        mDeviceInventory.setDeviceSettings(settings);
    }

    /** Test only method. */
    String getDeviceSettings() {
        return mDeviceInventory.getDeviceSettings();
    }

    List<AdiDeviceState> getImmutableDeviceInventory() {
        return mDeviceInventory.getImmutableDeviceInventory();
    }

    void addDeviceStateToInventory(AdiDeviceState deviceState) {
        mDeviceInventory.addDeviceStateToInventory(deviceState);
    }

    AdiDeviceState findDeviceStateForAudioDeviceAttributes(AudioDeviceAttributes ada,
            int canonicalType) {
        return mDeviceInventory.findDeviceStateForAudioDeviceAttributes(ada, canonicalType);
    }

    //------------------------------------------------
    // for testing purposes only
    void clearDeviceInventory() {
        mDeviceInventory.clearDeviceInventory();
    }
}
Loading