Add “Rename device” on Bluetooth screen
* Add BluetoothDeviceRenamePreferenceController in bluetooth_screen.xml * Refactor BluetoothDeviceRenamePreferenceController and BluetoothDeviceNamePreferenceController. Let these controllers can be init in xml. Bug: 78247352 Test: make -j50 RunSettingsRoboTests Change-Id: Iddd0cac1e973bbd82cb30fe4c18e8b359ba4ba2b
This commit is contained in:
@@ -16,12 +16,14 @@
|
||||
|
||||
<PreferenceScreen
|
||||
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">
|
||||
|
||||
<Preference
|
||||
android:key="bt_pair_rename_devices"
|
||||
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
|
||||
android:key="available_devices"
|
||||
|
@@ -19,6 +19,12 @@
|
||||
android:key="bluetooth_switchbar_screen"
|
||||
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
|
||||
android:key="bluetooth_screen_add_bt_devices"
|
||||
android:title="@string/connected_device_add_device_title"
|
||||
|
@@ -32,7 +32,6 @@ import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||
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.events.OnStart;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
@@ -44,16 +43,16 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr
|
||||
LifecycleObserver, OnStart, OnStop {
|
||||
private static final String TAG = "BluetoothNamePrefCtrl";
|
||||
|
||||
public static final String KEY_DEVICE_NAME = "device_name";
|
||||
|
||||
|
||||
@VisibleForTesting
|
||||
Preference mPreference;
|
||||
private LocalBluetoothManager mLocalManager;
|
||||
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);
|
||||
if (mLocalManager == null) {
|
||||
@@ -61,22 +60,12 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr
|
||||
return;
|
||||
}
|
||||
mLocalAdapter = mLocalManager.getBluetoothAdapter();
|
||||
|
||||
if (lifecycle != null) {
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor exclusively used for Slice.
|
||||
*/
|
||||
public BluetoothDeviceNamePreferenceController(Context context) {
|
||||
this(context, (Lifecycle) null);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
BluetoothDeviceNamePreferenceController(Context context, LocalBluetoothAdapter localAdapter) {
|
||||
super(context, KEY_DEVICE_NAME);
|
||||
BluetoothDeviceNamePreferenceController(Context context, LocalBluetoothAdapter localAdapter,
|
||||
String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
mLocalAdapter = localAdapter;
|
||||
}
|
||||
|
||||
@@ -102,11 +91,6 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr
|
||||
return mLocalAdapter != null ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return KEY_DEVICE_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
updateDeviceName(preference);
|
||||
@@ -134,7 +118,7 @@ public class BluetoothDeviceNamePreferenceController extends BasePreferenceContr
|
||||
public Preference createBluetoothDeviceNamePreference(PreferenceScreen screen, int order) {
|
||||
mPreference = new Preference(screen.getContext());
|
||||
mPreference.setOrder(order);
|
||||
mPreference.setKey(KEY_DEVICE_NAME);
|
||||
mPreference.setKey(getPreferenceKey());
|
||||
screen.addPreference(mPreference);
|
||||
|
||||
return mPreference;
|
||||
|
@@ -26,44 +26,35 @@ import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
public class BluetoothDeviceRenamePreferenceController extends
|
||||
BluetoothDeviceNamePreferenceController {
|
||||
|
||||
private final Fragment mFragment;
|
||||
private String mPrefKey;
|
||||
private Fragment mFragment;
|
||||
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.
|
||||
*/
|
||||
public BluetoothDeviceRenamePreferenceController(Context context, String prefKey) {
|
||||
super(context, (Lifecycle) null);
|
||||
mPrefKey = prefKey;
|
||||
mFragment = null;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
BluetoothDeviceRenamePreferenceController(Context context, String prefKey, Fragment fragment,
|
||||
LocalBluetoothAdapter localAdapter) {
|
||||
super(context, localAdapter);
|
||||
mPrefKey = prefKey;
|
||||
mFragment = fragment;
|
||||
public BluetoothDeviceRenamePreferenceController(Context context, String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPreferenceKey() {
|
||||
return mPrefKey;
|
||||
@VisibleForTesting
|
||||
BluetoothDeviceRenamePreferenceController(Context context, LocalBluetoothAdapter localAdapter,
|
||||
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
|
||||
@@ -78,7 +69,7 @@ public class BluetoothDeviceRenamePreferenceController extends
|
||||
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
if (TextUtils.equals(mPrefKey, preference.getKey()) && mFragment != null) {
|
||||
if (TextUtils.equals(getPreferenceKey(), preference.getKey()) && mFragment != null) {
|
||||
mMetricsFeatureProvider.action(mContext,
|
||||
MetricsProto.MetricsEvent.ACTION_BLUETOOTH_RENAME);
|
||||
LocalDeviceNameDialogFragment.newInstance()
|
||||
|
@@ -30,12 +30,8 @@ import com.android.settings.R;
|
||||
import com.android.settings.search.Indexable;
|
||||
import com.android.settingslib.bluetooth.BluetoothDeviceFilter;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
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.
|
||||
*/
|
||||
@@ -47,7 +43,6 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
|
||||
static final String KEY_AVAIL_DEVICES = "available_devices";
|
||||
@VisibleForTesting
|
||||
static final String KEY_FOOTER_PREF = "footer_preference";
|
||||
private static final String KEY_RENAME_DEVICES = "bt_pair_rename_devices";
|
||||
|
||||
@VisibleForTesting
|
||||
BluetoothProgressCategory mAvailableDevicesCategory;
|
||||
@@ -80,6 +75,12 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
|
||||
mAvailableDevicesCategory.setProgress(mLocalAdapter.isDiscovering());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
use(BluetoothDeviceRenamePreferenceController.class).setFragment(this);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void updateBluetooth() {
|
||||
if (mLocalAdapter.isEnabled()) {
|
||||
@@ -199,16 +200,6 @@ public class BluetoothPairingDetail extends DeviceListPreferenceFragment impleme
|
||||
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
|
||||
public String getDeviceListKey() {
|
||||
return KEY_AVAIL_DEVICES;
|
||||
|
@@ -24,6 +24,7 @@ import android.support.annotation.VisibleForTesting;
|
||||
import com.android.internal.logging.nano.MetricsProto;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.bluetooth.BluetoothDeviceRenamePreferenceController;
|
||||
import com.android.settings.bluetooth.BluetoothSwitchPreferenceController;
|
||||
import com.android.settings.core.SubSettingLauncher;
|
||||
import com.android.settings.core.TogglePreferenceController;
|
||||
@@ -78,6 +79,12 @@ public class BluetoothDashboardFragment extends DashboardFragment {
|
||||
mFooterPreference = mFooterPreferenceMixin.createFooterPreference();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
use(BluetoothDeviceRenamePreferenceController.class).setFragment(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
|
@@ -43,6 +43,7 @@ public class BluetoothDeviceNamePreferenceControllerTest {
|
||||
|
||||
private static final String DEVICE_NAME = "Nightshade";
|
||||
private static final int ORDER = 1;
|
||||
private static final String KEY_DEVICE_NAME = "test_key_name";
|
||||
|
||||
private Context mContext;
|
||||
@Mock
|
||||
@@ -61,8 +62,9 @@ public class BluetoothDeviceNamePreferenceControllerTest {
|
||||
|
||||
when(mPreferenceScreen.getContext()).thenReturn(mContext);
|
||||
mPreference = new Preference(mContext);
|
||||
mPreference.setKey(BluetoothDeviceNamePreferenceController.KEY_DEVICE_NAME);
|
||||
mController = spy(new BluetoothDeviceNamePreferenceController(mContext, mLocalAdapter));
|
||||
mPreference.setKey(KEY_DEVICE_NAME);
|
||||
mController = spy(new BluetoothDeviceNamePreferenceController(mContext, mLocalAdapter,
|
||||
KEY_DEVICE_NAME));
|
||||
doReturn(DEVICE_NAME).when(mController).getDeviceName();
|
||||
}
|
||||
|
||||
@@ -82,7 +84,7 @@ public class BluetoothDeviceNamePreferenceControllerTest {
|
||||
Preference preference =
|
||||
mController.createBluetoothDeviceNamePreference(mPreferenceScreen, ORDER);
|
||||
|
||||
assertThat(preference.getKey()).isEqualTo(mController.KEY_DEVICE_NAME);
|
||||
assertThat(preference.getKey()).isEqualTo(mController.getPreferenceKey());
|
||||
assertThat(preference.getOrder()).isEqualTo(ORDER);
|
||||
verify(mPreferenceScreen).addPreference(preference);
|
||||
}
|
||||
|
@@ -67,8 +67,9 @@ public class BluetoothDeviceRenamePreferenceControllerTest {
|
||||
mPreference = new Preference(mContext);
|
||||
mPreference.setKey(PREF_KEY);
|
||||
|
||||
mController = spy(new BluetoothDeviceRenamePreferenceController(
|
||||
mContext, PREF_KEY, mFragment, mLocalAdapter));
|
||||
mController = spy(new BluetoothDeviceRenamePreferenceController(mContext, mLocalAdapter,
|
||||
PREF_KEY));
|
||||
mController.setFragment(mFragment);
|
||||
doReturn(DEVICE_NAME).when(mController).getDeviceName();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user