Skip to content

Commit

Permalink
Merge branch 'dev_1_1_3' into 'master'
Browse files Browse the repository at this point in the history
Update to v1.1.3



See merge request !11
  • Loading branch information
magaman384 committed Oct 7, 2015
2 parents dd3c699 + 7c29770 commit 082c7cf
Show file tree
Hide file tree
Showing 15 changed files with 487 additions and 717 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -100,7 +100,7 @@ Session is a real time spent in the game, it starts when the game is launched an
"os_name": "Android",
"os_version": "5.0.1",
"sdk": "AndroidSDK",
"sdk_version": "1.1.2"
"sdk_version": "1.1.3"
"app_version": "1.0.0"
}
</code></pre>
Expand All @@ -121,7 +121,7 @@ Installation event is fired <strong>only once</strong> for the whole lifetime of
"os_name": "Android",
"os_version": "5.0.1",
"sdk": "AndroidSDK",
"sdk_version": "1.1.2"
"sdk_version": "1.1.3"
}
</code></pre>

Expand All @@ -139,7 +139,7 @@ Identification event is tracked each time the <code>identify()</code> method is
"os_name": "Android",
"os_version": "5.0.1",
"sdk": "AndroidSDK",
"sdk_version": "1.1.2"
"sdk_version": "1.1.3"
}
</code></pre>

Expand Down Expand Up @@ -225,7 +225,7 @@ Purchase events (called <code>hard_purchase</code>) contain all basic informatio
"os_name": "Android",
"os_version": "5.0.1",
"sdk": "AndroidSDK",
"sdk_version": "1.1.2"
"sdk_version": "1.1.3"
}
</code></pre>

