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:
@@ -23,7 +23,6 @@ import android.util.Log;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.connecteddevice.DevicePreferenceCallback;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
|
||||
/**
|
||||
@@ -39,9 +38,9 @@ public class AvailableMediaBluetoothDeviceUpdater extends BluetoothDeviceUpdater
|
||||
|
||||
private final AudioManager mAudioManager;
|
||||
|
||||
public AvailableMediaBluetoothDeviceUpdater(Context context, DashboardFragment fragment,
|
||||
DevicePreferenceCallback devicePreferenceCallback) {
|
||||
super(context, fragment, devicePreferenceCallback);
|
||||
public AvailableMediaBluetoothDeviceUpdater(Context context,
|
||||
DevicePreferenceCallback devicePreferenceCallback, int metricsCategory) {
|
||||
super(context, devicePreferenceCallback, metricsCategory);
|
||||
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
||||
}
|
||||
|
||||
@@ -102,7 +101,7 @@ public class AvailableMediaBluetoothDeviceUpdater extends BluetoothDeviceUpdater
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
mMetricsFeatureProvider.logClickedPreference(preference, mFragment.getMetricsCategory());
|
||||
mMetricsFeatureProvider.logClickedPreference(preference, mMetricsCategory);
|
||||
final CachedBluetoothDevice device = ((BluetoothDevicePreference) preference)
|
||||
.getBluetoothDevice();
|
||||
return device.setActive();
|
||||
|
@@ -17,10 +17,13 @@
|
||||
package com.android.settings.bluetooth;
|
||||
|
||||
import static com.android.settings.bluetooth.BluetoothDeviceDetailsFragment.FEATURE_AUDIO_ROUTING_ORDER;
|
||||
import static com.android.settings.bluetooth.BluetoothDeviceDetailsFragment.KEY_DEVICE_ADDRESS;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.util.FeatureFlagUtils;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
@@ -36,7 +39,8 @@ import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
public class BluetoothDetailsAudioRoutingController extends BluetoothDetailsController {
|
||||
|
||||
private static final String KEY_FEATURE_CONTROLS_GROUP = "feature_controls_group";
|
||||
private static final String KEY_AUDIO_ROUTING = "audio_routing";
|
||||
@VisibleForTesting
|
||||
static final String KEY_AUDIO_ROUTING = "audio_routing";
|
||||
|
||||
public BluetoothDetailsAudioRoutingController(Context context,
|
||||
PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle) {
|
||||
@@ -71,9 +75,13 @@ public class BluetoothDetailsAudioRoutingController extends BluetoothDetailsCont
|
||||
|
||||
private Preference createAudioRoutingPreference(Context context) {
|
||||
final Preference preference = new Preference(context);
|
||||
|
||||
preference.setKey(KEY_AUDIO_ROUTING);
|
||||
preference.setTitle(context.getString(R.string.bluetooth_audio_routing_title));
|
||||
preference.setSummary(context.getString(R.string.bluetooth_audio_routing_summary));
|
||||
final Bundle extras = preference.getExtras();
|
||||
extras.putString(KEY_DEVICE_ADDRESS, mCachedDevice.getAddress());
|
||||
preference.setFragment(BluetoothDetailsAudioRoutingFragment.class.getName());
|
||||
|
||||
return preference;
|
||||
}
|
||||
|
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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.bluetooth;
|
||||
|
||||
import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
|
||||
|
||||
import static com.android.settings.bluetooth.BluetoothDeviceDetailsFragment.KEY_DEVICE_ADDRESS;
|
||||
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.dashboard.RestrictedDashboardFragment;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
import com.android.settingslib.search.SearchIndexable;
|
||||
|
||||
/** Settings fragment containing bluetooth audio routing. */
|
||||
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
|
||||
public class BluetoothDetailsAudioRoutingFragment extends RestrictedDashboardFragment {
|
||||
|
||||
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||
new BaseSearchIndexProvider(R.xml.bluetooth_audio_routing_fragment);
|
||||
private static final String TAG = "BluetoothDetailsAudioRoutingFragment";
|
||||
@VisibleForTesting
|
||||
CachedBluetoothDevice mCachedDevice;
|
||||
|
||||
public BluetoothDetailsAudioRoutingFragment() {
|
||||
super(DISALLOW_CONFIG_BLUETOOTH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
final LocalBluetoothManager localBtMgr = Utils.getLocalBtManager(context);
|
||||
final CachedBluetoothDeviceManager cachedDeviceMgr = localBtMgr.getCachedDeviceManager();
|
||||
final BluetoothDevice bluetoothDevice = localBtMgr.getBluetoothAdapter().getRemoteDevice(
|
||||
getArguments().getString(KEY_DEVICE_ADDRESS));
|
||||
|
||||
mCachedDevice = cachedDeviceMgr.findDevice(bluetoothDevice);
|
||||
if (mCachedDevice == null) {
|
||||
// Close this page if device is null with invalid device mac address
|
||||
Log.w(TAG, "onAttach() CachedDevice is null! Can not find address: "
|
||||
+ bluetoothDevice.getAnonymizedAddress());
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: mCachedDevice will pass to control in next CLs.
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
// TODO(b/262839191): To be updated settings_enums.proto
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getPreferenceScreenResId() {
|
||||
return R.xml.bluetooth_audio_routing_fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getLogTag() {
|
||||
return TAG;
|
||||
}
|
||||
}
|
@@ -0,0 +1,200 @@
|
||||
/*
|
||||
* 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.bluetooth;
|
||||
|
||||
import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.accessibility.AccessibilityStatsLogUtils;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.HearingAidStatsLogUtils;
|
||||
|
||||
/**
|
||||
* Abstract class for providing basic interaction for a list of Bluetooth devices in bluetooth
|
||||
* device pairing detail page.
|
||||
*/
|
||||
public abstract class BluetoothDevicePairingDetailBase extends DeviceListPreferenceFragment {
|
||||
|
||||
protected boolean mInitialScanStarted;
|
||||
@VisibleForTesting
|
||||
protected BluetoothProgressCategory mAvailableDevicesCategory;
|
||||
|
||||
public BluetoothDevicePairingDetailBase() {
|
||||
super(DISALLOW_CONFIG_BLUETOOTH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initPreferencesFromPreferenceScreen() {
|
||||
mAvailableDevicesCategory = findPreference(getDeviceListKey());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
mInitialScanStarted = false;
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
if (mLocalManager == null) {
|
||||
Log.e(getLogTag(), "Bluetooth is not supported on this device");
|
||||
return;
|
||||
}
|
||||
updateBluetooth();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
if (mLocalManager == null) {
|
||||
Log.e(getLogTag(), "Bluetooth is not supported on this device");
|
||||
return;
|
||||
}
|
||||
disableScanning();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBluetoothStateChanged(int bluetoothState) {
|
||||
super.onBluetoothStateChanged(bluetoothState);
|
||||
updateContent(bluetoothState);
|
||||
if (bluetoothState == BluetoothAdapter.STATE_ON) {
|
||||
showBluetoothTurnedOnToast();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
|
||||
if (bondState == BluetoothDevice.BOND_BONDED) {
|
||||
// If one device is connected(bonded), then close this fragment.
|
||||
finish();
|
||||
return;
|
||||
} else if (bondState == BluetoothDevice.BOND_BONDING) {
|
||||
// Set the bond entry where binding process starts for logging hearing aid device info
|
||||
final int pageId = FeatureFactory.getFactory(
|
||||
getContext()).getMetricsFeatureProvider().getAttribution(getActivity());
|
||||
final int bondEntry = AccessibilityStatsLogUtils.convertToHearingAidInfoBondEntry(
|
||||
pageId);
|
||||
HearingAidStatsLogUtils.setBondEntryForDevice(bondEntry, cachedDevice);
|
||||
}
|
||||
if (mSelectedDevice != null && cachedDevice != null) {
|
||||
BluetoothDevice device = cachedDevice.getDevice();
|
||||
if (device != null && mSelectedDevice.equals(device)
|
||||
&& bondState == BluetoothDevice.BOND_NONE) {
|
||||
// If currently selected device failed to bond, restart scanning
|
||||
enableScanning();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProfileConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state,
|
||||
int bluetoothProfile) {
|
||||
// This callback is used to handle the case that bonded device is connected in pairing list.
|
||||
// 1. If user selected multiple bonded devices in pairing list, after connected
|
||||
// finish this page.
|
||||
// 2. If the bonded devices auto connected in paring list, after connected it will be
|
||||
// removed from paring list.
|
||||
if (cachedDevice != null && cachedDevice.isConnected()) {
|
||||
final BluetoothDevice device = cachedDevice.getDevice();
|
||||
if (device != null && mSelectedList.contains(device)) {
|
||||
finish();
|
||||
} else if (mDevicePreferenceMap.containsKey(cachedDevice)) {
|
||||
onDeviceDeleted(cachedDevice);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enableScanning() {
|
||||
// Clear all device states before first scan
|
||||
if (!mInitialScanStarted) {
|
||||
if (mAvailableDevicesCategory != null) {
|
||||
removeAllDevices();
|
||||
}
|
||||
mLocalManager.getCachedDeviceManager().clearNonBondedDevices();
|
||||
mInitialScanStarted = true;
|
||||
}
|
||||
super.enableScanning();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
|
||||
disableScanning();
|
||||
super.onDevicePreferenceClick(btPreference);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void updateBluetooth() {
|
||||
if (mBluetoothAdapter.isEnabled()) {
|
||||
updateContent(mBluetoothAdapter.getState());
|
||||
} else {
|
||||
// Turn on bluetooth if it is disabled
|
||||
mBluetoothAdapter.enable();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables the scanning when {@code bluetoothState} is on, or finish the page when
|
||||
* {@code bluetoothState} is off.
|
||||
*
|
||||
* @param bluetoothState the current Bluetooth state, the possible values that will handle here:
|
||||
* {@link android.bluetooth.BluetoothAdapter#STATE_OFF},
|
||||
* {@link android.bluetooth.BluetoothAdapter#STATE_ON},
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public void updateContent(int bluetoothState) {
|
||||
switch (bluetoothState) {
|
||||
case BluetoothAdapter.STATE_ON:
|
||||
mDevicePreferenceMap.clear();
|
||||
clearPreferenceGroupCache();
|
||||
mBluetoothAdapter.enable();
|
||||
enableScanning();
|
||||
break;
|
||||
|
||||
case BluetoothAdapter.STATE_OFF:
|
||||
finish();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all cached preferences in {@code preferenceGroup}.
|
||||
*/
|
||||
private void clearPreferenceGroupCache() {
|
||||
cacheRemoveAllPrefs(mAvailableDevicesCategory);
|
||||
removeCachedPrefs(mAvailableDevicesCategory);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void showBluetoothTurnedOnToast() {
|
||||
Toast.makeText(getContext(), R.string.connected_device_bluetooth_turned_on_toast,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
@@ -27,11 +27,9 @@ import androidx.preference.Preference;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.connecteddevice.DevicePreferenceCallback;
|
||||
import com.android.settings.core.SubSettingLauncher;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settings.widget.GearPreference;
|
||||
import com.android.settingslib.bluetooth.BluetoothCallback;
|
||||
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
|
||||
@@ -46,39 +44,42 @@ import java.util.Map;
|
||||
* {@link BluetoothCallback}. It notifies the upper level whether to add/remove the preference
|
||||
* through {@link DevicePreferenceCallback}
|
||||
*
|
||||
* In {@link BluetoothDeviceUpdater}, it uses {@link BluetoothDeviceFilter.Filter} to detect
|
||||
* whether the {@link CachedBluetoothDevice} is relevant.
|
||||
* In {@link BluetoothDeviceUpdater}, it uses {@link #isFilterMatched(CachedBluetoothDevice)} to
|
||||
* detect whether the {@link CachedBluetoothDevice} is relevant.
|
||||
*/
|
||||
public abstract class BluetoothDeviceUpdater implements BluetoothCallback,
|
||||
LocalBluetoothProfileManager.ServiceListener {
|
||||
private static final String TAG = "BluetoothDeviceUpdater";
|
||||
private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
|
||||
|
||||
protected final MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
protected final DevicePreferenceCallback mDevicePreferenceCallback;
|
||||
protected final Map<BluetoothDevice, Preference> mPreferenceMap;
|
||||
protected Context mContext;
|
||||
protected Context mPrefContext;
|
||||
protected DashboardFragment mFragment;
|
||||
@VisibleForTesting
|
||||
protected LocalBluetoothManager mLocalManager;
|
||||
protected int mMetricsCategory;
|
||||
|
||||
private static final String TAG = "BluetoothDeviceUpdater";
|
||||
private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
|
||||
|
||||
@VisibleForTesting
|
||||
final GearPreference.OnGearClickListener mDeviceProfilesListener = pref -> {
|
||||
launchDeviceDetails(pref);
|
||||
};
|
||||
|
||||
public BluetoothDeviceUpdater(Context context, DashboardFragment fragment,
|
||||
DevicePreferenceCallback devicePreferenceCallback) {
|
||||
this(context, fragment, devicePreferenceCallback, Utils.getLocalBtManager(context));
|
||||
public BluetoothDeviceUpdater(Context context,
|
||||
DevicePreferenceCallback devicePreferenceCallback, int metricsCategory) {
|
||||
this(context, devicePreferenceCallback, Utils.getLocalBtManager(context), metricsCategory);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
BluetoothDeviceUpdater(Context context, DashboardFragment fragment,
|
||||
DevicePreferenceCallback devicePreferenceCallback, LocalBluetoothManager localManager) {
|
||||
mFragment = fragment;
|
||||
BluetoothDeviceUpdater(Context context,
|
||||
DevicePreferenceCallback devicePreferenceCallback, LocalBluetoothManager localManager,
|
||||
int metricsCategory) {
|
||||
mContext = context;
|
||||
mDevicePreferenceCallback = devicePreferenceCallback;
|
||||
mPreferenceMap = new HashMap<>();
|
||||
mLocalManager = localManager;
|
||||
mMetricsCategory = metricsCategory;
|
||||
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
|
||||
}
|
||||
|
||||
@@ -90,7 +91,7 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback,
|
||||
Log.e(TAG, "registerCallback() Bluetooth is not supported on this device");
|
||||
return;
|
||||
}
|
||||
mLocalManager.setForegroundActivity(mFragment.getContext());
|
||||
mLocalManager.setForegroundActivity(mContext);
|
||||
mLocalManager.getEventManager().registerCallback(this);
|
||||
mLocalManager.getProfileManager().addServiceListener(this);
|
||||
forceUpdate();
|
||||
@@ -283,7 +284,7 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback,
|
||||
* {@link SubSettingLauncher} to launch {@link BluetoothDeviceDetailsFragment}
|
||||
*/
|
||||
protected void launchDeviceDetails(Preference preference) {
|
||||
mMetricsFeatureProvider.logClickedPreference(preference, mFragment.getMetricsCategory());
|
||||
mMetricsFeatureProvider.logClickedPreference(preference, mMetricsCategory);
|
||||
final CachedBluetoothDevice device =
|
||||
((BluetoothDevicePreference) preference).getBluetoothDevice();
|
||||
if (device == null) {
|
||||
@@ -293,11 +294,11 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback,
|
||||
args.putString(BluetoothDeviceDetailsFragment.KEY_DEVICE_ADDRESS,
|
||||
device.getDevice().getAddress());
|
||||
|
||||
new SubSettingLauncher(mFragment.getContext())
|
||||
new SubSettingLauncher(mContext)
|
||||
.setDestination(BluetoothDeviceDetailsFragment.class.getName())
|
||||
.setArguments(args)
|
||||
.setTitleRes(R.string.device_details_title)
|
||||
.setSourceMetricsCategory(mFragment.getMetricsCategory())
|
||||
.setSourceMetricsCategory(mMetricsCategory)
|
||||
.launch();
|
||||
}
|
||||
|
||||
|
@@ -16,31 +16,25 @@
|
||||
|
||||
package com.android.settings.bluetooth;
|
||||
|
||||
import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.accessibility.AccessibilityStatsLogUtils;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.HearingAidStatsLogUtils;
|
||||
import com.android.settingslib.search.Indexable;
|
||||
import com.android.settingslib.widget.FooterPreference;
|
||||
|
||||
/**
|
||||
* BluetoothPairingDetail is a page to scan bluetooth devices and pair them.
|
||||
*/
|
||||
public class BluetoothPairingDetail extends DeviceListPreferenceFragment implements
|
||||
public class BluetoothPairingDetail extends BluetoothDevicePairingDetailBase implements
|
||||
Indexable {
|
||||
private static final String TAG = "BluetoothPairingDetail";
|
||||
|
||||
@@ -49,35 +43,13 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
|
||||
@VisibleForTesting
|
||||
static final String KEY_FOOTER_PREF = "footer_preference";
|
||||
|
||||
@VisibleForTesting
|
||||
BluetoothProgressCategory mAvailableDevicesCategory;
|
||||
@VisibleForTesting
|
||||
FooterPreference mFooterPreference;
|
||||
@VisibleForTesting
|
||||
AlwaysDiscoverable mAlwaysDiscoverable;
|
||||
|
||||
private boolean mInitialScanStarted;
|
||||
|
||||
public BluetoothPairingDetail() {
|
||||
super(DISALLOW_CONFIG_BLUETOOTH);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
mInitialScanStarted = false;
|
||||
mAlwaysDiscoverable = new AlwaysDiscoverable(getContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
if (mLocalManager == null){
|
||||
Log.e(TAG, "Bluetooth is not supported on this device");
|
||||
return;
|
||||
}
|
||||
updateBluetooth();
|
||||
mAvailableDevicesCategory.setProgress(mBluetoothAdapter.isDiscovering());
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -86,32 +58,29 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
|
||||
use(BluetoothDeviceRenamePreferenceController.class).setFragment(this);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void updateBluetooth() {
|
||||
if (mBluetoothAdapter.isEnabled()) {
|
||||
updateContent(mBluetoothAdapter.getState());
|
||||
} else {
|
||||
// Turn on bluetooth if it is disabled
|
||||
mBluetoothAdapter.enable();
|
||||
}
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
mAlwaysDiscoverable = new AlwaysDiscoverable(getContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
mAvailableDevicesCategory.setProgress(mBluetoothAdapter.isDiscovering());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
if (mLocalManager == null){
|
||||
Log.e(TAG, "Bluetooth is not supported on this device");
|
||||
return;
|
||||
}
|
||||
// Make the device only visible to connected devices.
|
||||
mAlwaysDiscoverable.stop();
|
||||
disableScanning();
|
||||
}
|
||||
|
||||
@Override
|
||||
void initPreferencesFromPreferenceScreen() {
|
||||
mAvailableDevicesCategory = (BluetoothProgressCategory) findPreference(KEY_AVAIL_DEVICES);
|
||||
mFooterPreference = (FooterPreference) findPreference(KEY_FOOTER_PREF);
|
||||
public void initPreferencesFromPreferenceScreen() {
|
||||
super.initPreferencesFromPreferenceScreen();
|
||||
mFooterPreference = findPreference(KEY_FOOTER_PREF);
|
||||
mFooterPreference.setSelectable(false);
|
||||
}
|
||||
|
||||
@@ -120,23 +89,25 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
|
||||
return SettingsEnums.BLUETOOTH_PAIRING;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Will update footer and keep the device discoverable as long as the page is visible.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
@Override
|
||||
void enableScanning() {
|
||||
// Clear all device states before first scan
|
||||
if (!mInitialScanStarted) {
|
||||
if (mAvailableDevicesCategory != null) {
|
||||
removeAllDevices();
|
||||
public void updateContent(int bluetoothState) {
|
||||
super.updateContent(bluetoothState);
|
||||
if (bluetoothState == BluetoothAdapter.STATE_ON) {
|
||||
if (mInitialScanStarted) {
|
||||
// Don't show bonded devices when screen turned back on
|
||||
setFilter(BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER);
|
||||
addCachedDevices();
|
||||
}
|
||||
mLocalManager.getCachedDeviceManager().clearNonBondedDevices();
|
||||
mInitialScanStarted = true;
|
||||
setFilter(BluetoothDeviceFilter.ALL_FILTER);
|
||||
updateFooterPreference(mFooterPreference);
|
||||
mAlwaysDiscoverable.start();
|
||||
}
|
||||
super.enableScanning();
|
||||
}
|
||||
|
||||
@Override
|
||||
void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
|
||||
disableScanning();
|
||||
super.onDevicePreferenceClick(btPreference);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -146,78 +117,6 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
|
||||
mAvailableDevicesCategory.setProgress(started);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void updateContent(int bluetoothState) {
|
||||
switch (bluetoothState) {
|
||||
case BluetoothAdapter.STATE_ON:
|
||||
mDevicePreferenceMap.clear();
|
||||
mBluetoothAdapter.enable();
|
||||
|
||||
addDeviceCategory(mAvailableDevicesCategory,
|
||||
R.string.bluetooth_preference_found_media_devices,
|
||||
BluetoothDeviceFilter.ALL_FILTER, mInitialScanStarted);
|
||||
updateFooterPreference(mFooterPreference);
|
||||
mAlwaysDiscoverable.start();
|
||||
enableScanning();
|
||||
break;
|
||||
|
||||
case BluetoothAdapter.STATE_OFF:
|
||||
finish();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBluetoothStateChanged(int bluetoothState) {
|
||||
super.onBluetoothStateChanged(bluetoothState);
|
||||
updateContent(bluetoothState);
|
||||
if (bluetoothState == BluetoothAdapter.STATE_ON) {
|
||||
showBluetoothTurnedOnToast();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
|
||||
if (bondState == BluetoothDevice.BOND_BONDED) {
|
||||
// If one device is connected(bonded), then close this fragment.
|
||||
finish();
|
||||
return;
|
||||
} else if (bondState == BluetoothDevice.BOND_BONDING) {
|
||||
// Set the bond entry where binding process starts for logging hearing aid device info
|
||||
final int pageId = FeatureFactory.getFactory(
|
||||
getContext()).getMetricsFeatureProvider().getAttribution(getActivity());
|
||||
final int bondEntry = AccessibilityStatsLogUtils.convertToHearingAidInfoBondEntry(
|
||||
pageId);
|
||||
HearingAidStatsLogUtils.setBondEntryForDevice(bondEntry, cachedDevice);
|
||||
}
|
||||
if (mSelectedDevice != null && cachedDevice != null) {
|
||||
BluetoothDevice device = cachedDevice.getDevice();
|
||||
if (device != null && mSelectedDevice.equals(device)
|
||||
&& bondState == BluetoothDevice.BOND_NONE) {
|
||||
// If currently selected device failed to bond, restart scanning
|
||||
enableScanning();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProfileConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state,
|
||||
int bluetoothProfile) {
|
||||
// This callback is used to handle the case that bonded device is connected in pairing list.
|
||||
// 1. If user selected multiple bonded devices in pairing list, after connected
|
||||
// finish this page.
|
||||
// 2. If the bonded devices auto connected in paring list, after connected it will be
|
||||
// removed from paring list.
|
||||
if (cachedDevice != null && cachedDevice.isConnected()) {
|
||||
final BluetoothDevice device = cachedDevice.getDevice();
|
||||
if (device != null && mSelectedList.contains(device)) {
|
||||
finish();
|
||||
} else if (mDevicePreferenceMap.containsKey(cachedDevice)) {
|
||||
onDeviceDeleted(cachedDevice);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHelpResource() {
|
||||
return R.string.help_url_bluetooth;
|
||||
@@ -237,10 +136,4 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
|
||||
public String getDeviceListKey() {
|
||||
return KEY_AVAIL_DEVICES;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void showBluetoothTurnedOnToast() {
|
||||
Toast.makeText(getContext(), R.string.connected_device_bluetooth_turned_on_toast,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
@@ -24,7 +24,6 @@ import android.util.Log;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.connecteddevice.DevicePreferenceCallback;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
|
||||
/**
|
||||
@@ -39,9 +38,9 @@ public class ConnectedBluetoothDeviceUpdater extends BluetoothDeviceUpdater {
|
||||
|
||||
private final AudioManager mAudioManager;
|
||||
|
||||
public ConnectedBluetoothDeviceUpdater(Context context, DashboardFragment fragment,
|
||||
DevicePreferenceCallback devicePreferenceCallback) {
|
||||
super(context, fragment, devicePreferenceCallback);
|
||||
public ConnectedBluetoothDeviceUpdater(Context context,
|
||||
DevicePreferenceCallback devicePreferenceCallback, int metricsCategory) {
|
||||
super(context, devicePreferenceCallback, metricsCategory);
|
||||
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
||||
}
|
||||
|
||||
|
@@ -18,6 +18,11 @@ package com.android.settings.bluetooth;
|
||||
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.le.BluetoothLeScanner;
|
||||
import android.bluetooth.le.ScanCallback;
|
||||
import android.bluetooth.le.ScanFilter;
|
||||
import android.bluetooth.le.ScanResult;
|
||||
import android.bluetooth.le.ScanSettings;
|
||||
import android.os.Bundle;
|
||||
import android.os.SystemProperties;
|
||||
import android.text.BidiFormatter;
|
||||
@@ -33,6 +38,7 @@ import com.android.settings.dashboard.RestrictedDashboardFragment;
|
||||
import com.android.settingslib.bluetooth.BluetoothCallback;
|
||||
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -59,37 +65,51 @@ public abstract class DeviceListPreferenceFragment extends
|
||||
"persist.bluetooth.showdeviceswithoutnames";
|
||||
|
||||
private BluetoothDeviceFilter.Filter mFilter;
|
||||
private List<ScanFilter> mLeScanFilters;
|
||||
private ScanCallback mScanCallback;
|
||||
|
||||
@VisibleForTesting
|
||||
boolean mScanEnabled;
|
||||
protected boolean mScanEnabled;
|
||||
|
||||
BluetoothDevice mSelectedDevice;
|
||||
protected BluetoothDevice mSelectedDevice;
|
||||
|
||||
BluetoothAdapter mBluetoothAdapter;
|
||||
LocalBluetoothManager mLocalManager;
|
||||
protected BluetoothAdapter mBluetoothAdapter;
|
||||
protected LocalBluetoothManager mLocalManager;
|
||||
protected CachedBluetoothDeviceManager mCachedDeviceManager;
|
||||
|
||||
@VisibleForTesting
|
||||
PreferenceGroup mDeviceListGroup;
|
||||
protected PreferenceGroup mDeviceListGroup;
|
||||
|
||||
final HashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap =
|
||||
protected final HashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap =
|
||||
new HashMap<>();
|
||||
final List<BluetoothDevice> mSelectedList = new ArrayList<>();
|
||||
protected final List<BluetoothDevice> mSelectedList = new ArrayList<>();
|
||||
|
||||
boolean mShowDevicesWithoutNames;
|
||||
protected boolean mShowDevicesWithoutNames;
|
||||
|
||||
DeviceListPreferenceFragment(String restrictedKey) {
|
||||
public DeviceListPreferenceFragment(String restrictedKey) {
|
||||
super(restrictedKey);
|
||||
mFilter = BluetoothDeviceFilter.ALL_FILTER;
|
||||
}
|
||||
|
||||
final void setFilter(BluetoothDeviceFilter.Filter filter) {
|
||||
protected final void setFilter(BluetoothDeviceFilter.Filter filter) {
|
||||
mFilter = filter;
|
||||
}
|
||||
|
||||
final void setFilter(int filterType) {
|
||||
protected final void setFilter(int filterType) {
|
||||
mFilter = BluetoothDeviceFilter.getFilter(filterType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the bluetooth device scanning filter with {@link ScanFilter}s. It will change to start
|
||||
* {@link BluetoothLeScanner} which will scan BLE device only.
|
||||
*
|
||||
* @param leScanFilters list of settings to filter scan result
|
||||
*/
|
||||
protected void setFilter(List<ScanFilter> leScanFilters) {
|
||||
mFilter = null;
|
||||
mLeScanFilters = leScanFilters;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -100,6 +120,7 @@ public abstract class DeviceListPreferenceFragment extends
|
||||
return;
|
||||
}
|
||||
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
mCachedDeviceManager = mLocalManager.getCachedDeviceManager();
|
||||
mShowDevicesWithoutNames = SystemProperties.getBoolean(
|
||||
BLUETOOTH_SHOW_DEVICES_WITHOUT_NAMES_PROPERTY, false);
|
||||
|
||||
@@ -109,7 +130,7 @@ public abstract class DeviceListPreferenceFragment extends
|
||||
}
|
||||
|
||||
/** find and update preference that already existed in preference screen */
|
||||
abstract void initPreferencesFromPreferenceScreen();
|
||||
protected abstract void initPreferencesFromPreferenceScreen();
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
@@ -139,7 +160,7 @@ public abstract class DeviceListPreferenceFragment extends
|
||||
|
||||
void addCachedDevices() {
|
||||
Collection<CachedBluetoothDevice> cachedDevices =
|
||||
mLocalManager.getCachedDeviceManager().getCachedDevicesCopy();
|
||||
mCachedDeviceManager.getCachedDevicesCopy();
|
||||
for (CachedBluetoothDevice cachedDevice : cachedDevices) {
|
||||
onDeviceAdded(cachedDevice);
|
||||
}
|
||||
@@ -164,7 +185,7 @@ public abstract class DeviceListPreferenceFragment extends
|
||||
return super.onPreferenceTreeClick(preference);
|
||||
}
|
||||
|
||||
void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
|
||||
protected void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
|
||||
btPreference.onClicked();
|
||||
}
|
||||
|
||||
@@ -177,7 +198,8 @@ public abstract class DeviceListPreferenceFragment extends
|
||||
// Prevent updates while the list shows one of the state messages
|
||||
if (mBluetoothAdapter.getState() != BluetoothAdapter.STATE_ON) return;
|
||||
|
||||
if (mFilter.matches(cachedDevice.getDevice())) {
|
||||
if (mLeScanFilters != null
|
||||
|| (mFilter != null && mFilter.matches(cachedDevice.getDevice()))) {
|
||||
createDevicePreference(cachedDevice);
|
||||
}
|
||||
}
|
||||
@@ -227,7 +249,7 @@ public abstract class DeviceListPreferenceFragment extends
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void enableScanning() {
|
||||
protected void enableScanning() {
|
||||
// BluetoothAdapter already handles repeated scan requests
|
||||
if (!mScanEnabled) {
|
||||
startScanning();
|
||||
@@ -236,7 +258,7 @@ public abstract class DeviceListPreferenceFragment extends
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void disableScanning() {
|
||||
protected void disableScanning() {
|
||||
if (mScanEnabled) {
|
||||
stopScanning();
|
||||
mScanEnabled = false;
|
||||
@@ -250,31 +272,6 @@ public abstract class DeviceListPreferenceFragment extends
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add bluetooth device preferences to {@code preferenceGroup} which satisfy the {@code filter}
|
||||
*
|
||||
* This method will also (1) set the title for {@code preferenceGroup} and (2) change the
|
||||
* default preferenceGroup and filter
|
||||
* @param preferenceGroup
|
||||
* @param titleId
|
||||
* @param filter
|
||||
* @param addCachedDevices
|
||||
*/
|
||||
public void addDeviceCategory(PreferenceGroup preferenceGroup, int titleId,
|
||||
BluetoothDeviceFilter.Filter filter, boolean addCachedDevices) {
|
||||
cacheRemoveAllPrefs(preferenceGroup);
|
||||
preferenceGroup.setTitle(titleId);
|
||||
mDeviceListGroup = preferenceGroup;
|
||||
if (addCachedDevices) {
|
||||
// Don't show bonded devices when screen turned back on
|
||||
setFilter(BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER);
|
||||
addCachedDevices();
|
||||
}
|
||||
setFilter(filter);
|
||||
preferenceGroup.setEnabled(true);
|
||||
removeCachedPrefs(preferenceGroup);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the key of the {@link PreferenceGroup} that contains the bluetooth devices
|
||||
*/
|
||||
@@ -284,15 +281,65 @@ public abstract class DeviceListPreferenceFragment extends
|
||||
return mShowDevicesWithoutNames;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void startScanning() {
|
||||
if (mFilter != null) {
|
||||
startClassicScanning();
|
||||
} else if (mLeScanFilters != null) {
|
||||
startLeScanning();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void stopScanning() {
|
||||
if (mFilter != null) {
|
||||
stopClassicScanning();
|
||||
} else if (mLeScanFilters != null) {
|
||||
stopLeScanning();
|
||||
}
|
||||
}
|
||||
|
||||
private void startClassicScanning() {
|
||||
if (!mBluetoothAdapter.isDiscovering()) {
|
||||
mBluetoothAdapter.startDiscovery();
|
||||
}
|
||||
}
|
||||
|
||||
void stopScanning() {
|
||||
private void stopClassicScanning() {
|
||||
if (mBluetoothAdapter.isDiscovering()) {
|
||||
mBluetoothAdapter.cancelDiscovery();
|
||||
}
|
||||
}
|
||||
|
||||
private void startLeScanning() {
|
||||
final BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
|
||||
final ScanSettings settings = new ScanSettings.Builder()
|
||||
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY)
|
||||
.build();
|
||||
mScanCallback = new ScanCallback() {
|
||||
@Override
|
||||
public void onScanResult(int callbackType, ScanResult result) {
|
||||
final BluetoothDevice device = result.getDevice();
|
||||
CachedBluetoothDevice cachedDevice = mCachedDeviceManager.findDevice(device);
|
||||
if (cachedDevice == null) {
|
||||
cachedDevice = mCachedDeviceManager.addDevice(device);
|
||||
}
|
||||
onDeviceAdded(cachedDevice);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScanFailed(int errorCode) {
|
||||
Log.w(TAG, "BLE Scan failed with error code " + errorCode);
|
||||
}
|
||||
};
|
||||
scanner.startScan(mLeScanFilters, settings, mScanCallback);
|
||||
}
|
||||
|
||||
private void stopLeScanning() {
|
||||
final BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
|
||||
if (scanner != null) {
|
||||
scanner.stopScan(mScanCallback);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -27,8 +27,8 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserManager;
|
||||
import android.util.Log;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
|
||||
@@ -68,7 +68,7 @@ public final class DevicePickerFragment extends DeviceListPreferenceFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
void initPreferencesFromPreferenceScreen() {
|
||||
public void initPreferencesFromPreferenceScreen() {
|
||||
Intent intent = getActivity().getIntent();
|
||||
mNeedAuth = intent.getBooleanExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
|
||||
setFilter(intent.getIntExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
|
||||
@@ -136,7 +136,7 @@ public final class DevicePickerFragment extends DeviceListPreferenceFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
|
||||
public void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
|
||||
disableScanning();
|
||||
LocalBluetoothPreferences.persistSelectedDeviceInPicker(
|
||||
getActivity(), mSelectedDevice.getAddress());
|
||||
|
@@ -25,8 +25,6 @@ import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.connecteddevice.DevicePreferenceCallback;
|
||||
import com.android.settings.connecteddevice.PreviouslyConnectedDeviceDashboardFragment;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
|
||||
|
||||
@@ -44,15 +42,16 @@ public class SavedBluetoothDeviceUpdater extends BluetoothDeviceUpdater
|
||||
|
||||
private static final String PREF_KEY = "saved_bt";
|
||||
|
||||
private final boolean mDisplayConnected;
|
||||
private final boolean mShowConnectedDevice;
|
||||
|
||||
@VisibleForTesting
|
||||
BluetoothAdapter mBluetoothAdapter;
|
||||
|
||||
public SavedBluetoothDeviceUpdater(Context context, DashboardFragment fragment,
|
||||
DevicePreferenceCallback devicePreferenceCallback) {
|
||||
super(context, fragment, devicePreferenceCallback);
|
||||
mDisplayConnected = (fragment instanceof PreviouslyConnectedDeviceDashboardFragment);
|
||||
public SavedBluetoothDeviceUpdater(Context context,
|
||||
DevicePreferenceCallback devicePreferenceCallback, boolean showConnectedDevice,
|
||||
int metricsCategory) {
|
||||
super(context, devicePreferenceCallback, metricsCategory);
|
||||
mShowConnectedDevice = showConnectedDevice;
|
||||
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||
}
|
||||
|
||||
@@ -106,13 +105,13 @@ public class SavedBluetoothDeviceUpdater extends BluetoothDeviceUpdater
|
||||
+ cachedDevice.isConnected());
|
||||
}
|
||||
return device.getBondState() == BluetoothDevice.BOND_BONDED
|
||||
&& (mDisplayConnected || (!device.isConnected() && isDeviceInCachedDevicesList(
|
||||
&& (mShowConnectedDevice || (!device.isConnected() && isDeviceInCachedDevicesList(
|
||||
cachedDevice)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
mMetricsFeatureProvider.logClickedPreference(preference, mFragment.getMetricsCategory());
|
||||
mMetricsFeatureProvider.logClickedPreference(preference, mMetricsCategory);
|
||||
final CachedBluetoothDevice device = ((BluetoothDevicePreference) preference)
|
||||
.getBluetoothDevice();
|
||||
if (device.isConnected()) {
|
||||
|
@@ -42,6 +42,9 @@ import com.android.settingslib.bluetooth.BluetoothUtils.ErrorListener;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager.BluetoothManagerCallback;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
/**
|
||||
* Utils is a helper class that contains constants for various
|
||||
* Android resource IDs, debug logging flags, and static methods
|
||||
@@ -132,6 +135,24 @@ public final class Utils {
|
||||
return LocalBluetoothManager.getInstance(context, mOnInitCallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains a {@link LocalBluetoothManager}.
|
||||
*
|
||||
* To avoid StrictMode ThreadPolicy violation, will get it in another thread.
|
||||
*/
|
||||
public static LocalBluetoothManager getLocalBluetoothManager(Context context) {
|
||||
final FutureTask<LocalBluetoothManager> localBtManagerFutureTask = new FutureTask<>(
|
||||
// Avoid StrictMode ThreadPolicy violation
|
||||
() -> getLocalBtManager(context));
|
||||
try {
|
||||
localBtManagerFutureTask.run();
|
||||
return localBtManagerFutureTask.get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
Log.w(TAG, "Error getting LocalBluetoothManager.", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String createRemoteName(Context context, BluetoothDevice device) {
|
||||
String mRemoteName = device != null ? device.getAlias() : null;
|
||||
|
||||
|
Reference in New Issue
Block a user