|
36 | 36 | import android.net.wifi.WifiManager; |
37 | 37 | import android.os.Build; |
38 | 38 | import android.provider.Settings.Secure; |
39 | | -import androidx.annotation.RequiresPermission; |
40 | 39 | import android.text.TextUtils; |
41 | 40 | import android.util.TypedValue; |
42 | 41 |
|
|
65 | 64 | import java.io.OutputStreamWriter; |
66 | 65 | import java.io.PrintWriter; |
67 | 66 | import java.io.StringWriter; |
| 67 | +import java.lang.reflect.Method; |
68 | 68 | import java.net.HttpURLConnection; |
69 | 69 | import java.net.URL; |
70 | 70 | import java.net.URLConnection; |
|
78 | 78 | import java.util.Locale; |
79 | 79 | import java.util.Map; |
80 | 80 | import java.util.UUID; |
| 81 | +import java.util.concurrent.TimeUnit; |
81 | 82 | import java.util.zip.GZIPInputStream; |
82 | 83 |
|
83 | 84 | import javax.net.ssl.HttpsURLConnection; |
84 | 85 | import javax.net.ssl.SSLSocketFactory; |
85 | 86 |
|
| 87 | +import androidx.annotation.RequiresPermission; |
| 88 | + |
86 | 89 | /** |
87 | 90 | * Leanplum utilities. |
88 | 91 | * |
@@ -194,23 +197,40 @@ private static String getWifiMacAddressHash(Context context) { |
194 | 197 | } |
195 | 198 |
|
196 | 199 | /** |
197 | | - * Retrieves the advertising ID. Requires Google Play Services. Note: This method must not run on |
198 | | - * the main thread. |
| 200 | + * Retrieves the advertising ID. Requires Google Play Services or androidX. Note: This method must |
| 201 | + * not run on the main thread. |
199 | 202 | */ |
200 | | - private static DeviceIdInfo getAdvertisingId(Context caller) throws Exception { |
| 203 | + private static DeviceIdInfo getAdvertisingId(Context caller) { |
201 | 204 | try { |
202 | | - // Using reflection because the app will either crash or print warnings |
203 | | - // if the app doesn't link to Google Play Services, even if this method is not called. |
204 | | - Object adInfo = Class.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient") |
205 | | - .getMethod("getAdvertisingIdInfo", Context.class).invoke(null, caller); |
206 | | - String id = checkDeviceId( |
207 | | - "advertising id", (String) adInfo.getClass().getMethod("getId") |
| 205 | + final String[] classNames = { |
| 206 | + "androidx.ads.identifier.AdvertisingIdClient", |
| 207 | + "com.google.android.gms.ads.identifier.AdvertisingIdClient" |
| 208 | + }; |
| 209 | + |
| 210 | + for (String name : classNames) { |
| 211 | + try { |
| 212 | + Object adInfo = Class.forName(name) |
| 213 | + .getMethod("getAdvertisingIdInfo", Context.class) |
| 214 | + .invoke(null, caller); |
| 215 | + |
| 216 | + if (name.equals(classNames[0])) { |
| 217 | + Method get = adInfo.getClass().getMethod("get", long.class, TimeUnit.class); |
| 218 | + adInfo = get.invoke(adInfo, 5, TimeUnit.SECONDS); |
| 219 | + } |
| 220 | + |
| 221 | + String id = checkDeviceId("advertising id", (String) adInfo.getClass().getMethod("getId") |
208 | 222 | .invoke(adInfo)); |
209 | | - if (id != null) { |
210 | | - boolean limitTracking = (Boolean) adInfo.getClass() |
211 | | - .getMethod("isLimitAdTrackingEnabled").invoke(adInfo); |
212 | | - Log.v("Using advertising device id: " + id); |
213 | | - return new DeviceIdInfo(id, limitTracking); |
| 223 | + |
| 224 | + if (id != null) { |
| 225 | + boolean limitTracking = (Boolean) adInfo.getClass() |
| 226 | + .getMethod("isLimitAdTrackingEnabled") |
| 227 | + .invoke(adInfo); |
| 228 | + Log.v("Using advertising device id: " + id); |
| 229 | + return new DeviceIdInfo(id, limitTracking); |
| 230 | + } |
| 231 | + } catch (Throwable t) { |
| 232 | + Log.i("Couldn't get AdvertisingID using class: " + name); |
| 233 | + } |
214 | 234 | } |
215 | 235 | } catch (Throwable t) { |
216 | 236 | Log.e("Error getting advertising ID. Google Play Services are not available: ", t); |
|
0 commit comments