This source file includes following definitions.
- JNINamespace
- createForTests
- create
- onBatteryChargingChanged
- onApplicationStateChange
- isBatteryPower
- nativeOnBatteryChargingChanged
- nativeOnMainActivitySuspended
- nativeOnMainActivityResumed
package org.chromium.base;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Handler;
import android.os.Looper;
@JNINamespace("base::android")
public class PowerMonitor implements ApplicationStatus.ApplicationStateListener {
private static final long SUSPEND_DELAY_MS = 1 * 60 * 1000;
private static class LazyHolder {
private static final PowerMonitor INSTANCE = new PowerMonitor();
}
private static PowerMonitor sInstance;
private boolean mIsBatteryPower;
private final Handler mHandler = new Handler(Looper.getMainLooper());
private static final Runnable sSuspendTask = new Runnable() {
@Override
public void run() {
nativeOnMainActivitySuspended();
}
};
public static void createForTests(Context context) {
sInstance = LazyHolder.INSTANCE;
}
public static void create(Context context) {
context = context.getApplicationContext();
if (sInstance == null) {
sInstance = LazyHolder.INSTANCE;
ApplicationStatus.registerApplicationStateListener(sInstance);
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatusIntent = context.registerReceiver(null, ifilter);
onBatteryChargingChanged(batteryStatusIntent);
}
}
private PowerMonitor() {
}
public static void onBatteryChargingChanged(Intent intent) {
if (sInstance == null) {
return;
}
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
sInstance.mIsBatteryPower = chargePlug != BatteryManager.BATTERY_PLUGGED_USB &&
chargePlug != BatteryManager.BATTERY_PLUGGED_AC;
nativeOnBatteryChargingChanged();
}
@Override
public void onApplicationStateChange(int newState) {
if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) {
mHandler.removeCallbacks(sSuspendTask);
nativeOnMainActivityResumed();
} else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) {
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();
}