From b58c3656440ffe27716e990484dbbc56d5844b03 Mon Sep 17 00:00:00 2001 From: ArtursK Date: Wed, 21 Oct 2020 21:00:54 +0300 Subject: [PATCH] Reworked OpenUDID so that it is not a service anymore. Removed random UDID, made it the fallback for openUDID. --- app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 6 - .../java/ly/count/android/sdk/Countly.java | 2 - .../java/ly/count/android/sdk/DeviceId.java | 32 +-- .../ly/count/android/sdk/OpenUDIDAdapter.java | 101 +++++---- .../java/org/openudid/OpenUDID_manager.java | 207 ------------------ .../java/org/openudid/OpenUDID_service.java | 36 --- 7 files changed, 59 insertions(+), 327 deletions(-) delete mode 100644 sdk/src/main/java/org/openudid/OpenUDID_manager.java delete mode 100644 sdk/src/main/java/org/openudid/OpenUDID_service.java diff --git a/app/build.gradle b/app/build.gradle index 31aace798..ed9a2d3ef 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -72,7 +72,7 @@ dependencies { implementation 'com.huawei.hms:push:4.0.3.301' - //implementation 'ly.count.android:sdk:20.04.2' + //implementation 'ly.count.android:sdk:20.04.5' } apply plugin: 'com.google.gms.google-services' \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e07dad8ae..f30ccea89 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -130,12 +130,6 @@ - - - - - - diff --git a/sdk/src/main/java/ly/count/android/sdk/Countly.java b/sdk/src/main/java/ly/count/android/sdk/Countly.java index a933e9da7..6c7684d71 100644 --- a/sdk/src/main/java/ly/count/android/sdk/Countly.java +++ b/sdk/src/main/java/ly/count/android/sdk/Countly.java @@ -439,8 +439,6 @@ public synchronized Countly init(CountlyConfig config) { config.idMode = DeviceId.Type.OPEN_UDID; } else if (AdvertisingIdAdapter.isAdvertisingIdAvailable()) { config.idMode = DeviceId.Type.ADVERTISING_ID; - } else { - config.idMode = DeviceId.Type.RANDOM_UDID;//use this as fallback } } if (config.deviceID == null && config.idMode == DeviceId.Type.OPEN_UDID && !OpenUDIDAdapter.isOpenUDIDAvailable()) { diff --git a/sdk/src/main/java/ly/count/android/sdk/DeviceId.java b/sdk/src/main/java/ly/count/android/sdk/DeviceId.java index 10e2be98e..68d02a2be 100644 --- a/sdk/src/main/java/ly/count/android/sdk/DeviceId.java +++ b/sdk/src/main/java/ly/count/android/sdk/DeviceId.java @@ -18,7 +18,6 @@ public enum Type { OPEN_UDID,//OPEN_UDID generated UDID ADVERTISING_ID,//id provided by the android OS TEMPORARY_ID,//temporary device ID mode - RANDOM_UDID//fallback random UDID } private static final String TAG = "DeviceId"; @@ -118,10 +117,7 @@ protected void init(Context context, CountlyStore store, boolean raiseExceptions OpenUDIDAdapter.sync(context); } } else { - if (Countly.sharedInstance().isLoggingEnabled()) { - Log.w(TAG, "[DeviceId] Advertising ID is not available, switching to a random UDID"); - } - GenerateRandomUDID(store); + if (raiseExceptions) throw new IllegalStateException("OpenUDID is not available, please make sure that it is configured correctly"); } break; case ADVERTISING_ID: @@ -139,34 +135,16 @@ protected void init(Context context, CountlyStore store, boolean raiseExceptions OpenUDIDAdapter.sync(context); } } else { - // use random ID, otherwise without Advertising ID and OpenUDID this user is lost for Countly + // just do nothing, without Advertising ID and OpenUDID this user is lost for Countly if (Countly.sharedInstance().isLoggingEnabled()) { - Log.w(TAG, "[DeviceId] Advertising ID is not available, neither OpenUDID is, switching to a random UDID"); + Log.e(TAG, "[DeviceId] Advertising ID is not available, neither OpenUDID is"); } - GenerateRandomUDID(store); - } - break; - case RANDOM_UDID: - if(id == null) { - GenerateRandomUDID(store); + if (raiseExceptions) throw new IllegalStateException("OpenUDID is not available, please make sure that it is configured correctly"); } break; } } - void GenerateRandomUDID(CountlyStore store){ - if (Countly.sharedInstance().isLoggingEnabled()) { - Log.w(TAG, "[DeviceId] Generating new random UDID"); - } - - //generated new ID and clear override - String generatedID = UUID.randomUUID().toString(); - setId(Type.RANDOM_UDID, generatedID); - - store.setPreference(PREFERENCE_KEY_ID_ID, id); - store.setPreference(PREFERENCE_KEY_ID_TYPE, type.toString()); - } - private void storeOverriddenType(CountlyStore store, Type type) { // Using strings is safer when it comes to extending Enum values list store.setPreference(PREFERENCE_KEY_ID_TYPE, type == null ? null : type.toString()); @@ -189,8 +167,6 @@ private Type retrieveType(CountlyStore store, String preferenceName) { return Type.ADVERTISING_ID; } else if (typeString.equals(Type.TEMPORARY_ID.toString())) { return Type.TEMPORARY_ID; - } else if (typeString.equals(Type.RANDOM_UDID.toString())) { - return Type.RANDOM_UDID; } else { return null; } diff --git a/sdk/src/main/java/ly/count/android/sdk/OpenUDIDAdapter.java b/sdk/src/main/java/ly/count/android/sdk/OpenUDIDAdapter.java index c28d756b7..6c945af4d 100644 --- a/sdk/src/main/java/ly/count/android/sdk/OpenUDIDAdapter.java +++ b/sdk/src/main/java/ly/count/android/sdk/OpenUDIDAdapter.java @@ -1,67 +1,74 @@ package ly.count.android.sdk; +import android.annotation.SuppressLint; import android.content.Context; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; +import android.content.SharedPreferences; +import android.provider.Settings; +import android.util.Log; +import java.math.BigInteger; +import java.security.SecureRandom; +import java.util.UUID; public class OpenUDIDAdapter { - private final static String OPEN_UDID_MANAGER_CLASS_NAME = "org.openudid.OpenUDID_manager"; + public final static String PREF_KEY = "openudid"; + public final static String PREFS_NAME = "openudid_prefs"; + public final static String TAG = "OpenUDID"; + + private final static boolean LOG = true; //Display or not debug message + + private static String OpenUDID = null; + private static boolean mInitialized = false; public static boolean isOpenUDIDAvailable() { - boolean openUDIDAvailable = false; - try { - Class.forName(OPEN_UDID_MANAGER_CLASS_NAME); - openUDIDAvailable = true; - } catch (ClassNotFoundException ignored) { - } - return openUDIDAvailable; + return true; } - @SuppressWarnings("BooleanMethodIsAlwaysInverted") public static boolean isInitialized() { - boolean initialized = false; - try { - final Class cls = Class.forName(OPEN_UDID_MANAGER_CLASS_NAME); - final Method isInitializedMethod = cls.getMethod("isInitialized", (Class[]) null); - final Object result = isInitializedMethod.invoke(null, (Object[]) null); - if (result instanceof Boolean) { - initialized = (Boolean) result; - } - } catch (ClassNotFoundException ignored) { - } catch (NoSuchMethodException ignored) { - } catch (InvocationTargetException ignored) { - } catch (IllegalAccessException ignored) { - } - return initialized; + return mInitialized; } public static void sync(final Context context) { - try { - final Class cls = Class.forName(OPEN_UDID_MANAGER_CLASS_NAME); - final Method syncMethod = cls.getMethod("sync", Context.class); - syncMethod.invoke(null, context); - } catch (ClassNotFoundException ignored) { - } catch (NoSuchMethodException ignored) { - } catch (InvocationTargetException ignored) { - } catch (IllegalAccessException ignored) { + SharedPreferences mPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); + //Try to get the openudid from local preferences + OpenUDID = mPreferences.getString(PREF_KEY, null); + if (OpenUDID == null) //Not found + { + generateOpenUDID(context); + + if (LOG) Log.d(TAG, "OpenUDID: " + OpenUDID); + + storeOpenUDID(context);//Store it locally + mInitialized = true; + + } else {//Got it, you can now call getOpenUDID() + if (LOG) Log.d(TAG, "OpenUDID: " + OpenUDID); + mInitialized = true; } } - public static String getOpenUDID() { - String openUDID = null; - try { - final Class cls = Class.forName(OPEN_UDID_MANAGER_CLASS_NAME); - final Method getOpenUDIDMethod = cls.getMethod("getOpenUDID", (Class[]) null); - final Object result = getOpenUDIDMethod.invoke(null, (Object[]) null); - if (result instanceof String) { - openUDID = (String) result; - } - } catch (ClassNotFoundException ignored) { - } catch (NoSuchMethodException ignored) { - } catch (InvocationTargetException ignored) { - } catch (IllegalAccessException ignored) { + /* + * Generate a new OpenUDID + */ + @SuppressLint("HardwareIds") + private static void generateOpenUDID(Context context) { + if (LOG) Log.d(TAG, "Generating openUDID"); + //Try to get the ANDROID_ID + OpenUDID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); + if (OpenUDID == null || OpenUDID.equals("9774d56d682e549c") || OpenUDID.length() < 15) { + //if ANDROID_ID is null, or it's equals to the GalaxyTab generic ANDROID_ID or bad, generates a new one + OpenUDID = UUID.randomUUID().toString(); } - return openUDID; + } + + public static String getOpenUDID() { + return OpenUDID; + } + + private static void storeOpenUDID(Context context) { + SharedPreferences mPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); + final SharedPreferences.Editor e = mPreferences.edit(); + e.putString(PREF_KEY, OpenUDID); + e.apply(); } } diff --git a/sdk/src/main/java/org/openudid/OpenUDID_manager.java b/sdk/src/main/java/org/openudid/OpenUDID_manager.java deleted file mode 100644 index 4313ce763..000000000 --- a/sdk/src/main/java/org/openudid/OpenUDID_manager.java +++ /dev/null @@ -1,207 +0,0 @@ -package org.openudid; - -import android.annotation.SuppressLint; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; -import android.content.ServiceConnection; -import android.content.SharedPreferences; -import android.content.SharedPreferences.Editor; -import android.content.pm.ResolveInfo; -import android.content.pm.ServiceInfo; -import android.os.IBinder; -import android.os.RemoteException; -import android.provider.Settings.Secure; -import android.util.Log; - -import java.math.BigInteger; -import java.security.SecureRandom; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.TreeMap; - -public class OpenUDID_manager implements ServiceConnection { - public final static String PREF_KEY = "openudid"; - public final static String PREFS_NAME = "openudid_prefs"; - public final static String TAG = "OpenUDID"; - - private final static boolean LOG = true; //Display or not debug message - - private final Context mContext; //Application context - private List mMatchingIntents; //List of available OpenUDID Intents - private final Map mReceivedOpenUDIDs; //Map of OpenUDIDs found so far - - private final SharedPreferences mPreferences; //Preferences to store the OpenUDID - private final Random mRandom; - - private OpenUDID_manager(Context context) { - mPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); - mContext = context; - mRandom = new Random(); - mReceivedOpenUDIDs = new HashMap<>(); - } - - @Override - public void onServiceConnected(ComponentName className, IBinder service) { - //Get the OpenUDID from the remote service - try { - //Send a random number to the service - android.os.Parcel data = android.os.Parcel.obtain(); - data.writeInt(mRandom.nextInt()); - android.os.Parcel reply = android.os.Parcel.obtain(); - service.transact(1, android.os.Parcel.obtain(), reply, 0); - if (data.readInt() == reply.readInt()) //Check if the service returns us this number - { - final String _openUDID = reply.readString(); - if (_openUDID != null) { //if valid OpenUDID, save it - if (LOG) Log.d(TAG, "Received " + _openUDID); - - if (mReceivedOpenUDIDs.containsKey(_openUDID)) { - mReceivedOpenUDIDs.put(_openUDID, mReceivedOpenUDIDs.get(_openUDID) + 1); - } else { - mReceivedOpenUDIDs.put(_openUDID, 1); - } - } - } - } catch (RemoteException e) { - if (LOG) Log.e(TAG, "RemoteException: " + e.getMessage()); - } - mContext.unbindService(this); - - startService(); //Try the next one - } - - @Override - public void onServiceDisconnected(ComponentName className) { - } - - private void storeOpenUDID() { - final Editor e = mPreferences.edit(); - e.putString(PREF_KEY, OpenUDID); - e.apply(); - } - - /* - * Generate a new OpenUDID - */ - @SuppressLint("HardwareIds") - private void generateOpenUDID() { - if (LOG) Log.d(TAG, "Generating openUDID"); - //Try to get the ANDROID_ID - OpenUDID = Secure.getString(mContext.getContentResolver(), Secure.ANDROID_ID); - if (OpenUDID == null || OpenUDID.equals("9774d56d682e549c") || OpenUDID.length() < 15) { - //if ANDROID_ID is null, or it's equals to the GalaxyTab generic ANDROID_ID or bad, generates a new one - final SecureRandom random = new SecureRandom(); - OpenUDID = new BigInteger(64, random).toString(16); - } - } - - /* - * Start the oldest service - */ - private void startService() { - if (mMatchingIntents.size() > 0) { //There are some Intents untested - if (LOG) Log.d(TAG, "Trying service " + mMatchingIntents.get(0).loadLabel(mContext.getPackageManager())); - - final ServiceInfo servInfo = mMatchingIntents.get(0).serviceInfo; - final Intent i = new Intent(); - i.setComponent(new ComponentName(servInfo.applicationInfo.packageName, servInfo.name)); - mMatchingIntents.remove(0); - try { // try added by Lionscribe - mContext.bindService(i, this, Context.BIND_AUTO_CREATE); - } catch (SecurityException e) { - startService(); // ignore this one, and start next one - } - } else { //No more service to test - - getMostFrequentOpenUDID(); //Choose the most frequent - - if (OpenUDID == null) //No OpenUDID was chosen, generate one - { - generateOpenUDID(); - } - if (LOG) Log.d(TAG, "OpenUDID: " + OpenUDID); - - storeOpenUDID();//Store it locally - mInitialized = true; - } - } - - private void getMostFrequentOpenUDID() { - if (!mReceivedOpenUDIDs.isEmpty()) { - final TreeMap sorted_OpenUDIDS = new TreeMap(new ValueComparator()); - sorted_OpenUDIDS.putAll(mReceivedOpenUDIDs); - - OpenUDID = sorted_OpenUDIDS.firstKey(); - } - } - - private static String OpenUDID = null; - private static boolean mInitialized = false; - - /** - * The Method to call to get OpenUDID - * - * @return the OpenUDID - */ - public static String getOpenUDID() { - if (!mInitialized) Log.e("OpenUDID", "Initialisation isn't done"); - return OpenUDID; - } - - /** - * The Method to call to get OpenUDID - * - * @return the OpenUDID - */ - public static boolean isInitialized() { - return mInitialized; - } - - /** - * The Method the call at the init of your app - * - * @param context you current context - */ - public static void sync(Context context) { - //Initialise the Manager - OpenUDID_manager manager = new OpenUDID_manager(context); - - //Try to get the openudid from local preferences - OpenUDID = manager.mPreferences.getString(PREF_KEY, null); - if (OpenUDID == null) //Not found - { - //Get the list of all OpenUDID services available (including itself) - manager.mMatchingIntents = context.getPackageManager().queryIntentServices(new Intent("org.OpenUDID.GETUDID"), 0); - if (LOG) Log.d(TAG, manager.mMatchingIntents.size() + " services matches OpenUDID"); - - if (manager.mMatchingIntents != null) - //Start services one by one - { - manager.startService(); - } - } else {//Got it, you can now call getOpenUDID() - if (LOG) Log.d(TAG, "OpenUDID: " + OpenUDID); - mInitialized = true; - } - } - - /* - * Used to sort the OpenUDIDs collected by occurrence - */ - private class ValueComparator implements Comparator { - public int compare(Object a, Object b) { - - if (mReceivedOpenUDIDs.get(a) < mReceivedOpenUDIDs.get(b)) { - return 1; - } else if (mReceivedOpenUDIDs.get(a) == mReceivedOpenUDIDs.get(b)) { - return 0; - } else { - return -1; - } - } - } -} diff --git a/sdk/src/main/java/org/openudid/OpenUDID_service.java b/sdk/src/main/java/org/openudid/OpenUDID_service.java deleted file mode 100644 index aa0441864..000000000 --- a/sdk/src/main/java/org/openudid/OpenUDID_service.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.openudid; - -import android.app.Service; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.os.Binder; -import android.os.IBinder; - - -/* - * You have to add this in your manifest - - - - - - - -*/ - -public class OpenUDID_service extends Service { - @Override - public IBinder onBind(Intent arg0) { - return new Binder() { - @Override - public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) { - final SharedPreferences preferences = getSharedPreferences(OpenUDID_manager.PREFS_NAME, Context.MODE_PRIVATE); - - reply.writeInt(data.readInt()); //Return to the sender the input random number - reply.writeString(preferences.getString(OpenUDID_manager.PREF_KEY, null)); - return true; - } - }; - } -}