OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.base; | 5 package org.chromium.base; |
6 | 6 |
7 import android.content.BroadcastReceiver; | |
7 import android.content.Context; | 8 import android.content.Context; |
8 import android.content.Intent; | 9 import android.content.Intent; |
9 import android.content.IntentFilter; | 10 import android.content.IntentFilter; |
10 import android.os.BatteryManager; | 11 import android.os.BatteryManager; |
11 import android.os.Handler; | 12 import android.os.Handler; |
12 import android.os.Looper; | 13 import android.os.Looper; |
13 | 14 |
14 import org.chromium.base.annotations.CalledByNative; | 15 import org.chromium.base.annotations.CalledByNative; |
15 import org.chromium.base.annotations.JNINamespace; | 16 import org.chromium.base.annotations.JNINamespace; |
16 | 17 |
17 | |
18 /** | 18 /** |
19 * Integrates native PowerMonitor with the java side. | 19 * Integrates native PowerMonitor with the java side. |
20 */ | 20 */ |
21 @JNINamespace("base::android") | 21 @JNINamespace("base::android") |
22 public class PowerMonitor { | 22 public class PowerMonitor { |
23 private static class LazyHolder { | 23 private static class LazyHolder { |
24 private static final PowerMonitor INSTANCE = new PowerMonitor(); | 24 private static final PowerMonitor INSTANCE = new PowerMonitor(); |
25 } | 25 } |
26 private static PowerMonitor sInstance; | 26 private static PowerMonitor sInstance; |
27 | 27 |
28 private boolean mIsBatteryPower; | 28 private boolean mIsBatteryPower; |
29 private final Handler mHandler = new Handler(Looper.getMainLooper()); | 29 private final Handler mHandler = new Handler(Looper.getMainLooper()); |
30 | 30 |
31 public static void createForTests(Context context) { | 31 public static void createForTests() { |
32 // Applications will create this once the JNI side has been fully wired up both sides. For | 32 // Applications will create this once the JNI side has been fully wired up both sides. For |
33 // tests, we just need native -> java, that is, we don't need to notify java -> native on | 33 // tests, we just need native -> java, that is, we don't need to notify java -> native on |
34 // creation. | 34 // creation. |
35 sInstance = LazyHolder.INSTANCE; | 35 sInstance = LazyHolder.INSTANCE; |
36 } | 36 } |
37 | 37 |
38 /** | 38 /** |
39 * Create a PowerMonitor instance if none exists. | 39 * Create a PowerMonitor instance if none exists. |
40 * @param context The context to register broadcast receivers for. The appl ication context | |
41 * will be used from this parameter. | |
42 */ | 40 */ |
43 public static void create(Context context) { | 41 public static void create() { |
44 context = context.getApplicationContext(); | 42 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.
| |
45 if (sInstance == null) { | 43 |
46 sInstance = LazyHolder.INSTANCE; | 44 Context context = ContextUtils.getApplicationContext(); |
47 IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGE D); | 45 sInstance = LazyHolder.INSTANCE; |
48 Intent batteryStatusIntent = context.registerReceiver(null, ifilter) ; | 46 IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); |
49 if (batteryStatusIntent != null) onBatteryChargingChanged(batterySta tusIntent); | 47 Intent batteryStatusIntent = context.registerReceiver(null, ifilter); |
50 } | 48 if (batteryStatusIntent != null) onBatteryChargingChanged(batteryStatusI ntent); |
49 | |
50 IntentFilter powerConnectedFilter = new IntentFilter(); | |
51 powerConnectedFilter.addAction(Intent.ACTION_POWER_CONNECTED); | |
52 powerConnectedFilter.addAction(Intent.ACTION_POWER_DISCONNECTED); | |
53 context.registerReceiver(new BroadcastReceiver() { | |
54 @Override | |
55 public void onReceive(Context context, Intent intent) { | |
56 PowerMonitor.onBatteryChargingChanged(intent); | |
57 } | |
58 }, powerConnectedFilter); | |
51 } | 59 } |
52 | 60 |
53 private PowerMonitor() { | 61 private PowerMonitor() { |
54 } | 62 } |
55 | 63 |
56 public static void onBatteryChargingChanged(Intent intent) { | 64 private static void onBatteryChargingChanged(Intent intent) { |
57 if (sInstance == null) { | 65 assert sInstance != null; |
58 // We may be called by the framework intent-filter before being full y initialized. This | |
59 // is not a problem, since our constructor will check for the state later on. | |
60 return; | |
61 } | |
62 int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); | 66 int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); |
63 // If we're not plugged, assume we're running on battery power. | 67 // If we're not plugged, assume we're running on battery power. |
64 sInstance.mIsBatteryPower = chargePlug != BatteryManager.BATTERY_PLUGGED _USB | 68 sInstance.mIsBatteryPower = chargePlug != BatteryManager.BATTERY_PLUGGED _USB |
65 && chargePlug != BatteryManager.BATTERY_PLUGGED_AC; | 69 && chargePlug != BatteryManager.BATTERY_PLUGGED_AC; |
66 nativeOnBatteryChargingChanged(); | 70 nativeOnBatteryChargingChanged(); |
67 } | 71 } |
68 | 72 |
69 @CalledByNative | 73 @CalledByNative |
70 private static boolean isBatteryPower() { | 74 private static boolean isBatteryPower() { |
75 // Creation of the PowerMonitor can be deferred based on the browser sta rtup path. If the | |
76 // battery power is requested prior to the browser triggering the creati on, force it to be | |
77 // created now. | |
78 if (sInstance == null) create(); | |
79 | |
71 return sInstance.mIsBatteryPower; | 80 return sInstance.mIsBatteryPower; |
72 } | 81 } |
73 | 82 |
74 private static native void nativeOnBatteryChargingChanged(); | 83 private static native void nativeOnBatteryChargingChanged(); |
75 } | 84 } |
OLD | NEW |