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

Commit c9563a59 authored by Angela Wang's avatar Angela Wang Committed by Android (Google) Code Review
Browse files

Merge changes from topic "ha-preset" into main

* changes:
  Selects presets in device details page (2/2)
  Selects presets in device details page (1/2)
  Separate category controller out of HearingDeviceControlsController
parents c8d5701a b054b05b
Loading
Loading
Loading
Loading
+8 −8
Original line number Diff line number Diff line
@@ -150,14 +150,14 @@
    <string name="bluetooth_pair_other_ear_dialog_right_ear_positive_button">Pair right ear</string>
    <!-- Connected devices settings. Positive button of the dialog to help user to pair left ear of the hearing aid device. Dialog shows when only one of the hearing aid device set is connected. [CHAR LIMIT=20] -->
    <string name="bluetooth_pair_other_ear_dialog_left_ear_positive_button">Pair left ear</string>
    <!-- Title for all hearing devices related controls section. [CHAR LIMIT=60] -->
    <string name="bluetooth_device_controls_general">For all available hearing devices</string>
    <!-- Connected devices settings. Title of the preference to show the entrance of the hearing device settings related page. [CHAR LIMIT=65] -->
    <string name="bluetooth_device_controls_title">More hearing device settings</string>
    <!-- Connected devices settings. Summary of the preference to show the item in the hearing device settings related page. [CHAR LIMIT=120] -->
    <string name="bluetooth_device_controls_summary">Change cross-device settings like shortcut, and telecoil controls</string>
    <!-- Title for this device specific controls section. [CHAR LIMIT=30] -->
    <string name="bluetooth_device_controls_specific">For this device</string>
    <!-- Connected devices settings. Title of the preference to show the entrance of the hearing device settings page. [CHAR LIMIT=65 BACKUP_MESSAGE_ID=1895676556354697234] -->
    <string name="bluetooth_hearing_device_settings_title">Hearing device settings</string>
    <!-- Connected devices settings. Summary of the preference to show the entrance of the hearing device settings page. [CHAR LIMIT=65 BACKUP_MESSAGE_ID=8115767735418425663] -->
    <string name="bluetooth_hearing_device_settings_summary">Shortcut, hearing aid compatibility</string>
    <!-- Connected devices settings. Title for hearing aids presets. A preset is a set of hearing aid settings. User can apply different settings in different environments (e.g. Outdoor, Restaurant, Home) [CHAR LIMIT=60] -->
    <string name="bluetooth_hearing_aids_presets">Presets</string>
    <!-- Message when selecting hearing aids presets failed. [CHAR LIMIT=NONE] -->
    <string name="bluetooth_hearing_aids_presets_error">Couldn\u2019t update preset</string>
    <!-- Connected devices settings. Title of the preference to show the entrance of the audio output page. It can change different types of audio are played on phone or other bluetooth devices. [CHAR LIMIT=35] -->
    <string name="bluetooth_audio_routing_title">Audio output</string>
    <!-- Title for bluetooth audio routing page footer. [CHAR LIMIT=30] -->
+1 −1
Original line number Diff line number Diff line
@@ -69,7 +69,7 @@
        android:key="device_companion_apps"/>

    <PreferenceCategory
        android:key="device_controls_general" />
        android:key="hearing_device_group" />

    <PreferenceCategory
        android:key="bluetooth_audio_device_type_group"/>
+308 −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.settings.bluetooth;

import static com.android.settings.bluetooth.BluetoothDetailsHearingDeviceController.KEY_HEARING_DEVICE_GROUP;
import static com.android.settings.bluetooth.BluetoothDetailsHearingDeviceController.ORDER_HEARING_AIDS_PRESETS;

import android.bluetooth.BluetoothCsipSetCoordinator;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothHapClient;
import android.bluetooth.BluetoothHapPresetInfo;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.HapClientProfile;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume;
import com.android.settingslib.utils.ThreadUtils;

import java.util.List;

