Merge commit 'e414cd231656314442cb503f9cda3677561dc44a' into manual_merge_e414cd2316
Test: none Change-Id: I5dedcc30c9f15fa61532ecad2e0f68de4fd97c8e
This commit is contained in:
@@ -20,6 +20,11 @@ import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH;
|
|||||||
|
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.VisibleForTesting;
|
||||||
|
import android.view.Menu;
|
||||||
|
import android.view.MenuInflater;
|
||||||
|
import android.view.MenuItem;
|
||||||
|
|
||||||
import com.android.internal.logging.nano.MetricsProto;
|
import com.android.internal.logging.nano.MetricsProto;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
@@ -36,15 +41,42 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment
|
|||||||
public static final String KEY_DEVICE_ADDRESS = "device_address";
|
public static final String KEY_DEVICE_ADDRESS = "device_address";
|
||||||
private static final String TAG = "BTDeviceDetailsFrg";
|
private static final String TAG = "BTDeviceDetailsFrg";
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
static int EDIT_DEVICE_NAME_ITEM_ID = Menu.FIRST;
|
||||||
|
|
||||||
private String mDeviceAddress;
|
private String mDeviceAddress;
|
||||||
|
private LocalBluetoothManager mManager;
|
||||||
|
private CachedBluetoothDevice mCachedDevice;
|
||||||
|
|
||||||
public BluetoothDeviceDetailsFragment() {
|
public BluetoothDeviceDetailsFragment() {
|
||||||
super(DISALLOW_CONFIG_BLUETOOTH);
|
super(DISALLOW_CONFIG_BLUETOOTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
LocalBluetoothManager getLocalBluetoothManager(Context context) {
|
||||||
|
return Utils.getLocalBtManager(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
CachedBluetoothDevice getCachedDevice(String deviceAddress) {
|
||||||
|
BluetoothDevice remoteDevice =
|
||||||
|
mManager.getBluetoothAdapter().getRemoteDevice(deviceAddress);
|
||||||
|
return mManager.getCachedDeviceManager().findDevice(remoteDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BluetoothDeviceDetailsFragment newInstance(String deviceAddress) {
|
||||||
|
Bundle args = new Bundle(1);
|
||||||
|
args.putString(KEY_DEVICE_ADDRESS, deviceAddress);
|
||||||
|
BluetoothDeviceDetailsFragment fragment = new BluetoothDeviceDetailsFragment();
|
||||||
|
fragment.setArguments(args);
|
||||||
|
return fragment;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAttach(Context context) {
|
public void onAttach(Context context) {
|
||||||
mDeviceAddress = getArguments().getString(KEY_DEVICE_ADDRESS);
|
mDeviceAddress = getArguments().getString(KEY_DEVICE_ADDRESS);
|
||||||
|
mManager = getLocalBluetoothManager(context);
|
||||||
|
mCachedDevice = getCachedDevice(mDeviceAddress);
|
||||||
super.onAttach(context);
|
super.onAttach(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,21 +95,37 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment
|
|||||||
return R.xml.bluetooth_device_details_fragment;
|
return R.xml.bluetooth_device_details_fragment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||||
|
MenuItem item = menu.add(0, EDIT_DEVICE_NAME_ITEM_ID, 0, R.string.bluetooth_rename_button);
|
||||||
|
item.setIcon(R.drawable.ic_mode_edit);
|
||||||
|
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
|
||||||
|
super.onCreateOptionsMenu(menu, inflater);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onOptionsItemSelected(MenuItem menuItem) {
|
||||||
|
if (menuItem.getItemId() == EDIT_DEVICE_NAME_ITEM_ID) {
|
||||||
|
RemoteDeviceNameDialogFragment.newInstance(mCachedDevice).show(
|
||||||
|
getFragmentManager(), RemoteDeviceNameDialogFragment.TAG);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return super.onOptionsItemSelected(menuItem);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
|
protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
|
||||||
ArrayList<AbstractPreferenceController> controllers = new ArrayList<>();
|
ArrayList<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||||
LocalBluetoothManager manager = Utils.getLocalBtManager(context);
|
|
||||||
BluetoothDevice remoteDevice = manager.getBluetoothAdapter().getRemoteDevice(
|
if (mCachedDevice != null) {
|
||||||
mDeviceAddress);
|
|
||||||
CachedBluetoothDevice device = manager.getCachedDeviceManager().findDevice(remoteDevice);
|
|
||||||
if (device != null) {
|
|
||||||
Lifecycle lifecycle = getLifecycle();
|
Lifecycle lifecycle = getLifecycle();
|
||||||
controllers.add(new BluetoothDetailsHeaderController(context, this, device, lifecycle));
|
controllers.add(new BluetoothDetailsHeaderController(context, this, mCachedDevice,
|
||||||
controllers.add(new BluetoothDetailsButtonsController(context, this, device,
|
|
||||||
lifecycle));
|
lifecycle));
|
||||||
controllers.add(new BluetoothDetailsProfilesController(context, this, manager, device,
|
controllers.add(new BluetoothDetailsButtonsController(context, this, mCachedDevice,
|
||||||
lifecycle));
|
lifecycle));
|
||||||
controllers.add(new BluetoothDetailsMacAddressController(context, this, device,
|
controllers.add(new BluetoothDetailsProfilesController(context, this, mManager,
|
||||||
|
mCachedDevice, lifecycle));
|
||||||
|
controllers.add(new BluetoothDetailsMacAddressController(context, this, mCachedDevice,
|
||||||
lifecycle));
|
lifecycle));
|
||||||
}
|
}
|
||||||
return controllers;
|
return controllers;
|
||||||
|
@@ -111,7 +111,8 @@ public class BluetoothDeviceNamePreferenceController extends AbstractPreferenceC
|
|||||||
@Override
|
@Override
|
||||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||||
if (KEY_DEVICE_NAME.equals(preference.getKey())) {
|
if (KEY_DEVICE_NAME.equals(preference.getKey())) {
|
||||||
new BluetoothNameDialogFragment().show(mFragment.getFragmentManager(), "rename device");
|
LocalDeviceNameDialogFragment.newInstance()
|
||||||
|
.show(mFragment.getFragmentManager(), LocalDeviceNameDialogFragment.TAG);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -18,19 +18,13 @@ package com.android.settings.bluetooth;
|
|||||||
|
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
import android.app.DialogFragment;
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
|
||||||
import android.content.BroadcastReceiver;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.IntentFilter;
|
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.text.Editable;
|
import android.text.Editable;
|
||||||
import android.text.InputFilter;
|
import android.text.InputFilter;
|
||||||
import android.text.TextWatcher;
|
import android.text.TextWatcher;
|
||||||
import android.util.Log;
|
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@@ -40,25 +34,19 @@ import android.widget.Button;
|
|||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.android.internal.logging.nano.MetricsProto;
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
|
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
|
||||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dialog fragment for renaming the local Bluetooth device.
|
* Dialog fragment for renaming a Bluetooth device.
|
||||||
*/
|
*/
|
||||||
public final class BluetoothNameDialogFragment extends InstrumentedDialogFragment
|
abstract class BluetoothNameDialogFragment extends InstrumentedDialogFragment
|
||||||
implements TextWatcher {
|
implements TextWatcher {
|
||||||
private static final int BLUETOOTH_NAME_MAX_LENGTH_BYTES = 248;
|
private static final int BLUETOOTH_NAME_MAX_LENGTH_BYTES = 248;
|
||||||
|
|
||||||
private AlertDialog mAlertDialog;
|
private AlertDialog mAlertDialog;
|
||||||
private Button mOkButton;
|
private Button mOkButton;
|
||||||
|
|
||||||
// accessed from inner class (not private to avoid thunks)
|
|
||||||
static final String TAG = "BluetoothNameDialogFragment";
|
|
||||||
LocalBluetoothAdapter mLocalAdapter;
|
|
||||||
EditText mDeviceNameView;
|
EditText mDeviceNameView;
|
||||||
|
|
||||||
// This flag is set when the name is updated by code, to distinguish from user changes
|
// This flag is set when the name is updated by code, to distinguish from user changes
|
||||||
@@ -71,63 +59,43 @@ public final class BluetoothNameDialogFragment extends InstrumentedDialogFragmen
|
|||||||
private static final String KEY_NAME = "device_name";
|
private static final String KEY_NAME = "device_name";
|
||||||
private static final String KEY_NAME_EDITED = "device_name_edited";
|
private static final String KEY_NAME_EDITED = "device_name_edited";
|
||||||
|
|
||||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
/**
|
||||||
@Override
|
* @return the title to use for the dialog.
|
||||||
public void onReceive(Context context, Intent intent) {
|
*/
|
||||||
String action = intent.getAction();
|
abstract protected int getDialogTitle();
|
||||||
if (action.equals(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED)) {
|
|
||||||
updateDeviceName();
|
|
||||||
} else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED) &&
|
|
||||||
(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR) ==
|
|
||||||
BluetoothAdapter.STATE_ON)) {
|
|
||||||
updateDeviceName();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public int getMetricsCategory() {
|
* @return the current name used for this device.
|
||||||
return MetricsProto.MetricsEvent.DIALOG_BLUETOOTH_RENAME;
|
*/
|
||||||
}
|
abstract protected String getDeviceName();
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
* Set the device to the given name.
|
||||||
super.onCreate(savedInstanceState);
|
* @param deviceName the name to use
|
||||||
|
*/
|
||||||
LocalBluetoothManager localManager = Utils.getLocalBtManager(getActivity());
|
abstract protected void setDeviceName(String deviceName);
|
||||||
mLocalAdapter = localManager.getBluetoothAdapter();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
String deviceName = mLocalAdapter.getName();
|
String deviceName = getDeviceName();
|
||||||
if (savedInstanceState != null) {
|
if (savedInstanceState != null) {
|
||||||
deviceName = savedInstanceState.getString(KEY_NAME, deviceName);
|
deviceName = savedInstanceState.getString(KEY_NAME, deviceName);
|
||||||
mDeviceNameEdited = savedInstanceState.getBoolean(KEY_NAME_EDITED, false);
|
mDeviceNameEdited = savedInstanceState.getBoolean(KEY_NAME_EDITED, false);
|
||||||
}
|
}
|
||||||
mAlertDialog = new AlertDialog.Builder(getActivity())
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
|
||||||
.setTitle(R.string.bluetooth_rename_device)
|
.setTitle(getDialogTitle())
|
||||||
.setView(createDialogView(deviceName))
|
.setView(createDialogView(deviceName))
|
||||||
.setPositiveButton(R.string.bluetooth_rename_button,
|
.setPositiveButton(R.string.bluetooth_rename_button, (dialog, which) -> {
|
||||||
new DialogInterface.OnClickListener() {
|
setDeviceName(mDeviceNameView.getText().toString());
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
|
||||||
String deviceName = mDeviceNameView.getText().toString();
|
|
||||||
setDeviceName(deviceName);
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.setNegativeButton(android.R.string.cancel, null)
|
.setNegativeButton(android.R.string.cancel, null);
|
||||||
.create();
|
mAlertDialog = builder.create();
|
||||||
mAlertDialog.getWindow().setSoftInputMode(
|
mAlertDialog.getWindow().setSoftInputMode(
|
||||||
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
|
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
|
||||||
|
|
||||||
return mAlertDialog;
|
return mAlertDialog;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setDeviceName(String deviceName) {
|
|
||||||
Log.d(TAG, "Setting device name to " + deviceName);
|
|
||||||
mLocalAdapter.setName(deviceName);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSaveInstanceState(Bundle outState) {
|
public void onSaveInstanceState(Bundle outState) {
|
||||||
outState.putString(KEY_NAME, mDeviceNameView.getText().toString());
|
outState.putString(KEY_NAME, mDeviceNameView.getText().toString());
|
||||||
@@ -174,23 +142,14 @@ public final class BluetoothNameDialogFragment extends InstrumentedDialogFragmen
|
|||||||
mOkButton = mAlertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
|
mOkButton = mAlertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
|
||||||
mOkButton.setEnabled(mDeviceNameEdited); // Ok button enabled after user edits
|
mOkButton.setEnabled(mDeviceNameEdited); // Ok button enabled after user edits
|
||||||
}
|
}
|
||||||
IntentFilter filter = new IntentFilter();
|
|
||||||
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
|
|
||||||
filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
|
|
||||||
getActivity().registerReceiver(mReceiver, filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onPause() {
|
|
||||||
super.onPause();
|
|
||||||
getActivity().unregisterReceiver(mReceiver);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateDeviceName() {
|
void updateDeviceName() {
|
||||||
if (mLocalAdapter != null && mLocalAdapter.isEnabled()) {
|
String name = getDeviceName();
|
||||||
|
if (name != null) {
|
||||||
mDeviceNameUpdated = true;
|
mDeviceNameUpdated = true;
|
||||||
mDeviceNameEdited = false;
|
mDeviceNameEdited = false;
|
||||||
mDeviceNameView.setText(mLocalAdapter.getName());
|
mDeviceNameView.setText(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -313,7 +313,6 @@ public class BluetoothSettings extends DeviceListPreferenceFragment implements I
|
|||||||
// New version - uses a separate screen.
|
// New version - uses a separate screen.
|
||||||
args.putString(BluetoothDeviceDetailsFragment.KEY_DEVICE_ADDRESS,
|
args.putString(BluetoothDeviceDetailsFragment.KEY_DEVICE_ADDRESS,
|
||||||
device.getDevice().getAddress());
|
device.getDevice().getAddress());
|
||||||
BluetoothDeviceDetailsFragment fragment = new BluetoothDeviceDetailsFragment();
|
|
||||||
final SettingsActivity activity =
|
final SettingsActivity activity =
|
||||||
(SettingsActivity) BluetoothSettings.this.getActivity();
|
(SettingsActivity) BluetoothSettings.this.getActivity();
|
||||||
activity.startPreferencePanel(this,
|
activity.startPreferencePanel(this,
|
||||||
|
@@ -0,0 +1,97 @@
|
|||||||
|
/*
|
||||||
|
* 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 android.bluetooth.BluetoothAdapter;
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import com.android.internal.logging.nano.MetricsProto;
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||||
|
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||||
|
|
||||||
|
/** Provides a dialog for changing the advertised name of the local bluetooth adapter. */
|
||||||
|
public class LocalDeviceNameDialogFragment extends BluetoothNameDialogFragment {
|
||||||
|
public static final String TAG = "LocalAdapterName";
|
||||||
|
private LocalBluetoothAdapter mLocalAdapter;
|
||||||
|
|
||||||
|
public static LocalDeviceNameDialogFragment newInstance() {
|
||||||
|
return new LocalDeviceNameDialogFragment();
|
||||||
|
}
|
||||||
|
|
||||||
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
String action = intent.getAction();
|
||||||
|
if (BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED.equals(action) ||
|
||||||
|
(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action) &&
|
||||||
|
intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
|
||||||
|
== BluetoothAdapter.STATE_ON)) {
|
||||||
|
updateDeviceName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
LocalBluetoothManager localManager = Utils.getLocalBtManager(getActivity());
|
||||||
|
mLocalAdapter = localManager.getBluetoothAdapter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
IntentFilter filter = new IntentFilter();
|
||||||
|
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
|
||||||
|
filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
|
||||||
|
getActivity().registerReceiver(mReceiver, filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
getActivity().unregisterReceiver(mReceiver);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMetricsCategory() {
|
||||||
|
return MetricsProto.MetricsEvent.DIALOG_BLUETOOTH_RENAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getDialogTitle() {
|
||||||
|
return R.string.bluetooth_rename_device;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getDeviceName() {
|
||||||
|
if (mLocalAdapter != null && mLocalAdapter.isEnabled()) {
|
||||||
|
return mLocalAdapter.getName();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void setDeviceName(String deviceName) {
|
||||||
|
mLocalAdapter.setName(deviceName);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* 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 android.bluetooth.BluetoothDevice;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import com.android.internal.annotations.VisibleForTesting;
|
||||||
|
import com.android.internal.logging.nano.MetricsProto;
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||||
|
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||||
|
|
||||||
|
/** Provides a dialog for changing the display name of a remote bluetooth device. */
|
||||||
|
public class RemoteDeviceNameDialogFragment extends BluetoothNameDialogFragment {
|
||||||
|
public static final String TAG = "RemoteDeviceName";
|
||||||
|
private static final String KEY_CACHED_DEVICE_ADDRESS = "cached_device";
|
||||||
|
|
||||||
|
private CachedBluetoothDevice mDevice;
|
||||||
|
|
||||||
|
public static RemoteDeviceNameDialogFragment newInstance(CachedBluetoothDevice device) {
|
||||||
|
Bundle args = new Bundle(1);
|
||||||
|
args.putString(KEY_CACHED_DEVICE_ADDRESS, device.getDevice().getAddress());
|
||||||
|
RemoteDeviceNameDialogFragment fragment = new RemoteDeviceNameDialogFragment();
|
||||||
|
fragment.setArguments(args);
|
||||||
|
return fragment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
CachedBluetoothDevice getDevice(Context context) {
|
||||||
|
String deviceAddress = getArguments().getString(KEY_CACHED_DEVICE_ADDRESS);
|
||||||
|
LocalBluetoothManager manager = Utils.getLocalBtManager(context);
|
||||||
|
BluetoothDevice device = manager.getBluetoothAdapter().getRemoteDevice(deviceAddress);
|
||||||
|
return manager.getCachedDeviceManager().findDevice(device);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAttach(Context context) {
|
||||||
|
super.onAttach(context);
|
||||||
|
mDevice = getDevice(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMetricsCategory() {
|
||||||
|
return MetricsProto.MetricsEvent.DIALOG_BLUETOOTH_PAIRED_DEVICE_RENAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int getDialogTitle() {
|
||||||
|
return R.string.bluetooth_device_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getDeviceName() {
|
||||||
|
if (mDevice != null) {
|
||||||
|
return mDevice.getName();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void setDeviceName(String deviceName) {
|
||||||
|
if (mDevice != null) {
|
||||||
|
mDevice.setName(deviceName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* 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.mock;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.app.Fragment;
|
||||||
|
import android.app.FragmentManager;
|
||||||
|
import android.app.FragmentTransaction;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.view.MenuInflater;
|
||||||
|
import android.view.MenuItem;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.testutils.FakeFeatureFactory;
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||||
|
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Answers;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.fakes.RoboMenu;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class BluetoothDeviceDetailsFragmentTest {
|
||||||
|
private BluetoothDeviceDetailsFragment mFragment;
|
||||||
|
private Context mContext;
|
||||||
|
|
||||||
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
|
private CachedBluetoothDevice mCachedDevice;
|
||||||
|
|
||||||
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
|
private LocalBluetoothManager mLocalManager;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
FakeFeatureFactory.setupForTest(mContext);
|
||||||
|
|
||||||
|
String deviceAddress = "55:66:77:88:99:AA";
|
||||||
|
mFragment = spy(BluetoothDeviceDetailsFragment.newInstance(deviceAddress));
|
||||||
|
doReturn(mLocalManager).when(mFragment).getLocalBluetoothManager(any());
|
||||||
|
doReturn(mCachedDevice).when(mFragment).getCachedDevice(any());
|
||||||
|
|
||||||
|
when(mCachedDevice.getDevice().getAddress()).thenReturn(deviceAddress);
|
||||||
|
Bundle args = new Bundle();
|
||||||
|
args.putString(BluetoothDeviceDetailsFragment.KEY_DEVICE_ADDRESS, deviceAddress);
|
||||||
|
mFragment.setArguments(args);
|
||||||
|
mFragment.onAttach(mContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void renameControlGetsAdded() {
|
||||||
|
RoboMenu menu = new RoboMenu(mContext);
|
||||||
|
MenuInflater inflater = new MenuInflater(mContext);
|
||||||
|
mFragment.onCreateOptionsMenu(menu, inflater);
|
||||||
|
MenuItem item = menu.getItem(0);
|
||||||
|
assertThat(item.getTitle()).isEqualTo(mContext.getString(R.string.bluetooth_rename_button));
|
||||||
|
assertThat(item.getIcon()).isEqualTo(mContext.getDrawable(R.drawable.ic_mode_edit));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void renameControlClicked() {
|
||||||
|
RoboMenu menu = new RoboMenu(mContext);
|
||||||
|
MenuInflater inflater = new MenuInflater(mContext);
|
||||||
|
mFragment.onCreateOptionsMenu(menu, inflater);
|
||||||
|
MenuItem item = menu.getItem(0);
|
||||||
|
assertThat(item.getItemId()).isEqualTo(
|
||||||
|
BluetoothDeviceDetailsFragment.EDIT_DEVICE_NAME_ITEM_ID);
|
||||||
|
|
||||||
|
FragmentManager fragmentManager = mock(FragmentManager.class);
|
||||||
|
when(mFragment.getFragmentManager()).thenReturn(fragmentManager);
|
||||||
|
FragmentTransaction ft = mock(FragmentTransaction.class);
|
||||||
|
when(fragmentManager.beginTransaction()).thenReturn(ft);
|
||||||
|
|
||||||
|
ArgumentCaptor<Fragment> captor = ArgumentCaptor.forClass(Fragment.class);
|
||||||
|
mFragment.onOptionsItemSelected(item);
|
||||||
|
verify(ft).add(captor.capture(), eq(RemoteDeviceNameDialogFragment.TAG));
|
||||||
|
RemoteDeviceNameDialogFragment dialog = (RemoteDeviceNameDialogFragment) captor.getValue();
|
||||||
|
assertThat(dialog).isNotNull();
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,137 @@
|
|||||||
|
/*
|
||||||
|
* 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.anyString;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.app.AlertDialog;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.EditText;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.testutils.FakeFeatureFactory;
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||||
|
|
||||||
|
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;
|
||||||
|
import org.robolectric.shadows.ShadowDialog;
|
||||||
|
import org.robolectric.util.FragmentTestUtil;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class RemoteDeviceNameDialogFragmentTest {
|
||||||
|
|
||||||
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
|
private CachedBluetoothDevice mCachedDevice;
|
||||||
|
|
||||||
|
private RemoteDeviceNameDialogFragment mFragment;
|
||||||
|
private Context mContext;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
FakeFeatureFactory.setupForTest(mContext);
|
||||||
|
|
||||||
|
String deviceAddress = "55:66:77:88:99:AA";
|
||||||
|
when(mCachedDevice.getDevice().getAddress()).thenReturn(deviceAddress);
|
||||||
|
mFragment = spy(RemoteDeviceNameDialogFragment.newInstance(mCachedDevice));
|
||||||
|
doReturn(mCachedDevice).when(mFragment).getDevice(any());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method to set the mock device's name and show the dialog.
|
||||||
|
*
|
||||||
|
* @param deviceName what name to set
|
||||||
|
* @return the dialog created
|
||||||
|
*/
|
||||||
|
AlertDialog startDialog(String deviceName) {
|
||||||
|
when(mCachedDevice.getName()).thenReturn(deviceName);
|
||||||
|
FragmentTestUtil.startFragment(mFragment);
|
||||||
|
return (AlertDialog) ShadowDialog.getLatestDialog();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void deviceNameDisplayIsCorrect() {
|
||||||
|
String deviceName = "ABC Corp Headphones";
|
||||||
|
AlertDialog dialog = startDialog(deviceName);
|
||||||
|
EditText editText = (EditText) dialog.findViewById(R.id.edittext);
|
||||||
|
assertThat(editText.getText().toString()).isEqualTo(deviceName);
|
||||||
|
|
||||||
|
// Make sure that the "rename" button isn't enabled since the text hasn't changed yet, but
|
||||||
|
// the "cancel" button should be enabled.
|
||||||
|
Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
|
||||||
|
assertThat(positiveButton.isEnabled()).isFalse();
|
||||||
|
Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
|
||||||
|
assertThat(negativeButton.isEnabled()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void deviceNameEditSucceeds() {
|
||||||
|
String deviceNameInitial = "ABC Corp Headphones";
|
||||||
|
String deviceNameModified = "My Headphones";
|
||||||
|
AlertDialog dialog = startDialog(deviceNameInitial);
|
||||||
|
|
||||||
|
// Before modifying the text the "rename" button should be disabled but the cancel button
|
||||||
|
// should be enabled.
|
||||||
|
Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
|
||||||
|
Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
|
||||||
|
assertThat(negativeButton.isEnabled()).isTrue();
|
||||||
|
assertThat(positiveButton.isEnabled()).isFalse();
|
||||||
|
|
||||||
|
// Once we modify the text, the positive button should be clickable, and clicking it should
|
||||||
|
// cause a call to change the name.
|
||||||
|
EditText editText = (EditText) dialog.findViewById(R.id.edittext);
|
||||||
|
editText.setText(deviceNameModified);
|
||||||
|
assertThat(positiveButton.isEnabled()).isTrue();
|
||||||
|
positiveButton.performClick();
|
||||||
|
verify(mCachedDevice).setName(deviceNameModified);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void deviceNameEditThenCancelDoesntRename() {
|
||||||
|
String deviceNameInitial = "ABC Corp Headphones";
|
||||||
|
String deviceNameModified = "My Headphones";
|
||||||
|
AlertDialog dialog = startDialog(deviceNameInitial);
|
||||||
|
|
||||||
|
// Modifying the text but then hitting cancel should not cause the name to change.
|
||||||
|
Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
|
||||||
|
assertThat(negativeButton.isEnabled()).isTrue();
|
||||||
|
EditText editText = (EditText) dialog.findViewById(R.id.edittext);
|
||||||
|
editText.setText(deviceNameModified);
|
||||||
|
negativeButton.performClick();
|
||||||
|
verify(mCachedDevice, never()).setName(anyString());
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user