From bbf93681fe1383c06edc47c89b50d444df57e56b Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Sun, 12 Aug 2012 18:52:38 +0200 Subject: Implement starting a VPN on boot. (closes issue #62) --- src/de/blinkt/openvpn/LaunchVPN.java | 19 +++++------- src/de/blinkt/openvpn/LogWindow.java | 3 ++ src/de/blinkt/openvpn/OnBootReceiver.java | 33 ++++++++++++++++++++ src/de/blinkt/openvpn/OpenVpnManagementThread.java | 1 - src/de/blinkt/openvpn/OpenVpnService.java | 4 +-- src/de/blinkt/openvpn/ProfileManager.java | 36 ++++++++++++++++++++++ src/de/blinkt/openvpn/VpnProfile.java | 4 +++ 7 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 src/de/blinkt/openvpn/OnBootReceiver.java (limited to 'src') diff --git a/src/de/blinkt/openvpn/LaunchVPN.java b/src/de/blinkt/openvpn/LaunchVPN.java index bfc6256e..a2a0de3f 100644 --- a/src/de/blinkt/openvpn/LaunchVPN.java +++ b/src/de/blinkt/openvpn/LaunchVPN.java @@ -75,12 +75,14 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { static final String EXTRA_KEY = "de.blinkt.openvpn.shortcutProfileUUID"; static final String EXTRA_NAME = "de.blinkt.openvpn.shortcutProfileName"; + public static final String EXTRA_HIDELOG = "de.blinkt.openvpn.showNoLogWindow";; private static final int START_VPN_PROFILE= 70; + private ProfileManager mPM; private VpnProfile mSelectedProfile; - + private boolean mhideLog=false; private boolean mCmfixed=false; @@ -102,10 +104,12 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { // If the intent is a request to create a shortcut, we'll do that and exit + if(Intent.ACTION_MAIN.equals(action)) { // we got called to be the starting point, most likely a shortcut String shortcutUUID = intent.getStringExtra( EXTRA_KEY); String shortcutName = intent.getStringExtra( EXTRA_NAME); + mhideLog = intent.getBooleanExtra(EXTRA_HIDELOG, false); VpnProfile profileToConnect = ProfileManager.get(shortcutUUID); if(shortcutName != null && profileToConnect ==null) @@ -122,18 +126,9 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { mSelectedProfile = profileToConnect; launchVPN(); - - } else if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) { createListView(); } - - - - - - - } private void createListView() { @@ -318,7 +313,8 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { } else { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); boolean showlogwindow = prefs.getBoolean("showlogwindow", true); - if(showlogwindow) + + if(!mhideLog && showlogwindow) showLogWindow(); new startOpenVpnThread().start(); } @@ -372,7 +368,6 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { } - if (intent != null) { // Start the query try { diff --git a/src/de/blinkt/openvpn/LogWindow.java b/src/de/blinkt/openvpn/LogWindow.java index ae5277cd..6cc257a5 100644 --- a/src/de/blinkt/openvpn/LogWindow.java +++ b/src/de/blinkt/openvpn/LogWindow.java @@ -58,6 +58,8 @@ public class LogWindow extends ListActivity implements StateListener { OpenVPN.addLogListener(this); } + + private void initLogBuffer() { myEntries.clear(); for (LogItem litem : OpenVPN.getlogbuffer()) { @@ -207,6 +209,7 @@ public class LogWindow extends ListActivity implements StateListener { @Override public void onClick(DialogInterface dialog, int which) { + ProfileManager.onBootDelete(getApplicationContext()); OpenVpnManagementThread.stopOpenVPN(); } }); diff --git a/src/de/blinkt/openvpn/OnBootReceiver.java b/src/de/blinkt/openvpn/OnBootReceiver.java new file mode 100644 index 00000000..032501b6 --- /dev/null +++ b/src/de/blinkt/openvpn/OnBootReceiver.java @@ -0,0 +1,33 @@ +package de.blinkt.openvpn; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + + +public class OnBootReceiver extends BroadcastReceiver { + + // Debug: am broadcast -a android.intent.action.BOOT_COMPLETED + @Override + public void onReceive(Context context, Intent intent) { + + final String action = intent.getAction(); + + if(Intent.ACTION_BOOT_COMPLETED.equals(action)) { + VpnProfile bootProfile = ProfileManager.getOnBootProfile(context); + if(bootProfile != null) { + lauchVPN(bootProfile, context); + } + } + } + + void lauchVPN(VpnProfile profile,Context context) { + Intent startVpnIntent = new Intent(Intent.ACTION_MAIN); + startVpnIntent.setClass(context, LaunchVPN.class); + startVpnIntent.putExtra(LaunchVPN.EXTRA_KEY,profile.getUUIDString()); + startVpnIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startVpnIntent.putExtra(LaunchVPN.EXTRA_HIDELOG, true); + + context.startActivity(startVpnIntent); + } +} diff --git a/src/de/blinkt/openvpn/OpenVpnManagementThread.java b/src/de/blinkt/openvpn/OpenVpnManagementThread.java index c42e7516..4e7729ec 100644 --- a/src/de/blinkt/openvpn/OpenVpnManagementThread.java +++ b/src/de/blinkt/openvpn/OpenVpnManagementThread.java @@ -18,7 +18,6 @@ import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; -import android.content.Context; import android.content.SharedPreferences; import android.net.LocalServerSocket; import android.net.LocalSocket; diff --git a/src/de/blinkt/openvpn/OpenVpnService.java b/src/de/blinkt/openvpn/OpenVpnService.java index f56e873b..01def8eb 100644 --- a/src/de/blinkt/openvpn/OpenVpnService.java +++ b/src/de/blinkt/openvpn/OpenVpnService.java @@ -67,6 +67,7 @@ public class OpenVpnService extends VpnService implements StateListener { OpenVpnManagementThread.stopOpenVPN(); mServiceThread=null; stopSelf(); + ProfileManager.onBootDelete(this); }; private void hideNotification() { @@ -200,9 +201,8 @@ public class OpenVpnService extends VpnService implements StateListener { mServiceThread = new Thread(serviceThread, "OpenVPNServiceThread"); mServiceThread.start(); + ProfileManager.setOnBootProfile(this, mProfile); - - return START_NOT_STICKY; } diff --git a/src/de/blinkt/openvpn/ProfileManager.java b/src/de/blinkt/openvpn/ProfileManager.java index b1321b97..5d498c67 100644 --- a/src/de/blinkt/openvpn/ProfileManager.java +++ b/src/de/blinkt/openvpn/ProfileManager.java @@ -14,12 +14,17 @@ import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; +import android.preference.PreferenceManager; public class ProfileManager { private static final String PREFS_NAME = "VPNList"; + private static final String ONBOOTPROFILE = "onBootProfile"; + + + private static ProfileManager instance; private HashMap profiles=new HashMap(); @@ -47,6 +52,37 @@ public class ProfileManager { return instance; } + public static void onBootDelete(Context c) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); + Editor prefsedit = prefs.edit(); + prefsedit.putString(ONBOOTPROFILE, null); + prefsedit.apply(); + + } + + public static void setOnBootProfile(Context c, VpnProfile bootprofile) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); + Editor prefsedit = prefs.edit(); + + prefsedit.putString(ONBOOTPROFILE, bootprofile.getUUIDString()); + prefsedit.apply(); + + } + + public static VpnProfile getOnBootProfile(Context c) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); + + boolean useStartOnBoot = prefs.getBoolean("restartvpnonboot", false); + + + String mBootProfileUUID = prefs.getString(ONBOOTPROFILE,null); + if(useStartOnBoot && mBootProfileUUID!=null) + return get(c, mBootProfileUUID); + else + return null; + } + + public Collection getProfiles() { diff --git a/src/de/blinkt/openvpn/VpnProfile.java b/src/de/blinkt/openvpn/VpnProfile.java index a6dfe053..024874a4 100644 --- a/src/de/blinkt/openvpn/VpnProfile.java +++ b/src/de/blinkt/openvpn/VpnProfile.java @@ -24,6 +24,7 @@ import org.spongycastle.util.io.pem.PemWriter; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; import android.content.pm.ApplicationInfo; import android.preference.PreferenceManager; import android.security.KeyChain; @@ -710,6 +711,9 @@ public class VpnProfile implements Serializable{ public PrivateKey getKeystoreKey() { return mPrivateKey; } + + + } -- cgit v1.2.3