Expand Down
2 changes: 1 addition & 1 deletion maven-push.gradle
Expand Up @@ -47,7 +47,7 @@ afterEvaluate { project ->

pom.groupId = 'com.infinario.android.infinariosdk'
pom.artifactId = 'infinario-android-sdk'
pom.version = '1.1.2'
pom.version = '1.1.3'

repository(url: getReleaseRepositoryUrl()) {
authentication(userName: getRepositoryUsername(), password: getRepositoryPassword())
Expand Down
17 changes: 0 additions & 17 deletions src/main/AndroidManifest.xml
Expand Up @@ -21,23 +21,6 @@
</intent-filter>
</receiver>

<receiver android:name="com.infinario.android.infinariosdk.AlarmReceiver"
android:exported="true">
</receiver>

<receiver android:name="com.infinario.android.infinariosdk.BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>

<receiver android:name="com.infinario.android.infinariosdk.ConnectivityReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>

<receiver android:name="com.infinario.android.infinariosdk.ReferrerReceiver"
android:exported="true" >
<intent-filter>
Expand Down

This file was deleted.

23 changes: 0 additions & 23 deletions src/main/java/com/infinario/android/infinariosdk/BootReceiver.java

This file was deleted.

121 changes: 92 additions & 29 deletions src/main/java/com/infinario/android/infinariosdk/CommandManager.java
@@ -1,6 +1,7 @@
package com.infinario.android.infinariosdk;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import org.json.JSONArray;
Expand All @@ -22,25 +23,28 @@ public class CommandManager {
HttpHelper http;
Preferences preferences;
Object lockFlush;
boolean flushInProgress;
boolean flushMayNeedRestart;
AsyncTask<Void, Void, Void> flushTask;

public CommandManager(Context context, String target) {
public CommandManager(Context context, String target, String userAgent) {
queue = new DbQueue(context);
http = new HttpHelper(target);
preferences = Preferences.get(context);
http = new HttpHelper(target, userAgent);
lockFlush = new Object();

synchronized (lockFlush){
flushInProgress = false;
flushMayNeedRestart = false;
}
}

public boolean schedule(Command command) {
return queue.schedule(command);
}

public boolean executeBatch() {
if (!preferences.ensureCookieId()) {
Log.d(Contract.TAG, "Failed to negotiate cookie ID");
return false;
}

Set<Integer> failedRequests = new HashSet<>();
Set<Integer> deleteRequests = new HashSet<>();
Set<Integer> successfulRequests = new HashSet<>();
JSONArray results;
JSONArray commands = new JSONArray();
Expand All @@ -52,14 +56,13 @@ public boolean executeBatch() {
String status;

if (requests.isEmpty()) {
return false;
return true;
}

Log.i(Contract.TAG, "sending ids " + requests.get(0).getId() + " - " + requests.get(requests.size() - 1).getId());

for (Request r : requests) {
commands.put(setCookieId(setAge(r.getCommand())));
failedRequests.add(r.getId());
}

try {
Expand All @@ -70,6 +73,12 @@ public boolean executeBatch() {

data = http.post(Contract.BULK_URL, payload);

StringBuilder logResult = new StringBuilder();
logResult.append("Batch executed, ")
.append(requests.size())
.append(" prepared, ");


if (data != null) {
try {
results = data.getJSONArray("results");
Expand All @@ -85,52 +94,106 @@ public boolean executeBatch() {
status = result.getString("status").toLowerCase();

if (status.equals("ok")) {
failedRequests.remove(request.getId());
deleteRequests.add(request.getId());
successfulRequests.add(request.getId());
} else if (status.equals("retry")) {
failedRequests.remove(request.getId());
} else if (status.equals("error")) {
deleteRequests.add(request.getId());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
logResult.append(successfulRequests.size())
.append(" succeeded, ")
.append(deleteRequests.size() - successfulRequests.size());
} else {
logResult.append("0 succeeded, ")
.append(requests.size());
}
} else {
logResult.append("0 succeeded, ")
.append(requests.size());
}

queue.clear(successfulRequests, failedRequests);
logResult.append(" failed, rest was told to retry");
Log.i(Contract.TAG, logResult.toString());

Log.i(Contract.TAG, "Batch executed, " + requests.size() + " prepared, " + successfulRequests.size() + " succeeded, "
+ failedRequests.size() + " failed, rest was told to retry");
queue.clear(deleteRequests);

return successfulRequests.size() > 0 || failedRequests.size() > 0;
return requests.size() == deleteRequests.size();
}

public void flush() {
synchronized (lockFlush){
int retries = MAX_RETRIES;

while (retries > 0) {
if (!executeBatch()) {
if (queue.isEmpty()) {
break;
} else {
--retries;
public boolean flush(final int count) {
synchronized (lockFlush) {
flushMayNeedRestart = true;
if (!flushInProgress) {
flushInProgress = true;
flushTask = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
flushCommands(count);
return null;
}
}.execute();

return true;
}
else {
return false;
}
}
}

private void flushCommands(int maxRetries) {
while (true) {
int delayFlush = 1000;
int retryCounter = 0;

synchronized (lockFlush) {
if (!flushMayNeedRestart) {
flushInProgress = false;
break;
}
flushMayNeedRestart = false;
}

try{
while (!queue.isEmpty() && (retryCounter <= maxRetries)) {
if (!executeBatch()) {
if (maxRetries > 1) {
Thread.sleep(delayFlush);
delayFlush = exponentialIncrease(delayFlush);
}

retryCounter += 1;
}
}
} catch (Exception e) {
Log.e(Contract.TAG, e.getMessage().toString());
}
}
}


private int exponentialIncrease(int timeInMiliseconds)
{
int calculateDelay = timeInMiliseconds * 2;

return Contract.FLUSH_MAX_INTERVAL < calculateDelay
? Contract.FLUSH_MAX_INTERVAL
: calculateDelay;
}

private JSONObject setCookieId(JSONObject command) {
try {
JSONObject data = command.getJSONObject("data");

if (data.has("ids") && data.getJSONObject("ids").getString("cookie").isEmpty()) {
data.getJSONObject("ids").put("cookie", preferences.getCampaignCookieId());
data.getJSONObject("ids").put("cookie", preferences.getCookieId());
}

if (data.has("customer_ids") && data.getJSONObject("customer_ids").getString("cookie").isEmpty()) {
data.getJSONObject("customer_ids").put("cookie", preferences.getCampaignCookieId());
data.getJSONObject("customer_ids").put("cookie", preferences.getCookieId());
}
}
catch (JSONException ignored) {
Expand Down

This file was deleted.

Expand Up @@ -16,7 +16,7 @@ public class Contract {
* SDK
*/
public static final String SDK = "AndroidSDK";
public static final String VERSION = "1.1.2";
public static final String VERSION = "1.1.3";
public static final String OS = "Android";

/**
Expand Down Expand Up @@ -70,13 +70,11 @@ public class Contract {
/**
* Automatic flushing
*/
public static final long UPDATE_INTERVAL = 6 * AlarmManager.INTERVAL_HOUR;
public static final long FLUSH_DELAY = 10 * 1000;
public static final int FLUSH_COUNT = 50;
public static final boolean DEFAULT_AUTO_FLUSH = true;
public static final int PERIODIC_ALARM = 1;
public static final int DELAYED_ALARM = 2;
public static final int IMMEDIATE_ALARM = 3;
public static int FLUSH_MIN_INTERVAL = 1000;
public static int FLUSH_MAX_INTERVAL = 3600000;

/**
* Session
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/com/infinario/android/infinariosdk/DbQueue.java
Expand Up @@ -86,18 +86,11 @@ public boolean isEmpty() {

}

public void clear(Set<Integer> successful, Set<Integer> failed) {
public void clear(Set<Integer> delete) {
synchronized (lockAccess){
openDatabase();
try{
db.delete(Contract.TABLE_COMMANDS, Contract.COLUMN_ID + " IN (" + TextUtils.join(", ", successful) + ")", null);

db.execSQL(
"UPDATE " + Contract.TABLE_COMMANDS + " "
+ "SET " + Contract.COLUMN_RETRIES + " = " + Contract.COLUMN_RETRIES + " + 1 "
+ "WHERE " + Contract.COLUMN_ID + " IN (" + TextUtils.join(", ", failed) + ")");

db.delete(Contract.TABLE_COMMANDS, Contract.COLUMN_RETRIES + " > " + Contract.MAX_RETRIES, null);
db.delete(Contract.TABLE_COMMANDS, Contract.COLUMN_ID + " IN (" + TextUtils.join(", ", delete) + ")", null);
} finally {
closeDatabase();
}
Expand Down

0 comments on commit 082c7cf

Please sign in to comment.