Last active
July 6, 2017 08:39
-
-
Save hector6872/95132907fded58a1d563b314f8b11169 to your computer and use it in GitHub Desktop.
UidUtils
This file contains hidden or 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
public enum UidUtils { | |
INSTANCE; | |
private final static String DEFAULT_ID = "anonymous"; | |
private String androidId = DEFAULT_ID; | |
private String advertisingId = ""; | |
private boolean limitAdTrackingEnabled = false; | |
@SuppressLint("HardwareIds") public void init(@NonNull final Context context) { | |
androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); | |
new Thread(new Runnable() { | |
public void run() { | |
advertisingId = internalGetAdvertisingId(context); | |
limitAdTrackingEnabled = internalIsPlayTrackingEnabled(context); | |
} | |
}).start(); | |
} | |
public @NonNull String getUid() { | |
try { | |
if (!TextUtils.isEmpty(advertisingId) && !limitAdTrackingEnabled) { | |
return advertisingId; | |
} | |
} catch (Exception ignored) { | |
} | |
return !TextUtils.isEmpty(androidId) ? androidId : DEFAULT_ID; | |
} | |
public boolean isLimitAdTrackingEnabled() { | |
return limitAdTrackingEnabled; | |
} | |
private String internalGetAdvertisingId(@NonNull Context context) { | |
try { | |
Object AdvertisingInfoObject = getAdvertisingIdInfoObject(context); | |
return (String) invokeInstanceMethod(AdvertisingInfoObject, "getId", null); | |
} catch (Throwable t) { | |
return null; | |
} | |
} | |
private Boolean internalIsPlayTrackingEnabled(@NonNull Context context) { | |
try { | |
Object AdvertisingInfoObject = getAdvertisingIdInfoObject(context); | |
Boolean isLimitedTrackingEnabled = | |
(Boolean) invokeInstanceMethod(AdvertisingInfoObject, "isLimitAdTrackingEnabled", null); | |
return isLimitedTrackingEnabled == null ? false : isLimitedTrackingEnabled; | |
} catch (Throwable t) { | |
return false; | |
} | |
} | |
private Object getAdvertisingIdInfoObject(@NonNull Context context) throws Exception { | |
return invokeStaticMethod("com.google.android.gms.ads.identifier.AdvertisingIdClient", "getAdvertisingIdInfo", | |
new Class[] { Context.class }, context); | |
} | |
private Object invokeStaticMethod(@NonNull String className, @NonNull String methodName, Class[] classArgs, | |
Object... args) throws Exception { | |
Class classObject = Class.forName(className); | |
return invokeMethod(classObject, methodName, null, classArgs, args); | |
} | |
private Object invokeMethod(@NonNull Class classObject, @NonNull String methodName, Object instance, Class[] cArgs, | |
Object... args) throws Exception { | |
@SuppressWarnings("unchecked") Method methodObject = classObject.getMethod(methodName, cArgs); | |
if (methodObject == null) { | |
return null; | |
} | |
return methodObject.invoke(instance, args); | |
} | |
private Object invokeInstanceMethod(@NonNull Object instance, @NonNull String methodName, Class[] classArgs, | |
Object... args) throws Exception { | |
Class classObject = instance.getClass(); | |
return invokeMethod(classObject, methodName, instance, classArgs, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment