|
22 | 22 | import android.text.TextUtils; |
23 | 23 | import android.util.TypedValue; |
24 | 24 |
|
| 25 | +import com.google.android.gms.ads.identifier.AdvertisingIdClient; |
25 | 26 | import com.leanplum.Leanplum; |
26 | 27 | import com.leanplum.LeanplumActivityHelper; |
27 | 28 | import com.leanplum.LeanplumDeviceIdMode; |
@@ -181,24 +182,17 @@ private static String getWifiMacAddressHash(Context context) { |
181 | 182 | */ |
182 | 183 | private static DeviceIdInfo getAdvertisingId(Context caller) throws Exception { |
183 | 184 | try { |
184 | | - // Using reflection because the app will either crash or print warnings |
185 | | - // if the app doesn't link to Google Play Services, even if this method is not called. |
186 | | - Object adInfo = Class.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient") |
187 | | - .getMethod("getAdvertisingIdInfo", Context.class).invoke(null, caller); |
188 | | - String id = checkDeviceId( |
189 | | - "advertising id", (String) adInfo.getClass().getMethod("getId").invoke(adInfo)); |
190 | | - if (id != null) { |
191 | | - boolean limitTracking = (Boolean) adInfo.getClass() |
192 | | - .getMethod("isLimitAdTrackingEnabled").invoke(adInfo); |
193 | | - Log.v("Using advertising device id: " + id); |
194 | | - return new DeviceIdInfo(id, limitTracking); |
195 | | - } |
196 | | - } catch (Exception e) { |
197 | | - if (e.getClass().getName().equals("GooglePlayServicesNotAvailableException")) { |
198 | | - Log.w("Error getting advertising ID. Google Play services are not available."); |
199 | | - } else { |
200 | | - throw e; |
| 185 | + AdvertisingIdClient.Info info = AdvertisingIdClient.getAdvertisingIdInfo(caller); |
| 186 | + if (info != null) { |
| 187 | + String advertisingId = info.getId(); |
| 188 | + String deviceId = checkDeviceId("advertising id", advertisingId); |
| 189 | + if (deviceId != null) { |
| 190 | + boolean limitedTracking = info.isLimitAdTrackingEnabled(); |
| 191 | + return new DeviceIdInfo(deviceId, limitedTracking); |
| 192 | + } |
201 | 193 | } |
| 194 | + } catch (Throwable ignored) { |
| 195 | + Log.e("Error getting advertising ID. Google Play Services are not available."); |
202 | 196 | } |
203 | 197 | return null; |
204 | 198 | } |
|
0 commit comments