File tree 2 files changed +23
-5
lines changed
androidTest/java/com/google/firebase/crashlytics/internal/common
main/java/com/google/firebase/crashlytics/internal/common
2 files changed +23
-5
lines changed Original file line number Diff line number Diff line change @@ -58,6 +58,18 @@ public void testNullIntent() {
58
58
assertEquals (1 , state .getBatteryVelocity ());
59
59
}
60
60
61
+ public void testTooManyReceivers () {
62
+ Context mockContext = mock (Context .class );
63
+ when (mockContext .registerReceiver (isNull (), any ()))
64
+ .thenThrow (new IllegalStateException ("Too many receivers" ));
65
+
66
+ BatteryState state = BatteryState .get (mockContext );
67
+
68
+ assertNull (state .getBatteryLevel ());
69
+ assertFalse (state .isPowerConnected ());
70
+ assertEquals (1 , state .getBatteryVelocity ());
71
+ }
72
+
61
73
public void testEmptyIntent () {
62
74
final Context mockContext = mock (Context .class );
63
75
when (mockContext .registerReceiver (isNull (), any ())).thenReturn (new Intent ());
Original file line number Diff line number Diff line change 18
18
import android .content .Intent ;
19
19
import android .content .IntentFilter ;
20
20
import android .os .BatteryManager ;
21
+ import com .google .firebase .crashlytics .internal .Logger ;
21
22
22
23
/** A utility class representing the state of the battery. */
23
24
class BatteryState {
@@ -66,11 +67,16 @@ public static BatteryState get(Context context) {
66
67
boolean powerConnected = false ;
67
68
Float level = null ;
68
69
69
- final IntentFilter ifilter = new IntentFilter (Intent .ACTION_BATTERY_CHANGED );
70
- final Intent batteryStatusIntent = context .registerReceiver (null , ifilter );
71
- if (batteryStatusIntent != null ) {
72
- powerConnected = isPowerConnected (batteryStatusIntent );
73
- level = getLevel (batteryStatusIntent );
70
+ try {
71
+ final IntentFilter ifilter = new IntentFilter (Intent .ACTION_BATTERY_CHANGED );
72
+ final Intent batteryStatusIntent = context .registerReceiver (/*receiver=*/ null , ifilter );
73
+ if (batteryStatusIntent != null ) {
74
+ powerConnected = isPowerConnected (batteryStatusIntent );
75
+ level = getLevel (batteryStatusIntent );
76
+ }
77
+ } catch (IllegalStateException ex ) {
78
+ // This happens on some devices when the app registers too many receivers.
79
+ Logger .getLogger ().e ("An error occurred getting battery state." , ex );
74
80
}
75
81
76
82
return new BatteryState (level , powerConnected );
You can’t perform that action at this time.
0 commit comments