Merge changes I13495cad,I3a44c4c4,I15bff230,I8a492866,Ia7ffe34a

* changes:
  [Pair hearing devices] Add pair hearing device functionality
  [Pair hearing devices] Extract common behavior in BluetoothPairingDetail into Base class
  [Pair hearing devices] Add "Hearing devices" to show connected hearing devices
  [Pair hearing devices] Add "Saved devices" to show bonded but not connected hearing devices
  [Audio routing] Setup basic structure for audio routing page
This commit is contained in:
Jason Hsu
2023-02-09 04:04:03 +00:00
committed by Android (Google) Code Review
43 changed files with 2079 additions and 470 deletions

View File

@@ -29,7 +29,6 @@ import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.FeatureFlagUtils;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.FragmentManager;
@@ -55,8 +54,6 @@ import com.android.settingslib.core.lifecycle.events.OnStop;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.stream.Collectors;
/**
@@ -84,7 +81,8 @@ public class AccessibilityHearingAidPreferenceController extends BasePreferenceC
public AccessibilityHearingAidPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mLocalBluetoothManager = getLocalBluetoothManager();
mLocalBluetoothManager = com.android.settings.bluetooth.Utils.getLocalBluetoothManager(
context);
mProfileManager = mLocalBluetoothManager.getProfileManager();
mCachedDeviceManager = mLocalBluetoothManager.getCachedDeviceManager();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
@@ -269,19 +267,6 @@ public class AccessibilityHearingAidPreferenceController extends BasePreferenceC
return false;
}
private LocalBluetoothManager getLocalBluetoothManager() {
final FutureTask<LocalBluetoothManager> localBtManagerFutureTask = new FutureTask<>(
// Avoid StrictMode ThreadPolicy violation
() -> com.android.settings.bluetooth.Utils.getLocalBtManager(mContext));
try {
localBtManagerFutureTask.run();
return localBtManagerFutureTask.get();
} catch (InterruptedException | ExecutionException e) {
Log.w(TAG, "Error getting LocalBluetoothManager.", e);
return null;
}
}
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
void setPreference(Preference preference) {
mHearingAidPreference = preference;

View File

@@ -19,6 +19,7 @@ package com.android.settings.accessibility;
import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
@@ -44,6 +45,13 @@ public class AccessibilityHearingAidsFragment extends AccessibilityShortcutPrefe
super(DISALLOW_CONFIG_BLUETOOTH);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
use(AvailableHearingDevicePreferenceController.class).init(this);
use(SavedHearingDevicePreferenceController.class).init(this);
}
@Override
public void onCreate(Bundle savedInstanceState) {
mFeatureName = getContext().getString(R.string.accessibility_hearingaid_title);

View File

@@ -0,0 +1,109 @@
/*
* 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.settings.accessibility;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import androidx.fragment.app.FragmentManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.bluetooth.BluetoothDeviceUpdater;
import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
/**
* Controller to update the {@link androidx.preference.PreferenceCategory} for all
* connected hearing devices, including ASHA and HAP profile.
* Parent class {@link BaseBluetoothDevicePreferenceController} will use
* {@link DevicePreferenceCallback} to add/remove {@link Preference}.
*/
public class AvailableHearingDevicePreferenceController extends
BaseBluetoothDevicePreferenceController implements LifecycleObserver, OnStart, OnStop,
BluetoothCallback {
private static final String TAG = "AvailableHearingDevicePreferenceController";
private BluetoothDeviceUpdater mAvailableHearingDeviceUpdater;
private final LocalBluetoothManager mLocalBluetoothManager;
private FragmentManager mFragmentManager;
public AvailableHearingDevicePreferenceController(Context context,
String preferenceKey) {
super(context, preferenceKey);
mLocalBluetoothManager = com.android.settings.bluetooth.Utils.getLocalBluetoothManager(
context);
}
/**
* Initializes objects in this controller. Need to call this before onStart().
*
* <p>Should not call this more than 1 time.
*
* @param fragment The {@link DashboardFragment} uses the controller.
*/
public void init(DashboardFragment fragment) {
if (mAvailableHearingDeviceUpdater != null) {
throw new IllegalStateException("Should not call init() more than 1 time.");
}
mAvailableHearingDeviceUpdater = new AvailableHearingDeviceUpdater(fragment.getContext(),
this, fragment.getMetricsCategory());
mFragmentManager = fragment.getParentFragmentManager();
}
@Override
public void onStart() {
mAvailableHearingDeviceUpdater.registerCallback();
mAvailableHearingDeviceUpdater.refreshPreference();
mLocalBluetoothManager.getEventManager().registerCallback(this);
}
@Override
public void onStop() {
mAvailableHearingDeviceUpdater.unregisterCallback();
mLocalBluetoothManager.getEventManager().unregisterCallback(this);
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
if (isAvailable()) {
final Context context = screen.getContext();
mAvailableHearingDeviceUpdater.setPrefContext(context);
mAvailableHearingDeviceUpdater.forceUpdate();
}
}
@Override
public void onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile) {
if (activeDevice == null) {
return;
}
if (bluetoothProfile == BluetoothProfile.HEARING_AID) {
HearingAidUtils.launchHearingAidPairingDialog(mFragmentManager, activeDevice);
}
}
}

