Merge "Add device name preference in pairing page"

This commit is contained in:
Lei Yu
2017-12-12 18:05:34 +00:00
committed by Android (Google) Code Review
6 changed files with 50 additions and 25 deletions

View File

@@ -19,7 +19,9 @@
android:title="@string/bluetooth_pairing_pref_title"> android:title="@string/bluetooth_pairing_pref_title">
<Preference <Preference
android:key="device_name"/> android:key="bt_pair_rename_devices"
android:title="@string/bluetooth_device_name"
android:summary="@string/summary_placeholder" />
<com.android.settings.bluetooth.BluetoothProgressCategory <com.android.settings.bluetooth.BluetoothProgressCategory
android:key="available_devices" android:key="available_devices"

View File

@@ -29,10 +29,9 @@ import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin; import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter; import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart; import com.android.settingslib.core.lifecycle.events.OnStart;
@@ -41,8 +40,8 @@ import com.android.settingslib.core.lifecycle.events.OnStop;
/** /**
* Controller that shows and updates the bluetooth device name * Controller that shows and updates the bluetooth device name
*/ */
public class BluetoothDeviceNamePreferenceController extends AbstractPreferenceController public class BluetoothDeviceNamePreferenceController extends BasePreferenceController implements
implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop { LifecycleObserver, OnStart, OnStop {
private static final String TAG = "BluetoothNamePrefCtrl"; private static final String TAG = "BluetoothNamePrefCtrl";
public static final String KEY_DEVICE_NAME = "device_name"; public static final String KEY_DEVICE_NAME = "device_name";
@@ -62,12 +61,22 @@ public class BluetoothDeviceNamePreferenceController extends AbstractPreferenceC
return; return;
} }
mLocalAdapter = mLocalManager.getBluetoothAdapter(); mLocalAdapter = mLocalManager.getBluetoothAdapter();
lifecycle.addObserver(this);
if (lifecycle != null) {
lifecycle.addObserver(this);
}
}
/**
* Constructor exclusively used for Slice.
*/
public BluetoothDeviceNamePreferenceController(Context context) {
this(context, (Lifecycle) null);
} }
@VisibleForTesting @VisibleForTesting
BluetoothDeviceNamePreferenceController(Context context, LocalBluetoothAdapter localAdapter) { BluetoothDeviceNamePreferenceController(Context context, LocalBluetoothAdapter localAdapter) {
super(context); super(context, KEY_DEVICE_NAME);
mLocalAdapter = localAdapter; mLocalAdapter = localAdapter;
} }
@@ -89,8 +98,8 @@ public class BluetoothDeviceNamePreferenceController extends AbstractPreferenceC
} }
@Override @Override
public boolean isAvailable() { public int getAvailabilityStatus() {
return mLocalAdapter != null; return mLocalAdapter != null ? AVAILABLE : DISABLED_UNSUPPORTED;
} }
@Override @Override

View File