/**
 * The controller of the hearing aid presets.
 */
public class BluetoothDetailsHearingAidsPresetsController extends
        BluetoothDetailsController implements Preference.OnPreferenceChangeListener,
        BluetoothHapClient.Callback, OnResume, OnPause {

    private static final boolean DEBUG = true;
    private static final String TAG = "BluetoothDetailsHearingAidsPresetsController";
    static final String KEY_HEARING_AIDS_PRESETS = "hearing_aids_presets";

    private final HapClientProfile mHapClientProfile;
    @Nullable
    private ListPreference mPreference;

    public BluetoothDetailsHearingAidsPresetsController(@NonNull Context context,
            @NonNull PreferenceFragmentCompat fragment,
            @NonNull LocalBluetoothManager manager,
            @NonNull CachedBluetoothDevice device,
            @NonNull Lifecycle lifecycle) {
        super(context, fragment, device, lifecycle);
        mHapClientProfile = manager.getProfileManager().getHapClientProfile();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mHapClientProfile != null) {
            mHapClientProfile.registerCallback(ThreadUtils.getBackgroundExecutor(), this);
        }
    }

    @Override
    public void onPause() {
        if (mHapClientProfile != null) {
            mHapClientProfile.unregisterCallback(this);
        }
        super.onPause();
    }

    @Override
    public boolean onPreferenceChange(@NonNull Preference preference, @Nullable Object newValue) {
        if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
            if (newValue instanceof final String value
                    && preference instanceof final ListPreference listPreference) {
                final int index = listPreference.findIndexOfValue(value);
                final String presetName = listPreference.getEntries()[index].toString();
                final int presetIndex = Integer.parseInt(
                        listPreference.getEntryValues()[index].toString());
                listPreference.setSummary(presetName);
                boolean supportSynchronizedPresets = mHapClientProfile.supportsSynchronizedPresets(
                        mCachedDevice.getDevice());
                int hapGroupId = mHapClientProfile.getHapGroup(mCachedDevice.getDevice());
                if (supportSynchronizedPresets
                        && hapGroupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
                    if (DEBUG) {
                        Log.d(TAG, "onPreferenceChange, selectPresetForGroup "
                                + ", presetName: " + presetName
                                + ", presetIndex: " + presetIndex
                                + ", hapGroupId: "  + hapGroupId
                                + ", device: " + mCachedDevice.getAddress());
                    }
                    mHapClientProfile.selectPresetForGroup(hapGroupId, presetIndex);
                } else {
                    if (DEBUG) {
                        Log.d(TAG, "onPreferenceChange, selectPreset "
                                + ", presetName: " + presetName
                                + ", presetIndex: " + presetIndex
                                + ", device: " + mCachedDevice.getAddress());
                    }
                    mHapClientProfile.selectPreset(mCachedDevice.getDevice(), presetIndex);
                    final CachedBluetoothDevice subDevice = mCachedDevice.getSubDevice();
                    if (subDevice != null) {
                        if (DEBUG) {
                            Log.d(TAG, "onPreferenceChange, selectPreset for subDevice"
                                    + ", device: " + subDevice.getAddress());
                        }
                        mHapClientProfile.selectPreset(subDevice.getDevice(), presetIndex);
                    }
                    for (final CachedBluetoothDevice memberDevice :
                            mCachedDevice.getMemberDevice()) {
                        if (DEBUG) {
                            Log.d(TAG, "onPreferenceChange, selectPreset for memberDevice"
                                    + ", device: " + memberDevice.getAddress());
                        }
                        mHapClientProfile.selectPreset(memberDevice.getDevice(), presetIndex);
                    }
                }
                return true;
            }
        }
        return false;
    }

    @Nullable
    @Override
    public String getPreferenceKey() {
        return KEY_HEARING_AIDS_PRESETS;
    }

    @Override
    protected void init(PreferenceScreen screen) {
        PreferenceCategory deviceControls = screen.findPreference(KEY_HEARING_DEVICE_GROUP);
        if (deviceControls != null) {
            mPreference = createPresetPreference(deviceControls.getContext());
            deviceControls.addPreference(mPreference);
        }
    }

    @Override
    protected void refresh() {
        if (!isAvailable() || mPreference == null) {
            return;
        }
        mPreference.setEnabled(mCachedDevice.isConnectedHapClientDevice());

        loadAllPresetInfo();
        if (mPreference.getEntries().length == 0) {
            mPreference.setEnabled(false);
        } else {
            int activePresetIndex = mHapClientProfile.getActivePresetIndex(
                    mCachedDevice.getDevice());
            if (activePresetIndex != BluetoothHapClient.PRESET_INDEX_UNAVAILABLE) {
                mPreference.setValue(Integer.toString(activePresetIndex));
                mPreference.setSummary(mPreference.getEntry());
            } else {
                mPreference.setSummary(null);
            }
        }
    }

    @Override
    public boolean isAvailable() {
        if (mHapClientProfile == null) {
            return false;
        }
        return mCachedDevice.getProfiles().stream().anyMatch(
                profile -> profile instanceof HapClientProfile);
    }

    @Override
    public void onPresetSelected(@NonNull BluetoothDevice device, int presetIndex, int reason) {
        if (device.equals(mCachedDevice.getDevice())) {
            if (DEBUG) {
                Log.d(TAG, "onPresetSelected, device: " + device.getAddress()
                        + ", presetIndex: " + presetIndex + ", reason: " + reason);
            }
            mContext.getMainExecutor().execute(this::refresh);
        }
    }

    @Override
    public void onPresetSelectionFailed(@NonNull BluetoothDevice device, int reason) {
        if (device.equals(mCachedDevice.getDevice())) {
            if (DEBUG) {
                Log.d(TAG,
                        "onPresetSelectionFailed, device: " + device.getAddress()
                                + ", reason: " + reason);
            }
            mContext.getMainExecutor().execute(() -> {
                refresh();
                showErrorToast();
            });
        }
    }

    @Override
    public void onPresetSelectionForGroupFailed(int hapGroupId, int reason) {
        if (hapGroupId == mHapClientProfile.getHapGroup(mCachedDevice.getDevice())) {
            if (DEBUG) {
                Log.d(TAG, "onPresetSelectionForGroupFailed, group: " + hapGroupId
                        + ", reason: " + reason);
            }
            mContext.getMainExecutor().execute(() -> {
                refresh();
                showErrorToast();
            });
        }
    }

    @Override
    public void onPresetInfoChanged(@NonNull BluetoothDevice device,
            @NonNull List<BluetoothHapPresetInfo> presetInfoList, int reason) {
        if (device.equals(mCachedDevice.getDevice())) {
            if (DEBUG) {
                Log.d(TAG, "onPresetInfoChanged, device: " + device.getAddress()
                        + ", reason: " + reason
                        + ", infoList: " + presetInfoList);
            }
            mContext.getMainExecutor().execute(this::refresh);
        }
    }

    @Override
    public void onSetPresetNameFailed(@NonNull BluetoothDevice device, int reason) {
        if (device.equals(mCachedDevice.getDevice())) {
            if (DEBUG) {
                Log.d(TAG,
                        "onSetPresetNameFailed, device: " + device.getAddress()
                                + ", reason: " + reason);
            }
            mContext.getMainExecutor().execute(() -> {
                refresh();
                showErrorToast();
            });
        }
    }

    @Override
    public void onSetPresetNameForGroupFailed(int hapGroupId, int reason) {
        if (hapGroupId == mHapClientProfile.getHapGroup(mCachedDevice.getDevice())) {
            if (DEBUG) {
                Log.d(TAG, "onSetPresetNameForGroupFailed, group: " + hapGroupId
                        + ", reason: " + reason);
            }
            mContext.getMainExecutor().execute(() -> {
                refresh();
                showErrorToast();
            });
        }
    }

    private ListPreference createPresetPreference(Context context) {
        ListPreference preference = new ListPreference(context);
        preference.setKey(KEY_HEARING_AIDS_PRESETS);
        preference.setOrder(ORDER_HEARING_AIDS_PRESETS);
        preference.setTitle(context.getString(R.string.bluetooth_hearing_aids_presets));
        preference.setOnPreferenceChangeListener(this);
        return preference;
    }

    private void loadAllPresetInfo() {
        if (mPreference == null) {
            return;
        }
        List<BluetoothHapPresetInfo> infoList = mHapClientProfile.getAllPresetInfo(
                mCachedDevice.getDevice());
        CharSequence[] presetNames = new CharSequence[infoList.size()];
        CharSequence[] presetIndexes = new CharSequence[infoList.size()];
        for (int i = 0; i < infoList.size(); i++) {
            presetNames[i] = infoList.get(i).getName();
            presetIndexes[i] = Integer.toString(infoList.get(i).getIndex());
        }
        mPreference.setEntries(presetNames);
        mPreference.setEntryValues(presetIndexes);
    }

    @VisibleForTesting
    @Nullable
    ListPreference getPreference() {
        return mPreference;
    }

    void showErrorToast() {
        Toast.makeText(mContext, R.string.bluetooth_hearing_aids_presets_error,
                Toast.LENGTH_SHORT).show();
    }
}
+116 −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.settings.bluetooth;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;

