@CheckReturnValue
  @NonNull public static Observable<Boolean> observePowerSavingState(
      @NonNull Context context, @StringRes int enablePrefResId, @BoolRes int defaultValueResId) {
    final RxSharedPrefs prefs = AnyApplication.prefs(context);
    return Observable.combineLatest(
            prefs
                .getString(
                    R.string.settings_key_power_save_mode,
                    R.string.settings_default_power_save_mode_value)
                .asObservable(),
            enablePrefResId == 0
                ? Observable.just(true)
                : prefs.getBoolean(enablePrefResId, defaultValueResId).asObservable(),
            RxBroadcastReceivers.fromIntentFilter(
                    context.getApplicationContext(), getBatteryStateIntentFilter())
                .startWith(new Intent(Intent.ACTION_BATTERY_OKAY)),
            RxBroadcastReceivers.fromIntentFilter(
                    context.getApplicationContext(), getChargerStateIntentFilter())
                .startWith(new Intent(Intent.ACTION_POWER_DISCONNECTED)),
            getOsPowerSavingStateObservable(context),
            (powerSavingPref, enabledPref, batteryIntent, chargerIntent, osPowerSavingState) -> {
              if (!enabledPref) return false;

              switch (powerSavingPref) {
                case "never":
                  return false;
                case "always":
                  return true;
                default:
                  return osPowerSavingState
                      || (Intent.ACTION_BATTERY_LOW.equals(batteryIntent.getAction())
                          && Intent.ACTION_POWER_DISCONNECTED.equals(chargerIntent.getAction()));
              }
            })
        .distinctUntilChanged();
  }
@Test
  @Config(sdk = Build.VERSION_CODES.LOLLIPOP)
  public void testWhenLowPowerSavingModeWithDevicePowerSavingState() {
    Context context = Mockito.spy(getApplicationContext());
    final PowerManager powerManager =
        (PowerManager) getApplicationContext().getSystemService(Service.POWER_SERVICE);
    Mockito.doReturn(powerManager).when(context).getSystemService(Service.POWER_SERVICE);
    ShadowPowerManager shadowPowerManager = Shadows.shadowOf(powerManager);

    AtomicReference<Boolean> state = new AtomicReference<>(null);
    final Observable<Boolean> powerSavingState = PowerSaving.observePowerSavingState(context, 0);
    Assert.assertNull(state.get());

    final Disposable disposable = powerSavingState.subscribe(state::set);
    // starts as false
    Assert.assertEquals(Boolean.FALSE, state.get());

    sendPowerSavingState(shadowPowerManager, false);
    Assert.assertEquals(Boolean.FALSE, state.get());

    sendPowerSavingState(shadowPowerManager, true);
    Assert.assertEquals(Boolean.TRUE, state.get());

    sendPowerSavingState(shadowPowerManager, false);
    Assert.assertEquals(Boolean.FALSE, state.get());

    disposable.dispose();

    sendPowerSavingState(shadowPowerManager, true);
    Assert.assertEquals(Boolean.FALSE, state.get());
    sendPowerSavingState(shadowPowerManager, false);
    Assert.assertEquals(Boolean.FALSE, state.get());
  }