@Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "Received " + intent); Util.logExtras(intent); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SinkholeService.this); int delay = Integer.parseInt(prefs.getString("screen_delay", "0")); boolean interactive = Intent.ACTION_SCREEN_ON.equals(intent.getAction()); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); PendingIntent pi = PendingIntent.getBroadcast( context, 0, new Intent(ACTION_SCREEN_OFF_DELAYED), PendingIntent.FLAG_UPDATE_CURRENT); am.cancel(pi); if (interactive || delay == 0) { last_interactive = interactive; reload(null, "interactive state changed", SinkholeService.this); } else { if (ACTION_SCREEN_OFF_DELAYED.equals(intent.getAction())) { last_interactive = interactive; reload(null, "interactive state changed", SinkholeService.this); } else { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) am.set(AlarmManager.RTC_WAKEUP, new Date().getTime() + delay * 60 * 1000L, pi); else am.setAndAllowWhileIdle( AlarmManager.RTC_WAKEUP, new Date().getTime() + delay * 60 * 1000L, pi); } } // Start/stop stats PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); mServiceHandler.sendEmptyMessage(pm.isInteractive() ? MSG_STATS_START : MSG_STATS_STOP); }
@SuppressWarnings("deprecation") @SuppressLint("NewApi") public static boolean isScreenOn(@NonNull PowerManager pm) { return Device.hasKitKatWatchApi() ? pm.isInteractive() : pm.isScreenOn(); }