View File

@@ -0,0 +1,51 @@
/*
* 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.settings.accessibility;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import com.android.settings.bluetooth.AvailableMediaBluetoothDeviceUpdater;
import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
/**
* Maintains and updates connected hearing devices, including ASHA and HAP profile.
*/
public class AvailableHearingDeviceUpdater extends AvailableMediaBluetoothDeviceUpdater {
private static final String PREF_KEY = "connected_hearing_device";
public AvailableHearingDeviceUpdater(Context context,
DevicePreferenceCallback devicePreferenceCallback, int metricsCategory) {
super(context, devicePreferenceCallback, metricsCategory);
}
@Override
public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
final BluetoothDevice device = cachedDevice.getDevice();
final boolean isConnectedHearingAidDevice = (cachedDevice.isConnectedHearingAidDevice()
&& (device.getBondState() == BluetoothDevice.BOND_BONDED));
return isConnectedHearingAidDevice && isDeviceInCachedDevicesList(cachedDevice);
}
@Override
protected String getPreferenceKey() {
return PREF_KEY;
}
}

View File

@@ -0,0 +1,73 @@
/*
* 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.settings.accessibility;
import android.content.Context;
import android.content.pm.PackageManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settings.core.BasePreferenceController;
/**
* Abstract base class for bluetooth preference controller to handle UI logic, e.g. availability
* status, preference added, and preference removed.
*/
public abstract class BaseBluetoothDevicePreferenceController extends BasePreferenceController
implements DevicePreferenceCallback {
private PreferenceCategory mPreferenceCategory;
public BaseBluetoothDevicePreferenceController(Context context,
String preferenceKey) {
super(context, preferenceKey);
}
@Override
public int getAvailabilityStatus() {
return (mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH))
? AVAILABLE
: CONDITIONALLY_UNAVAILABLE;
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreferenceCategory = screen.findPreference(getPreferenceKey());
mPreferenceCategory.setVisible(false);
}
@Override
public void onDeviceAdded(Preference preference) {
if (mPreferenceCategory.getPreferenceCount() == 0) {
mPreferenceCategory.setVisible(true);
}
mPreferenceCategory.addPreference(preference);
}
@Override
public void onDeviceRemoved(Preference preference) {
mPreferenceCategory.removePreference(preference);
if (mPreferenceCategory.getPreferenceCount() == 0) {
mPreferenceCategory.setVisible(false);
}
}
}

View File

