|
| 1 | +package com.adjust.sdk; |
| 2 | + |
| 3 | +import java.lang.reflect.Field; |
| 4 | +import java.lang.reflect.Method; |
| 5 | + |
| 6 | +import android.content.Context; |
| 7 | + |
| 8 | +public class Reflection { |
| 9 | + |
| 10 | + public static String getPlayAdId(Context context) { |
| 11 | + try { |
| 12 | + Object AdvertisingInfoObject = getAdvertisingInfoObject(context); |
| 13 | + |
| 14 | + String playAdid = (String) invokeInstanceMethod(AdvertisingInfoObject, "getId", null); |
| 15 | + |
| 16 | + return playAdid; |
| 17 | + } |
| 18 | + catch (Throwable t) { |
| 19 | + return null; |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public static boolean isPlayTrackingEnabled(Context context) { |
| 24 | + try { |
| 25 | + Object AdvertisingInfoObject = getAdvertisingInfoObject(context); |
| 26 | + |
| 27 | + Boolean isLimitedTrackingEnabled = (Boolean) invokeInstanceMethod(AdvertisingInfoObject, "isLimitAdTrackingEnabled", null); |
| 28 | + |
| 29 | + return !isLimitedTrackingEnabled; |
| 30 | + } |
| 31 | + catch (Throwable t) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public static boolean isGooglePlayServicesAvailable(Context context) { |
| 37 | + try { |
| 38 | + Integer isGooglePlayServicesAvailableStatusCode = (Integer) invokeStaticMethod( |
| 39 | + "com.google.android.gms.common.GooglePlayServicesUtil", |
| 40 | + "isGooglePlayServicesAvailable", |
| 41 | + new Class[] {Context.class}, context |
| 42 | + ); |
| 43 | + |
| 44 | + boolean isGooglePlayServicesAvailable = (Boolean) isConnectionResultSuccess(isGooglePlayServicesAvailableStatusCode); |
| 45 | + |
| 46 | + return isGooglePlayServicesAvailable; |
| 47 | + } |
| 48 | + catch (Throwable t) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public static String getMacAddress(Context context) { |
| 54 | + try { |
| 55 | + String macSha1 = (String) invokeStaticMethod( |
| 56 | + "com.adjust.sdk.deviceIds.MacAddressUtil", |
| 57 | + "getMacAddress", |
| 58 | + new Class[] {Context.class}, context |
| 59 | + ); |
| 60 | + |
| 61 | + return macSha1; |
| 62 | + } |
| 63 | + catch (Throwable t) { |
| 64 | + return null; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + public static String getAndroidId(Context context) { |
| 69 | + try { |
| 70 | + String androidId = (String) invokeStaticMethod("com.adjust.sdk.deviceIds.AndroidIdUtil", "getAndroidId" |
| 71 | + ,new Class[] {Context.class}, context); |
| 72 | + |
| 73 | + return androidId; |
| 74 | + } |
| 75 | + catch (Throwable t) { |
| 76 | + return null; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static Object getAdvertisingInfoObject(Context context) |
| 81 | + throws Exception { |
| 82 | + return invokeStaticMethod("com.google.android.gms.ads.identifier.AdvertisingIdClient", |
| 83 | + "getAdvertisingIdInfo", |
| 84 | + new Class[] {Context.class} , context |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + private static boolean isConnectionResultSuccess(Integer statusCode) { |
| 89 | + if (statusCode == null) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + Class ConnectionResultClass = Class.forName("com.google.android.gms.common.ConnectionResult"); |
| 95 | + |
| 96 | + Field SuccessField = ConnectionResultClass.getField("SUCCESS"); |
| 97 | + |
| 98 | + int successStatusCode = SuccessField.getInt(null); |
| 99 | + |
| 100 | + return successStatusCode == statusCode; |
| 101 | + } |
| 102 | + catch (Throwable t) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static Object invokeStaticMethod(String className, String methodName, Class[] cArgs, Object... args) |
| 108 | + throws Exception { |
| 109 | + Class classObject = Class.forName(className); |
| 110 | + |
| 111 | + return invokeMethod(classObject, methodName, null, cArgs, args); |
| 112 | + } |
| 113 | + |
| 114 | + private static Object invokeInstanceMethod(Object instance, String methodName, Class[] cArgs, Object... args) |
| 115 | + throws Exception { |
| 116 | + Class classObject = instance.getClass(); |
| 117 | + |
| 118 | + return invokeMethod(classObject, methodName, instance, cArgs, args); |
| 119 | + } |
| 120 | + |
| 121 | + private static Object invokeMethod(Class classObject, String methodName, Object instance, Class[] cArgs, Object... args) |
| 122 | + throws Exception { |
| 123 | + Method methodObject = classObject.getMethod(methodName, cArgs); |
| 124 | + |
| 125 | + Object resultObject = methodObject.invoke(instance, args); |
| 126 | + |
| 127 | + return resultObject; |
| 128 | + } |
| 129 | +} |
0 commit comments