Merge pull request #1824 from TomHennen/fix1811
better device id generation
This commit is contained in:
commit
016f52fa20
@ -4,6 +4,7 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v7.app.ActionBarActivity;
|
import android.support.v7.app.ActionBarActivity;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@ -19,7 +20,6 @@ import android.widget.Spinner;
|
|||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.ViewFlipper;
|
import android.widget.ViewFlipper;
|
||||||
|
|
||||||
import java.security.SecureRandom;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
@ -204,6 +204,7 @@ public class GpodnetAuthenticationActivity extends ActionBarActivity {
|
|||||||
chooseDevice.setEnabled(true);
|
chooseDevice.setEnabled(true);
|
||||||
}
|
}
|
||||||
devices.set(gpodnetDevices);
|
devices.set(gpodnetDevices);
|
||||||
|
deviceID.setText(generateDeviceID(gpodnetDevices));
|
||||||
createNewDevice.setEnabled(true);
|
createNewDevice.setEnabled(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -224,7 +225,7 @@ public class GpodnetAuthenticationActivity extends ActionBarActivity {
|
|||||||
createNewDevice.setOnClickListener(new View.OnClickListener() {
|
createNewDevice.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
if (checkDeviceIDText(deviceID, txtvError, devices.get())) {
|
if (checkDeviceIDText(deviceID, caption, txtvError, devices.get())) {
|
||||||
final String deviceStr = deviceID.getText().toString();
|
final String deviceStr = deviceID.getText().toString();
|
||||||
final String captionStr = caption.getText().toString();
|
final String captionStr = caption.getText().toString();
|
||||||
|
|
||||||
@ -272,7 +273,6 @@ public class GpodnetAuthenticationActivity extends ActionBarActivity {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
deviceID.setText(generateDeviceID());
|
|
||||||
chooseDevice.setOnClickListener(v -> {
|
chooseDevice.setOnClickListener(v -> {
|
||||||
final int position = spinnerDevices.getSelectedItemPosition();
|
final int position = spinnerDevices.getSelectedItemPosition();
|
||||||
if (position != AdapterView.INVALID_POSITION) {
|
if (position != AdapterView.INVALID_POSITION) {
|
||||||
@ -283,31 +283,50 @@ public class GpodnetAuthenticationActivity extends ActionBarActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private String generateDeviceID() {
|
private String generateDeviceID(List<GpodnetDevice> gpodnetDevices) {
|
||||||
final int DEVICE_ID_LENGTH = 10;
|
// devices names must be of a certain form:
|
||||||
StringBuilder buffer = new StringBuilder(DEVICE_ID_LENGTH);
|
// https://gpoddernet.readthedocs.org/en/latest/api/reference/general.html#devices
|
||||||
SecureRandom random = new SecureRandom();
|
// This is more restrictive than needed, but I think it makes for more readable names.
|
||||||
for (int i = 0; i < DEVICE_ID_LENGTH; i++) {
|
String baseId = Build.MODEL.replaceAll("\\W", "");
|
||||||
buffer.append(random.nextInt(10));
|
String id = baseId;
|
||||||
|
int num = 0;
|
||||||
|
|
||||||
|
while (isDeviceWithIdInList(id, gpodnetDevices)) {
|
||||||
|
id = baseId + "_" + num;
|
||||||
|
num++;
|
||||||
}
|
}
|
||||||
return buffer.toString();
|
|
||||||
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean checkDeviceIDText(EditText deviceID, TextView txtvError, List<GpodnetDevice> devices) {
|
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, EditText caption, TextView txtvError, List<GpodnetDevice> devices) {
|
||||||
String text = deviceID.getText().toString();
|
String text = deviceID.getText().toString();
|
||||||
if (text.length() == 0) {
|
if (text.length() == 0) {
|
||||||
txtvError.setText(R.string.gpodnetauth_device_errorEmpty);
|
txtvError.setText(R.string.gpodnetauth_device_errorEmpty);
|
||||||
txtvError.setVisibility(View.VISIBLE);
|
txtvError.setVisibility(View.VISIBLE);
|
||||||
return false;
|
return false;
|
||||||
|
} else if (caption.length() == 0) {
|
||||||
|
txtvError.setText(R.string.gpodnetauth_device_caption_errorEmpty);
|
||||||
|
txtvError.setVisibility(View.VISIBLE);
|
||||||
|
return false;
|
||||||
} else {
|
} else {
|
||||||
if (devices != null) {
|
if (devices != null) {
|
||||||
for (GpodnetDevice device : devices) {
|
if (isDeviceWithIdInList(text, devices)) {
|
||||||
if (device.getId().equals(text)) {
|
txtvError.setText(R.string.gpodnetauth_device_errorAlreadyUsed);
|
||||||
txtvError.setText(R.string.gpodnetauth_device_errorAlreadyUsed);
|
txtvError.setVisibility(View.VISIBLE);
|
||||||
txtvError.setVisibility(View.VISIBLE);
|
return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
txtvError.setVisibility(View.GONE);
|
txtvError.setVisibility(View.GONE);
|
||||||
return true;
|
return true;
|
||||||
|
@ -488,6 +488,7 @@
|
|||||||
<string name="gpodnetauth_device_chooseExistingDevice">Choose existing device:</string>
|
<string name="gpodnetauth_device_chooseExistingDevice">Choose existing device:</string>
|
||||||
<string name="gpodnetauth_device_errorEmpty">Device ID must not be empty</string>
|
<string name="gpodnetauth_device_errorEmpty">Device ID must not be empty</string>
|
||||||
<string name="gpodnetauth_device_errorAlreadyUsed">Device ID already in use</string>
|
<string name="gpodnetauth_device_errorAlreadyUsed">Device ID already in use</string>
|
||||||
|
<string name="gpodnetauth_device_caption_errorEmpty">Caption must not be empty</string>
|
||||||
|
|
||||||
<string name="gpodnetauth_device_butChoose">Choose</string>
|
<string name="gpodnetauth_device_butChoose">Choose</string>
|
||||||
<string name="gpodnetauth_finish_title">Login successful!</string>
|
<string name="gpodnetauth_finish_title">Login successful!</string>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user