import com.android.settings.accessibility.Flags;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.lifecycle.Lifecycle;

import com.google.common.annotations.VisibleForTesting;

import java.util.ArrayList;
import java.util.List;

/**
 * The controller of the hearing device controls.
 *
 * <p><b>Note:</b> It is responsible for creating the sub-controllers inside this preference
 * category controller.
 */
public class BluetoothDetailsHearingDeviceController extends BluetoothDetailsController {

    public static final int ORDER_HEARING_DEVICE_SETTINGS = 1;
    public static final int ORDER_HEARING_AIDS_PRESETS = 2;
    static final String KEY_HEARING_DEVICE_GROUP = "hearing_device_group";

    private final List<BluetoothDetailsController> mControllers = new ArrayList<>();
    private Lifecycle mLifecycle;
    private LocalBluetoothManager mManager;

    public BluetoothDetailsHearingDeviceController(@NonNull Context context,
            @NonNull PreferenceFragmentCompat fragment,
            @NonNull LocalBluetoothManager manager,
            @NonNull CachedBluetoothDevice device,
            @NonNull Lifecycle lifecycle) {
        super(context, fragment, device, lifecycle);
        mManager = manager;
        mLifecycle = lifecycle;
    }

    @VisibleForTesting
    void setSubControllers(
            BluetoothDetailsHearingDeviceSettingsController hearingDeviceSettingsController,
            BluetoothDetailsHearingAidsPresetsController presetsController) {
        mControllers.clear();
        mControllers.add(hearingDeviceSettingsController);
        mControllers.add(presetsController);
    }

