Skip to content

Commit e61d525

Browse files
committed
Google Play Services refac
1 parent 8befa87 commit e61d525

File tree

5 files changed

+66
-35
lines changed

5 files changed

+66
-35
lines changed

Adjust/src/com/adjust/sdk/ActivityHandler.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public class ActivityHandler extends HandlerThread {
5353
private static final String TIME_TRAVEL = "Time travel!";
5454
private static final String ADJUST_PREFIX = "adjust_";
5555

56-
private final SessionHandler sessionHandler;
56+
private SessionHandler sessionHandler;
5757
private IPackageHandler packageHandler;
5858
private OnFinishedListener onFinishedListener;
5959
private ActivityState activityState;
60-
private final Logger logger;
60+
private Logger logger;
6161
private static ScheduledExecutorService timer;
62-
private final Context context;
62+
private Context context;
6363
private String environment;
6464
private String defaultTracker;
6565
private boolean eventBuffering;
@@ -76,17 +76,8 @@ public class ActivityHandler extends HandlerThread {
7676

7777
public ActivityHandler(Activity activity) {
7878
super(LOGTAG, MIN_PRIORITY);
79-
setDaemon(true);
80-
start();
81-
TIMER_INTERVAL = AdjustFactory.getTimerInterval();
82-
SESSION_INTERVAL = AdjustFactory.getSessionInterval();
83-
SUBSESSION_INTERVAL = AdjustFactory.getSubsessionInterval();
84-
sessionHandler = new SessionHandler(getLooper(), this);
85-
context = activity.getApplicationContext();
86-
clientSdk = Constants.CLIENT_SDK;
87-
enabled = true;
8879

89-
logger = AdjustFactory.getLogger();
80+
initActivityHandler(activity);
9081

9182
Message message = Message.obtain();
9283
message.arg1 = SessionHandler.INIT_BUNDLE;
@@ -96,8 +87,23 @@ public ActivityHandler(Activity activity) {
9687
public ActivityHandler(Activity activity, String appToken,
9788
String environment, String logLevel, boolean eventBuffering) {
9889
super(LOGTAG, MIN_PRIORITY);
90+
91+
initActivityHandler(activity);
92+
93+
this.appToken = appToken;
94+
this.environment = environment;
95+
this.eventBuffering = eventBuffering;
96+
logger.setLogLevelString(logLevel);
97+
98+
Message message = Message.obtain();
99+
message.arg1 = SessionHandler.INIT_PRESET;
100+
sessionHandler.sendMessage(message);
101+
}
102+
103+
private void initActivityHandler(Activity activity) {
99104
setDaemon(true);
100105
start();
106+
101107
TIMER_INTERVAL = AdjustFactory.getTimerInterval();
102108
SESSION_INTERVAL = AdjustFactory.getSessionInterval();
103109
SUBSESSION_INTERVAL = AdjustFactory.getSubsessionInterval();
@@ -108,14 +114,10 @@ public ActivityHandler(Activity activity, String appToken,
108114

109115
logger = AdjustFactory.getLogger();
110116

111-
this.appToken = appToken;
112-
this.environment = environment;
113-
this.eventBuffering = eventBuffering;
114-
logger.setLogLevelString(logLevel);
115-
116-
Message message = Message.obtain();
117-
message.arg1 = SessionHandler.INIT_PRESET;
118-
sessionHandler.sendMessage(message);
117+
String gpsAdid = Util.getGpsAdid(context);
118+
if (gpsAdid == null) {
119+
logger.info("Unable to get Google Play Services Advertising ID at start time");
120+
}
119121
}
120122

121123
public void setSdkPrefix(String sdkPrefx) {

Adjust/src/com/adjust/sdk/Adjust.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,12 @@ public static void appWillOpenUrl(Uri url) {
161161

162162

163163
// Special appDidLaunch method used by SDK wrappers such as our Adobe Air SDK.
164-
protected static void appDidLaunch(Activity activity, String appToken, String environment, String logLevel, boolean eventBuffering) {
164+
public static void appDidLaunch(Activity activity, String appToken, String environment, String logLevel, boolean eventBuffering) {
165165
activityHandler = new ActivityHandler(activity, appToken, environment, logLevel, eventBuffering);
166166
}
167167

168168
// Special method used by SDK wrappers such as our Adobe Air SDK.
169-
protected static void setSdkPrefix(String sdkPrefix) {
169+
public static void setSdkPrefix(String sdkPrefix) {
170170
activityHandler.setSdkPrefix(sdkPrefix);
171171
}
172172

Adjust/src/com/adjust/sdk/Util.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.BufferedReader;
2626
import java.io.FileReader;
2727
import java.io.IOException;
28+
import java.lang.reflect.Method;
2829
import java.math.BigInteger;
2930
import java.security.MessageDigest;
3031
import java.text.SimpleDateFormat;
@@ -48,9 +49,6 @@
4849
import android.text.TextUtils;
4950
import android.util.DisplayMetrics;
5051

51-
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
52-
53-
5452
/**
5553
* Collects utility functions used by Adjust.
5654
*/
@@ -352,17 +350,36 @@ public static String dateFormat(long date) {
352350
}
353351

354352
public static String getGpsAdid(Context context) {
355-
String gpsAdid = null;
356353
try {
357-
AdvertisingIdClient.Info info = AdvertisingIdClient.getAdvertisingIdInfo(context);
358-
if (!info.isLimitAdTrackingEnabled()) {
359-
gpsAdid = info.getId();
354+
Class AdvertisingIdClientClass = Class.forName("com.google.android.gms.ads.identifier.AdvertisingIdClient");
355+
356+
Class[] cArg = new Class[1];
357+
cArg[0] = Context.class;
358+
Method getAdvertisingInfoMethod = AdvertisingIdClientClass.getMethod("getAdvertisingIdInfo", cArg);
359+
360+
Object AdvertisingInfoObject = getAdvertisingInfoMethod.invoke(null, context);
361+
362+
Class AdvertisingInfoClass = AdvertisingInfoObject.getClass();
363+
364+
Method isLimitedTrackingEnabledMethod = AdvertisingInfoClass.getMethod("isLimitAdTrackingEnabled");
365+
366+
Object isLimitedTrackingEnabledObject = isLimitedTrackingEnabledMethod.invoke(AdvertisingInfoObject);
367+
368+
Boolean isLimitedTrackingEnabled = (Boolean) isLimitedTrackingEnabledObject;
369+
370+
if (!isLimitedTrackingEnabled) {
371+
Method getIdMethod = AdvertisingInfoClass.getMethod("getId");
372+
373+
Object getIdObject = getIdMethod.invoke(AdvertisingInfoObject);
374+
375+
String gpsAdid = (String) getIdObject;
360376
}
361-
} catch (Exception e) {
362-
Logger logger = AdjustFactory.getLogger();
363-
logger.error(String.format("Error getting Google Play Services advertising ID, (%s)", e.getMessage()));
377+
}
378+
catch (Exception e) {
379+
}
380+
catch (NoClassDefFoundError ncdffe) {
364381
}
365382

366-
return gpsAdid;
383+
return null;
367384
}
368385
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ for **each** Activity of your app:
133133
- Add the `import` statement at the top of the file.
134134
- In your Activity's `onResume` method call `Adjust.onResume`. Create the
135135
method if needed.
136-
- In your Activity's `orPause` method call `Adjust.onPause`. Create the
136+
- In your Activity's `onPause` method call `Adjust.onPause`. Create the
137137
method if needed.
138138

139139
After these steps your activity should look like this:

doc/google_play_services.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Google Play Services
2+
3+
We access the Google Play Services (GPS) to get the advertising ID if the user didn’t opt out.
4+
To know more about the advertising ID check the [Android documentation][doc]
5+
6+
We have a private jar of GPS, so the app using adjust SDK is not forced to use GPS as well.
7+
You can remove the GPS jar if you want to save space or to link with your own GPS jar.
8+
9+
To link to your own, first delete our GPS jar located at `libs/google-play-services.jar`.
10+
Then link the adjust SDK project to your GPS jar.
11+
12+
[doc]:https://developer.android.com/google/play-services/id.html

0 commit comments

Comments
 (0)