MarshmallowNetworkObservingStrategy implements NetworkObservingStrategy { protected boolean isIdleMode(final Context context) { final String packageName = context.getPackageName(); final PowerManager manager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); boolean isIgnoringOptimizations = manager.isIgnoringBatteryOptimizations(packageName); return manager.isDeviceIdleMode() && !isIgnoringOptimizations; } @SuppressWarnings("NullAway") // networkCallback cannot be initialized here  MarshmallowNetworkObservingStrategy(); @Override Observable<Connectivity> observeNetworkConnectivity(final Context context); @Override void onError(final String message, final Exception exception);  }
@Test public void shouldNotBeInIdleModeWhenDeviceIsNotInIdleAndIsNotIgnoringBatteryOptimizations() { preparePowerManagerMocks(Boolean.FALSE, Boolean.FALSE); final boolean isIdleMode = strategy.isIdleMode(contextMock); assertThat(isIdleMode).isFalse(); }
@Test public void shouldBeInIdleModeWhenDeviceIsNotIgnoringBatteryOptimizations() { preparePowerManagerMocks(Boolean.TRUE, Boolean.FALSE); final boolean isIdleMode = strategy.isIdleMode(contextMock); assertThat(isIdleMode).isTrue(); }
@Test public void shouldNotBeInIdleModeWhenDeviceIsInIdleModeAndIgnoringBatteryOptimizations() { preparePowerManagerMocks(Boolean.TRUE, Boolean.TRUE); final boolean isIdleMode = strategy.isIdleMode(contextMock); assertThat(isIdleMode).isFalse(); }
@Test public void shouldNotBeInIdleModeWhenDeviceIsNotInIdleMode() { preparePowerManagerMocks(Boolean.FALSE, Boolean.TRUE); final boolean isIdleMode = strategy.isIdleMode(contextMock); assertThat(isIdleMode).isFalse(); }