@@ -0,0 +1,82 @@
/*
* 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.settings.accessibility;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothUuid;
import android.bluetooth.le.ScanFilter;
import androidx.annotation.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.bluetooth.BluetoothDevicePairingDetailBase;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import java.util.Collections;
/**
* HearingDevicePairingDetail is a page to scan hearing devices. This page shows scanning icons and
* pairing them.
*/
public class HearingDevicePairingDetail extends BluetoothDevicePairingDetailBase {
private static final String TAG = "HearingDevicePairingDetail";
@VisibleForTesting
static final String KEY_AVAILABLE_HEARING_DEVICES = "available_hearing_devices";
public HearingDevicePairingDetail() {
super();
final ScanFilter filter = new ScanFilter.Builder()
.setServiceData(BluetoothUuid.HEARING_AID, new byte[]{0}, new byte[]{0})
.build();
setFilter(Collections.singletonList(filter));
}
@Override
public void onStart() {
super.onStart();
mAvailableDevicesCategory.setProgress(mBluetoothAdapter.isEnabled());
}
@Override
public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
super.onDeviceBondStateChanged(cachedDevice, bondState);
mAvailableDevicesCategory.setProgress(bondState == BluetoothDevice.BOND_NONE);
}
@Override
public int getMetricsCategory() {
// TODO(b/262839191): To be updated settings_enums.proto
return 0;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.hearing_device_pairing_detail;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
public String getDeviceListKey() {
return KEY_AVAILABLE_HEARING_DEVICES;
}
}

View File

@@ -0,0 +1,89 @@
/*
* 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.settings.accessibility;
import android.content.Context;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.bluetooth.BluetoothDeviceUpdater;
import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnResume;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
/**
* Controller to update the {@link androidx.preference.PreferenceCategory} for all
* saved ((bonded but not connected)) hearing devices, including ASHA and HAP profile.
* Parent class {@link BaseBluetoothDevicePreferenceController} will use
* {@link DevicePreferenceCallback} to add/remove {@link Preference}.
*/
public class SavedHearingDevicePreferenceController extends
BaseBluetoothDevicePreferenceController implements LifecycleObserver, OnStart, OnResume,
OnStop {
private BluetoothDeviceUpdater mSavedHearingDeviceUpdater;
public SavedHearingDevicePreferenceController(Context context,
String preferenceKey) {
super(context, preferenceKey);
}
/**
* Initializes objects in this controller. Need to call this before onStart().
*
* <p>Should not call this more than 1 time.
*
* @param fragment The {@link DashboardFragment} uses the controller.
*/
public void init(DashboardFragment fragment) {
if (mSavedHearingDeviceUpdater != null) {
throw new IllegalStateException("Should not call init() more than 1 time.");
}
mSavedHearingDeviceUpdater = new SavedHearingDeviceUpdater(fragment.getContext(), this,
fragment.getMetricsCategory());
}
@Override
public void onStart() {
mSavedHearingDeviceUpdater.registerCallback();
}
@Override
public void onResume() {
mSavedHearingDeviceUpdater.refreshPreference();
}
@Override
public void onStop() {
mSavedHearingDeviceUpdater.unregisterCallback();
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
if (isAvailable()) {
final Context context = screen.getContext();
mSavedHearingDeviceUpdater.setPrefContext(context);
mSavedHearingDeviceUpdater.forceUpdate();
}
}
}

View File

@@ -0,0 +1,53 @@
/*
* 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.settings.accessibility;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import com.android.settings.bluetooth.SavedBluetoothDeviceUpdater;
import com.android.settings.connecteddevice.DevicePreferenceCallback;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
/**
* Maintains and updates saved (bonded but not connected) hearing devices, including ASHA and HAP
* profile.
*/
public class SavedHearingDeviceUpdater extends SavedBluetoothDeviceUpdater {
private static final String PREF_KEY = "saved_hearing_device";
public SavedHearingDeviceUpdater(Context context,
DevicePreferenceCallback devicePreferenceCallback, int metricsCategory) {
super(context, devicePreferenceCallback, /* showConnectedDevice= */ false, metricsCategory);
}
@Override
public boolean isFilterMatched(CachedBluetoothDevice cachedDevice) {
final BluetoothDevice device = cachedDevice.getDevice();
final boolean isSavedHearingAidDevice = cachedDevice.isHearingAidDevice()
&& device.getBondState() == BluetoothDevice.BOND_BONDED
&& !device.isConnected();
return isSavedHearingAidDevice && isDeviceInCachedDevicesList(cachedDevice);
}
@Override
protected String getPreferenceKey() {
return PREF_KEY;
}
}