From db95a4bc681797ef4c952950c76fc797044473d5 Mon Sep 17 00:00:00 2001 From: Kelson Warner Date: Thu, 8 Oct 2020 12:33:58 -0700 Subject: [PATCH] AMP-30334 add base 36 generator --- .../amplitude/identitymanager/Identity.java | 18 +++++--------- .../identitymanager/IdentityManager.java | 2 +- .../identitymanager/IdentityUtils.java | 24 +++++++++++++++++++ 3 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/amplitude/identitymanager/IdentityUtils.java diff --git a/src/main/java/com/amplitude/identitymanager/Identity.java b/src/main/java/com/amplitude/identitymanager/Identity.java index 82d74706..d4e229ee 100644 --- a/src/main/java/com/amplitude/identitymanager/Identity.java +++ b/src/main/java/com/amplitude/identitymanager/Identity.java @@ -1,26 +1,20 @@ package com.amplitude.identitymanager; -import android.util.Log; - public class Identity { private String _userId; private String _deviceId; - private void logIdentityWarning(String message) { - Log.w(Identity.class.getName(), message); - } - public String initializeDeviceId(String deviceId) { - if (deviceId == null) { + if (_deviceId == null) { String deviceIdToUse = ""; if (deviceId.length() > 0) { deviceIdToUse = deviceId; } else { - deviceIdToUse = "generateBase36Id"; + deviceIdToUse = IdentityUtils.generateBase36Id(); } _deviceId = deviceIdToUse; } else { - logIdentityWarning("Cannot set device ID twice for same identity. Skipping operation."); + IdentityUtils.logIdentityWarning("Cannot set device ID twice for same identity. Skipping operation."); } return deviceId; @@ -28,9 +22,9 @@ public String initializeDeviceId(String deviceId) { public String initializeDeviceId() { if (_deviceId == null) { - _deviceId = "generateBase36Id"; + _deviceId = IdentityUtils.generateBase36Id(); } else { - logIdentityWarning("Cannot set device ID twice for same identity. Skipping operation."); + IdentityUtils.logIdentityWarning("Cannot set device ID twice for same identity. Skipping operation."); } return _deviceId; @@ -38,7 +32,7 @@ public String initializeDeviceId() { public String getDeviceId() { if (_deviceId == null) { - logIdentityWarning("Did not detect device ID; generating one for this instance."); + IdentityUtils.logIdentityWarning("Did not detect device ID; generating one for this instance."); return initializeDeviceId(); } else { return _deviceId; diff --git a/src/main/java/com/amplitude/identitymanager/IdentityManager.java b/src/main/java/com/amplitude/identitymanager/IdentityManager.java index 8edd31bc..c5225d9d 100644 --- a/src/main/java/com/amplitude/identitymanager/IdentityManager.java +++ b/src/main/java/com/amplitude/identitymanager/IdentityManager.java @@ -26,4 +26,4 @@ public Identity getInstance() { return identityToSet; } } -} \ No newline at end of file +} diff --git a/src/main/java/com/amplitude/identitymanager/IdentityUtils.java b/src/main/java/com/amplitude/identitymanager/IdentityUtils.java new file mode 100644 index 00000000..0351f6a0 --- /dev/null +++ b/src/main/java/com/amplitude/identitymanager/IdentityUtils.java @@ -0,0 +1,24 @@ +package com.amplitude.identitymanager; + +import android.util.Log; + +public class IdentityUtils { + static private final int device_id_length = 25; + static private final String base_36_char_set = "abcdefghijklmnopqrstuvwxyz0123456789"; + static private final int base_36_radix = 36; + + static void logIdentityWarning(String message) { + Log.w(Identity.class.getName(), message); + } + + static String generateBase36Id() { + String stringBuilder = ""; + for (int idx = 0; idx < device_id_length; idx++) { + double randomIdx = Math.floor(Math.random() * base_36_radix); + char nextChar = base_36_char_set.charAt((int)randomIdx); + stringBuilder += nextChar; + } + + return stringBuilder; + } +}