Skip to content

Commit 1ac1e25

Browse files
committed
Merge pull request #41 from adjust/disable_SDK
V3.2.0 Disable SDK
2 parents dda07be + f6d01a4 commit 1ac1e25

File tree

15 files changed

+277
-59
lines changed

15 files changed

+277
-59
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,6 @@ build/
6363
#for oh-my-zsh jira plugin (https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins#jira)
6464
.jira-url
6565
atlassian-ide-plugin.xml
66+
67+
# add exception of google play services jar
68+
!google-play-services.jar
1.79 MB
Binary file not shown.

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

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class ActivityHandler extends HandlerThread {
6161
private String defaultTracker;
6262
private boolean eventBuffering;
6363
private boolean dropOfflineActivities;
64+
private boolean enabled;
6465

6566
private String appToken;
6667
private String macSha1;
@@ -80,6 +81,7 @@ public ActivityHandler(Activity activity) {
8081
sessionHandler = new SessionHandler(getLooper(), this);
8182
context = activity.getApplicationContext();
8283
clientSdk = Constants.CLIENT_SDK;
84+
enabled = true;
8385

8486
logger = AdjustFactory.getLogger();
8587

@@ -99,6 +101,7 @@ public ActivityHandler(Activity activity, String appToken,
99101
sessionHandler = new SessionHandler(getLooper(), this);
100102
context = activity.getApplicationContext();
101103
clientSdk = Constants.CLIENT_SDK;
104+
enabled = true;
102105

103106
logger = AdjustFactory.getLogger();
104107

@@ -133,7 +136,7 @@ public void trackSubsessionEnd() {
133136
}
134137

135138
public void trackEvent(String eventToken, Map<String, String> parameters) {
136-
PackageBuilder builder = new PackageBuilder();
139+
PackageBuilder builder = new PackageBuilder(context);
137140
builder.setEventToken(eventToken);
138141
builder.setCallbackParameters(parameters);
139142

@@ -144,7 +147,7 @@ public void trackEvent(String eventToken, Map<String, String> parameters) {
144147
}
145148

146149
public void trackRevenue(double amountInCents, String eventToken, Map<String, String> parameters) {
147-
PackageBuilder builder = new PackageBuilder();
150+
PackageBuilder builder = new PackageBuilder(context);
148151
builder.setAmountInCents(amountInCents);
149152
builder.setEventToken(eventToken);
150153
builder.setCallbackParameters(parameters);
@@ -173,6 +176,25 @@ public void run() {
173176
handler.post(runnable);
174177
}
175178

179+
public void setEnabled(Boolean enabled) {
180+
this.enabled = enabled;
181+
if (checkActivityState(activityState))
182+
activityState.enabled = enabled;
183+
if (enabled) {
184+
this.trackSubsessionStart();
185+
} else {
186+
this.trackSubsessionEnd();
187+
}
188+
}
189+
190+
public Boolean isEnabled() {
191+
if (checkActivityState(activityState)) {
192+
return activityState.enabled;
193+
} else {
194+
return this.enabled;
195+
}
196+
}
197+
176198
private static final class SessionHandler extends Handler {
177199
private static final int INIT_BUNDLE = 72630;
178200
private static final int INIT_PRESET = 72633;
@@ -260,6 +282,11 @@ private void startInternal() {
260282
return;
261283
}
262284

285+
if (activityState != null
286+
&& !activityState.enabled) {
287+
return;
288+
}
289+
263290
packageHandler.resumeSending();
264291
startTimer();
265292

@@ -273,6 +300,7 @@ private void startInternal() {
273300

274301
transferSessionPackage();
275302
activityState.resetSessionAttributes(now);
303+
activityState.enabled = this.enabled;
276304
writeActivityState();
277305
logger.info("First session");
278306
return;
@@ -321,7 +349,7 @@ private void endInternal() {
321349

322350
packageHandler.pauseSending();
323351
stopTimer();
324-
updateActivityState();
352+
updateActivityState(System.currentTimeMillis());
325353
writeActivityState();
326354
}
327355

@@ -330,9 +358,14 @@ private void trackEventInternal(PackageBuilder eventBuilder) {
330358
return;
331359
}
332360

333-
activityState.createdAt = System.currentTimeMillis();
361+
if (!activityState.enabled) {
362+
return;
363+
}
364+
365+
long now = System.currentTimeMillis();
366+
activityState.createdAt = now;
334367
activityState.eventCount++;
335-
updateActivityState();
368+
updateActivityState(now);
336369

337370
injectGeneralAttributes(eventBuilder);
338371
activityState.injectEventAttributes(eventBuilder);
@@ -354,9 +387,15 @@ private void trackRevenueInternal(PackageBuilder revenueBuilder) {
354387
return;
355388
}
356389

357-
activityState.createdAt = System.currentTimeMillis();
390+
if (!activityState.enabled) {
391+
return;
392+
}
393+
394+
long now = System.currentTimeMillis();
395+
396+
activityState.createdAt = now;
358397
activityState.eventCount++;
359-
updateActivityState();
398+
updateActivityState(now);
360399

361400
injectGeneralAttributes(revenueBuilder);
362401
activityState.injectEventAttributes(revenueBuilder);
@@ -385,12 +424,11 @@ && checkActivityState(activityState)
385424
&& revenueBuilder.isValidForRevenue();
386425
}
387426

388-
private void updateActivityState() {
427+
private void updateActivityState(long now) {
389428
if (!checkActivityState(activityState)) {
390429
return;
391430
}
392431

393-
long now = System.currentTimeMillis();
394432
long lastInterval = now - activityState.lastActivity;
395433
if (lastInterval < 0) {
396434
logger.error(TIME_TRAVEL);
@@ -465,7 +503,7 @@ public static Boolean deleteActivityState(Context context) {
465503
}
466504

467505
private void transferSessionPackage() {
468-
PackageBuilder builder = new PackageBuilder();
506+
PackageBuilder builder = new PackageBuilder(context);
469507
injectGeneralAttributes(builder);
470508
injectReferrer(builder);
471509
activityState.injectSessionAttributes(builder);
@@ -518,9 +556,14 @@ private void stopTimer() {
518556
}
519557

520558
private void timerFired() {
559+
if (null != activityState
560+
&& !activityState.enabled) {
561+
return;
562+
}
563+
521564
packageHandler.sendFirstPackage();
522565

523-
updateActivityState();
566+
updateActivityState(System.currentTimeMillis());
524567
writeActivityState();
525568
}
526569

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class ActivityState implements Serializable {
2424

2525
// persistent data
2626
protected String uuid;
27+
protected Boolean enabled;
2728

2829
// global counters
2930
protected int eventCount;
@@ -41,6 +42,7 @@ public class ActivityState implements Serializable {
4142
protected ActivityState() {
4243
// create UUID for new devices
4344
uuid = Util.createUuid();
45+
enabled = true;
4446

4547
eventCount = 0; // no events yet
4648
sessionCount = 0; // the first session just started
@@ -71,6 +73,7 @@ protected void injectEventAttributes(PackageBuilder builder) {
7173
builder.setEventCount(eventCount);
7274
}
7375

76+
@Override
7477
public String toString() {
7578
return String.format(Locale.US,
7679
"ec:%d sc:%d ssc:%d sl:%.1f ts:%.1f la:%s",
@@ -91,6 +94,7 @@ private void readObject(ObjectInputStream stream) throws NotActiveException, IOE
9194
createdAt = fields.get("createdAt", -1l);
9295
lastInterval = fields.get("lastInterval", -1l);
9396
uuid = (String)fields.get("uuid", null);
97+
enabled = fields.get("enabled", true);
9498

9599
// create UUID for migrating devices
96100
if (uuid == null) {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,34 @@ public static void trackRevenue(double amountInCents, String eventToken, Map<Str
121121
}
122122
}
123123

124+
/**
125+
* Enable or disable the adjust SDK
126+
*
127+
* @param enabled The flag to enable or disable the adjust SDK
128+
*/
129+
public static void setEnabled(Boolean enabled) {
130+
try {
131+
activityHandler.setEnabled(enabled);
132+
} catch (NullPointerException e) {
133+
if (logger != null)
134+
logger.error(NO_ACTIVITY_HANDLER_FOUND);
135+
}
136+
}
137+
138+
/**
139+
* Check if the SDK is enabled or disabled
140+
*/
141+
public static Boolean isEnabled() {
142+
try {
143+
return activityHandler.isEnabled();
144+
} catch (NullPointerException e) {
145+
if (logger != null)
146+
logger.error(NO_ACTIVITY_HANDLER_FOUND);
147+
}
148+
return false;
149+
}
150+
151+
124152
// Special appDidLaunch method used by SDK wrappers such as our Adobe Air SDK.
125153
protected static void appDidLaunch(Activity activity, String appToken, String environment, String logLevel, boolean eventBuffering) {
126154
activityHandler = new ActivityHandler(activity, appToken, environment, logLevel, eventBuffering);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public interface Constants {
1919
int THIRTY_MINUTES = 30 * ONE_MINUTE;
2020

2121
String BASE_URL = "https://app.adjust.io";
22-
String CLIENT_SDK = "android3.0.0";
22+
String CLIENT_SDK = "android3.2.0";
2323
String LOGTAG = "Adjust";
2424

2525
String SESSION_STATE_FILENAME = "AdjustIoActivityState";

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@
99

1010
package com.adjust.sdk;
1111

12-
import android.text.TextUtils;
13-
import android.util.Base64;
14-
import java.text.SimpleDateFormat;
15-
import java.util.Date;
1612
import java.util.HashMap;
1713
import java.util.Locale;
1814
import java.util.Map;
15+
1916
import org.json.JSONObject;
2017

18+
import android.content.Context;
19+
import android.text.TextUtils;
20+
import android.util.Base64;
21+
2122
public class PackageBuilder {
2223

23-
private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'Z";
24+
private Context context;
2425

2526
// general
2627
private String appToken;
@@ -49,10 +50,10 @@ public class PackageBuilder {
4950
private double amountInCents;
5051
private Map<String, String> callbackParameters;
5152

52-
private static SimpleDateFormat dateFormat;
53-
54-
public PackageBuilder()
55-
{ }
53+
public PackageBuilder(Context context)
54+
{
55+
this.context = context;
56+
}
5657

5758
public void setAppToken(String appToken) {
5859
this.appToken = appToken;
@@ -237,6 +238,8 @@ private Map<String, String> getDefaultParameters() {
237238
addString(parameters, "android_uuid", uuid);
238239
addString(parameters, "fb_id", fbAttributionId);
239240
addString(parameters, "environment", environment);
241+
String gpsAdid = Util.getGpsAdid(context);
242+
addString(parameters, "gps_adid", gpsAdid);
240243

241244
// session related (used for events as well)
242245
addInt(parameters, "session_count", sessionCount);
@@ -293,8 +296,7 @@ private void addDate(Map<String, String> parameters, String key, long value) {
293296
return;
294297
}
295298

296-
Date date = new Date(value);
297-
String dateString = getDateFormat().format(date);
299+
String dateString = Util.dateFormat(value);
298300
addString(parameters, key, dateString);
299301
}
300302

@@ -318,11 +320,4 @@ private void addMap(Map<String, String> parameters, String key, Map<String, Stri
318320

319321
addString(parameters, key, encodedMap);
320322
}
321-
322-
private SimpleDateFormat getDateFormat() {
323-
if (null == dateFormat) {
324-
dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
325-
}
326-
return dateFormat;
327-
}
328323
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ private HttpUriRequest getRequest(ActivityPackage activityPackage) throws Unsupp
205205
pairs.add(pair);
206206
}
207207

208+
long now = System.currentTimeMillis();
209+
String dateString = Util.dateFormat(now);
210+
NameValuePair sentAtPair = new BasicNameValuePair("sent_at", dateString);
211+
pairs.add(sentAtPair);
212+
208213
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs);
209214
entity.setContentType(URLEncodedUtils.CONTENT_TYPE);
210215
request.setEntity(entity);

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.IOException;
2828
import java.math.BigInteger;
2929
import java.security.MessageDigest;
30+
import java.text.SimpleDateFormat;
3031
import java.util.Locale;
3132
import java.util.UUID;
3233
import java.util.regex.Matcher;
@@ -47,12 +48,17 @@
4748
import android.text.TextUtils;
4849
import android.util.DisplayMetrics;
4950

51+
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
52+
5053

5154
/**
5255
* Collects utility functions used by Adjust.
5356
*/
5457
public class Util {
5558

59+
private static SimpleDateFormat dateFormat;
60+
private static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'Z";
61+
5662
protected static String getUserAgent(final Context context) {
5763
final Resources resources = context.getResources();
5864
final DisplayMetrics displayMetrics = resources.getDisplayMetrics();
@@ -337,4 +343,26 @@ public static String quote(String string) {
337343

338344
return String.format("'%s'", string);
339345
}
346+
347+
public static String dateFormat(long date) {
348+
if (null == dateFormat) {
349+
dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
350+
}
351+
return dateFormat.format(date);
352+
}
353+
354+
public static String getGpsAdid(Context context) {
355+
String gpsAdid = null;
356+
try {
357+
AdvertisingIdClient.Info info = AdvertisingIdClient.getAdvertisingIdInfo(context);
358+
if (!info.isLimitAdTrackingEnabled()) {
359+
gpsAdid = info.getId();
360+
}
361+
} catch (Exception e) {
362+
Logger logger = AdjustFactory.getLogger();
363+
logger.error(String.format("Error getting Google Play Services advertising ID, (%s)", e.getMessage()));
364+
}
365+
366+
return gpsAdid;
367+
}
340368
}

0 commit comments

Comments
 (0)