Merge "Add “Rename device” on Bluetooth screen" into pi-dev

This commit is contained in:
TreeHugger Robot
2018-05-17 09:08:30 +00:00
committed by Android (Google) Code Review
8 changed files with 57 additions and 73 deletions

View File

@@ -16,12 +16,14 @@
<PreferenceScreen <PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/bluetooth_pairing_pref_title"> android:title="@string/bluetooth_pairing_pref_title">
<Preference <Preference
android:key="bt_pair_rename_devices" android:key="bt_pair_rename_devices"
android:title="@string/bluetooth_device_name" android:title="@string/bluetooth_device_name"
android:summary="@string/summary_placeholder" /> android:summary="@string/summary_placeholder"
settings:controller="com.android.settings.bluetooth.BluetoothDeviceRenamePreferenceController"/>
<com.android.settings.bluetooth.BluetoothProgressCategory <com.android.settings.bluetooth.BluetoothProgressCategory
android:key="available_devices" android:key="available_devices"

View File

@@ -19,6 +19,12 @@
android:key="bluetooth_switchbar_screen" android:key="bluetooth_switchbar_screen"
android:title="@string/bluetooth_settings_title"> android:title="@string/bluetooth_settings_title">
<Preference
android:key="bluetooth_screen_bt_pair_rename_devices"
android:title="@string/bluetooth_device_name"
android:summary="@string/summary_placeholder"
settings:controller="com.android.settings.bluetooth.BluetoothDeviceRenamePreferenceController"/>
<com.android.settingslib.RestrictedPreference <com.android.settingslib.RestrictedPreference
android:key="bluetooth_screen_add_bt_devices" android:key="bluetooth_screen_add_bt_devices"
android:title="@string/bluetooth_pairing_pref_title" android:title="@string/bluetooth_pairing_pref_title"

View File

