Skip to content

Commit b2568f5

Browse files
authored
Adding support for androidx Advertising Client (#390)
1 parent c71df6b commit b2568f5

File tree

1 file changed

+35
-15
lines changed
  • AndroidSDKCore/src/main/java/com/leanplum/internal

1 file changed

+35
-15
lines changed

AndroidSDKCore/src/main/java/com/leanplum/internal/Util.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import android.net.wifi.WifiManager;
3737
import android.os.Build;
3838
import android.provider.Settings.Secure;
39-
import androidx.annotation.RequiresPermission;
4039
import android.text.TextUtils;
4140
import android.util.TypedValue;
4241

@@ -65,6 +64,7 @@
6564
import java.io.OutputStreamWriter;
6665
import java.io.PrintWriter;
6766
import java.io.StringWriter;
67+
import java.lang.reflect.Method;
6868
import java.net.HttpURLConnection;
6969
import java.net.URL;
7070
import java.net.URLConnection;
@@ -78,11 +78,14 @@
7878
import java.util.Locale;
7979
import java.util.Map;
8080
import java.util.UUID;
81+
import java.util.concurrent.TimeUnit;
8182
import java.util.zip.GZIPInputStream;
8283

8384
import javax.net.ssl.HttpsURLConnection;
8485
import javax.net.ssl.SSLSocketFactory;
8586

87+
import androidx.annotation.RequiresPermission;
88+
8689
/**
8790
* Leanplum utilities.
8891
*
@@ -194,23 +197,40 @@ private static String getWifiMacAddressHash(Context context) {
194197
}
195198

196199
/**
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.
199202
*/
200-
private static DeviceIdInfo getAdvertisingId(Context caller) throws Exception {
203+
private static DeviceIdInfo getAdvertisingId(Context caller) {
201204
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")
208222
.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+
}
214234
}
215235
} catch (Throwable t) {
216236
Log.e("Error getting advertising ID. Google Play Services are not available: ", t);

0 commit comments

Comments
 (0)