| Index: base/android/java/src/org/chromium/base/SystemMonitor.java
|
| diff --git a/base/android/java/src/org/chromium/base/SystemMonitor.java b/base/android/java/src/org/chromium/base/SystemMonitor.java
|
| index f43f115be1274a66bdd72a8e7e9183c7680cf59b..30f61a6ea86cad352b3ea6e2f788a7488bac2d35 100644
|
| --- a/base/android/java/src/org/chromium/base/SystemMonitor.java
|
| +++ b/base/android/java/src/org/chromium/base/SystemMonitor.java
|
| @@ -4,10 +4,12 @@
|
|
|
| package org.chromium.base;
|
|
|
| +import android.app.Activity;
|
| import android.content.Context;
|
| import android.content.Intent;
|
| import android.content.IntentFilter;
|
| import android.os.BatteryManager;
|
| +import android.os.Handler;
|
| import android.os.Looper;
|
|
|
|
|
| @@ -15,22 +17,35 @@ import android.os.Looper;
|
| * Integrates native SystemMonitor with the java side.
|
| */
|
| @JNINamespace("base::android")
|
| -public class SystemMonitor {
|
| +public class SystemMonitor implements ActivityStatus.StateListener {
|
| + private static final long SUSPEND_DELAY_MS = 1 * 60 * 1000; // 1 minute.
|
| private static SystemMonitor sInstance;
|
|
|
| private boolean mIsBatteryPower;
|
| + private final Handler mHandler = new Handler(Looper.getMainLooper());
|
| +
|
| + // Asynchronous task used to fire the "paused" event to the native side 1 minute after the main
|
| + // activity transitioned to the "paused" state. This event is not sent immediately because it
|
| + // would be too aggressive. An Android activity can be in the "paused" state quite often. This
|
| + // can happen when a dialog window shows up for instance.
|
| + private static final Runnable sSuspendTask = new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + nativeOnMainActivitySuspended();
|
| + }
|
| + };
|
|
|
| public static void createForTests(Context context) {
|
| - // Applications will create this once the
|
| - // JNI side has been fully wired up both sides.
|
| - // For tests, we just need native -> java, that is,
|
| - // we don't need to notify java -> native on creation.
|
| + // Applications will create this once the JNI side has been fully wired up both sides. For
|
| + // tests, we just need native -> java, that is, we don't need to notify java -> native on
|
| + // creation.
|
| sInstance = new SystemMonitor();
|
| }
|
|
|
| public static void create(Context context) {
|
| if (sInstance == null) {
|
| sInstance = new SystemMonitor();
|
| + ActivityStatus.registerStateListener(sInstance);
|
| IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
|
| Intent batteryStatusIntent = context.registerReceiver(null, ifilter);
|
| onBatteryChargingChanged(batteryStatusIntent);
|
| @@ -42,9 +57,8 @@ public class SystemMonitor {
|
|
|
| public static void onBatteryChargingChanged(Intent intent) {
|
| if (sInstance == null) {
|
| - // We may be called by the framework intent-filter before being
|
| - // fully initialized. This is not a problem, since our constructor
|
| - // will check for the state later on.
|
| + // We may be called by the framework intent-filter before being fully initialized. This
|
| + // is not a problem, since our constructor will check for the state later on.
|
| return;
|
| }
|
| int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
|
| @@ -54,11 +68,23 @@ public class SystemMonitor {
|
| nativeOnBatteryChargingChanged();
|
| }
|
|
|
| + @Override
|
| + public void onActivityStateChange(int newState) {
|
| + if (newState == ActivityStatus.RESUMED) {
|
| + // Remove the callback from the message loop in case it hasn't been executed yet.
|
| + mHandler.removeCallbacks(sSuspendTask);
|
| + nativeOnMainActivityResumed();
|
| + } else if (newState == ActivityStatus.PAUSED) {
|
| + mHandler.postDelayed(sSuspendTask, SUSPEND_DELAY_MS);
|
| + }
|
| + }
|
| +
|
| @CalledByNative
|
| private static boolean isBatteryPower() {
|
| return sInstance.mIsBatteryPower;
|
| }
|
|
|
| private static native void nativeOnBatteryChargingChanged();
|
| -
|
| + private static native void nativeOnMainActivitySuspended();
|
| + private static native void nativeOnMainActivityResumed();
|
| }
|
|
|