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

Commit 82e4ed3b authored by Angela Wang's avatar Angela Wang
Browse files

Selects presets in device details page (1/2)

Enables users to select their presets in Bluetooth device details page if the device supports HAP.

This CL only contains the UI elements. The full functionality will be introduce in the next CL.

Bug: 300015207
Test: atest BluetoothDetailsHearingDeviceControllerTest
Test: atest BluetoothDetailsHearingAidsPresetsControllerTest
Change-Id: I1ab4781191b0c9e1033a29c30ca61671878bb7e1
parent 4af270b2
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -154,6 +154,8 @@
    <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>
    <!-- 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] -->
+204 −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.BluetoothDevice;
import android.bluetooth.BluetoothHapClient;
import android.bluetooth.BluetoothHapPresetInfo;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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())) {
            // TODO(b/300015207): Update the settings to remote device
            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());
        // TODO(b/300015207): Load preset from remote and show in UI
    }

    @Override
    public boolean isAvailable() {
        return false;
    }

    @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);
            }
            // TODO(b/300015207): Update the UI
        }
    }

    @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);
            }
            // TODO(b/300015207): Update the UI
        }
    }

    @Override
    public void onPresetSelectionForGroupFailed(int hapGroupId, int reason) {
        if (hapGroupId == mHapClientProfile.getHapGroup(mCachedDevice.getDevice())) {
            if (DEBUG) {
                Log.d(TAG, "onPresetSelectionForGroupFailed, group: " + hapGroupId
                        + ", reason: " + reason);
            }
            // TODO(b/300015207): Update the UI
        }
    }

    @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);
            }
            // TODO(b/300015207): Update the UI
        }
    }

    @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);
            }
            // TODO(b/300015207): Update the UI
        }
    }

    @Override
    public void onSetPresetNameForGroupFailed(int hapGroupId, int reason) {
        if (hapGroupId == mHapClientProfile.getHapGroup(mCachedDevice.getDevice())) {
            if (DEBUG) {
                Log.d(TAG, "onSetPresetNameForGroupFailed, group: " + hapGroupId
                        + ", reason: " + reason);
            }
            // TODO(b/300015207): Update the UI
        }
    }

    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;
    }
}
+15 −1
Original line number Diff line number Diff line
@@ -22,7 +22,9 @@ 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;
@@ -37,24 +39,32 @@ import java.util.List;
 * 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) {
            BluetoothDetailsHearingDeviceSettingsController hearingDeviceSettingsController,
            BluetoothDetailsHearingAidsPresetsController presetsController) {
        mControllers.clear();
        mControllers.add(hearingDeviceSettingsController);
        mControllers.add(presetsController);
    }

    @Override
@@ -93,6 +103,10 @@ public class BluetoothDetailsHearingDeviceController extends BluetoothDetailsCon
            mControllers.add(new BluetoothDetailsHearingDeviceSettingsController(mContext,
                    mFragment, mCachedDevice, mLifecycle));
        }
        if (Flags.enableHearingAidPresetControl()) {
            mControllers.add(new BluetoothDetailsHearingAidsPresetsController(mContext, mFragment,
                    mManager, mCachedDevice, mLifecycle));
        }
    }

    @NonNull
+2 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
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;
@@ -87,6 +88,7 @@ public class BluetoothDetailsHearingDeviceSettingsController extends BluetoothDe
    private Preference createHearingDeviceSettingsPreference(Context context) {
        final ArrowPreference preference = new ArrowPreference(context);
        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));
+2 −2
Original line number Diff line number Diff line
@@ -331,8 +331,8 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment
            controllers.add(new BluetoothDetailsExtraOptionsController(context, this, mCachedDevice,
                    lifecycle));
            BluetoothDetailsHearingDeviceController hearingDeviceController =
                    new BluetoothDetailsHearingDeviceController(context, this, mCachedDevice,
                            lifecycle);
                    new BluetoothDetailsHearingDeviceController(context, this, mManager,
                            mCachedDevice, lifecycle);
            controllers.add(hearingDeviceController);
            hearingDeviceController.initSubControllers(isLaunchFromHearingDevicePage());
            controllers.addAll(hearingDeviceController.getSubControllers());
Loading