@Test public void batteryStateChanges() { Application application = RuntimeEnvironment.application; TestSubscriber<BatteryState> o = new TestSubscriber<>(); RxBatteryManager.batteryChanges(application).subscribe(o); o.assertValues(); Intent intent1 = new Intent(Intent.ACTION_BATTERY_CHANGED) .putExtra(BatteryManager.EXTRA_HEALTH, BatteryManager.BATTERY_HEALTH_COLD) .putExtra(BatteryManager.EXTRA_ICON_SMALL, 0x3def2) .putExtra(BatteryManager.EXTRA_LEVEL, 10) .putExtra(BatteryManager.EXTRA_PLUGGED, 0) .putExtra(BatteryManager.EXTRA_PRESENT, true) .putExtra(BatteryManager.EXTRA_SCALE, 100) .putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_CHARGING) .putExtra(BatteryManager.EXTRA_TECHNOLOGY, "unknown") .putExtra(BatteryManager.EXTRA_TEMPERATURE, 40) .putExtra(BatteryManager.EXTRA_VOLTAGE, 10000); application.sendBroadcast(intent1); BatteryState event1 = BatteryState.create(BatteryHealth.COLD, 0x3def2, 10, 0, true, 100, BatteryStatus.CHARGING, "unknown", 40, 10000); o.assertValues(event1); }
@CheckResult @NonNull public static Observable<BatteryState> batteryChanges(@NonNull final Context context) { checkNotNull(context, "context == null"); IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); return RxBroadcastReceiver.create(context, filter) .map(new Func1<Intent, BatteryState>() { @Override public BatteryState call(Intent intent) { int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1); int iconSmall = intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, -1); int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY); int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); return BatteryState.create(BatteryHealth.of(health), iconSmall, level, plugged, present, scale, BatteryStatus.of(status), technology, temperature, voltage); } }); }
RxBatteryManager { @CheckResult @NonNull public static Observable<BatteryState> batteryChanges(@NonNull final Context context) { checkNotNull(context, "context == null"); IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); return RxBroadcastReceiver.create(context, filter) .map(new Func1<Intent, BatteryState>() { @Override public BatteryState call(Intent intent) { int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1); int iconSmall = intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, -1); int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY); int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); return BatteryState.create(BatteryHealth.of(health), iconSmall, level, plugged, present, scale, BatteryStatus.of(status), technology, temperature, voltage); } }); } }
RxBatteryManager { @CheckResult @NonNull public static Observable<BatteryState> batteryChanges(@NonNull final Context context) { checkNotNull(context, "context == null"); IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); return RxBroadcastReceiver.create(context, filter) .map(new Func1<Intent, BatteryState>() { @Override public BatteryState call(Intent intent) { int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1); int iconSmall = intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, -1); int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY); int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); return BatteryState.create(BatteryHealth.of(health), iconSmall, level, plugged, present, scale, BatteryStatus.of(status), technology, temperature, voltage); } }); } private  RxBatteryManager(); }
RxBatteryManager { @CheckResult @NonNull public static Observable<BatteryState> batteryChanges(@NonNull final Context context) { checkNotNull(context, "context == null"); IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); return RxBroadcastReceiver.create(context, filter) .map(new Func1<Intent, BatteryState>() { @Override public BatteryState call(Intent intent) { int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1); int iconSmall = intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, -1); int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY); int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); return BatteryState.create(BatteryHealth.of(health), iconSmall, level, plugged, present, scale, BatteryStatus.of(status), technology, temperature, voltage); } }); } private  RxBatteryManager(); @CheckResult @NonNull // static Observable<BatteryState> batteryChanges(@NonNull final Context context); }
RxBatteryManager { @CheckResult @NonNull public static Observable<BatteryState> batteryChanges(@NonNull final Context context) { checkNotNull(context, "context == null"); IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); return RxBroadcastReceiver.create(context, filter) .map(new Func1<Intent, BatteryState>() { @Override public BatteryState call(Intent intent) { int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1); int iconSmall = intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, -1); int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean present = intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false); int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY); int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); return BatteryState.create(BatteryHealth.of(health), iconSmall, level, plugged, present, scale, BatteryStatus.of(status), technology, temperature, voltage); } }); } private  RxBatteryManager(); @CheckResult @NonNull // static Observable<BatteryState> batteryChanges(@NonNull final Context context);  }