Skip to content

Commit

Permalink
Revert "Moving from private inner class to public outer class to test"
Browse files Browse the repository at this point in the history
This reverts commit 5fffc66.
  • Loading branch information
kwonye committed Mar 29, 2015
1 parent 5fffc66 commit c363f2f
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 181 deletions.

This file was deleted.

169 changes: 169 additions & 0 deletions WordPress/src/main/java/org/wordpress/android/WordPress.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package org.wordpress.android;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.app.ProgressDialog;
import android.content.ComponentCallbacks2;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.database.sqlite.SQLiteException;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.text.TextUtils;
Expand Down Expand Up @@ -35,6 +39,7 @@
import org.wordpress.android.networking.OAuthAuthenticatorFactory;
import org.wordpress.android.networking.RestClientUtils;
import org.wordpress.android.networking.SelfSignedSSLCertsManager;
import org.wordpress.android.ui.ActivityId;
import org.wordpress.android.ui.accounts.helpers.UpdateBlogListTask.GenericUpdateBlogListTask;
import org.wordpress.android.ui.notifications.utils.NotificationsUtils;
import org.wordpress.android.ui.notifications.utils.SimperiumUtils;
Expand All @@ -45,7 +50,9 @@
import org.wordpress.android.util.AppLog.T;
import org.wordpress.android.util.BitmapLruCache;
import org.wordpress.android.util.CoreEvents;
import org.wordpress.android.util.DateTimeUtils;
import org.wordpress.android.util.HelpshiftHelper;
import org.wordpress.android.util.NetworkUtils;
import org.wordpress.android.util.PackageUtils;
import org.wordpress.android.util.ProfilingUtils;
import org.wordpress.android.util.RateLimitedTask;
Expand All @@ -58,6 +65,8 @@
import java.lang.ref.WeakReference;
import java.lang.reflect.Type;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -659,4 +668,164 @@ public static String getUserAgent() {
}
return mUserAgent;
}

/**
* Detect when the app goes to the background and come back to the foreground.
*
* Turns out that when your app has no more visible UI, a callback is triggered.
* The callback, implemented in this custom class, is called ComponentCallbacks2 (yes, with a two).
*
* This class also uses ActivityLifecycleCallbacks and a timer used as guard,
* to make sure to detect the send to background event and not other events.
*
*/
private class ApplicationLifecycleMonitor implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {
private final int DEFAULT_TIMEOUT = 2 * 60; // 2 minutes
private Date lastPingDate;
private Date mApplicationOpenedDate;
boolean isInBackground = true;

@Override
public void onConfigurationChanged(final Configuration newConfig) {
}

@Override
public void onLowMemory() {
}

@Override
public void onTrimMemory(final int level) {
if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
// We're in the Background
isInBackground = 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);
} else {
isInBackground = false;
}

boolean evictBitmaps = false;
switch (level) {
case TRIM_MEMORY_COMPLETE:
case TRIM_MEMORY_MODERATE:
case TRIM_MEMORY_RUNNING_MODERATE:
case TRIM_MEMORY_RUNNING_CRITICAL:
case TRIM_MEMORY_RUNNING_LOW:
evictBitmaps = true;
break;
default:
break;
}

if (evictBitmaps && mBitmapCache != null) {
mBitmapCache.evictAll();
}
}

private boolean isPushNotificationPingNeeded() {
if (lastPingDate == null) {
// first startup
return false;
}

Date now = new Date();
if (DateTimeUtils.secondsBetween(now, lastPingDate) >= DEFAULT_TIMEOUT) {
lastPingDate = now;
return true;
}
return false;
}

/**
* Check if user has valid credentials, and that at least 2 minutes are passed
* since the last ping, then try to update the PN token.
*/
private void updatePushNotificationTokenIfNotLimited() {
// Synch Push Notifications settings
if (isPushNotificationPingNeeded() && WordPress.hasDotComToken(mContext)) {
String token = null;
try {
// Register for Google Cloud Messaging
GCMRegistrar.checkDevice(mContext);
GCMRegistrar.checkManifest(mContext);
token = GCMRegistrar.getRegistrationId(mContext);
String gcmId = BuildConfig.GCM_ID;
if (gcmId == null || token == null || token.equals("") ) {
AppLog.e(T.NOTIFS, "Could not ping the PNs backend, Token or gmcID not found");
} else {
// Send the token to WP.com
NotificationsUtils.registerDeviceForPushNotifications(mContext, token);
}
} catch (Exception e) {
AppLog.e(T.NOTIFS, "Could not ping the PNs backend: " + e.getMessage());
}
}
}

/**
* This method is called when:
* 1. the app starts (but it's not opened by a service, i.e. an activity is resumed)
* 2. the app was in background and is now foreground
*/
public void onFromBackground() {
AnalyticsTracker.beginSession();
mApplicationOpenedDate = new Date();
AnalyticsTracker.track(AnalyticsTracker.Stat.APPLICATION_OPENED);
if (NetworkUtils.isNetworkAvailable(mContext)) {
// Rate limited PN Token Update
updatePushNotificationTokenIfNotLimited();

if (hasDotComToken(mContext)) {
// Rate limited WPCom blog list Update
sUpdateWordPressComBlogList.runIfNotLimited();
}

// Rate limited blog options Update
sUpdateCurrentBlogOption.runIfNotLimited();
}
}

@Override
public void onActivityResumed(Activity activity) {
if (isInBackground) {
// was in background before
onFromBackground();
}
isInBackground = false;
}

@Override
public void onActivityCreated(Activity arg0, Bundle arg1) {
}

@Override
public void onActivityDestroyed(Activity arg0) {
}

@Override
public void onActivityPaused(Activity arg0) {
lastPingDate = new Date();
}

@Override
public void onActivitySaveInstanceState(Activity arg0, Bundle arg1) {
}

@Override
public void onActivityStarted(Activity arg0) {
}

@Override
public void onActivityStopped(Activity arg0) {
}
}
}

0 comments on commit c363f2f

Please sign in to comment.