From 661cc31f36331dd9565df7c4a0ebd8e6ba510051 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Mon, 13 Aug 2012 23:09:21 +0200 Subject: Allow editing the VPN Config from the log screen to ease configuration iterations --- src/de/blinkt/openvpn/LogWindow.java | 68 ++++++++++++++++++++++++++----- src/de/blinkt/openvpn/OpenVpnService.java | 4 +- src/de/blinkt/openvpn/ProfileManager.java | 19 +++++++-- 3 files changed, 75 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/de/blinkt/openvpn/LogWindow.java b/src/de/blinkt/openvpn/LogWindow.java index 6cc257a5..94425fa8 100644 --- a/src/de/blinkt/openvpn/LogWindow.java +++ b/src/de/blinkt/openvpn/LogWindow.java @@ -32,6 +32,7 @@ import de.blinkt.openvpn.OpenVPN.LogListener; import de.blinkt.openvpn.OpenVPN.StateListener; public class LogWindow extends ListActivity implements StateListener { + private static final int START_VPN_CONFIG = 0; private String[] mBconfig=null; @@ -58,8 +59,8 @@ public class LogWindow extends ListActivity implements StateListener { OpenVPN.addLogListener(this); } - - + + private void initLogBuffer() { myEntries.clear(); for (LogItem litem : OpenVPN.getlogbuffer()) { @@ -70,7 +71,7 @@ public class LogWindow extends ListActivity implements StateListener { String getLogStr() { String str = ""; for(String entry:myEntries) { - str+=entry + '\n'; + str+=entry + '\n'; } return str; } @@ -205,15 +206,16 @@ public class LogWindow extends ListActivity implements StateListener { Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.title_cancel); builder.setMessage(R.string.cancel_connection_query); + builder.setNegativeButton(android.R.string.no, null); builder.setPositiveButton(android.R.string.yes, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - ProfileManager.onBootDelete(getApplicationContext()); + ProfileManager.setConntectedVpnProfileDisconnected(getApplicationContext()); OpenVpnManagementThread.stopOpenVPN(); } }); - builder.setNegativeButton(android.R.string.no, null); + builder.show(); return true; } else if(item.getItemId()==R.id.info) { @@ -221,6 +223,18 @@ public class LogWindow extends ListActivity implements StateListener { OpenVPN.triggerLogBuilderConfig(); } else if(item.getItemId()==R.id.send) { ladapter.shareLog(); + } else if(item.getItemId()==R.id.edit_vpn) { + VpnProfile lastConnectedprofile = ProfileManager.getLastConnectedVpn(); + + if(lastConnectedprofile!=null) { + Intent vprefintent = new Intent(this,VPNPreferences.class) + .putExtra(VpnProfile.EXTRA_PROFILEUUID,lastConnectedprofile.getUUIDString()); + startActivityForResult(vprefintent,START_VPN_CONFIG); + } else { + Toast.makeText(this, R.string.log_no_last_vpn, Toast.LENGTH_LONG).show(); + } + + } return super.onOptionsItemSelected(item); @@ -240,6 +254,38 @@ public class LogWindow extends ListActivity implements StateListener { OpenVPN.addSpeedListener(this); } + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == START_VPN_CONFIG && resultCode==RESULT_OK) { + String configuredVPN = data.getStringExtra(VpnProfile.EXTRA_PROFILEUUID); + + final VpnProfile profile = ProfileManager.get(configuredVPN); + ProfileManager.getInstance(this).saveProfile(this, profile); + // Name could be modified, reset List adapter + + AlertDialog.Builder dialog = new AlertDialog.Builder(this); + dialog.setTitle(R.string.configuration_changed); + dialog.setMessage(R.string.restart_vpn_after_change); + + + dialog.setPositiveButton(R.string.restart, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Intent intent = new Intent(getBaseContext(), LaunchVPN.class); + intent.putExtra(LaunchVPN.EXTRA_KEY, profile.getUUIDString()); + intent.setAction(Intent.ACTION_MAIN); + startActivity(intent); + } + + + }); + dialog.setNegativeButton(R.string.ignore, null); + dialog.create().show(); + } + super.onActivityResult(requestCode, resultCode, data); + } + @Override protected void onStop() { super.onStop(); @@ -252,14 +298,14 @@ public class LogWindow extends ListActivity implements StateListener { setContentView(R.layout.logwindow); ListView lv = getListView(); - + lv.setOnItemLongClickListener(new OnItemLongClickListener() { - + @Override public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { ClipboardManager clipboard = (ClipboardManager) - getSystemService(Context.CLIPBOARD_SERVICE); + getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = ClipData.newPlainText("Log Entry",((TextView) view).getText()); clipboard.setPrimaryClip(clip); Toast.makeText(getBaseContext(), R.string.copied_entry, Toast.LENGTH_SHORT).show(); @@ -276,7 +322,7 @@ public class LogWindow extends ListActivity implements StateListener { @Override public void updateState(final String status,final String logmessage) { runOnUiThread(new Runnable() { - + @Override public void run() { String prefix=status+ ":"; @@ -285,9 +331,9 @@ public class LogWindow extends ListActivity implements StateListener { mSpeedView.setText(prefix + logmessage); } }); - + } - + @Override protected void onDestroy() { super.onDestroy(); diff --git a/src/de/blinkt/openvpn/OpenVpnService.java b/src/de/blinkt/openvpn/OpenVpnService.java index 01def8eb..594b07d4 100644 --- a/src/de/blinkt/openvpn/OpenVpnService.java +++ b/src/de/blinkt/openvpn/OpenVpnService.java @@ -67,7 +67,7 @@ public class OpenVpnService extends VpnService implements StateListener { OpenVpnManagementThread.stopOpenVPN(); mServiceThread=null; stopSelf(); - ProfileManager.onBootDelete(this); + ProfileManager.setConntectedVpnProfileDisconnected(this); }; private void hideNotification() { @@ -201,7 +201,7 @@ public class OpenVpnService extends VpnService implements StateListener { mServiceThread = new Thread(serviceThread, "OpenVPNServiceThread"); mServiceThread.start(); - ProfileManager.setOnBootProfile(this, mProfile); + ProfileManager.setConnectedVpnProfile(this, mProfile); return START_NOT_STICKY; } diff --git a/src/de/blinkt/openvpn/ProfileManager.java b/src/de/blinkt/openvpn/ProfileManager.java index 5d498c67..a40e6fcb 100644 --- a/src/de/blinkt/openvpn/ProfileManager.java +++ b/src/de/blinkt/openvpn/ProfileManager.java @@ -26,6 +26,10 @@ public class ProfileManager { private static ProfileManager instance; + + + + private static VpnProfile mLastConnectedVpn=null; private HashMap profiles=new HashMap(); @@ -52,7 +56,7 @@ public class ProfileManager { return instance; } - public static void onBootDelete(Context c) { + public static void setConntectedVpnProfileDisconnected(Context c) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); Editor prefsedit = prefs.edit(); prefsedit.putString(ONBOOTPROFILE, null); @@ -60,12 +64,13 @@ public class ProfileManager { } - public static void setOnBootProfile(Context c, VpnProfile bootprofile) { + public static void setConnectedVpnProfile(Context c, VpnProfile connectedrofile) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); Editor prefsedit = prefs.edit(); - prefsedit.putString(ONBOOTPROFILE, bootprofile.getUUIDString()); + prefsedit.putString(ONBOOTPROFILE, connectedrofile.getUUIDString()); prefsedit.apply(); + mLastConnectedVpn=connectedrofile; } @@ -183,6 +188,8 @@ public class ProfileManager { profiles.remove(vpnentry); saveProfileList(context); context.deleteFile(vpnentry + ".vp"); + if(mLastConnectedVpn==profile) + mLastConnectedVpn=null; } @@ -193,4 +200,10 @@ public class ProfileManager { return get(profileUUID); } + + + public static VpnProfile getLastConnectedVpn() { + return mLastConnectedVpn; + } + } -- cgit v1.2.3