Skip to content

Commit

Permalink
Merge pull request #4346 from pingwang2011/timob-14060-3_1_X
Browse files Browse the repository at this point in the history
Timob 14060 3 1 x: Parity: Making the app background on Android does not send ti.end event
  • Loading branch information
srahim committed Jun 3, 2013
2 parents e4e968b + 61d0323 commit b2e777a
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -66,6 +68,7 @@ public abstract class TiApplication extends Application implements Handler.Callb
private static final String SYSTEM_UNIT = "system";
private static final String TAG = "TiApplication";
private static final long STATS_WAIT = 300000;
private static final long TIME_SEPARATION_ANALYTICS = 1000;
private static final int MSG_SEND_ANALYTICS = 100;
private static final long SEND_ANALYTICS_DELAY = 30000; // Time analytics send request sits in queue before starting service.
private static final String PROPERTY_DEPLOY_TYPE = "ti.deploytype";
Expand Down Expand Up @@ -462,12 +465,6 @@ public void setRootActivity(TiRootActivity rootActivity)
postAnalyticsEvent(TiAnalyticsEventFactory.createAppEnrollEvent(this,deployType));
}

if (needsStartEvent()) {
String deployType = systemProperties.getString("ti.deploytype", "unknown");

postAnalyticsEvent(TiAnalyticsEventFactory.createAppStartEvent(this, deployType));
}

} else {
needsEnrollEvent = false;
needsStartEvent = false;
Expand Down Expand Up @@ -639,19 +636,6 @@ public synchronized void postAnalyticsEvent(TiAnalyticsEvent event)
return;
}

if (Log.isDebugModeEnabled()) {
StringBuilder sb = new StringBuilder();
sb.append("Analytics Event: type=").append(event.getEventType())
.append("\n event=").append(event.getEventEvent())
.append("\n timestamp=").append(event.getEventTimestamp())
.append("\n mid=").append(event.getEventMid())
.append("\n sid=").append(event.getEventSid())
.append("\n aguid=").append(event.getEventAppGuid())
.append("\n isJSON=").append(event.mustExpandPayload())
.append("\n payload=").append(event.getEventPayload());
Log.d(TAG, sb.toString());
}

if (event.getEventType() == TiAnalyticsEventFactory.EVENT_APP_ENROLL) {
if (needsEnrollEvent) {
analyticsModel.addEvent(event);
Expand All @@ -661,16 +645,30 @@ public synchronized void postAnalyticsEvent(TiAnalyticsEvent event)
}

} else if (event.getEventType() == TiAnalyticsEventFactory.EVENT_APP_START) {
if (needsStartEvent) {
analyticsModel.addEvent(event);
needsStartEvent = false;
sendAnalytics();
lastAnalyticsTriggered = System.currentTimeMillis();
HashMap<Integer,String> tsForEndEvent = analyticsModel.getLastTimestampForEventType(TiAnalyticsEventFactory.EVENT_APP_END);
if (tsForEndEvent.size() == 1) {
for (Integer key : tsForEndEvent.keySet()) {
try {
SimpleDateFormat dateFormat = TiAnalyticsEvent.getDateFormatForTimestamp();
long lastEnd = dateFormat.parse(tsForEndEvent.get(key)).getTime(); //in millisecond
long start = dateFormat.parse(event.getEventTimestamp()).getTime();
// If the new activity starts immediately after the previous activity pauses, we consider
// the app is still in foreground so will not send any analytics events
if (start - lastEnd < TIME_SEPARATION_ANALYTICS) {
analyticsModel.deleteEvents(new int[] {key});
return;
}
} catch (ParseException e) {
Log.e(TAG, "Incorrect timestamp. Unable to send the ti.start event.", e);
}
}
}
analyticsModel.addEvent(event);
sendAnalytics();
lastAnalyticsTriggered = System.currentTimeMillis();
return;

} else if (event.getEventType() == TiAnalyticsEventFactory.EVENT_APP_END) {
needsStartEvent = true;
analyticsModel.addEvent(event);
sendAnalytics();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiMessenger;
import org.appcelerator.titanium.TiLifecycle.OnLifecycleEvent;
import org.appcelerator.titanium.analytics.TiAnalyticsEventFactory;
import org.appcelerator.titanium.proxy.ActionBarProxy;
import org.appcelerator.titanium.proxy.ActivityProxy;
import org.appcelerator.titanium.proxy.IntentProxy;
Expand Down Expand Up @@ -927,6 +928,11 @@ protected void onPause()
}
}
}

// Checkpoint for ti.end event
if (tiApp != null) {
tiApp.postAnalyticsEvent(TiAnalyticsEventFactory.createAppEndEvent());
}
}

@Override
Expand Down Expand Up @@ -974,6 +980,10 @@ protected void onResume()
}

isResumed = true;

// Checkpoint for ti.start event
String deployType = tiApp.getSystemProperties().getString("ti.deploytype", "unknown");
tiApp.postAnalyticsEvent(TiAnalyticsEventFactory.createAppStartEvent(tiApp, deployType));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.appcelerator.kroll.KrollRuntime;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.util.KrollAssetHelper;
import org.appcelerator.titanium.analytics.TiAnalyticsEventFactory;
import org.appcelerator.titanium.util.TiColorHelper;
import org.appcelerator.titanium.util.TiPlatformHelper;
import org.appcelerator.titanium.util.TiUrl;
Expand Down Expand Up @@ -415,10 +414,6 @@ protected void onDestroy()
return;
}

