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; }
/** * Returns true if the device is in Doze/Idle mode. Should be called before checking the network connection because * the ConnectionManager may report the device is connected when it isn't during Idle mode. */ @TargetApi(23) private static boolean isDozing(Context context) { if (VERSION.SDK_INT >= 23) { PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); return powerManager.isDeviceIdleMode() && !powerManager.isIgnoringBatteryOptimizations(context.getPackageName()); } else { return false; } }
private boolean checkIdleRequirement(Context context) { if (!isIdleRequired()) { return true; } PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); return Util.SDK_INT >= 23 ? powerManager.isDeviceIdleMode() : Util.SDK_INT >= 20 ? !powerManager.isInteractive() : !powerManager.isScreenOn(); }
@SuppressWarnings("deprecation") public static boolean isIdle(Context context) { PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { /* * isDeviceIdleMode() is a very strong requirement and could cause a job * to be never run. isDeviceIdleMode() returns true in doze mode, but jobs * are delayed until the device leaves doze mode */ return powerManager.isDeviceIdleMode() || !powerManager.isInteractive(); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { return !powerManager.isInteractive(); } else { return !powerManager.isScreenOn(); } }
@TargetApi(Build.VERSION_CODES.M) private void preparePowerManagerMocks(final Boolean idleMode, final Boolean ignoreOptimizations) { final String packageName = "com.github.pwittchen.test"; when(contextMock.getPackageName()).thenReturn(packageName); when(contextMock.getSystemService(Context.POWER_SERVICE)).thenReturn(powerManager); when(powerManager.isDeviceIdleMode()).thenReturn(idleMode); when(powerManager.isIgnoringBatteryOptimizations(packageName)).thenReturn(ignoreOptimizations); }
@SuppressWarnings("deprecation") private void setupDeviceIdle(Job job, boolean requirement, boolean deviceIdle) { PowerManager powerManager = mock(PowerManager.class); when(powerManager.isDeviceIdleMode()).thenReturn(deviceIdle); when(powerManager.isInteractive()).thenReturn(!deviceIdle); when(powerManager.isScreenOn()).thenReturn(!deviceIdle); when(powerManager.isInteractive()).thenReturn(!deviceIdle); when(job.getParams().getRequest().requiresDeviceIdle()).thenReturn(requirement); when(job.getContext().getSystemService(Context.POWER_SERVICE)).thenReturn(powerManager); }
@Test @Config(minSdk = M) public void isDeviceIdleMode_shouldGetAndSet() { assertThat(powerManager.isDeviceIdleMode()).isFalse(); shadowOf(powerManager).setIsDeviceIdleMode(true); assertThat(powerManager.isDeviceIdleMode()).isTrue(); shadowOf(powerManager).setIsDeviceIdleMode(false); assertThat(powerManager.isDeviceIdleMode()).isFalse(); }
public static boolean isDeviceInIdleState(Context context) { if (hasMarshmallow()) { PowerManager pm = context.getSystemService(PowerManager.class); if (pm != null && pm.isDeviceIdleMode()) { return true; } } return false; }
@Override @TargetApi(Build.VERSION_CODES.M) public void onReceive(Context context, Intent intent) { Log.i(TAG, "Received " + intent); Util.logExtras(intent); PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); Log.i(TAG, "device idle=" + pm.isDeviceIdleMode()); // Reload rules when coming from idle mode if (!pm.isDeviceIdleMode()) reload("idle state changed", KcaVpnService.this); } };
@Override public void onReceive(Context context, Intent intent) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); MyLog.v(this, () -> "onReceive " + (pm == null || pm.isDeviceIdleMode() ? " idle" : " " + UriUtils.getConnectionState(context)) ); if (pm == null || pm.isDeviceIdleMode()) return; initializeApp(context); }
@NonNull public static DozeState getDozeState(@NonNull Context context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { return DozeState.IRRELEVANT_OLD_ANDROID_API; } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { return DozeState.UNKNOWN_TOO_OLD_ANDROID_API_FOR_CHECKING; } final PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (pm == null) { return DozeState.ERROR_GETTING_STATE; } return pm.isDeviceIdleMode() ? DozeState.DOZE_TURNED_ON_IDLE : pm.isInteractive() ? DozeState.NORMAL_INTERACTIVE : DozeState.NORMAL_NON_INTERACTIVE; }
@SuppressWarnings({"deprecation", "ConstantConditions"}) public static boolean isIdle(Context context) { PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return powerManager.isDeviceIdleMode() || !powerManager.isInteractive(); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { return !powerManager.isInteractive(); } else { return !powerManager.isScreenOn(); } }
@SuppressWarnings("deprecation") public static boolean isIdle(Context context) { PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { /* * isDeviceIdleMode() is a very strong requirement and could cause a job * to be never run. isDeviceIdleMode() returns true in doze mode, but jobs * are delayed until the device leaves doze mode */ return powerManager.isDeviceIdleMode() || !powerManager.isInteractive(); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) { return !powerManager.isInteractive(); } else { return !powerManager.isScreenOn(); } }