    @Override
    public boolean isAvailable() {
        return mControllers.stream().anyMatch(BluetoothDetailsController::isAvailable);
    }

    @Override
    @NonNull
    public String getPreferenceKey() {
        return KEY_HEARING_DEVICE_GROUP;
    }

    @Override
    protected void init(PreferenceScreen screen) {

    }

    @Override
    protected void refresh() {

    }

    /**
     * Initiates the sub controllers controlled by this group controller.
     *
     * <p><b>Note:</b> The caller must call this method when creating this class.
     *
     * @param isLaunchFromHearingDevicePage a boolean that determines if the caller is launch from
     *                                      hearing device page
     */
    void initSubControllers(boolean isLaunchFromHearingDevicePage) {
        mControllers.clear();
        // Don't need to show the entrance to hearing device page when launched from the same page
        if (!isLaunchFromHearingDevicePage) {
            mControllers.add(new BluetoothDetailsHearingDeviceSettingsController(mContext,
                    mFragment, mCachedDevice, mLifecycle));
        }
        if (Flags.enableHearingAidPresetControl()) {
            mControllers.add(new BluetoothDetailsHearingAidsPresetsController(mContext, mFragment,
                    mManager, mCachedDevice, mLifecycle));
        }
    }

    @NonNull
    public List<BluetoothDetailsController> getSubControllers() {
        return mControllers;
    }
}
+21 −17
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

