Error handling

This commit is contained in:
M66B 2015-10-25 19:02:33 +01:00
parent 56d3def908
commit 9877db4cfd
3 changed files with 40 additions and 7 deletions

View File

@ -25,6 +25,7 @@ import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
@ -58,13 +59,19 @@ public class ActivityMain extends AppCompatActivity implements SharedPreferences
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Log.i(TAG, "Switch on");
Intent intent = VpnService.prepare(ActivityMain.this);
if (intent == null) {
Intent prepare = VpnService.prepare(ActivityMain.this);
if (prepare == null) {
Log.e(TAG, "Prepare done");
onActivityResult(REQUEST_VPN, RESULT_OK, null);
} else {
Log.i(TAG, "Start intent=" + intent);
startActivityForResult(intent, REQUEST_VPN);
Log.i(TAG, "Start intent=" + prepare);
try {
startActivityForResult(prepare, REQUEST_VPN);
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
onActivityResult(REQUEST_VPN, RESULT_CANCELED, null);
Toast.makeText(ActivityMain.this, ex.toString(), Toast.LENGTH_LONG).show();
}
}
} else {
Log.i(TAG, "Switch off");

View File

@ -12,6 +12,7 @@ import android.net.VpnService;
import android.os.ParcelFileDescriptor;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
import java.io.IOException;
@ -70,7 +71,8 @@ public class BlackHoleService extends VpnService {
Log.i(TAG, "Allowing " + rule.info.packageName);
try {
builder.addDisallowedApplication(rule.info.packageName);
} catch (PackageManager.NameNotFoundException ignored) {
} catch (PackageManager.NameNotFoundException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
}
@ -80,7 +82,18 @@ public class BlackHoleService extends VpnService {
builder.setConfigureIntent(pi);
// Start VPN service
vpn = builder.establish();
try {
vpn = builder.establish();
} catch (Throwable ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
// Disable firewall
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("enabled", false).apply();
// Feedback
Util.toast(ex.toString(), Toast.LENGTH_LONG, this);
}
}
private void vpnStop() {
@ -88,7 +101,8 @@ public class BlackHoleService extends VpnService {
try {
vpn.close();
vpn = null;
} catch (IOException ignored) {
} catch (IOException ex) {
Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
}
}

View File

@ -7,7 +7,10 @@ import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
import java.util.Set;
@ -28,6 +31,15 @@ public class Util {
}
public static void toast(final String text, final int length, final Context context) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, text, length).show();
}
});
}
public static void logExtras(String tag, Intent intent) {
logBundle(tag, intent.getExtras());
}