diff options
author | Tom Hennen <tom.hennen@gmail.com> | 2016-03-25 09:38:28 -0400 |
---|---|---|
committer | Tom Hennen <tom.hennen@gmail.com> | 2016-03-25 09:38:28 -0400 |
commit | e413d85c210ad5435381ebea678b914f69b59d3e (patch) | |
tree | 3d925ab21fd00f86375b2d46aa7831f1013ac4e5 /app/src/main/java/de/danoeh/antennapod/activity | |
parent | a2c1d6f9f7cab759f37d1d180170e5e1624865a5 (diff) | |
download | AntennaPod-e413d85c210ad5435381ebea678b914f69b59d3e.zip |
better device id generation
Diffstat (limited to 'app/src/main/java/de/danoeh/antennapod/activity')
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/activity/gpoddernet/GpodnetAuthenticationActivity.java | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/activity/gpoddernet/GpodnetAuthenticationActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/gpoddernet/GpodnetAuthenticationActivity.java index e5418c924..7ca73fa92 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/gpoddernet/GpodnetAuthenticationActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/gpoddernet/GpodnetAuthenticationActivity.java @@ -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) { |