package com.android.settings.bluetooth;

import static com.android.settings.bluetooth.BluetoothDetailsHearingDeviceController.KEY_HEARING_DEVICE_GROUP;
import static com.android.settings.bluetooth.BluetoothDetailsHearingDeviceController.ORDER_HEARING_DEVICE_SETTINGS;

import android.content.Context;
import android.text.TextUtils;

@@ -36,15 +39,13 @@ import com.google.common.annotations.VisibleForTesting;
/**
 * The controller of the hearing device settings to launch Hearing device page.
 */
public class BluetoothDetailsHearingDeviceControlsController extends BluetoothDetailsController
public class BluetoothDetailsHearingDeviceSettingsController extends BluetoothDetailsController
        implements Preference.OnPreferenceClickListener {

    @VisibleForTesting
    static final String KEY_DEVICE_CONTROLS_GENERAL_GROUP = "device_controls_general";
    @VisibleForTesting
    static final String KEY_HEARING_DEVICE_CONTROLS = "hearing_device_controls";
    static final String KEY_HEARING_DEVICE_SETTINGS = "hearing_device_settings";

    public BluetoothDetailsHearingDeviceControlsController(Context context,
    public BluetoothDetailsHearingDeviceSettingsController(Context context,
            PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle) {
        super(context, fragment, device, lifecycle);
        lifecycle.addObserver(this);
@@ -57,37 +58,40 @@ public class BluetoothDetailsHearingDeviceControlsController extends BluetoothDe

    @Override
    protected void init(PreferenceScreen screen) {
        if (!mCachedDevice.isHearingAidDevice()) {
        if (!isAvailable()) {
            return;
        }

        final PreferenceCategory prefCategory = screen.findPreference(getPreferenceKey());
        final Preference pref = createHearingDeviceControlsPreference(prefCategory.getContext());
        prefCategory.addPreference(pref);
        final PreferenceCategory group = screen.findPreference(KEY_HEARING_DEVICE_GROUP);
        final Preference pref = createHearingDeviceSettingsPreference(group.getContext());
        group.addPreference(pref);
    }

    @Override
    protected void refresh() {}
    protected void refresh() {

    }

    @Override
    public String getPreferenceKey() {
        return KEY_DEVICE_CONTROLS_GENERAL_GROUP;
        return KEY_HEARING_DEVICE_SETTINGS;
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        if (TextUtils.equals(preference.getKey(), KEY_HEARING_DEVICE_CONTROLS)) {
        if (TextUtils.equals(preference.getKey(), KEY_HEARING_DEVICE_SETTINGS)) {
            launchAccessibilityHearingDeviceSettings();
            return true;
        }
        return false;
    }

    private Preference createHearingDeviceControlsPreference(Context context) {
    private Preference createHearingDeviceSettingsPreference(Context context) {
        final ArrowPreference preference = new ArrowPreference(context);
        preference.setKey(KEY_HEARING_DEVICE_CONTROLS);
        preference.setTitle(context.getString(R.string.bluetooth_device_controls_title));
        preference.setSummary(context.getString(R.string.bluetooth_device_controls_summary));
        preference.setKey(KEY_HEARING_DEVICE_SETTINGS);
        preference.setOrder(ORDER_HEARING_DEVICE_SETTINGS);
        preference.setTitle(context.getString(R.string.bluetooth_hearing_device_settings_title));
        preference.setSummary(
                context.getString(R.string.bluetooth_hearing_device_settings_summary));
        preference.setOnPreferenceClickListener(this);

        return preference;
Loading