Skip to content

Commit 0310c2f

Browse files
committed
Merge pull request #10 from adeven/clean_up
Clean up, add pom.xml, close #11
2 parents 8356b3e + 49fa9c3 commit 0310c2f

File tree

8 files changed

+612
-241
lines changed

8 files changed

+612
-241
lines changed

AdjustIo/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="com.adeven.adjustio" >
3-
<uses-sdk android:minSdkVersion="8"/>
3+
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>
44
</manifest>

AdjustIo/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>adjustio-android</artifactId>
7+
<groupId>com.adeven.adjustio</groupId>
8+
<version>1.5</version>
9+
<packaging>jar</packaging>
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<android.version>4.1.1.4</android.version>
13+
<android.version.platform>16</android.version.platform>
14+
</properties>
15+
<dependencies>
16+
<dependency>
17+
<artifactId>android</artifactId>
18+
<version>${android.version}</version>
19+
<groupId>com.google.android</groupId>
20+
<scope>provided</scope>
21+
</dependency>
22+
</dependencies>
23+
<build>
24+
<sourceDirectory>src</sourceDirectory>
25+
<pluginManagement>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-compiler-plugin</artifactId>
30+
<version>2.5.1</version>
31+
<configuration>
32+
<source>1.6</source>
33+
<target>1.6</target>
34+
</configuration>
35+
</plugin>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-source-plugin</artifactId>
39+
<version>2.2</version>
40+
</plugin>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-javadoc-plugin</artifactId>
44+
<version>2.9</version>
45+
</plugin>
46+
<plugin>
47+
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
48+
<artifactId>android-maven-plugin</artifactId>
49+
<version>3.5.3</version>
50+
<extensions>true</extensions>
51+
<configuration>
52+
<sdk>
53+
<platform>${android.version.platform}</platform>
54+
</sdk>
55+
</configuration>
56+
</plugin>
57+
</plugins>
58+
</pluginManagement>
59+
</build>
60+
</project>

AdjustIo/src/com/adeven/adjustio/AdjustIo.java

Lines changed: 167 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,93 +11,211 @@
1111

1212
import java.util.Map;
1313

