forked from Countly/countly-sdk-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworked OpenUDID so that it is not a service anymore. Removed random…
… UDID, made it the fallback for openUDID.
- Loading branch information
1 parent
b18d6a4
commit b58c365
Showing
7 changed files
with
59 additions
and
327 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 54 additions & 47 deletions
101
sdk/src/main/java/ly/count/android/sdk/OpenUDIDAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.