From 77a53b34dab255a5c600dc18ba47eae9c0ecd2bf Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 24 Dec 2017 09:02:13 +0100 Subject: [PATCH] Retry interactive failed state change --- .../eu/faircode/netguard/ServiceSinkhole.java | 53 +++++++++++-------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/ServiceSinkhole.java b/app/src/main/java/eu/faircode/netguard/ServiceSinkhole.java index 5ae6af6d..b23c635e 100644 --- a/app/src/main/java/eu/faircode/netguard/ServiceSinkhole.java +++ b/app/src/main/java/eu/faircode/netguard/ServiceSinkhole.java @@ -1916,39 +1916,48 @@ public class ServiceSinkhole extends VpnService implements SharedPreferences.OnS executor.submit(new Runnable() { @Override public void run() { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSinkhole.this); - int delay; - try { - delay = Integer.parseInt(prefs.getString("screen_delay", "0")); - } catch (NumberFormatException ignored) { - delay = 0; - } - boolean interactive = Intent.ACTION_SCREEN_ON.equals(intent.getAction()); - AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent i = new Intent(ACTION_SCREEN_OFF_DELAYED); i.setPackage(context.getPackageName()); PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); am.cancel(pi); - if (interactive || delay == 0) { - last_interactive = interactive; - reload("interactive state changed", ServiceSinkhole.this, true); - } else { - if (ACTION_SCREEN_OFF_DELAYED.equals(intent.getAction())) { + try { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSinkhole.this); + int delay; + try { + delay = Integer.parseInt(prefs.getString("screen_delay", "0")); + } catch (NumberFormatException ignored) { + delay = 0; + } + boolean interactive = Intent.ACTION_SCREEN_ON.equals(intent.getAction()); + + if (interactive || delay == 0) { last_interactive = interactive; reload("interactive state changed", ServiceSinkhole.this, true); } 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); + if (ACTION_SCREEN_OFF_DELAYED.equals(intent.getAction())) { + last_interactive = interactive; + reload("interactive state changed", ServiceSinkhole.this, true); + } 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 - statsHandler.sendEmptyMessage( - Util.isInteractive(ServiceSinkhole.this) && !powersaving ? MSG_STATS_START : MSG_STATS_STOP); + // Start/stop stats + statsHandler.sendEmptyMessage( + Util.isInteractive(ServiceSinkhole.this) && !powersaving ? MSG_STATS_START : MSG_STATS_STOP); + } catch (Throwable ex) { + Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) + am.set(AlarmManager.RTC_WAKEUP, new Date().getTime() + 15 * 1000L, pi); + else + am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, new Date().getTime() + 15 * 1000L, pi); + } } }); }