From 242ec83248d6b8929e92d9c3655b6c50d275e3db Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 10 Jan 2016 10:14:32 +0100 Subject: [PATCH] Another attempt to workaround start at boot issues --- .../java/eu/faircode/netguard/Receiver.java | 7 +++--- .../eu/faircode/netguard/SinkholeService.java | 12 +++++++--- .../java/eu/faircode/netguard/Widget.java | 24 +++++++++---------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/eu/faircode/netguard/Receiver.java b/app/src/main/java/eu/faircode/netguard/Receiver.java index c654a678..55624438 100644 --- a/app/src/main/java/eu/faircode/netguard/Receiver.java +++ b/app/src/main/java/eu/faircode/netguard/Receiver.java @@ -84,10 +84,9 @@ public class Receiver extends BroadcastReceiver { SinkholeService.start("receiver", context); } else if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction())) { - if (prefs.getBoolean("enabled", false)) { - if (VpnService.prepare(context) == null) - SinkholeService.start("receiver", context); - } else if (prefs.getBoolean("show_stats", false)) + if (prefs.getBoolean("enabled", false)) + SinkholeService.start("receiver", context); + else if (prefs.getBoolean("show_stats", false)) SinkholeService.run("receiver", context); } } catch (Throwable ex) { diff --git a/app/src/main/java/eu/faircode/netguard/SinkholeService.java b/app/src/main/java/eu/faircode/netguard/SinkholeService.java index f3995118..994b8672 100644 --- a/app/src/main/java/eu/faircode/netguard/SinkholeService.java +++ b/app/src/main/java/eu/faircode/netguard/SinkholeService.java @@ -183,6 +183,13 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS String reason = intent.getStringExtra(EXTRA_REASON); Log.i(TAG, "Executing intent=" + intent + " command=" + cmd + " reason=" + reason + " vpn=" + (vpn != null)); + // Check if prepared + if (cmd == Command.start || cmd == Command.reload) + if (VpnService.prepare(SinkholeService.this) != null) { + Log.w(TAG, "VPN not prepared"); + return; + } + // Listen for phone state changes TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); if (tm != null && !phone_state && @@ -306,14 +313,13 @@ public class SinkholeService extends VpnService implements SharedPreferences.OnS } catch (Throwable ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); - if (Util.isConnected(SinkholeService.this)) { + if (!(ex instanceof IllegalStateException)) { // Disable firewall prefs.edit().putBoolean("enabled", false).apply(); Widget.updateWidgets(SinkholeService.this); // Report exception - if (!(ex instanceof IllegalStateException)) - Util.sendCrashReport(ex, SinkholeService.this); + Util.sendCrashReport(ex, SinkholeService.this); } } } diff --git a/app/src/main/java/eu/faircode/netguard/Widget.java b/app/src/main/java/eu/faircode/netguard/Widget.java index a6d0bc78..5bf34aa3 100644 --- a/app/src/main/java/eu/faircode/netguard/Widget.java +++ b/app/src/main/java/eu/faircode/netguard/Widget.java @@ -76,10 +76,8 @@ public class Widget extends AppWidgetProvider { } else if (INTENT_ON.equals(intent.getAction())) try { - if (VpnService.prepare(context) == null) { - prefs.edit().putBoolean("enabled", true).apply(); - SinkholeService.start("widget", context); - } + prefs.edit().putBoolean("enabled", true).apply(); + SinkholeService.start("widget", context); } catch (Throwable ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); Util.sendCrashReport(ex, context); @@ -91,21 +89,23 @@ public class Widget extends AppWidgetProvider { boolean enabled = prefs.getBoolean("enabled", false); try { - PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, ActivityMain.class), PendingIntent.FLAG_UPDATE_CURRENT); try { + PendingIntent pi; if (VpnService.prepare(context) == null) pi = PendingIntent.getBroadcast(context, 0, new Intent(enabled ? INTENT_OFF : INTENT_ON), PendingIntent.FLAG_UPDATE_CURRENT); + else + pi = PendingIntent.getActivity(context, 0, new Intent(context, ActivityMain.class), PendingIntent.FLAG_UPDATE_CURRENT); + + for (int id : appWidgetIds) { + RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); + views.setOnClickPendingIntent(R.id.ivEnabled, pi); + views.setImageViewResource(R.id.ivEnabled, enabled ? R.mipmap.ic_launcher : R.drawable.ic_security_white_24dp_60); + appWidgetManager.updateAppWidget(id, views); + } } catch (Throwable ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); Util.sendCrashReport(ex, context); } - - for (int id : appWidgetIds) { - RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget); - views.setOnClickPendingIntent(R.id.ivEnabled, pi); - views.setImageViewResource(R.id.ivEnabled, enabled ? R.mipmap.ic_launcher : R.drawable.ic_security_white_24dp_60); - appWidgetManager.updateAppWidget(id, views); - } } catch (Throwable ex) { Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex)); Util.sendCrashReport(ex, context);