Try hard to display a unique device name in ControlCenter

(Makes it a little easier to deal with muliple "MI" devices, for example)
here
cpfeiffer 2015-12-28 00:16:00 +01:00
parent e3d0c63676
commit 9ea2977143
2 changed files with 39 additions and 1 deletions

View File

@ -47,7 +47,7 @@ public class GBDeviceAdapter extends ArrayAdapter<GBDevice> {
ImageView deviceImageView = (ImageView) view.findViewById(R.id.device_image);
ProgressBar busyIndicator = (ProgressBar) view.findViewById(R.id.device_busy_indicator);
deviceNameLabel.setText(device.getName());
deviceNameLabel.setText(getUniqueDeviceName(device));
deviceInfoLabel.setText(device.getInfoString());
if (device.isBusy()) {
@ -93,4 +93,25 @@ public class GBDeviceAdapter extends ArrayAdapter<GBDevice> {
return view;
}
private String getUniqueDeviceName(GBDevice device) {
String deviceName = device.getName();
if (!isUniqueDeviceName(device, deviceName)) {
deviceName = deviceName + " " + device.getShortAddress();
}
return deviceName;
}
private boolean isUniqueDeviceName(GBDevice device, String deviceName) {
for (int i = 0; i < getCount(); i++) {
GBDevice item = getItem(i);
if (item == device) {
continue;
}
if (deviceName.equals(item.getName())) {
return false;
}
}
return true;
}
}

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v4.content.LocalBroadcastManager;
import org.slf4j.Logger;
@ -309,6 +310,22 @@ public class GBDevice implements Parcelable {
return "Device " + getName() + ", " + getAddress() + ", " + getStateString();
}
/**
* Returns a shortened form of the device's address, in order to form a
* unique name in companion with #getName().
*/
@NonNull
public String getShortAddress() {
String address = getAddress();
if (address != null) {
if (address.length() > 5) {
return address.substring(address.length() - 5);
}
return address;
}
return "";
}
public enum State {
// Note: the order is important!
NOT_CONNECTED,