14-
import android.app.Application;
15-
14+
import android.content.Context;
15+
16+
/**
17+
* The main interface to AdjustIo.
18+
*
19+
* Use the methods of this class to tell AdjustIo about the usage of your app.
20+
* See the README for details.
21+
*
22+
* @author wellle
23+
* @since 11.10.12
24+
*/
1625
public class AdjustIo {
17-
// Tell AdjustIo that the application did launch. This is required to
18-
// initialize AdjustIo. Call this in the onCreate method of your launch
19-
// activity.
20-
public static void appDidLaunch(Application app) {
21-
if (!Util.checkPermissions(app)) {
26+
27+
/**
28+
* Tell AdjustIo that the application did launch.
29+
*
30+
* This is required to initialize AdjustIo.
31+
* Call this in the onCreate method of your launch activity.
32+
*
33+
* @param context Your application context
34+
* Generally obtained by calling getApplication()
35+
*/
36+
37+
public static void appDidLaunch(Context context) {
38+
if (!Util.checkPermissions(context)) {
2239
return;
2340
}
2441

25-
String macAddress = Util.getMacAddress(app);
42+
String macAddress = Util.getMacAddress(context);
2643

27-
appId = app.getPackageName();
44+
packageName = context.getPackageName();
2845
macSha1 = Util.sha1(macAddress);
2946
macShort = macAddress.replaceAll(":", "");
30-
userAgent = Util.getUserAgent(app);
31-
androidId = Util.getAndroidId(app);
32-
attributionId = Util.getAttributionId(app);
47+
userAgent = Util.getUserAgent(context);
48+
androidId = Util.getAndroidId(context);
49+
attributionId = Util.getAttributionId(context);
3350

3451
trackSessionStart();
3552
}
3653

37-
// Track any kind of event. You can assign a callback url to the event which
38-
// will get called every time the event is reported. You can also provide
39-
// parameters that will be forwarded to these callbacks.
40-
public static void trackEvent(String eventId) {
41-
trackEvent(eventId, null);
54+
55+
/**
56+
* Track any kind of event.
57+
*
58+
* You can assign a callback url to the event which
59+
* will get called every time the event is reported. You can also provide
60+
* parameters that will be forwarded to these callbacks.
61+
*
62+
* @param eventToken The token for this kind of event
63+
* It must be exactly six characters long
64+
* You create them in your dashboard at http://www.adjust.io
65+
* @param parameters An optional dictionary containing callback parameters
66+
* Provide key-value-pairs to be forwarded to your callbacks
67+
*/
68+
69+
public static void trackEvent(String eventToken) {
70+
trackEvent(eventToken, null);
4271
}
4372

44-
public static void trackEvent(String eventId, Map<String,String> parameters) {
45-
String paramString = Util.getBase64EncodedParameters(parameters);
73+
public static void trackEvent(String eventToken, Map<String, String> parameters) {
74+
if (eventToken.length() != 6) {
75+
Logger.error(
76+
"Event tracking only works with proper event tokens. " +
77+
"Find them in your dashboard at http://www.adjust.io " +
78+
"or contact [email protected]"
79+
);
80+
return;
81+
}
4682

47-
RequestTask requestTask = new RequestTask("/event");
48-
requestTask.setSuccessMessage("Tracked event " + eventId + ".");
49-
requestTask.setFailureMessage("Failed to track event " + eventId + ".");
50-
requestTask.setUserAgent(userAgent);
51-
requestTask.execute("id", eventId, "app_id", appId, "mac", macShort, "android_id", androidId, "params", paramString);
83+
String paramString = Util.getBase64EncodedParameters(parameters);
84+
String successMessage = "Tracked event: '" + eventToken + "'";
85+
String failureMessage = "Failed to track event: '" + eventToken + "'";
86+
87+
TrackingPackage event = new TrackingPackage.Builder()
88+
.setPath("/event")
89+
.setSuccessMessage(successMessage)
90+
.setFailureMessage(failureMessage)
91+
.setUserAgent(userAgent)
92+
.addTrackingParameter(EVENT_TOKEN, eventToken)
93+
.addTrackingParameter(PACKAGE_NAME, packageName)
94+
.addTrackingParameter(MAC_SHORT, macShort)
95+
.addTrackingParameter(ANDROID_ID, androidId)
96+
.addTrackingParameter(PARAMETERS, paramString)
97+
.build();
98+
getRequestThread().track(event);
5299
}
53100

54-
// Tell AdjustIo that the current user generated some revenue. The amount is
55-
// measured in cents and rounded to on digit after the decimal point. If you
56-
// want to differentiate between various types of revenues you can do so by
57-
// providing different eventIds. If your revenue events have callbacks, you
58-
// can also pass in parameters that will be forwarded to your server.
101+
102+
/**
103+
* Tell AdjustIo that the current user generated some revenue.
104+
*
105+
* The amount is measured in cents and rounded to on digit after the decimal
106+
* point. If you want to differentiate between various types of specific revenues
107+
* you can do so by using different event tokens. If your revenue events have
108+
* callbacks, you can also pass in parameters that will be forwarded to your
109+
* server.
110+
*
111+
* @param amountInCents The amount in cents (example: 1.5f means one and a half cents)
112+
* @param eventToken The token for this revenue event (see above)
113+
* @param parameters Parameters for this revenue event (see above)
114+
*/
115+
59116
public static void trackRevenue(float amountInCents) {
60117
AdjustIo.trackRevenue(amountInCents, null);
61118
}
62119

63-
public static void trackRevenue(float amountInCents, String eventId) {
64-
AdjustIo.trackRevenue(amountInCents, eventId, null);
120+
public static void trackRevenue(float amountInCents, String eventToken) {
121+
AdjustIo.trackRevenue(amountInCents, eventToken, null);
65122
}
66123

67-
public static void trackRevenue(float amountInCents, String eventId, Map<String,String> parameters) {
124+
public static void trackRevenue(float amountInCents, String eventToken, Map<String, String> parameters) {
125+
if (eventToken != null && eventToken.length() != 6) {
126+
Logger.error(
127+
"Specific revenue tracking only works with proper event tokens. " +
128+
"Find them in your dashboard at http://www.adjust.io " +
129+
"or contact [email protected]"
130+
);
131+
return;
132+
}
133+
68134
int amountInMillis = Math.round(10 * amountInCents);
135+
amountInCents = amountInMillis/10.0f; // now rounded to one decimal point
69136
String amount = Integer.toString(amountInMillis);
70137
String paramString = Util.getBase64EncodedParameters(parameters);
138+
String successMessage = "Tracked revenue: " + amountInCents + " Cent";
139+
String failureMessage = "Failed to track revenue: " + amountInCents + " Cent";
140+
141+
if (eventToken != null) {
142+
String eventString = " (event token: '" + eventToken + "')";
143+
successMessage += eventString;
144+
failureMessage += eventString;
145+
}
146+
147+
TrackingPackage revenue = new TrackingPackage.Builder()
148+
.setPath("/revenue")
149+
.setSuccessMessage(successMessage)
150+
.setFailureMessage(failureMessage)
151+
.setUserAgent(userAgent)
152+
.addTrackingParameter(PACKAGE_NAME, packageName)
153+
.addTrackingParameter(MAC_SHORT, macShort)
154+
.addTrackingParameter(ANDROID_ID, androidId)
155+
.addTrackingParameter(AMOUNT, amount)
156+
.addTrackingParameter(EVENT_TOKEN, eventToken)
157+
.addTrackingParameter(PARAMETERS, paramString)
158+
.build();
159+
getRequestThread().track(revenue);
160+
}
161+
71162

72-
RequestTask requestTask = new RequestTask("/revenue");
73-
requestTask.setSuccessMessage("Tracked revenue.");
74-
requestTask.setFailureMessage("Failed to track revenue.");
75-
requestTask.setUserAgent(userAgent);
76-
requestTask.execute("app_id", appId, "mac", macShort, "android_id", androidId, "amount", amount, "event_id", eventId, "params", paramString);
163+
/**
164+
* Change the verbosity of AdjustIo's logs.
165+
*
166+
* @param logLevel The desired minimum log level (default: info)
167+
* Must be one of the following:
168+
* - Log.VERBOSE (enable all logging)
169+
* - Log.DEBUG
170+
* - Log.INFO (the default)
171+
* - Log.WARN (disable info logging)
172+
* - Log.ERROR (disable warnings as well)
173+
* - Log.ASSERT (disable errors as well)
174+
*/
175+
176+
public static void setLogLevel(int logLevel) {
177+
Logger.setLogLevel(logLevel);
77178
}
78179

180+
79181
// This line marks the end of the public interface.
80182

81-
private static String appId;
183+
private static final String PACKAGE_NAME = "app_id";
184+
private static final String MAC_SHA1 = "mac_sha1";
185+
private static final String MAC_SHORT = "mac";
186+
private static final String ANDROID_ID = "android_id";
187+
private static final String ATTRIBUTION_ID = "fb_id";
188+
private static final String EVENT_TOKEN = "event_id";
189+
private static final String PARAMETERS = "params";
190+
private static final String AMOUNT = "amount";
191+
192+
private static String packageName;
82193
private static String macSha1;
83194
private static String macShort;
84195
private static String userAgent;
85196
private static String androidId;
86197
private static String attributionId;
87198

88199
private static void trackSessionStart() {
89-
RequestTask requestTask = new RequestTask("/startup");
90-
requestTask.setSuccessMessage("Tracked session start.");
91-
requestTask.setFailureMessage("Failed to track session start.");
92-
requestTask.setUserAgent(userAgent);
93-
requestTask.execute("app_id", appId, "mac", macShort, "mac_sha1", macSha1, "android_id", androidId, "fb_id", attributionId);
200+
TrackingPackage sessionStart = new TrackingPackage.Builder()
201+
.setPath("/startup")
202+
.setSuccessMessage("Tracked session start.")
203+
.setFailureMessage("Failed to track session start.")
204+
.setUserAgent(userAgent)
205+
.addTrackingParameter(PACKAGE_NAME, packageName)
206+
.addTrackingParameter(MAC_SHORT, macShort)
207+
.addTrackingParameter(MAC_SHA1, macSha1)
208+
.addTrackingParameter(ANDROID_ID, androidId)
209+
.addTrackingParameter(ATTRIBUTION_ID, attributionId)
210+
.build();
211+
getRequestThread().track(sessionStart);
94212
}
95213

96-
private static void trackSessionEnd() {
97-
RequestTask requestTask = new RequestTask("/shutdown");
98-
requestTask.setSuccessMessage("Tracked session end.");
99-
requestTask.setFailureMessage("Failed to track session end.");
100-
requestTask.setUserAgent(userAgent);
101-
requestTask.execute("app_id", appId, "mac", macShort, "android_id", androidId);
214+
private static RequestThread requestThread;
215+
private static RequestThread getRequestThread() {
216+
if (requestThread == null) {
217+
requestThread = new RequestThread();
218+
}
219+
return requestThread;
102220
}
103221
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//
2+
// Logger.java
3+
// AdjustIo
4+
//
5+
// Created by Benjamin Weiss on 17.4.13
6+
// Copyright (c) 2012 adeven. All rights reserved.
7+
// See the file MIT-LICENSE for copying permission.
8+
//
9+
10+
package com.adeven.adjustio;
11+
12+
import android.util.Log;
13+
14+
/**
15+
* A Wrapper that allows easy toggles of Logging.
16+
*
17+
* @author keyboardsurfer
18+
* @since 17.4.13
19+
*/
20+
public class Logger {
21+
protected static final String LOGTAG = "AdjustIo";
22+
23+
private static int logLevel = Log.INFO;
24+
25+
public static void setLogLevel(int logLevel) {
26+
Logger.logLevel = logLevel;
27+
}
28+
29+
public static void verbose(String message) {
30+
if (logLevel <= Log.VERBOSE) {
31+
Log.v(LOGTAG, message);
32+
}
33+
}
34+
35+
public static void verbose(String context, String name, String value) {
36+
verbose("[" + context + "] " + name + ": '" + value + "'");
37+
}
38+
39+
public static void debug(String message) {
40+
if (logLevel <= Log.DEBUG) {
41+
Log.d(LOGTAG, message);
42+
}
43+
}
44+
45+
public static void info(String message) {
46+
if (logLevel <= Log.INFO) {
47+
Log.i(LOGTAG, message);
48+
}
49+
}
50+
51+
public static void warn(String message) {
52+
if (logLevel <= Log.WARN) {
53+
Log.w(LOGTAG, message);
54+
}
55+
}
56+
57+
public static void error(String message) {
58+
if (logLevel <= Log.ERROR) {
59+
Log.e(LOGTAG, message);
60+
}
61+
}
62+
63+
public static void error(String message, Throwable throwable) {
64+
if (logLevel <= Log.ERROR) {
65+
Log.e(LOGTAG, message, throwable);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)