Another attempt to workaround start at boot issues

This commit is contained in:
M66B 2016-01-10 10:14:32 +01:00
parent 06f213594c
commit 242ec83248
3 changed files with 24 additions and 19 deletions

View File

@ -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) {

View File

@ -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);
}
}
}

View File

@ -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);