From 6e45b114718d636e71034e74bbac92e768ce063b Mon Sep 17 00:00:00 2001 From: Kelson Warner Date: Thu, 8 Oct 2020 12:09:05 -0700 Subject: [PATCH 1/5] AMP-30334 add initial classes and functionality --- .../amplitude/identitymanager/Identity.java | 59 +++++++++++++++++++ .../identitymanager/IdentityManager.java | 29 +++++++++ 2 files changed, 88 insertions(+) create mode 100644 src/main/java/com/amplitude/identitymanager/Identity.java create mode 100644 src/main/java/com/amplitude/identitymanager/IdentityManager.java diff --git a/src/main/java/com/amplitude/identitymanager/Identity.java b/src/main/java/com/amplitude/identitymanager/Identity.java new file mode 100644 index 00000000..cb8f820f --- /dev/null +++ b/src/main/java/com/amplitude/identitymanager/Identity.java @@ -0,0 +1,59 @@ +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) { + String deviceIdToUse = ""; + if (deviceId.length() > 0) { + deviceIdToUse = deviceId; + } else { + deviceIdToUse = "generateBase36Id"; + } + _deviceId = deviceIdToUse; + } else { + logIdentityWarning("Cannot set device ID twice for same identity. Skipping operation."); + } + + return deviceId; + } + + public String initializeDeviceId() { + if (_deviceId == null) { + _deviceId = "generateBase36Id"; + } else { + logIdentityWarning("Cannot set device ID twice for same identity. Skipping operation."); + } + + return _deviceId; + } + + public String getDeviceId() { + if (_deviceId == null) { + logIdentityWarning("Did not detect device ID; generating one for this instance."); + return initializeDeviceId(); + } else { + return _deviceId; + } + } + + public void setUserId(String userId) { + _userId = userId; + } + + public void setUserId(Integer userId) { + _userId = userId.toString(); + } + + public String getUserId() { + return _userId; + } +} diff --git a/src/main/java/com/amplitude/identitymanager/IdentityManager.java b/src/main/java/com/amplitude/identitymanager/IdentityManager.java new file mode 100644 index 00000000..8edd31bc --- /dev/null +++ b/src/main/java/com/amplitude/identitymanager/IdentityManager.java @@ -0,0 +1,29 @@ +package com.amplitude.identitymanager; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class IdentityManager { + static final Map instanceMap = new ConcurrentHashMap(); + private final String default_instance = "$default_instance"; + + public Identity getInstance(String instanceName) { + if (instanceMap.containsKey(instanceName)) { + return instanceMap.get(instanceName); + } else { + Identity identityToSet = new Identity(); + instanceMap.put(instanceName, identityToSet); + return identityToSet; + } + } + + public Identity getInstance() { + if (instanceMap.containsKey(default_instance)) { + return instanceMap.get(default_instance); + } else { + Identity identityToSet = new Identity(); + instanceMap.put(default_instance, identityToSet); + return identityToSet; + } + } +} \ No newline at end of file From 825f34f9e1d001f658737642e2530852f65e32a6 Mon Sep 17 00:00:00 2001 From: Kelson Warner Date: Thu, 8 Oct 2020 12:15:17 -0700 Subject: [PATCH 2/5] AMP-30334 add skeleton for addIdentityChangedListener and useAdvertisingIdForDeviceId --- src/main/java/com/amplitude/identitymanager/Identity.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/amplitude/identitymanager/Identity.java b/src/main/java/com/amplitude/identitymanager/Identity.java index cb8f820f..82d74706 100644 --- a/src/main/java/com/amplitude/identitymanager/Identity.java +++ b/src/main/java/com/amplitude/identitymanager/Identity.java @@ -56,4 +56,8 @@ public void setUserId(Integer userId) { public String getUserId() { return _userId; } + + public void addIdentityChangedListener(){} + + public void useAdvertisingIdForDeviceId() {} } From db95a4bc681797ef4c952950c76fc797044473d5 Mon Sep 17 00:00:00 2001 From: Kelson Warner Date: Thu, 8 Oct 2020 12:33:58 -0700 Subject: [PATCH 3/5] 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; + } +} From 5afa93c344ec3cd1d76cb72f819991bb5dc810aa Mon Sep 17 00:00:00 2001 From: Kelson Warner Date: Thu, 8 Oct 2020 17:39:35 -0700 Subject: [PATCH 4/5] AMP-30334 add mock advertising id methods --- .../java/com/amplitude/api/AmplitudeLog.java | 6 +- .../amplitude/identitymanager/Identity.java | 89 ++++++++++++------- .../identitymanager/IdentityDeviceInfo.java | 27 ++++++ .../identitymanager/IdentityManager.java | 10 +-- .../identitymanager/IdentityUtils.java | 11 +-- .../identitymanager/IdentityUtilsTest.java | 4 + 6 files changed, 98 insertions(+), 49 deletions(-) create mode 100644 src/main/java/com/amplitude/identitymanager/IdentityDeviceInfo.java create mode 100644 src/test/java/com/amplitude/identitymanager/IdentityUtilsTest.java diff --git a/src/main/java/com/amplitude/api/AmplitudeLog.java b/src/main/java/com/amplitude/api/AmplitudeLog.java index 52c215a4..2db1b4ef 100644 --- a/src/main/java/com/amplitude/api/AmplitudeLog.java +++ b/src/main/java/com/amplitude/api/AmplitudeLog.java @@ -76,17 +76,17 @@ int v(String tag, String msg, Throwable tr) { return 0; } - int w(String tag, String msg) { + public int w(String tag, String msg) { if (enableLogging && logLevel <= Log.WARN) return Log.w(tag, msg); return 0; } - int w(String tag, Throwable tr) { + public int w(String tag, Throwable tr) { if (enableLogging && logLevel <= Log.WARN) return Log.w(tag, tr); return 0; } - int w(String tag, String msg, Throwable tr) { + public int w(String tag, String msg, Throwable tr) { if (enableLogging && logLevel <= Log.WARN) return Log.w(tag, msg, tr); return 0; } diff --git a/src/main/java/com/amplitude/identitymanager/Identity.java b/src/main/java/com/amplitude/identitymanager/Identity.java index d4e229ee..ac3e2121 100644 --- a/src/main/java/com/amplitude/identitymanager/Identity.java +++ b/src/main/java/com/amplitude/identitymanager/Identity.java @@ -1,57 +1,84 @@ package com.amplitude.identitymanager; +import com.amplitude.api.AmplitudeLog; + public class Identity { - private String _userId; - private String _deviceId; + private String userId; + private String deviceId; + private boolean shouldUseAdvertisingIdForDeviceId = false; + private IdentityListener listener; + + public interface IdentityListener { + void onUserIdChanged(String userId); + void onDeviceIdChanged(String deviceId); + } + + public Identity() { + this(false, null); + } + + public Identity(boolean useAdvertisingId) { + this(useAdvertisingId, null); + } + + public Identity(boolean useAdvertisingId, String userId) { + this.userId = userId; + this.shouldUseAdvertisingIdForDeviceId = useAdvertisingId; + + initializeDeviceId(); + } public String initializeDeviceId(String deviceId) { - if (_deviceId == null) { + if (this.deviceId == null) { String deviceIdToUse = ""; - if (deviceId.length() > 0) { - deviceIdToUse = deviceId; + if (shouldUseAdvertisingIdForDeviceId) { + deviceIdToUse = IdentityDeviceInfo.getAdvertisingId(); } else { - deviceIdToUse = IdentityUtils.generateBase36Id(); + if (deviceId.length() > 0) { + deviceIdToUse = deviceId; + } else { + deviceIdToUse = IdentityUtils.generateBase36Id(); + } } - _deviceId = deviceIdToUse; + this.deviceId = deviceIdToUse; } else { - IdentityUtils.logIdentityWarning("Cannot set device ID twice for same identity. Skipping operation."); + AmplitudeLog.getLogger().w(Identity.class.getName(), "Cannot set device ID twice for same identity. Skipping operation."); } - - return deviceId; + return this.deviceId; } public String initializeDeviceId() { - if (_deviceId == null) { - _deviceId = IdentityUtils.generateBase36Id(); - } else { - IdentityUtils.logIdentityWarning("Cannot set device ID twice for same identity. Skipping operation."); - } - - return _deviceId; + return initializeDeviceId(null); } public String getDeviceId() { - if (_deviceId == null) { - IdentityUtils.logIdentityWarning("Did not detect device ID; generating one for this instance."); - return initializeDeviceId(); - } else { - return _deviceId; - } + return deviceId; } - public void setUserId(String userId) { - _userId = userId; + // Should this exist? Otherwise, when would we call the onDeviceIdChanged listener? + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + if (listener != null) { + listener.onDeviceIdChanged(deviceId); + } } - public void setUserId(Integer userId) { - _userId = userId.toString(); + public String getUserId() { + return userId; } - public String getUserId() { - return _userId; + public void setUserId(String userId) { + this.userId = userId; + if (listener != null) { + listener.onUserIdChanged(userId); + } } - public void addIdentityChangedListener(){} + public void addIdentityChangedListener(IdentityListener listener){ + this.listener = listener; + } - public void useAdvertisingIdForDeviceId() {} + public void useAdvertisingIdForDeviceId() { + shouldUseAdvertisingIdForDeviceId = true; + } } diff --git a/src/main/java/com/amplitude/identitymanager/IdentityDeviceInfo.java b/src/main/java/com/amplitude/identitymanager/IdentityDeviceInfo.java new file mode 100644 index 00000000..bf3b6b6d --- /dev/null +++ b/src/main/java/com/amplitude/identitymanager/IdentityDeviceInfo.java @@ -0,0 +1,27 @@ +package com.amplitude.identitymanager; + +import android.os.Build; + +public class IdentityDeviceInfo { + public static String getAdvertisingId() { + // This should not be called on the main thread. + if ("Amazon".equals(getManufacturer())) { + return getAndCacheAmazonAdvertisingId(); + } else { + return getAndCacheGoogleAdvertisingId(); + } + } + + private static String getManufacturer() { + return Build.MANUFACTURER; + } + + private static String getAndCacheAmazonAdvertisingId() { + return "amazonadvertisingid"; + } + + private static String getAndCacheGoogleAdvertisingId() { + return "advertisingid"; + } +} + diff --git a/src/main/java/com/amplitude/identitymanager/IdentityManager.java b/src/main/java/com/amplitude/identitymanager/IdentityManager.java index c5225d9d..3125d78e 100644 --- a/src/main/java/com/amplitude/identitymanager/IdentityManager.java +++ b/src/main/java/com/amplitude/identitymanager/IdentityManager.java @@ -5,7 +5,7 @@ public class IdentityManager { static final Map instanceMap = new ConcurrentHashMap(); - private final String default_instance = "$default_instance"; + private final static String DEFAULT_INSTANCE = "$default_instance"; public Identity getInstance(String instanceName) { if (instanceMap.containsKey(instanceName)) { @@ -18,12 +18,6 @@ public Identity getInstance(String instanceName) { } public Identity getInstance() { - if (instanceMap.containsKey(default_instance)) { - return instanceMap.get(default_instance); - } else { - Identity identityToSet = new Identity(); - instanceMap.put(default_instance, identityToSet); - return identityToSet; - } + return getInstance(DEFAULT_INSTANCE); } } diff --git a/src/main/java/com/amplitude/identitymanager/IdentityUtils.java b/src/main/java/com/amplitude/identitymanager/IdentityUtils.java index 0351f6a0..92457789 100644 --- a/src/main/java/com/amplitude/identitymanager/IdentityUtils.java +++ b/src/main/java/com/amplitude/identitymanager/IdentityUtils.java @@ -1,21 +1,18 @@ package com.amplitude.identitymanager; -import android.util.Log; +import java.util.Random; 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 = ""; + Random rand = new Random(); 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); + int randomIdx = rand.nextInt(base_36_radix); + char nextChar = base_36_char_set.charAt(randomIdx); stringBuilder += nextChar; } diff --git a/src/test/java/com/amplitude/identitymanager/IdentityUtilsTest.java b/src/test/java/com/amplitude/identitymanager/IdentityUtilsTest.java new file mode 100644 index 00000000..a119f34b --- /dev/null +++ b/src/test/java/com/amplitude/identitymanager/IdentityUtilsTest.java @@ -0,0 +1,4 @@ +package com.amplitude.identitymanager; + +public class IdentityUtilsTest { +} From 91f9a09b0936792cbb51461ed36751633081c28d Mon Sep 17 00:00:00 2001 From: Kelson Warner Date: Thu, 8 Oct 2020 17:59:41 -0700 Subject: [PATCH 5/5] AMP-30334 delete test file for now --- .../java/com/amplitude/identitymanager/IdentityUtilsTest.java | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 src/test/java/com/amplitude/identitymanager/IdentityUtilsTest.java diff --git a/src/test/java/com/amplitude/identitymanager/IdentityUtilsTest.java b/src/test/java/com/amplitude/identitymanager/IdentityUtilsTest.java deleted file mode 100644 index a119f34b..00000000 --- a/src/test/java/com/amplitude/identitymanager/IdentityUtilsTest.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.amplitude.identitymanager; - -public class IdentityUtilsTest { -}