Skip to content

Commit

Permalink
Reworked OpenUDID so that it is not a service anymore. Removed random…
Browse files Browse the repository at this point in the history
… UDID, made it the fallback for openUDID.
  • Loading branch information
ArtursKadikis committed Oct 21, 2020
1 parent b18d6a4 commit b58c365
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 327 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
6 changes: 0 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,6 @@
</activity>
<activity android:name=".ActivityExampleKotlin" />

<service android:name="org.openudid.OpenUDID_service" android:exported="false">
<intent-filter>
<action android:name="org.OpenUDID.GETUDID" />
</intent-filter>
</service>

<receiver android:name="ly.count.android.sdk.ReferrerReceiver" android:exported="true">
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER" />
Expand Down
2 changes: 0 additions & 2 deletions sdk/src/main/java/ly/count/android/sdk/Countly.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
32 changes: 4 additions & 28 deletions sdk/src/main/java/ly/count/android/sdk/DeviceId.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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:
Expand All @@ -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());
Expand All @@ -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;
}
Expand Down
101 changes: 54 additions & 47 deletions sdk/src/main/java/ly/count/android/sdk/OpenUDIDAdapter.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Loading

0 comments on commit b58c365

Please sign in to comment.