Chromium Code Reviews| Index: base/android/java/src/org/chromium/base/PowerMonitor.java |
| diff --git a/base/android/java/src/org/chromium/base/PowerMonitor.java b/base/android/java/src/org/chromium/base/PowerMonitor.java |
| index 334a0b56d367e3e69b8f0979217fa3a712ac931a..34079c55deea9f279de3af28a528b058fb53afe0 100644 |
| --- a/base/android/java/src/org/chromium/base/PowerMonitor.java |
| +++ b/base/android/java/src/org/chromium/base/PowerMonitor.java |
| @@ -4,6 +4,7 @@ |
| package org.chromium.base; |
| +import android.content.BroadcastReceiver; |
| import android.content.Context; |
| import android.content.Intent; |
| import android.content.IntentFilter; |
| @@ -14,7 +15,6 @@ import android.os.Looper; |
| import org.chromium.base.annotations.CalledByNative; |
| import org.chromium.base.annotations.JNINamespace; |
| - |
| /** |
| * Integrates native PowerMonitor with the java side. |
| */ |
| @@ -28,7 +28,7 @@ public class PowerMonitor { |
| private boolean mIsBatteryPower; |
| private final Handler mHandler = new Handler(Looper.getMainLooper()); |
| - public static void createForTests(Context context) { |
| + public static void createForTests() { |
| // 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. |
| @@ -37,28 +37,32 @@ public class PowerMonitor { |
| /** |
| * Create a PowerMonitor instance if none exists. |
| - * @param context The context to register broadcast receivers for. The application context |
| - * will be used from this parameter. |
| */ |
| - public static void create(Context context) { |
| - context = context.getApplicationContext(); |
| - if (sInstance == null) { |
| - sInstance = LazyHolder.INSTANCE; |
| - IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); |
| - Intent batteryStatusIntent = context.registerReceiver(null, ifilter); |
| - if (batteryStatusIntent != null) onBatteryChargingChanged(batteryStatusIntent); |
| - } |
| + public static void create() { |
| + if (sInstance != null) return; |
|
nyquist
2017/01/23 22:42:01
Since this is a public method, should we assert an
Ted C
2017/01/23 22:49:34
Done.
|
| + |
| + Context context = ContextUtils.getApplicationContext(); |
| + sInstance = LazyHolder.INSTANCE; |
| + IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); |
| + Intent batteryStatusIntent = context.registerReceiver(null, ifilter); |
| + if (batteryStatusIntent != null) onBatteryChargingChanged(batteryStatusIntent); |
| + |
| + IntentFilter powerConnectedFilter = new IntentFilter(); |
| + powerConnectedFilter.addAction(Intent.ACTION_POWER_CONNECTED); |
| + powerConnectedFilter.addAction(Intent.ACTION_POWER_DISCONNECTED); |
| + context.registerReceiver(new BroadcastReceiver() { |
| + @Override |
| + public void onReceive(Context context, Intent intent) { |
| + PowerMonitor.onBatteryChargingChanged(intent); |
| + } |
| + }, powerConnectedFilter); |
| } |
| private PowerMonitor() { |
| } |
| - 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. |
| - return; |
| - } |
| + private static void onBatteryChargingChanged(Intent intent) { |
| + assert sInstance != null; |
| int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); |
| // If we're not plugged, assume we're running on battery power. |
| sInstance.mIsBatteryPower = chargePlug != BatteryManager.BATTERY_PLUGGED_USB |
| @@ -68,6 +72,11 @@ public class PowerMonitor { |
| @CalledByNative |
| private static boolean isBatteryPower() { |
| + // Creation of the PowerMonitor can be deferred based on the browser startup path. If the |
| + // battery power is requested prior to the browser triggering the creation, force it to be |
| + // created now. |
| + if (sInstance == null) create(); |
| + |
| return sInstance.mIsBatteryPower; |
| } |