Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2425)

Unified Diff: base/android/java/src/org/chromium/base/PowerMonitor.java

Issue 2642333003: Dynamically register the power broadcast receiver. (Closed)
Patch Set: Assert on UI thread for creation Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/BUILD.gn ('k') | base/android/java/src/org/chromium/base/PowerStatusReceiver.java » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..bd06d9b77a31077d121b241e27fbef40e90aa67d 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,34 @@ 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() {
+ ThreadUtils.assertOnUiThread();
+
+ if (sInstance != null) return;
+
+ 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 +74,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;
}
« no previous file with comments | « base/BUILD.gn ('k') | base/android/java/src/org/chromium/base/PowerStatusReceiver.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698