@@ -32,7 +32,6 @@ import com.android.settings.R;
import com.android.settings.core.BasePreferenceController; 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.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;
import com.android.settingslib.core.lifecycle.events.OnStop; import com.android.settingslib.core.lifecycle.events.OnStop;
@@ -44,16 +43,16 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr
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";
@VisibleForTesting @VisibleForTesting
Preference mPreference; Preference mPreference;
private LocalBluetoothManager mLocalManager; private LocalBluetoothManager mLocalManager;
private LocalBluetoothAdapter mLocalAdapter; private LocalBluetoothAdapter mLocalAdapter;
public BluetoothDeviceNamePreferenceController(Context context, Lifecycle lifecycle) { /**
this(context, (LocalBluetoothAdapter) null); * Constructor exclusively used for Slice.
*/
public BluetoothDeviceNamePreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mLocalManager = Utils.getLocalBtManager(context); mLocalManager = Utils.getLocalBtManager(context);
if (mLocalManager == null) { if (mLocalManager == null) {
@@ -61,22 +60,12 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr
return; return;
} }
mLocalAdapter = mLocalManager.getBluetoothAdapter(); mLocalAdapter = mLocalManager.getBluetoothAdapter();
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, KEY_DEVICE_NAME); String preferenceKey) {
super(context, preferenceKey);
mLocalAdapter = localAdapter; mLocalAdapter = localAdapter;
} }
@@ -102,11 +91,6 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr
return mLocalAdapter != null ? AVAILABLE : UNSUPPORTED_ON_DEVICE; return mLocalAdapter != null ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
} }
@Override
public String getPreferenceKey() {
return KEY_DEVICE_NAME;
}
@Override @Override
public void updateState(Preference preference) { public void updateState(Preference preference) {
updateDeviceName(preference); updateDeviceName(preference);
@@ -134,7 +118,7 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr
public Preference createBluetoothDeviceNamePreference(PreferenceScreen screen, int order) { public Preference createBluetoothDeviceNamePreference(PreferenceScreen screen, int order) {
mPreference = new Preference(screen.getContext()); mPreference = new Preference(screen.getContext());
mPreference.setOrder(order); mPreference.setOrder(order);
mPreference.setKey(KEY_DEVICE_NAME); mPreference.setKey(getPreferenceKey());
screen.addPreference(mPreference); screen.addPreference(mPreference);
return mPreference; return mPreference;

View File

@@ -26,44 +26,35 @@ import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.overlay.FeatureFactory; import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter; import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.core.lifecycle.Lifecycle;
public class BluetoothDeviceRenamePreferenceController extends public class BluetoothDeviceRenamePreferenceController extends
BluetoothDeviceNamePreferenceController { BluetoothDeviceNamePreferenceController {
private final Fragment mFragment; private Fragment mFragment;
private String mPrefKey;
private MetricsFeatureProvider mMetricsFeatureProvider; private MetricsFeatureProvider mMetricsFeatureProvider;
public BluetoothDeviceRenamePreferenceController(Context context, String prefKey,
Fragment fragment, Lifecycle lifecycle) {
super(context, lifecycle);
mPrefKey = prefKey;
mFragment = fragment;
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
}
/** /**
* Constructor exclusively used for Slice. * Constructor exclusively used for Slice.
*/ */
public BluetoothDeviceRenamePreferenceController(Context context, String prefKey) { public BluetoothDeviceRenamePreferenceController(Context context, String preferenceKey) {
super(context, (Lifecycle) null); super(context, preferenceKey);
mPrefKey = prefKey;
mFragment = null;
}
@VisibleForTesting
BluetoothDeviceRenamePreferenceController(Context context, String prefKey, Fragment fragment,
LocalBluetoothAdapter localAdapter) {
super(context, localAdapter);
mPrefKey = prefKey;
mFragment = fragment;
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider(); mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
} }
@Override @VisibleForTesting
public String getPreferenceKey() { BluetoothDeviceRenamePreferenceController(Context context, LocalBluetoothAdapter localAdapter,
return mPrefKey; String preferenceKey) {
super(context, localAdapter, preferenceKey);
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
}
/**
* Set the {@link Fragment} that used to show {@link LocalDeviceNameDialogFragment}
* in {@code handlePreferenceTreeClick}
*/
@VisibleForTesting
public void setFragment(Fragment fragment) {
mFragment = fragment;
} }
@Override @Override
@@ -78,7 +69,7 @@ public class BluetoothDeviceRenamePreferenceController extends
@Override @Override
public boolean handlePreferenceTreeClick(Preference preference) { public boolean handlePreferenceTreeClick(Preference preference) {
if (TextUtils.equals(mPrefKey, preference.getKey()) && mFragment != null) { if (TextUtils.equals(getPreferenceKey(), 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

@@ -30,12 +30,8 @@ import com.android.settings.R;
import com.android.settings.search.Indexable; import com.android.settings.search.Indexable;
import com.android.settingslib.bluetooth.BluetoothDeviceFilter; import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.widget.FooterPreference; 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. * BluetoothPairingDetail is a page to scan bluetooth devices and pair them.
*/ */
@@ -47,7 +43,6 @@ 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 @VisibleForTesting
BluetoothProgressCategory mAvailableDevicesCategory; BluetoothProgressCategory mAvailableDevicesCategory;
@@ -80,6 +75,12 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
mAvailableDevicesCategory.setProgress(mLocalAdapter.isDiscovering()); mAvailableDevicesCategory.setProgress(mLocalAdapter.isDiscovering());
} }
@Override
public void onAttach(Context context) {
super.onAttach(context);
use(BluetoothDeviceRenamePreferenceController.class).setFragment(this);
}
@VisibleForTesting @VisibleForTesting
void updateBluetooth() { void updateBluetooth() {
if (mLocalAdapter.isEnabled()) { if (mLocalAdapter.isEnabled()) {
@@ -199,16 +200,6 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
return R.xml.bluetooth_pairing_detail; return R.xml.bluetooth_pairing_detail;
} }
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(
new BluetoothDeviceRenamePreferenceController(context, KEY_RENAME_DEVICES, this,
getLifecycle()));
return controllers;
}
@Override @Override
public String getDeviceListKey() { public String getDeviceListKey() {
return KEY_AVAIL_DEVICES; return KEY_AVAIL_DEVICES;

View File

@@ -24,6 +24,7 @@ import android.support.annotation.VisibleForTesting;
import com.android.internal.logging.nano.MetricsProto; import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsActivity; import com.android.settings.SettingsActivity;
import com.android.settings.bluetooth.BluetoothDeviceRenamePreferenceController;
import com.android.settings.bluetooth.BluetoothSwitchPreferenceController; import com.android.settings.bluetooth.BluetoothSwitchPreferenceController;
import com.android.settings.core.SubSettingLauncher; import com.android.settings.core.SubSettingLauncher;
import com.android.settings.core.TogglePreferenceController; import com.android.settings.core.TogglePreferenceController;
@@ -78,6 +79,12 @@ public class BluetoothDashboardFragment extends DashboardFragment {
mFooterPreference = mFooterPreferenceMixin.createFooterPreference(); mFooterPreference = mFooterPreferenceMixin.createFooterPreference();
} }
@Override
public void onAttach(Context context) {
super.onAttach(context);
use(BluetoothDeviceRenamePreferenceController.class).setFragment(this);
}
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);

View File

@@ -43,6 +43,7 @@ public class BluetoothDeviceNamePreferenceControllerTest {
private static final String DEVICE_NAME = "Nightshade"; private static final String DEVICE_NAME = "Nightshade";
private static final int ORDER = 1; private static final int ORDER = 1;
private static final String KEY_DEVICE_NAME = "test_key_name";
private Context mContext; private Context mContext;
@Mock @Mock
@@ -61,8 +62,9 @@ public class BluetoothDeviceNamePreferenceControllerTest {
when(mPreferenceScreen.getContext()).thenReturn(mContext); when(mPreferenceScreen.getContext()).thenReturn(mContext);
mPreference = new Preference(mContext); mPreference = new Preference(mContext);
mPreference.setKey(BluetoothDeviceNamePreferenceController.KEY_DEVICE_NAME); mPreference.setKey(KEY_DEVICE_NAME);
mController = spy(new BluetoothDeviceNamePreferenceController(mContext, mLocalAdapter)); mController = spy(new BluetoothDeviceNamePreferenceController(mContext, mLocalAdapter,
KEY_DEVICE_NAME));
doReturn(DEVICE_NAME).when(mController).getDeviceName(); doReturn(DEVICE_NAME).when(mController).getDeviceName();
} }
@@ -82,7 +84,7 @@ public class BluetoothDeviceNamePreferenceControllerTest {
Preference preference = Preference preference =
mController.createBluetoothDeviceNamePreference(mPreferenceScreen, ORDER); mController.createBluetoothDeviceNamePreference(mPreferenceScreen, ORDER);
assertThat(preference.getKey()).isEqualTo(mController.KEY_DEVICE_NAME); assertThat(preference.getKey()).isEqualTo(mController.getPreferenceKey());
assertThat(preference.getOrder()).isEqualTo(ORDER); assertThat(preference.getOrder()).isEqualTo(ORDER);
verify(mPreferenceScreen).addPreference(preference); verify(mPreferenceScreen).addPreference(preference);
} }

View File

@@ -67,8 +67,9 @@ public class BluetoothDeviceRenamePreferenceControllerTest {
mPreference = new Preference(mContext); mPreference = new Preference(mContext);
mPreference.setKey(PREF_KEY); mPreference.setKey(PREF_KEY);
mController = spy(new BluetoothDeviceRenamePreferenceController( mController = spy(new BluetoothDeviceRenamePreferenceController(mContext, mLocalAdapter,
mContext, PREF_KEY, mFragment, mLocalAdapter)); PREF_KEY));
mController.setFragment(mFragment);
doReturn(DEVICE_NAME).when(mController).getDeviceName(); doReturn(DEVICE_NAME).when(mController).getDeviceName();
} }