better device id generation

This commit is contained in:
Tom Hennen 2016-03-25 09:38:28 -04:00
parent a2c1d6f9f7
commit e413d85c21

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
@ -19,7 +20,6 @@ import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ViewFlipper;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
@ -204,6 +204,7 @@ public class GpodnetAuthenticationActivity extends ActionBarActivity {
chooseDevice.setEnabled(true);
}
devices.set(gpodnetDevices);
deviceID.setText(generateDeviceID(gpodnetDevices));
createNewDevice.setEnabled(true);
}
}
@ -272,7 +273,6 @@ public class GpodnetAuthenticationActivity extends ActionBarActivity {
}
});
deviceID.setText(generateDeviceID());
chooseDevice.setOnClickListener(v -> {
final int position = spinnerDevices.getSelectedItemPosition();
if (position != AdapterView.INVALID_POSITION) {
@ -283,15 +283,32 @@ public class GpodnetAuthenticationActivity extends ActionBarActivity {
}
private String generateDeviceID() {
final int DEVICE_ID_LENGTH = 10;
StringBuilder buffer = new StringBuilder(DEVICE_ID_LENGTH);
SecureRandom random = new SecureRandom();
for (int i = 0; i < DEVICE_ID_LENGTH; i++) {
buffer.append(random.nextInt(10));
private String generateDeviceID(List<GpodnetDevice> gpodnetDevices) {
// devices names must be of a certain form:
// https://gpoddernet.readthedocs.org/en/latest/api/reference/general.html#devices
// This is more restrictive than needed, but I think it makes for more readable names.
String baseId = Build.MODEL.replaceAll("\\W", "");
String id = baseId;
int num = 0;
while (isDeviceWithIdInList(id, gpodnetDevices)) {
id = baseId + "_" + num;
num++;
}
return buffer.toString();
return id;
}
private boolean isDeviceWithIdInList(String id, List<GpodnetDevice> gpodnetDevices) {
if (gpodnetDevices == null) {
return false;
}
for (GpodnetDevice device : gpodnetDevices) {
if (device.getId().equals(id)) {
return true;
}
}
return false;
}
private boolean checkDeviceIDText(EditText deviceID, TextView txtvError, List<GpodnetDevice> devices) {