@@ -20,6 +20,7 @@ import android.app.Fragment;
import android.content.Context; import android.content.Context;
import android.support.annotation.VisibleForTesting; import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.text.TextUtils;
import com.android.internal.logging.nano.MetricsProto; import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.core.instrumentation.MetricsFeatureProvider; import com.android.settings.core.instrumentation.MetricsFeatureProvider;
@@ -30,29 +31,39 @@ import com.android.settingslib.core.lifecycle.Lifecycle;
public class BluetoothDeviceRenamePreferenceController extends public class BluetoothDeviceRenamePreferenceController extends
BluetoothDeviceNamePreferenceController { BluetoothDeviceNamePreferenceController {
public static final String PREF_KEY = "bt_rename_device";
private final Fragment mFragment; private final Fragment mFragment;
private String mPrefKey;
private MetricsFeatureProvider mMetricsFeatureProvider; private MetricsFeatureProvider mMetricsFeatureProvider;
public BluetoothDeviceRenamePreferenceController(Context context, Fragment fragment, public BluetoothDeviceRenamePreferenceController(Context context, String prefKey,
Lifecycle lifecycle) { Fragment fragment, Lifecycle lifecycle) {
super(context, lifecycle); super(context, lifecycle);
mPrefKey = prefKey;
mFragment = fragment; mFragment = fragment;
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
} }
/**
* Constructor exclusively used for Slice.
*/
public BluetoothDeviceRenamePreferenceController(Context context, String prefKey) {
super(context, (Lifecycle) null);
mPrefKey = prefKey;
mFragment = null;
}
@VisibleForTesting @VisibleForTesting
BluetoothDeviceRenamePreferenceController(Context context, Fragment fragment, BluetoothDeviceRenamePreferenceController(Context context, String prefKey, Fragment fragment,
LocalBluetoothAdapter localAdapter) { LocalBluetoothAdapter localAdapter) {
super(context, localAdapter); super(context, localAdapter);
mPrefKey = prefKey;
mFragment = fragment; mFragment = fragment;
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
} }
@Override @Override
public String getPreferenceKey() { public String getPreferenceKey() {
return PREF_KEY; return mPrefKey;
} }
@Override @Override
@@ -62,7 +73,7 @@ public class BluetoothDeviceRenamePreferenceController extends
@Override @Override
public boolean handlePreferenceTreeClick(Preference preference) { public boolean handlePreferenceTreeClick(Preference preference) {
if (PREF_KEY.equals(preference.getKey())) { if (TextUtils.equals(mPrefKey, preference.getKey()) && mFragment != null) {
mMetricsFeatureProvider.action(mContext, mMetricsFeatureProvider.action(mContext,
MetricsProto.MetricsEvent.ACTION_BLUETOOTH_RENAME); MetricsProto.MetricsEvent.ACTION_BLUETOOTH_RENAME);
LocalDeviceNameDialogFragment.newInstance() LocalDeviceNameDialogFragment.newInstance()

View File

@@ -46,9 +46,8 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
static final String KEY_AVAIL_DEVICES = "available_devices"; static final String KEY_AVAIL_DEVICES = "available_devices";
@VisibleForTesting @VisibleForTesting
static final String KEY_FOOTER_PREF = "footer_preference"; static final String KEY_FOOTER_PREF = "footer_preference";
private static final String KEY_RENAME_DEVICES = "bt_pair_rename_devices";
@VisibleForTesting
BluetoothDeviceNamePreferenceController mDeviceNamePrefController;
@VisibleForTesting @VisibleForTesting
BluetoothProgressCategory mAvailableDevicesCategory; BluetoothProgressCategory mAvailableDevicesCategory;
@VisibleForTesting @VisibleForTesting
@@ -195,10 +194,10 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
@Override @Override
protected List<AbstractPreferenceController> getPreferenceControllers(Context context) { protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
List<AbstractPreferenceController> controllers = new ArrayList<>(); final List<AbstractPreferenceController> controllers = new ArrayList<>();
mDeviceNamePrefController = new BluetoothDeviceNamePreferenceController(context, controllers.add(
getLifecycle()); new BluetoothDeviceRenamePreferenceController(context, KEY_RENAME_DEVICES, this,
controllers.add(mDeviceNamePrefController); getLifecycle()));
return controllers; return controllers;
} }

View File

@@ -73,6 +73,7 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
static final String KEY_PAIRED_DEVICES = "paired_devices"; static final String KEY_PAIRED_DEVICES = "paired_devices";
@VisibleForTesting @VisibleForTesting
static final String KEY_FOOTER_PREF = "footer_preference"; static final String KEY_FOOTER_PREF = "footer_preference";
private static final String KEY_RENAME_DEVICES = "bt_rename_device";
@VisibleForTesting @VisibleForTesting
PreferenceGroup mPairedDevicesCategory; PreferenceGroup mPairedDevicesCategory;
@@ -369,7 +370,9 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
controllers.add(mDeviceNamePrefController); controllers.add(mDeviceNamePrefController);
controllers.add(mPairingPrefController); controllers.add(mPairingPrefController);
controllers.add(new BluetoothFilesPreferenceController(context)); controllers.add(new BluetoothFilesPreferenceController(context));
controllers.add(new BluetoothDeviceRenamePreferenceController(context, this, lifecycle)); controllers.add(
new BluetoothDeviceRenamePreferenceController(context, KEY_RENAME_DEVICES, this,
lifecycle));
return controllers; return controllers;
} }

View File

@@ -47,6 +47,7 @@ import org.robolectric.annotation.Config;
public class BluetoothDeviceRenamePreferenceControllerTest { public class BluetoothDeviceRenamePreferenceControllerTest {
private static final String DEVICE_NAME = "Nightshade"; private static final String DEVICE_NAME = "Nightshade";
private static final String PREF_KEY = "bt_rename_devices";
@Mock @Mock
private LocalBluetoothAdapter mLocalAdapter; private LocalBluetoothAdapter mLocalAdapter;
@@ -66,10 +67,10 @@ public class BluetoothDeviceRenamePreferenceControllerTest {
mContext = spy(RuntimeEnvironment.application); mContext = spy(RuntimeEnvironment.application);
mPreference = new Preference(mContext); mPreference = new Preference(mContext);
mPreference.setKey(BluetoothDeviceRenamePreferenceController.PREF_KEY); mPreference.setKey(PREF_KEY);
mController = new BluetoothDeviceRenamePreferenceController( mController = new BluetoothDeviceRenamePreferenceController(
mContext, mFragment, mLocalAdapter); mContext, PREF_KEY, mFragment, mLocalAdapter);
} }
@Test @Test