if (tiApp != null) {
tiApp.postAnalyticsEvent(TiAnalyticsEventFactory.createAppEndEvent());
}

// Create a new session ID for next session
TiPlatformHelper.resetSid();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class TiAnalyticsEvent
private static final String TAG = "TitaniumAnalyticsEvent";

private static TimeZone utc = TimeZone.getTimeZone("UTC");
private static SimpleDateFormat isoDateFormatter =
private static final SimpleDateFormat isoDateFormatter =
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
static {
// Workaround for setting the timezone since there is a bug in android 2.2 and earlier
Expand Down Expand Up @@ -122,4 +122,9 @@ public boolean mustExpandPayload() {
public static String getTimestamp() {
return isoDateFormatter.format(new Date());
}

public static SimpleDateFormat getDateFormatForTimestamp()
{
return isoDateFormatter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.appcelerator.titanium.analytics;

import java.util.HashMap;
import java.util.LinkedHashMap;

import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.util.TiPlatformHelper;
Expand Down Expand Up @@ -141,6 +142,19 @@ private void doMigration_3(SQLiteDatabase db)

public void addEvent(final TiAnalyticsEvent event)
{
if (Log.isDebugModeEnabled()) {
StringBuilder sb = new StringBuilder();
sb.append("add Analytics Event to db: type=").append(event.getEventType())
.append("\n event=").append(event.getEventEvent())
.append("\n timestamp=").append(event.getEventTimestamp())
.append("\n mid=").append(event.getEventMid())
.append("\n sid=").append(event.getEventSid())
.append("\n aguid=").append(event.getEventAppGuid())
.append("\n isJSON=").append(event.mustExpandPayload())
.append("\n payload=").append(event.getEventPayload());
Log.d(TAG, sb.toString());
}

SQLiteDatabase db = null;
try {
db = getWritableDatabase();
Expand Down Expand Up @@ -184,6 +198,7 @@ public void deleteEvents(int records[])
}
sb.append(")");
db.execSQL(sb.toString());
Log.d(TAG, "delete Analytics Event: " + sb.toString(), Log.DEBUG_MODE);
} catch (SQLException e) {
Log.e(TAG, "Error deleting events :" + e);
} finally {
Expand Down Expand Up @@ -225,8 +240,9 @@ public boolean hasEvents() {
return result;
}

public HashMap<Integer,JSONObject> getEventsAsJSON(int limit) {
HashMap<Integer, JSONObject> result = new HashMap<Integer,JSONObject>(limit);
public LinkedHashMap<Integer,JSONObject> getEventsAsJSON(int limit) {
// Use LinkedHashMap to preserve the item order in which keys were inserted into the map.
LinkedHashMap<Integer, JSONObject> result = new LinkedHashMap<Integer,JSONObject>(limit);

SQLiteDatabase db = null;
Cursor c = null;
Expand All @@ -235,7 +251,7 @@ public HashMap<Integer,JSONObject> getEventsAsJSON(int limit) {

String sql =
"select _id, EventId, Type, Event, Timestamp, MID, SID, AppGUID, isJSON, Payload from Events " +
" order by 1 limit " +
" order by Timestamp asc limit " +
limit
;

Expand Down Expand Up @@ -278,6 +294,41 @@ public HashMap<Integer,JSONObject> getEventsAsJSON(int limit) {
return result;
}

/*
* Get the most recent timestamp given the event type.
*/
public HashMap<Integer,String> getLastTimestampForEventType(String type) {
HashMap<Integer, String> result = new HashMap<Integer,String>();
SQLiteDatabase db = null;
Cursor c = null;
try {
db = getReadableDatabase();

String sql =
"select _id, Timestamp from Events where Type=\"" + type + "\"" + " order by Timestamp desc";

c = db.rawQuery(sql, null);

if (c.moveToNext()) {
result.put(c.getInt(0), c.getString(1));
if (Log.isDebugModeEnabled()) {
Log.d(TAG, "get the most recent timestamp for event " + type + ", id = " + c.getInt(0) + ", timestamp = " + c.getString(1));
}
}
} catch (SQLException e) {
Log.e(TAG, "Error retrieving timpestamp for event " + type + ": ", e);
} finally {
if (c != null) {
c.close();
}
if (db != null) {
db.close();
}
}

return result;
}

public boolean needsEnrollEvent() {
boolean result = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
package org.appcelerator.titanium.analytics;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.http.client.HttpClient;
Expand Down Expand Up @@ -96,7 +96,7 @@ public void run() {
while(model.hasEvents()) {
if(canSend())
{
HashMap<Integer,JSONObject> events = model.getEventsAsJSON(BUCKET_SIZE_FAST_NETWORK);
LinkedHashMap<Integer,JSONObject> events = model.getEventsAsJSON(BUCKET_SIZE_FAST_NETWORK);

int len = events.size();
int[] eventIds = new int[len];
Expand All @@ -110,6 +110,11 @@ public void run() {
// and a queue blocked by bad records.
eventIds[i] = id;
records.put(events.get(id));

if (Log.isDebugModeEnabled()) {
JSONObject obj = events.get(id);
Log.d(TAG, "Sending event: type = " + obj.getString("type") + ", timestamp = " + obj.getString("ts"));
}
}
boolean deleteEvents = true;
if (records.length() > 0) {
Expand Down

0 comments on commit b2e777a

Please sign in to comment.