Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 60 additions & 24 deletions WordPress/src/main/java/org/wordpress/android/WordPress.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import de.greenrobot.event.EventBus;
import io.fabric.sdk.android.Fabric;
Expand Down Expand Up @@ -698,8 +700,11 @@ private class ApplicationLifecycleMonitor implements Application.ActivityLifecyc
private final int DEFAULT_TIMEOUT = 2 * 60; // 2 minutes
private Date mLastPingDate;
private Date mApplicationOpenedDate;
boolean mIsInBackground = true;
boolean mFirstActivityResumed = true;
private Timer mActivityTransitionTimer;
private TimerTask mActivityTransitionTimerTask;
private final long MAX_ACTIVITY_TRANSITION_TIME_MS = 2000;
boolean mIsInBackground = true;

@Override
public void onConfigurationChanged(final Configuration newConfig) {
Expand All @@ -713,25 +718,6 @@ public void onLowMemory() {

@Override
public void onTrimMemory(final int level) {
if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
// We're in the Background
mIsInBackground = true;
String lastActivityString = AppPrefs.getLastActivityStr();
ActivityId lastActivity = ActivityId.getActivityIdFromName(lastActivityString);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("last_visible_screen", lastActivity.toString());
if (mApplicationOpenedDate != null) {
Date now = new Date();
properties.put("time_in_app", DateTimeUtils.secondsBetween(now, mApplicationOpenedDate));
mApplicationOpenedDate = null;
}
AnalyticsTracker.track(AnalyticsTracker.Stat.APPLICATION_CLOSED, properties);
AnalyticsTracker.endSession(false);
onAppGoesToBackground();
} else {
mIsInBackground = false;
}

boolean evictBitmaps = false;
switch (level) {
case TRIM_MEMORY_COMPLETE:
Expand Down Expand Up @@ -776,17 +762,64 @@ private void updatePushNotificationTokenIfNotLimited() {
}
}

public void onAppGoesToBackground() {
AppLog.i(T.UTILS, "App goes to background");
ConnectionChangeReceiver.setEnabled(WordPress.this, false);
/**
* The two methods below (startActivityTransitionTimer and stopActivityTransitionTimer)
* are used to track when the app goes to background.
*
* Our implementation uses `onActivityPaused` and `onActivityResumed` of ApplicationLifecycleMonitor
* to start and stop the timer that detects when the app goes to background.
*
* So when the user is simply navigating between the activities, the onActivityPaused() calls `startActivityTransitionTimer`
* and starts the timer, but almost immediately the new activity being entered, the ApplicationLifecycleMonitor cancels the timer
* in its onActivityResumed method, that in order calls `stopActivityTransitionTimer`.
* And so mIsInBackground would be false.
*
* In the case the app is sent to background, the TimerTask is instead executed, and the code that handles all the background logic is run.
*/
private void startActivityTransitionTimer() {
this.mActivityTransitionTimer = new Timer();
this.mActivityTransitionTimerTask = new TimerTask() {
public void run() {
AppLog.i(T.UTILS, "App goes to background");
// We're in the Background
mIsInBackground = true;
String lastActivityString = AppPrefs.getLastActivityStr();
ActivityId lastActivity = ActivityId.getActivityIdFromName(lastActivityString);
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("last_visible_screen", lastActivity.toString());
if (mApplicationOpenedDate != null) {
Date now = new Date();
properties.put("time_in_app", DateTimeUtils.secondsBetween(now, mApplicationOpenedDate));
mApplicationOpenedDate = null;
}
AnalyticsTracker.track(AnalyticsTracker.Stat.APPLICATION_CLOSED, properties);
AnalyticsTracker.endSession(false);
ConnectionChangeReceiver.setEnabled(WordPress.this, false);
}
};

this.mActivityTransitionTimer.schedule(mActivityTransitionTimerTask,
MAX_ACTIVITY_TRANSITION_TIME_MS);
}

private void stopActivityTransitionTimer() {
if (this.mActivityTransitionTimerTask != null) {
this.mActivityTransitionTimerTask.cancel();
}

if (this.mActivityTransitionTimer != null) {
this.mActivityTransitionTimer.cancel();
}

mIsInBackground = false;
}

/**
* This method is called when:
* 1. the app starts (but it's not opened by a service or a broadcast receiver, i.e. an activity is resumed)
* 2. the app was in background and is now foreground
*/
public void onAppComesFromBackground() {
private void onAppComesFromBackground() {
AppLog.i(T.UTILS, "App comes from background");
ConnectionChangeReceiver.setEnabled(WordPress.this, true);
AnalyticsUtils.refreshMetadata();
Expand All @@ -811,6 +844,8 @@ public void onActivityResumed(Activity activity) {
// was in background before
onAppComesFromBackground();
}
stopActivityTransitionTimer();

mIsInBackground = false;
if (mFirstActivityResumed) {
deferredInit(activity);
Expand All @@ -829,6 +864,7 @@ public void onActivityDestroyed(Activity arg0) {
@Override
public void onActivityPaused(Activity arg0) {
mLastPingDate = new Date();
startActivityTransitionTimer();
}

@Override
Expand Down