Split BluetoothSettings into two pages
This cl splits the BluetoothSettings into paired device page and pairing page, including small changes about: 1. Refactor the pages so they could get as much as static preference from xml file rather than dynamically add/remove them everytime. 2. Remove creating method in BluetoothDeviceNamePreferenceController and add it in xml file 3. Create BluetoothPairingDetail page, basically move the logic from BluetoothSettings. 4. Make pairing preference clickable and jump to BluetoothPairingDetail 5. Add and update bunch of tests Bug: 35877041 Test: RunSettingsRoboTests Change-Id: Ief9e9690c612f7b46c58e866e5cecc511af642c8
This commit is contained in:
@@ -378,6 +378,11 @@
|
|||||||
<!-- Description for bluetooth device name summary [CHAR LIMIT=none] -->
|
<!-- Description for bluetooth device name summary [CHAR LIMIT=none] -->
|
||||||
<string name="bluetooth_device_name_summary">Visible as <xliff:g id="device_name">^1</xliff:g> to other devices</string>
|
<string name="bluetooth_device_name_summary">Visible as <xliff:g id="device_name">^1</xliff:g> to other devices</string>
|
||||||
|
|
||||||
|
<!-- Title for paired device group [CHAR LIMIT=none] -->
|
||||||
|
<string name="bluetooth_paired_device_title">Your devices</string>
|
||||||
|
<!-- Title for pairing bluetooth device page [CHAR LIMIT=none] -->
|
||||||
|
<string name="bluetooth_pairing_page_title">Pair bluetooth device</string>
|
||||||
|
|
||||||
<!-- Date & time settings screen title -->
|
<!-- Date & time settings screen title -->
|
||||||
<string name="date_and_time">Date & time</string>
|
<string name="date_and_time">Date & time</string>
|
||||||
<!-- The title of the activity to pick a time zone. -->
|
<!-- The title of the activity to pick a time zone. -->
|
||||||
|
30
res/xml/bluetooth_pairing_detail.xml
Normal file
30
res/xml/bluetooth_pairing_detail.xml
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Copyright (C) 2017 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.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<PreferenceScreen
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:title="@string/bluetooth_settings">
|
||||||
|
|
||||||
|
<Preference
|
||||||
|
android:key="device_name"/>
|
||||||
|
|
||||||
|
<com.android.settings.bluetooth.BluetoothProgressCategory
|
||||||
|
android:key="available_devices"
|
||||||
|
android:title="@string/bluetooth_paired_device_title"/>
|
||||||
|
|
||||||
|
<com.android.settingslib.widget.FooterPreference/>
|
||||||
|
|
||||||
|
</PreferenceScreen>
|
@@ -16,6 +16,15 @@
|
|||||||
|
|
||||||
<PreferenceScreen
|
<PreferenceScreen
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:title="@string/bluetooth_settings" >
|
android:title="@string/bluetooth_settings">
|
||||||
|
|
||||||
|
<Preference
|
||||||
|
android:key="device_name"/>
|
||||||
|
|
||||||
|
<PreferenceCategory
|
||||||
|
android:key="paired_devices"
|
||||||
|
android:title="@string/bluetooth_paired_device_title"/>
|
||||||
|
|
||||||
|
<com.android.settingslib.widget.FooterPreference/>
|
||||||
|
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
||||||
|
@@ -77,6 +77,11 @@ public class BluetoothDeviceNamePreferenceController extends PreferenceControlle
|
|||||||
mLocalAdapter = localAdapter;
|
mLocalAdapter = localAdapter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void displayPreference(PreferenceScreen screen) {
|
||||||
|
mPreference = screen.findPreference(KEY_DEVICE_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
mContext.registerReceiver(mReceiver,
|
mContext.registerReceiver(mReceiver,
|
||||||
|
194
src/com/android/settings/bluetooth/BluetoothPairingDetail.java
Normal file
194
src/com/android/settings/bluetooth/BluetoothPairingDetail.java
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 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.content.Context;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.VisibleForTesting;
|
||||||
|
|
||||||
|
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.core.PreferenceController;
|
||||||
|
import com.android.settings.search.Indexable;
|
||||||
|
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
||||||
|
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||||
|
import com.android.settingslib.widget.FooterPreference;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BluetoothPairingDetail is a page to scan bluetooth devices and pair them.
|
||||||
|
*/
|
||||||
|
public class BluetoothPairingDetail extends DeviceListPreferenceFragment implements
|
||||||
|
Indexable {
|
||||||
|
private static final String TAG = "BluetoothPairingDetail";
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
static final String KEY_AVAIL_DEVICES = "available_devices";
|
||||||
|
@VisibleForTesting
|
||||||
|
static final String KEY_FOOTER_PREF = "footer_preference";
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
BluetoothDeviceNamePreferenceController mDeviceNamePrefController;
|
||||||
|
@VisibleForTesting
|
||||||
|
BluetoothProgressCategory mAvailableDevicesCategory;
|
||||||
|
@VisibleForTesting
|
||||||
|
FooterPreference mFooterPreference;
|
||||||
|
|
||||||
|
private boolean mInitialScanStarted;
|
||||||
|
|
||||||
|
public BluetoothPairingDetail() {
|
||||||
|
super(DISALLOW_CONFIG_BLUETOOTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
|
super.onActivityCreated(savedInstanceState);
|
||||||
|
mInitialScanStarted = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
|
||||||
|
if (mLocalAdapter != null) {
|
||||||
|
updateContent(mLocalAdapter.getBluetoothState());
|
||||||
|
mAvailableDevicesCategory.setProgress(mLocalAdapter.isDiscovering());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStop() {
|
||||||
|
super.onStop();
|
||||||
|
|
||||||
|
// Make the device only visible to connected devices.
|
||||||
|
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE);
|
||||||
|
mLocalAdapter.stopScanning();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void initPreferencesFromPreferenceScreen() {
|
||||||
|
mAvailableDevicesCategory = (BluetoothProgressCategory) findPreference(KEY_AVAIL_DEVICES);
|
||||||
|
mFooterPreference = (FooterPreference) findPreference(KEY_FOOTER_PREF);
|
||||||
|
mFooterPreference.setSelectable(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMetricsCategory() {
|
||||||
|
//TODO(b/38383542): add bluetooth pairing category
|
||||||
|
return MetricsEvent.BLUETOOTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
void startScanning() {
|
||||||
|
if (mAvailableDevicesCategory != null) {
|
||||||
|
removeAllDevices();
|
||||||
|
}
|
||||||
|
|
||||||
|
mLocalManager.getCachedDeviceManager().clearNonBondedDevices();
|
||||||
|
mInitialScanStarted = true;
|
||||||
|
mLocalAdapter.startScanning(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
|
||||||
|
mLocalAdapter.stopScanning();
|
||||||
|
super.onDevicePreferenceClick(btPreference);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onScanningStateChanged(boolean started) {
|
||||||
|
mAvailableDevicesCategory.setProgress(started);
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
void updateContent(int bluetoothState) {
|
||||||
|
switch (bluetoothState) {
|
||||||
|
case BluetoothAdapter.STATE_ON:
|
||||||
|
mDevicePreferenceMap.clear();
|
||||||
|
mLocalAdapter.setBluetoothEnabled(true);
|
||||||
|
|
||||||
|
addDeviceCategory(mAvailableDevicesCategory,
|
||||||
|
R.string.bluetooth_preference_found_devices,
|
||||||
|
BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, mInitialScanStarted);
|
||||||
|
updateFooterPreference(mFooterPreference);
|
||||||
|
|
||||||
|
if (!mInitialScanStarted) {
|
||||||
|
startScanning();
|
||||||
|
}
|
||||||
|
|
||||||
|
// mLocalAdapter.setScanMode is internally synchronized so it is okay for multiple
|
||||||
|
// threads to execute.
|
||||||
|
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BluetoothAdapter.STATE_OFF:
|
||||||
|
finish();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onBluetoothStateChanged(int bluetoothState) {
|
||||||
|
super.onBluetoothStateChanged(bluetoothState);
|
||||||
|
updateContent(bluetoothState);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
|
||||||
|
if (bondState == BluetoothDevice.BOND_BONDED) {
|
||||||
|
// If one device is connected(bonded), then close this fragment.
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getHelpResource() {
|
||||||
|
return R.string.help_url_bluetooth;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getLogTag() {
|
||||||
|
return TAG;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getPreferenceScreenResId() {
|
||||||
|
return R.xml.bluetooth_pairing_detail;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<PreferenceController> getPreferenceControllers(Context context) {
|
||||||
|
List<PreferenceController> controllers = new ArrayList<>();
|
||||||
|
mDeviceNamePrefController = new BluetoothDeviceNamePreferenceController(context,
|
||||||
|
this, getLifecycle());
|
||||||
|
controllers.add(mDeviceNamePrefController);
|
||||||
|
|
||||||
|
return controllers;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDeviceListKey() {
|
||||||
|
return KEY_AVAIL_DEVICES;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -16,12 +16,12 @@
|
|||||||
|
|
||||||
package com.android.settings.bluetooth;
|
package com.android.settings.bluetooth;
|
||||||
|
|
||||||
import android.app.Fragment;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.support.v14.preference.PreferenceFragment;
|
import android.support.v14.preference.PreferenceFragment;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
import android.os.UserHandle;
|
||||||
|
|
||||||
|
import com.android.settings.SettingsActivity;
|
||||||
import com.android.settings.core.PreferenceController;
|
import com.android.settings.core.PreferenceController;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
|
||||||
@@ -34,11 +34,14 @@ public class BluetoothPairingPreferenceController extends PreferenceController {
|
|||||||
|
|
||||||
public static final String KEY_PAIRING = "pref_bt_pairing";
|
public static final String KEY_PAIRING = "pref_bt_pairing";
|
||||||
private PreferenceFragment mFragment;
|
private PreferenceFragment mFragment;
|
||||||
|
private SettingsActivity mActivity;
|
||||||
private Preference mPreference;
|
private Preference mPreference;
|
||||||
|
|
||||||
public BluetoothPairingPreferenceController(Context context, PreferenceFragment fragment) {
|
public BluetoothPairingPreferenceController(Context context, PreferenceFragment fragment,
|
||||||
|
SettingsActivity activity) {
|
||||||
super(context);
|
super(context);
|
||||||
mFragment = fragment;
|
mFragment = fragment;
|
||||||
|
mActivity = activity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -54,7 +57,9 @@ public class BluetoothPairingPreferenceController extends PreferenceController {
|
|||||||
@Override
|
@Override
|
||||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||||
if (KEY_PAIRING.equals(preference.getKey())) {
|
if (KEY_PAIRING.equals(preference.getKey())) {
|
||||||
//TODO: open the pairing page
|
mActivity.startPreferencePanelAsUser(mFragment, BluetoothPairingDetail.class.getName(),
|
||||||
|
null, R.string.bluetooth_pairing_page_title, null,
|
||||||
|
new UserHandle(UserHandle.myUserId()));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,10 +71,11 @@ public class BluetoothPairingPreferenceController extends PreferenceController {
|
|||||||
*
|
*
|
||||||
* @return bluetooth preference that created in this method
|
* @return bluetooth preference that created in this method
|
||||||
*/
|
*/
|
||||||
public Preference createBluetoothPairingPreference() {
|
public Preference createBluetoothPairingPreference(int order) {
|
||||||
mPreference = new Preference(mFragment.getPreferenceScreen().getContext());
|
mPreference = new Preference(mFragment.getPreferenceScreen().getContext());
|
||||||
mPreference.setKey(KEY_PAIRING);
|
mPreference.setKey(KEY_PAIRING);
|
||||||
mPreference.setIcon(R.drawable.ic_add);
|
mPreference.setIcon(R.drawable.ic_add);
|
||||||
|
mPreference.setOrder(order);
|
||||||
mPreference.setTitle(R.string.bluetooth_pairing_pref_title);
|
mPreference.setTitle(R.string.bluetooth_pairing_pref_title);
|
||||||
|
|
||||||
return mPreference;
|
return mPreference;
|
||||||
|
@@ -19,7 +19,6 @@ package com.android.settings.bluetooth;
|
|||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.content.BroadcastReceiver;
|
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@@ -29,10 +28,8 @@ import android.os.Bundle;
|
|||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.support.annotation.VisibleForTesting;
|
import android.support.annotation.VisibleForTesting;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceCategory;
|
|
||||||
import android.support.v7.preference.PreferenceGroup;
|
import android.support.v7.preference.PreferenceGroup;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
import android.text.BidiFormatter;
|
|
||||||
import android.text.Spannable;
|
import android.text.Spannable;
|
||||||
import android.text.style.TextAppearanceSpan;
|
import android.text.style.TextAppearanceSpan;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@@ -74,11 +71,9 @@ import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
|
|||||||
* connection management.
|
* connection management.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
// TODO: Refactor this fragment
|
|
||||||
public class BluetoothSettings extends DeviceListPreferenceFragment implements Indexable {
|
public class BluetoothSettings extends DeviceListPreferenceFragment implements Indexable {
|
||||||
private static final String TAG = "BluetoothSettings";
|
private static final String TAG = "BluetoothSettings";
|
||||||
|
|
||||||
private static final int MENU_ID_SCAN = Menu.FIRST;
|
|
||||||
private static final int MENU_ID_SHOW_RECEIVED = Menu.FIRST + 1;
|
private static final int MENU_ID_SHOW_RECEIVED = Menu.FIRST + 1;
|
||||||
|
|
||||||
/* Private intent to show the list of received files */
|
/* Private intent to show the list of received files */
|
||||||
@@ -87,47 +82,32 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
private static final String BTOPP_PACKAGE =
|
private static final String BTOPP_PACKAGE =
|
||||||
"com.android.bluetooth";
|
"com.android.bluetooth";
|
||||||
|
|
||||||
private static final String KEY_PAIRED_DEVICES = "paired_devices";
|
private static final int PAIRED_DEVICE_ORDER = 1;
|
||||||
|
private static final int PAIRING_PREF_ORDER = 2;
|
||||||
|
|
||||||
private static View mSettingsDialogView = null;
|
@VisibleForTesting
|
||||||
|
static final String KEY_PAIRED_DEVICES = "paired_devices";
|
||||||
|
@VisibleForTesting
|
||||||
|
static final String KEY_FOOTER_PREF = "footer_preference";
|
||||||
|
|
||||||
private BluetoothEnabler mBluetoothEnabler;
|
@VisibleForTesting
|
||||||
|
PreferenceGroup mPairedDevicesCategory;
|
||||||
private PreferenceGroup mPairedDevicesCategory;
|
@VisibleForTesting
|
||||||
private PreferenceGroup mAvailableDevicesCategory;
|
FooterPreference mFooterPreference;
|
||||||
private Preference mDeviceNamePreference;
|
|
||||||
private Preference mPairingPreference;
|
private Preference mPairingPreference;
|
||||||
private boolean mAvailableDevicesCategoryIsPresent;
|
private BluetoothEnabler mBluetoothEnabler;
|
||||||
|
|
||||||
private boolean mInitialScanStarted;
|
|
||||||
private boolean mInitiateDiscoverable;
|
|
||||||
|
|
||||||
private SwitchBar mSwitchBar;
|
private SwitchBar mSwitchBar;
|
||||||
|
|
||||||
private final IntentFilter mIntentFilter;
|
private final IntentFilter mIntentFilter;
|
||||||
private BluetoothDeviceNamePreferenceController mDeviceNamePrefController;
|
private BluetoothDeviceNamePreferenceController mDeviceNamePrefController;
|
||||||
private BluetoothPairingPreferenceController mPairingPrefController;
|
@VisibleForTesting
|
||||||
|
BluetoothPairingPreferenceController mPairingPrefController;
|
||||||
|
|
||||||
// For Search
|
// For Search
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final String DATA_KEY_REFERENCE = "main_toggle_bluetooth";
|
static final String DATA_KEY_REFERENCE = "main_toggle_bluetooth";
|
||||||
|
|
||||||
// accessed from inner class (not private to avoid thunks)
|
|
||||||
FooterPreference mMyDevicePreference;
|
|
||||||
|
|
||||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
final String action = intent.getAction();
|
|
||||||
final int state =
|
|
||||||
intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
|
|
||||||
|
|
||||||
if (state == BluetoothAdapter.STATE_ON) {
|
|
||||||
mInitiateDiscoverable = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
public BluetoothSettings() {
|
public BluetoothSettings() {
|
||||||
super(DISALLOW_CONFIG_BLUETOOTH);
|
super(DISALLOW_CONFIG_BLUETOOTH);
|
||||||
mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
|
mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
|
||||||
@@ -141,8 +121,6 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
@Override
|
@Override
|
||||||
public void onActivityCreated(Bundle savedInstanceState) {
|
public void onActivityCreated(Bundle savedInstanceState) {
|
||||||
super.onActivityCreated(savedInstanceState);
|
super.onActivityCreated(savedInstanceState);
|
||||||
mInitialScanStarted = false;
|
|
||||||
mInitiateDiscoverable = true;
|
|
||||||
|
|
||||||
final SettingsActivity activity = (SettingsActivity) getActivity();
|
final SettingsActivity activity = (SettingsActivity) getActivity();
|
||||||
mSwitchBar = activity.getSwitchBar();
|
mSwitchBar = activity.getSwitchBar();
|
||||||
@@ -161,28 +139,11 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void addPreferencesForActivity() {
|
void initPreferencesFromPreferenceScreen() {
|
||||||
final Context prefContext = getPrefContext();
|
mPairingPreference = mPairingPrefController.createBluetoothPairingPreference(
|
||||||
|
PAIRING_PREF_ORDER);
|
||||||
mDeviceNamePreference = mDeviceNamePrefController.createBluetoothDeviceNamePreference(
|
mFooterPreference = (FooterPreference) findPreference(KEY_FOOTER_PREF);
|
||||||
getPreferenceScreen(), 1 /* order */);
|
mPairedDevicesCategory = (PreferenceGroup) findPreference(KEY_PAIRED_DEVICES);
|
||||||
|
|
||||||
mPairedDevicesCategory = new PreferenceCategory(prefContext);
|
|
||||||
mPairedDevicesCategory.setKey(KEY_PAIRED_DEVICES);
|
|
||||||
mPairedDevicesCategory.setOrder(2);
|
|
||||||
getPreferenceScreen().addPreference(mPairedDevicesCategory);
|
|
||||||
|
|
||||||
mAvailableDevicesCategory = new BluetoothProgressCategory(prefContext);
|
|
||||||
mAvailableDevicesCategory.setSelectable(false);
|
|
||||||
mAvailableDevicesCategory.setOrder(3);
|
|
||||||
getPreferenceScreen().addPreference(mAvailableDevicesCategory);
|
|
||||||
|
|
||||||
mMyDevicePreference = mFooterPreferenceMixin.createFooterPreference();
|
|
||||||
mMyDevicePreference.setSelectable(false);
|
|
||||||
|
|
||||||
mPairingPreference = mPairingPrefController.createBluetoothPairingPreference();
|
|
||||||
|
|
||||||
setHasOptionsMenu(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -193,19 +154,14 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
mBluetoothEnabler.resume(getActivity());
|
mBluetoothEnabler.resume(getActivity());
|
||||||
}
|
}
|
||||||
super.onStart();
|
super.onStart();
|
||||||
|
|
||||||
mInitiateDiscoverable = true;
|
|
||||||
|
|
||||||
if (isUiRestricted()) {
|
if (isUiRestricted()) {
|
||||||
setDeviceListGroup(getPreferenceScreen());
|
getPreferenceScreen().removeAll();
|
||||||
if (!isUiRestrictedByOnlyAdmin()) {
|
if (!isUiRestrictedByOnlyAdmin()) {
|
||||||
getEmptyTextView().setText(R.string.bluetooth_empty_list_user_restricted);
|
getEmptyTextView().setText(R.string.bluetooth_empty_list_user_restricted);
|
||||||
}
|
}
|
||||||
removeAllDevices();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
getActivity().registerReceiver(mReceiver, mIntentFilter);
|
|
||||||
if (mLocalAdapter != null) {
|
if (mLocalAdapter != null) {
|
||||||
updateContent(mLocalAdapter.getBluetoothState());
|
updateContent(mLocalAdapter.getBluetoothState());
|
||||||
}
|
}
|
||||||
@@ -224,8 +180,6 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
if (isUiRestricted()) {
|
if (isUiRestricted()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
getActivity().unregisterReceiver(mReceiver);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -234,13 +188,6 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
// If the user is not allowed to configure bluetooth, do not show the menu.
|
// If the user is not allowed to configure bluetooth, do not show the menu.
|
||||||
if (isUiRestricted()) return;
|
if (isUiRestricted()) return;
|
||||||
|
|
||||||
boolean bluetoothIsEnabled = mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON;
|
|
||||||
boolean isDiscovering = mLocalAdapter.isDiscovering();
|
|
||||||
int textId = isDiscovering ? R.string.bluetooth_searching_for_devices :
|
|
||||||
R.string.bluetooth_search_for_devices;
|
|
||||||
menu.add(Menu.NONE, MENU_ID_SCAN, 0, textId)
|
|
||||||
.setEnabled(bluetoothIsEnabled && !isDiscovering)
|
|
||||||
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
|
|
||||||
menu.add(Menu.NONE, MENU_ID_SHOW_RECEIVED, 0, R.string.bluetooth_show_received_files)
|
menu.add(Menu.NONE, MENU_ID_SHOW_RECEIVED, 0, R.string.bluetooth_show_received_files)
|
||||||
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
|
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
|
||||||
super.onCreateOptionsMenu(menu, inflater);
|
super.onCreateOptionsMenu(menu, inflater);
|
||||||
@@ -249,14 +196,6 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
case MENU_ID_SCAN:
|
|
||||||
if (mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON) {
|
|
||||||
mMetricsFeatureProvider.action(getActivity(),
|
|
||||||
MetricsEvent.ACTION_BLUETOOTH_SCAN);
|
|
||||||
startScanning();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
|
|
||||||
case MENU_ID_SHOW_RECEIVED:
|
case MENU_ID_SHOW_RECEIVED:
|
||||||
mMetricsFeatureProvider.action(getActivity(),
|
mMetricsFeatureProvider.action(getActivity(),
|
||||||
MetricsEvent.ACTION_BLUETOOTH_FILES);
|
MetricsEvent.ACTION_BLUETOOTH_FILES);
|
||||||
@@ -268,104 +207,37 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startScanning() {
|
|
||||||
if (isUiRestricted()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!mAvailableDevicesCategoryIsPresent) {
|
|
||||||
getPreferenceScreen().addPreference(mAvailableDevicesCategory);
|
|
||||||
mAvailableDevicesCategoryIsPresent = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mAvailableDevicesCategory != null) {
|
|
||||||
setDeviceListGroup(mAvailableDevicesCategory);
|
|
||||||
removeAllDevices();
|
|
||||||
}
|
|
||||||
|
|
||||||
mLocalManager.getCachedDeviceManager().clearNonBondedDevices();
|
|
||||||
mAvailableDevicesCategory.removeAll();
|
|
||||||
mInitialScanStarted = true;
|
|
||||||
mLocalAdapter.startScanning(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void onDevicePreferenceClick(BluetoothDevicePreference btPreference) {
|
public String getDeviceListKey() {
|
||||||
mLocalAdapter.stopScanning();
|
return KEY_PAIRED_DEVICES;
|
||||||
super.onDevicePreferenceClick(btPreference);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addDeviceCategory(PreferenceGroup preferenceGroup, int titleId,
|
|
||||||
BluetoothDeviceFilter.Filter filter, boolean addCachedDevices) {
|
|
||||||
cacheRemoveAllPrefs(preferenceGroup);
|
|
||||||
preferenceGroup.setTitle(titleId);
|
|
||||||
setFilter(filter);
|
|
||||||
setDeviceListGroup(preferenceGroup);
|
|
||||||
if (addCachedDevices) {
|
|
||||||
addCachedDevices();
|
|
||||||
}
|
|
||||||
preferenceGroup.setEnabled(true);
|
|
||||||
removeCachedPrefs(preferenceGroup);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateContent(int bluetoothState) {
|
private void updateContent(int bluetoothState) {
|
||||||
final PreferenceScreen preferenceScreen = getPreferenceScreen();
|
|
||||||
int messageId = 0;
|
int messageId = 0;
|
||||||
|
|
||||||
switch (bluetoothState) {
|
switch (bluetoothState) {
|
||||||
case BluetoothAdapter.STATE_ON:
|
case BluetoothAdapter.STATE_ON:
|
||||||
|
displayEmptyMessage(false);
|
||||||
mDevicePreferenceMap.clear();
|
mDevicePreferenceMap.clear();
|
||||||
|
|
||||||
if (isUiRestricted()) {
|
if (isUiRestricted()) {
|
||||||
messageId = R.string.bluetooth_empty_list_user_restricted;
|
messageId = R.string.bluetooth_empty_list_user_restricted;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
getPreferenceScreen().removeAll();
|
|
||||||
getPreferenceScreen().addPreference(mDeviceNamePreference);
|
|
||||||
getPreferenceScreen().addPreference(mPairedDevicesCategory);
|
|
||||||
getPreferenceScreen().addPreference(mAvailableDevicesCategory);
|
|
||||||
getPreferenceScreen().addPreference(mMyDevicePreference);
|
|
||||||
|
|
||||||
// Paired devices category
|
|
||||||
addDeviceCategory(mPairedDevicesCategory,
|
addDeviceCategory(mPairedDevicesCategory,
|
||||||
R.string.bluetooth_preference_paired_devices,
|
R.string.bluetooth_preference_paired_devices,
|
||||||
BluetoothDeviceFilter.BONDED_DEVICE_FILTER, true);
|
BluetoothDeviceFilter.BONDED_DEVICE_FILTER, true);
|
||||||
mPairedDevicesCategory.addPreference(mPairingPreference);
|
mPairedDevicesCategory.addPreference(mPairingPreference);
|
||||||
int numberOfPairedDevices = mPairedDevicesCategory.getPreferenceCount();
|
updateFooterPreference(mFooterPreference);
|
||||||
|
|
||||||
if (isUiRestricted() || numberOfPairedDevices <= 0) {
|
|
||||||
if (preferenceScreen.findPreference(KEY_PAIRED_DEVICES) != null) {
|
|
||||||
preferenceScreen.removePreference(mPairedDevicesCategory);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (preferenceScreen.findPreference(KEY_PAIRED_DEVICES) == null) {
|
|
||||||
preferenceScreen.addPreference(mPairedDevicesCategory);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Available devices category
|
|
||||||
addDeviceCategory(mAvailableDevicesCategory,
|
|
||||||
R.string.bluetooth_preference_found_devices,
|
|
||||||
BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, mInitialScanStarted);
|
|
||||||
|
|
||||||
if (!mInitialScanStarted) {
|
|
||||||
startScanning();
|
|
||||||
}
|
|
||||||
|
|
||||||
updateMyDevicePreference(mMyDevicePreference);
|
|
||||||
getActivity().invalidateOptionsMenu();
|
getActivity().invalidateOptionsMenu();
|
||||||
|
|
||||||
// mLocalAdapter.setScanMode is internally synchronized so it is okay for multiple
|
|
||||||
// threads to execute.
|
|
||||||
if (mInitiateDiscoverable) {
|
|
||||||
// Make the device visible to other devices.
|
|
||||||
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||||
mInitiateDiscoverable = false;
|
|
||||||
}
|
|
||||||
return; // not break
|
return; // not break
|
||||||
|
|
||||||
case BluetoothAdapter.STATE_TURNING_OFF:
|
case BluetoothAdapter.STATE_TURNING_OFF:
|
||||||
messageId = R.string.bluetooth_turning_off;
|
messageId = R.string.bluetooth_turning_off;
|
||||||
|
mLocalAdapter.stopScanning();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case BluetoothAdapter.STATE_OFF:
|
case BluetoothAdapter.STATE_OFF:
|
||||||
@@ -377,12 +249,10 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
|
|
||||||
case BluetoothAdapter.STATE_TURNING_ON:
|
case BluetoothAdapter.STATE_TURNING_ON:
|
||||||
messageId = R.string.bluetooth_turning_on;
|
messageId = R.string.bluetooth_turning_on;
|
||||||
mInitialScanStarted = false;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
setDeviceListGroup(preferenceScreen);
|
displayEmptyMessage(true);
|
||||||
removeAllDevices();
|
|
||||||
if (messageId != 0) {
|
if (messageId != 0) {
|
||||||
getEmptyTextView().setText(messageId);
|
getEmptyTextView().setText(messageId);
|
||||||
}
|
}
|
||||||
@@ -421,18 +291,21 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
getPreferenceScreen().removeAll();
|
|
||||||
setTextSpan(emptyView.getText(), briefText);
|
setTextSpan(emptyView.getText(), briefText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
void displayEmptyMessage(boolean display) {
|
||||||
|
final Activity activity = getActivity();
|
||||||
|
activity.findViewById(android.R.id.list_container).setVisibility(
|
||||||
|
display ? View.INVISIBLE : View.VISIBLE);
|
||||||
|
activity.findViewById(android.R.id.empty).setVisibility(
|
||||||
|
display ? View.VISIBLE : View.GONE);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBluetoothStateChanged(int bluetoothState) {
|
public void onBluetoothStateChanged(int bluetoothState) {
|
||||||
super.onBluetoothStateChanged(bluetoothState);
|
super.onBluetoothStateChanged(bluetoothState);
|
||||||
// If BT is turned off/on staying in the same BT Settings screen
|
|
||||||
// discoverability to be set again
|
|
||||||
if (BluetoothAdapter.STATE_ON == bluetoothState) {
|
|
||||||
mInitiateDiscoverable = true;
|
|
||||||
}
|
|
||||||
updateContent(bluetoothState);
|
updateContent(bluetoothState);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -440,15 +313,14 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
public void onScanningStateChanged(boolean started) {
|
public void onScanningStateChanged(boolean started) {
|
||||||
super.onScanningStateChanged(started);
|
super.onScanningStateChanged(started);
|
||||||
// Update options' enabled state
|
// Update options' enabled state
|
||||||
if (getActivity() != null) {
|
final Activity activity = getActivity();
|
||||||
getActivity().invalidateOptionsMenu();
|
if (activity != null) {
|
||||||
|
activity.invalidateOptionsMenu();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
|
public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
|
||||||
setDeviceListGroup(getPreferenceScreen());
|
|
||||||
removeAllDevices();
|
|
||||||
updateContent(mLocalAdapter.getBluetoothState());
|
updateContent(mLocalAdapter.getBluetoothState());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -462,15 +334,6 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
|
||||||
void updateMyDevicePreference(Preference myDevicePreference) {
|
|
||||||
final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
|
|
||||||
|
|
||||||
myDevicePreference.setTitle(getString(
|
|
||||||
R.string.bluetooth_footer_mac_message,
|
|
||||||
bidiFormatter.unicodeWrap(mLocalAdapter.getAddress())));
|
|
||||||
}
|
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void setLocalBluetoothAdapter(LocalBluetoothAdapter localAdapter) {
|
void setLocalBluetoothAdapter(LocalBluetoothAdapter localAdapter) {
|
||||||
mLocalAdapter = localAdapter;
|
mLocalAdapter = localAdapter;
|
||||||
@@ -504,6 +367,7 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
void initDevicePreference(BluetoothDevicePreference preference) {
|
void initDevicePreference(BluetoothDevicePreference preference) {
|
||||||
|
preference.setOrder(PAIRED_DEVICE_ORDER);
|
||||||
CachedBluetoothDevice cachedDevice = preference.getCachedDevice();
|
CachedBluetoothDevice cachedDevice = preference.getCachedDevice();
|
||||||
if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
|
if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
|
||||||
// Only paired device have an associated advanced settings screen
|
// Only paired device have an associated advanced settings screen
|
||||||
@@ -531,7 +395,8 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
List<PreferenceController> controllers = new ArrayList<>();
|
List<PreferenceController> controllers = new ArrayList<>();
|
||||||
mDeviceNamePrefController = new BluetoothDeviceNamePreferenceController(context,
|
mDeviceNamePrefController = new BluetoothDeviceNamePreferenceController(context,
|
||||||
this, getLifecycle());
|
this, getLifecycle());
|
||||||
mPairingPrefController = new BluetoothPairingPreferenceController(context, this);
|
mPairingPrefController = new BluetoothPairingPreferenceController(context, this,
|
||||||
|
(SettingsActivity) getActivity());
|
||||||
controllers.add(mDeviceNamePrefController);
|
controllers.add(mDeviceNamePrefController);
|
||||||
controllers.add(mPairingPrefController);
|
controllers.add(mPairingPrefController);
|
||||||
|
|
||||||
|
@@ -19,12 +19,15 @@ package com.android.settings.bluetooth;
|
|||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.VisibleForTesting;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceCategory;
|
import android.support.v7.preference.PreferenceCategory;
|
||||||
import android.support.v7.preference.PreferenceGroup;
|
import android.support.v7.preference.PreferenceGroup;
|
||||||
|
import android.text.BidiFormatter;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import com.android.settings.dashboard.RestrictedDashboardFragment;
|
import com.android.settings.dashboard.RestrictedDashboardFragment;
|
||||||
|
import com.android.settings.R;
|
||||||
import com.android.settingslib.bluetooth.BluetoothCallback;
|
import com.android.settingslib.bluetooth.BluetoothCallback;
|
||||||
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
||||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||||
@@ -47,7 +50,6 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
|
|
||||||
private static final String TAG = "DeviceListPreferenceFragment";
|
private static final String TAG = "DeviceListPreferenceFragment";
|
||||||
|
|
||||||
private static final String KEY_BT_DEVICE_LIST = "bt_device_list";
|
|
||||||
private static final String KEY_BT_SCAN = "bt_scan";
|
private static final String KEY_BT_SCAN = "bt_scan";
|
||||||
|
|
||||||
private BluetoothDeviceFilter.Filter mFilter;
|
private BluetoothDeviceFilter.Filter mFilter;
|
||||||
@@ -57,7 +59,8 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
LocalBluetoothAdapter mLocalAdapter;
|
LocalBluetoothAdapter mLocalAdapter;
|
||||||
LocalBluetoothManager mLocalManager;
|
LocalBluetoothManager mLocalManager;
|
||||||
|
|
||||||
private PreferenceGroup mDeviceListGroup;
|
@VisibleForTesting
|
||||||
|
PreferenceGroup mDeviceListGroup;
|
||||||
|
|
||||||
final WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap =
|
final WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference> mDevicePreferenceMap =
|
||||||
new WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference>();
|
new WeakHashMap<CachedBluetoothDevice, BluetoothDevicePreference>();
|
||||||
@@ -86,17 +89,13 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
}
|
}
|
||||||
mLocalAdapter = mLocalManager.getBluetoothAdapter();
|
mLocalAdapter = mLocalManager.getBluetoothAdapter();
|
||||||
|
|
||||||
addPreferencesForActivity();
|
initPreferencesFromPreferenceScreen();
|
||||||
|
|
||||||
mDeviceListGroup = (PreferenceCategory) findPreference(KEY_BT_DEVICE_LIST);
|
mDeviceListGroup = (PreferenceCategory) findPreference(getDeviceListKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
void setDeviceListGroup(PreferenceGroup preferenceGroup) {
|
/** find and update preference that already existed in preference screen */
|
||||||
mDeviceListGroup = preferenceGroup;
|
abstract void initPreferencesFromPreferenceScreen();
|
||||||
}
|
|
||||||
|
|
||||||
/** Add preferences from the subclass. */
|
|
||||||
abstract void addPreferencesForActivity();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
@@ -105,8 +104,6 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
|
|
||||||
mLocalManager.setForegroundActivity(getActivity());
|
mLocalManager.setForegroundActivity(getActivity());
|
||||||
mLocalManager.getEventManager().registerCallback(this);
|
mLocalManager.getEventManager().registerCallback(this);
|
||||||
|
|
||||||
updateProgressUi(mLocalAdapter.isDiscovering());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -122,7 +119,6 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
}
|
}
|
||||||
|
|
||||||
void removeAllDevices() {
|
void removeAllDevices() {
|
||||||
mLocalAdapter.stopScanning();
|
|
||||||
mDevicePreferenceMap.clear();
|
mDevicePreferenceMap.clear();
|
||||||
mDeviceListGroup.removeAll();
|
mDeviceListGroup.removeAll();
|
||||||
}
|
}
|
||||||
@@ -157,6 +153,7 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
btPreference.onClicked();
|
btPreference.onClicked();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
|
public void onDeviceAdded(CachedBluetoothDevice cachedDevice) {
|
||||||
if (mDevicePreferenceMap.get(cachedDevice) != null) {
|
if (mDevicePreferenceMap.get(cachedDevice) != null) {
|
||||||
return;
|
return;
|
||||||
@@ -202,6 +199,16 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
// Does nothing by default
|
// Does nothing by default
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
void updateFooterPreference(Preference myDevicePreference) {
|
||||||
|
final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
|
||||||
|
|
||||||
|
myDevicePreference.setTitle(getString(
|
||||||
|
R.string.bluetooth_footer_mac_message,
|
||||||
|
bidiFormatter.unicodeWrap(mLocalAdapter.getAddress())));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
|
public void onDeviceDeleted(CachedBluetoothDevice cachedDevice) {
|
||||||
BluetoothDevicePreference preference = mDevicePreferenceMap.remove(cachedDevice);
|
BluetoothDevicePreference preference = mDevicePreferenceMap.remove(cachedDevice);
|
||||||
if (preference != null) {
|
if (preference != null) {
|
||||||
@@ -209,21 +216,39 @@ public abstract class DeviceListPreferenceFragment extends
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onScanningStateChanged(boolean started) {
|
@Override
|
||||||
updateProgressUi(started);
|
public void onScanningStateChanged(boolean started) {}
|
||||||
}
|
|
||||||
|
|
||||||
private void updateProgressUi(boolean start) {
|
@Override
|
||||||
if (mDeviceListGroup instanceof BluetoothProgressCategory) {
|
public void onBluetoothStateChanged(int bluetoothState) {}
|
||||||
((BluetoothProgressCategory) mDeviceListGroup).setProgress(start);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onBluetoothStateChanged(int bluetoothState) {
|
/**
|
||||||
if (bluetoothState == BluetoothAdapter.STATE_OFF) {
|
* Add bluetooth device preferences to {@code preferenceGroup} which satisfy the {@code filter}
|
||||||
updateProgressUi(false);
|
*
|
||||||
|
* 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;
|
||||||
|
setFilter(filter);
|
||||||
|
if (addCachedDevices) {
|
||||||
|
addCachedDevices();
|
||||||
}
|
}
|
||||||
|
preferenceGroup.setEnabled(true);
|
||||||
|
removeCachedPrefs(preferenceGroup);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) { }
|
public void onConnectionStateChanged(CachedBluetoothDevice cachedDevice, int state) { }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the key of the {@link PreferenceGroup} that contains the bluetooth devices
|
||||||
|
*/
|
||||||
|
public abstract String getDeviceListKey();
|
||||||
}
|
}
|
||||||
|
@@ -40,8 +40,9 @@ import java.util.List;
|
|||||||
* BluetoothSettings is the Settings screen for Bluetooth configuration and
|
* BluetoothSettings is the Settings screen for Bluetooth configuration and
|
||||||
* connection management.
|
* connection management.
|
||||||
*/
|
*/
|
||||||
public final class DevicePickerFragment extends DeviceListPreferenceObsoleteFragment {
|
public final class DevicePickerFragment extends DeviceListPreferenceFragment {
|
||||||
private static final int MENU_ID_REFRESH = Menu.FIRST;
|
private static final int MENU_ID_REFRESH = Menu.FIRST;
|
||||||
|
private static final String KEY_BT_DEVICE_LIST = "bt_device_list";
|
||||||
private static final String TAG = "DevicePickerFragment";
|
private static final String TAG = "DevicePickerFragment";
|
||||||
|
|
||||||
public DevicePickerFragment() {
|
public DevicePickerFragment() {
|
||||||
@@ -54,7 +55,7 @@ public final class DevicePickerFragment extends DeviceListPreferenceObsoleteFrag
|
|||||||
private boolean mStartScanOnStart;
|
private boolean mStartScanOnStart;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
void addPreferencesForActivity() {
|
void initPreferencesFromPreferenceScreen() {
|
||||||
Intent intent = getActivity().getIntent();
|
Intent intent = getActivity().getIntent();
|
||||||
mNeedAuth = intent.getBooleanExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
|
mNeedAuth = intent.getBooleanExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false);
|
||||||
setFilter(intent.getIntExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
|
setFilter(intent.getIntExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE,
|
||||||
@@ -167,6 +168,11 @@ public final class DevicePickerFragment extends DeviceListPreferenceObsoleteFrag
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDeviceListKey() {
|
||||||
|
return KEY_BT_DEVICE_LIST;
|
||||||
|
}
|
||||||
|
|
||||||
private void sendDevicePickedIntent(BluetoothDevice device) {
|
private void sendDevicePickedIntent(BluetoothDevice device) {
|
||||||
Intent intent = new Intent(BluetoothDevicePicker.ACTION_DEVICE_SELECTED);
|
Intent intent = new Intent(BluetoothDevicePicker.ACTION_DEVICE_SELECTED);
|
||||||
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
|
intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
com.android.settings.bluetooth.DevicePickerFragment
|
com.android.settings.bluetooth.DevicePickerFragment
|
||||||
|
com.android.settings.bluetooth.BluetoothPairingDetail
|
||||||
com.android.settings.notification.ZenModePrioritySettings
|
com.android.settings.notification.ZenModePrioritySettings
|
||||||
com.android.settings.accounts.AccountDetailDashboardFragment
|
com.android.settings.accounts.AccountDetailDashboardFragment
|
||||||
com.android.settings.fuelgauge.PowerUsageAnomalyDetails
|
com.android.settings.fuelgauge.PowerUsageAnomalyDetails
|
||||||
|
@@ -0,0 +1,133 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 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.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.anyBoolean;
|
||||||
|
import static org.mockito.Matchers.anyInt;
|
||||||
|
import static org.mockito.Mockito.doNothing;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.os.UserManager;
|
||||||
|
import android.support.v7.preference.PreferenceGroup;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
||||||
|
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||||
|
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||||
|
import com.android.settingslib.widget.FooterPreference;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Answers;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class BluetoothPairingDetailTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private UserManager mUserManager;
|
||||||
|
@Mock
|
||||||
|
private Resources mResource;
|
||||||
|
@Mock
|
||||||
|
private LocalBluetoothAdapter mLocalAdapter;
|
||||||
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
|
private LocalBluetoothManager mLocalManager;
|
||||||
|
@Mock
|
||||||
|
private PreferenceGroup mPreferenceGroup;
|
||||||
|
private BluetoothPairingDetail mFragment;
|
||||||
|
private Context mContext;
|
||||||
|
private BluetoothProgressCategory mAvailableDevicesCategory;
|
||||||
|
private FooterPreference mFooterPreference;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
mContext = RuntimeEnvironment.application;
|
||||||
|
mFragment = spy(new BluetoothPairingDetail());
|
||||||
|
doReturn(mContext).when(mFragment).getContext();
|
||||||
|
doReturn(mResource).when(mFragment).getResources();
|
||||||
|
|
||||||
|
mAvailableDevicesCategory = spy(new BluetoothProgressCategory(mContext));
|
||||||
|
mFooterPreference = new FooterPreference(mContext);
|
||||||
|
|
||||||
|
mFragment.mLocalAdapter = mLocalAdapter;
|
||||||
|
mFragment.mLocalManager = mLocalManager;
|
||||||
|
mFragment.mDeviceListGroup = mPreferenceGroup;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInitPreferencesFromPreferenceScreen_findPreferences() {
|
||||||
|
doReturn(mAvailableDevicesCategory).when(mFragment).findPreference(
|
||||||
|
BluetoothPairingDetail.KEY_AVAIL_DEVICES);
|
||||||
|
doReturn(mFooterPreference).when(mFragment).findPreference(
|
||||||
|
BluetoothPairingDetail.KEY_FOOTER_PREF);
|
||||||
|
|
||||||
|
mFragment.initPreferencesFromPreferenceScreen();
|
||||||
|
|
||||||
|
assertThat(mFragment.mAvailableDevicesCategory).isEqualTo(mAvailableDevicesCategory);
|
||||||
|
assertThat(mFragment.mFooterPreference).isEqualTo(mFooterPreference);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStartScanning_startScanAndRemoveDevices() {
|
||||||
|
mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory;
|
||||||
|
mFragment.mDeviceListGroup = mAvailableDevicesCategory;
|
||||||
|
|
||||||
|
mFragment.startScanning();
|
||||||
|
|
||||||
|
verify(mLocalAdapter).startScanning(true);
|
||||||
|
verify(mAvailableDevicesCategory).removeAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateContent_stateOn_addDevices() {
|
||||||
|
mFragment.mAvailableDevicesCategory = mAvailableDevicesCategory;
|
||||||
|
mFragment.mFooterPreference = mFooterPreference;
|
||||||
|
doNothing().when(mFragment).addDeviceCategory(any(), anyInt(), any(), anyBoolean());
|
||||||
|
|
||||||
|
mFragment.updateContent(BluetoothAdapter.STATE_ON);
|
||||||
|
|
||||||
|
verify(mFragment).addDeviceCategory(mAvailableDevicesCategory,
|
||||||
|
R.string.bluetooth_preference_found_devices,
|
||||||
|
BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER, false);
|
||||||
|
verify(mLocalAdapter).setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateContent_stateOff_finish() {
|
||||||
|
mFragment.updateContent(BluetoothAdapter.STATE_OFF);
|
||||||
|
|
||||||
|
verify(mFragment).finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -18,6 +18,11 @@ package com.android.settings.bluetooth;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.anyInt;
|
||||||
|
import static org.mockito.Matchers.anyString;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.app.FragmentManager;
|
import android.app.FragmentManager;
|
||||||
@@ -27,6 +32,7 @@ import android.support.v14.preference.PreferenceFragment;
|
|||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
import com.android.settings.SettingsActivity;
|
||||||
import com.android.settings.SettingsRobolectricTestRunner;
|
import com.android.settings.SettingsRobolectricTestRunner;
|
||||||
import com.android.settings.TestConfig;
|
import com.android.settings.TestConfig;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
@@ -45,7 +51,7 @@ import org.robolectric.annotation.Config;
|
|||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
public class BluetoothPairingPreferenceControllerTest {
|
public class BluetoothPairingPreferenceControllerTest {
|
||||||
|
private static final int ORDER = 1;
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private PreferenceFragment mFragment;
|
private PreferenceFragment mFragment;
|
||||||
@@ -59,6 +65,8 @@ public class BluetoothPairingPreferenceControllerTest {
|
|||||||
private FragmentTransaction mFragmentTransaction;
|
private FragmentTransaction mFragmentTransaction;
|
||||||
@Mock
|
@Mock
|
||||||
private PreferenceScreen mPreferenceScreen;
|
private PreferenceScreen mPreferenceScreen;
|
||||||
|
@Mock
|
||||||
|
private SettingsActivity mSettingsActivity;
|
||||||
private Preference mPreference;
|
private Preference mPreference;
|
||||||
|
|
||||||
private BluetoothPairingPreferenceController mController;
|
private BluetoothPairingPreferenceController mController;
|
||||||
@@ -70,16 +78,29 @@ public class BluetoothPairingPreferenceControllerTest {
|
|||||||
mContext = RuntimeEnvironment.application;
|
mContext = RuntimeEnvironment.application;
|
||||||
when(mFragment.getPreferenceScreen().getContext()).thenReturn(mContext);
|
when(mFragment.getPreferenceScreen().getContext()).thenReturn(mContext);
|
||||||
|
|
||||||
mController = new BluetoothPairingPreferenceController(mContext, mFragment);
|
mPreference = new Preference(mContext);
|
||||||
|
mPreference.setKey(BluetoothPairingPreferenceController.KEY_PAIRING);
|
||||||
|
|
||||||
|
mController = new BluetoothPairingPreferenceController(mContext, mFragment,
|
||||||
|
mSettingsActivity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreateBluetoothPairingPreference() {
|
public void testCreateBluetoothPairingPreference() {
|
||||||
Preference pref = mController.createBluetoothPairingPreference();
|
Preference pref = mController.createBluetoothPairingPreference(ORDER);
|
||||||
|
|
||||||
assertThat(pref.getKey()).isEqualTo(BluetoothPairingPreferenceController.KEY_PAIRING);
|
assertThat(pref.getKey()).isEqualTo(BluetoothPairingPreferenceController.KEY_PAIRING);
|
||||||
assertThat(pref.getIcon()).isEqualTo(mContext.getDrawable(R.drawable.ic_add));
|
assertThat(pref.getIcon()).isEqualTo(mContext.getDrawable(R.drawable.ic_add));
|
||||||
|
assertThat(pref.getOrder()).isEqualTo(ORDER);
|
||||||
assertThat(pref.getTitle()).isEqualTo(
|
assertThat(pref.getTitle()).isEqualTo(
|
||||||
mContext.getString(R.string.bluetooth_pairing_pref_title));
|
mContext.getString(R.string.bluetooth_pairing_pref_title));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testHandlePreferenceTreeClick_startFragment() {
|
||||||
|
mController.handlePreferenceTreeClick(mPreference);
|
||||||
|
|
||||||
|
verify(mSettingsActivity).startPreferencePanelAsUser(eq(mFragment), anyString(), any(),
|
||||||
|
anyInt(), any(), any());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -23,16 +23,21 @@ import static org.mockito.Matchers.eq;
|
|||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
|
import android.support.v7.preference.PreferenceGroup;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.SettingsRobolectricTestRunner;
|
import com.android.settings.SettingsRobolectricTestRunner;
|
||||||
import com.android.settings.TestConfig;
|
import com.android.settings.TestConfig;
|
||||||
import com.android.settings.testutils.FakeFeatureFactory;
|
import com.android.settings.testutils.FakeFeatureFactory;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||||
|
import com.android.settingslib.widget.FooterPreference;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -54,28 +59,43 @@ public class BluetoothSettingsTest {
|
|||||||
private UserManager mUserManager;
|
private UserManager mUserManager;
|
||||||
@Mock
|
@Mock
|
||||||
private Resources mResource;
|
private Resources mResource;
|
||||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
|
||||||
private Context mContext;
|
|
||||||
@Mock
|
@Mock
|
||||||
private LocalBluetoothAdapter mLocalAdapter;
|
private LocalBluetoothAdapter mLocalAdapter;
|
||||||
|
@Mock
|
||||||
|
private Activity mActivity;
|
||||||
|
@Mock
|
||||||
|
private PreferenceGroup mPairedDevicesCategory;
|
||||||
|
@Mock
|
||||||
|
private BluetoothPairingPreferenceController mPairingPreferenceController;
|
||||||
|
private Context mContext;
|
||||||
private BluetoothSettings mFragment;
|
private BluetoothSettings mFragment;
|
||||||
private Preference mMyDevicePreference;
|
|
||||||
private FakeFeatureFactory mFeatureFactory;
|
private FakeFeatureFactory mFeatureFactory;
|
||||||
|
private Preference mFooterPreference;
|
||||||
|
private TextView mEmptyMessage;
|
||||||
|
private View mContainer;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
FakeFeatureFactory.setupForTest(mContext);
|
FakeFeatureFactory.setupForTest(mContext);
|
||||||
mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
|
mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
|
||||||
|
|
||||||
mFragment = spy(new BluetoothSettings());
|
mFragment = spy(new BluetoothSettings());
|
||||||
|
|
||||||
doReturn(mContext).when(mFragment).getContext();
|
doReturn(mContext).when(mFragment).getContext();
|
||||||
doReturn(mResource).when(mFragment).getResources();
|
doReturn(mResource).when(mFragment).getResources();
|
||||||
|
doReturn(mActivity).when(mFragment).getActivity();
|
||||||
|
|
||||||
mMyDevicePreference = new Preference(RuntimeEnvironment.application);
|
mContainer = new View(mContext);
|
||||||
|
mEmptyMessage = new TextView(mContext);
|
||||||
|
doReturn(mContainer).when(mActivity).findViewById(android.R.id.list_container);
|
||||||
|
doReturn(mEmptyMessage).when(mActivity).findViewById(android.R.id.empty);
|
||||||
|
|
||||||
|
mFooterPreference = new FooterPreference(RuntimeEnvironment.application);
|
||||||
mFragment.setLocalBluetoothAdapter(mLocalAdapter);
|
mFragment.setLocalBluetoothAdapter(mLocalAdapter);
|
||||||
|
mFragment.mPairingPrefController = mPairingPreferenceController;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -89,9 +109,38 @@ public class BluetoothSettingsTest {
|
|||||||
doReturn(FOOTAGE_MAC_STRING).when(mFragment).getString(
|
doReturn(FOOTAGE_MAC_STRING).when(mFragment).getString(
|
||||||
eq(R.string.bluetooth_footer_mac_message), any());
|
eq(R.string.bluetooth_footer_mac_message), any());
|
||||||
|
|
||||||
mFragment.updateMyDevicePreference(mMyDevicePreference);
|
mFragment.updateFooterPreference(mFooterPreference);
|
||||||
|
|
||||||
assertThat(mMyDevicePreference.getTitle()).isEqualTo(FOOTAGE_MAC_STRING);
|
assertThat(mFooterPreference.getTitle()).isEqualTo(FOOTAGE_MAC_STRING);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDisplayEmptyMessage_showEmptyMessage() {
|
||||||
|
mFragment.displayEmptyMessage(true);
|
||||||
|
|
||||||
|
assertThat(mContainer.getVisibility()).isEqualTo(View.INVISIBLE);
|
||||||
|
assertThat(mEmptyMessage.getVisibility()).isEqualTo(View.VISIBLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDisplayEmptyMessage_hideEmptyMessage() {
|
||||||
|
mFragment.displayEmptyMessage(false);
|
||||||
|
|
||||||
|
assertThat(mContainer.getVisibility()).isEqualTo(View.VISIBLE);
|
||||||
|
assertThat(mEmptyMessage.getVisibility()).isEqualTo(View.GONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInitPreferencesFromPreferenceScreen() {
|
||||||
|
doReturn(mPairedDevicesCategory).when(mFragment).findPreference(
|
||||||
|
BluetoothSettings.KEY_PAIRED_DEVICES);
|
||||||
|
doReturn(mFooterPreference).when(mFragment).findPreference(
|
||||||
|
BluetoothSettings.KEY_FOOTER_PREF);
|
||||||
|
|
||||||
|
mFragment.initPreferencesFromPreferenceScreen();
|
||||||
|
|
||||||
|
assertThat(mFragment.mPairedDevicesCategory).isEqualTo(mPairedDevicesCategory);
|
||||||
|
assertThat(mFragment.mFooterPreference).isEqualTo(mFooterPreference);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@@ -0,0 +1,131 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2017 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.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.os.UserManager;
|
||||||
|
import android.support.v7.preference.Preference;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.core.PreferenceController;
|
||||||
|
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||||
|
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class DeviceListPreferenceFragmentTest {
|
||||||
|
private static final String FOOTAGE_MAC_STRING = "Bluetooth mac: xxxx";
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private UserManager mUserManager;
|
||||||
|
@Mock
|
||||||
|
private Resources mResource;
|
||||||
|
@Mock
|
||||||
|
private Context mContext;
|
||||||
|
@Mock
|
||||||
|
private LocalBluetoothAdapter mLocalAdapter;
|
||||||
|
private TestFragment mFragment;
|
||||||
|
private Preference mMyDevicePreference;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
mFragment = spy(new TestFragment());
|
||||||
|
doReturn(mContext).when(mFragment).getContext();
|
||||||
|
doReturn(mResource).when(mFragment).getResources();
|
||||||
|
mFragment.mLocalAdapter = mLocalAdapter;
|
||||||
|
|
||||||
|
mMyDevicePreference = new Preference(RuntimeEnvironment.application);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setUpdateMyDevicePreference_setTitleCorrectly() {
|
||||||
|
doReturn(FOOTAGE_MAC_STRING).when(mFragment).getString(
|
||||||
|
eq(R.string.bluetooth_footer_mac_message), any());
|
||||||
|
|
||||||
|
mFragment.updateFooterPreference(mMyDevicePreference);
|
||||||
|
|
||||||
|
assertThat(mMyDevicePreference.getTitle()).isEqualTo(FOOTAGE_MAC_STRING);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fragment to test since {@code DeviceListPreferenceFragment} is abstract
|
||||||
|
*/
|
||||||
|
public static class TestFragment extends DeviceListPreferenceFragment {
|
||||||
|
|
||||||
|
public TestFragment() {
|
||||||
|
super("");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMetricsCategory() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void initPreferencesFromPreferenceScreen() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDeviceListKey() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getLogTag() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getPreferenceScreenResId() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<PreferenceController> getPreferenceControllers(Context context) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user