From 1e57ad0ef473917864ea1f5983e2ca79932b30c6 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Wed, 5 Jun 2013 14:39:48 +0200 Subject: Add support for asking username/password if none is set (closes issue #174) --- icsopenvpn.iml | 71 ++++++++++++++++++++++ res/layout/userpass.xml | 39 ++++++++++++ res/values/strings.xml | 2 +- src/de/blinkt/openvpn/LaunchVPN.java | 57 +++++++++++------ src/de/blinkt/openvpn/VpnProfile.java | 7 +-- .../blinkt/openvpn/core/DeviceStateReceiver.java | 1 + 6 files changed, 153 insertions(+), 24 deletions(-) create mode 100644 icsopenvpn.iml create mode 100644 res/layout/userpass.xml diff --git a/icsopenvpn.iml b/icsopenvpn.iml new file mode 100644 index 00000000..922231a6 --- /dev/null +++ b/icsopenvpn.iml @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/layout/userpass.xml b/res/layout/userpass.xml new file mode 100644 index 00000000..d13953c6 --- /dev/null +++ b/res/layout/userpass.xml @@ -0,0 +1,39 @@ + + + + + + + + + + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index b7589271..c77f079c 100755 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -155,7 +155,6 @@ imported profile %d Broken Images <p>Official HTC images are known to have a strange routing problem causing traffic not to flow through the tunnel (See also <a href=\"http://code.google.com/p/ics-openvpn/issues/detail?id=18\">Issue 18</a> in the bug tracker.)</p><p>The official SONY images from Xperia arc S and Xperia Ray have been reported to be missing the VPNService completely from the image. Other Sony images may be affected as well. (See also <a href=\"http://code.google.com/p/ics-openvpn/issues/detail?id=29\">Issue 29</a> in the bug tracker.)</p><p>On custom build images the tun module might be missing or the rights of /dev/tun might be wrong. Some CM9 images need the fix ownership option under general settings.</p><p>Most important: If you have a broken image, report it to your vendor. The more people report the issue to the vendor the more likely you will get a fix.</p> - The username must not be empty. PKCS12 File Encryption Key Private Key Password Password @@ -282,5 +281,6 @@ Pause VPN connection after screen off Pausing connection in screen off state: less than %1$s in %2$ss Warning: Persistent tun not enabled for this VPN. Traffic will use the normal Internet connection when the screen is off. + Save Password \ No newline at end of file diff --git a/src/de/blinkt/openvpn/LaunchVPN.java b/src/de/blinkt/openvpn/LaunchVPN.java index 80075086..074bd235 100644 --- a/src/de/blinkt/openvpn/LaunchVPN.java +++ b/src/de/blinkt/openvpn/LaunchVPN.java @@ -17,12 +17,16 @@ import android.os.Bundle; import android.os.Parcelable; import android.preference.PreferenceManager; import android.text.InputType; +import android.text.Layout; +import android.text.TextUtils; import android.text.method.PasswordTransformationMethod; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; +import android.widget.CheckBox; import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import de.blinkt.openvpn.core.OpenVPN; @@ -207,6 +211,8 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { private void askForPW(final int type) { final EditText entry = new EditText(this); + final View userpwlayout = getLayoutInflater().inflate(R.layout.userpass, null); + entry.setSingleLine(); entry.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); entry.setTransformationMethod(new PasswordTransformationMethod()); @@ -214,24 +220,41 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener { AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("Need " + getString(type)); dialog.setMessage("Enter the password for profile " + mSelectedProfile.mName); - dialog.setView(entry); - - dialog.setPositiveButton(android.R.string.ok, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - String pw = entry.getText().toString(); - if(type == R.string.password) { - mSelectedProfile.mTransientPW = pw; - } else { - mSelectedProfile.mTransientPCKS12PW = pw; - } - onActivityResult(START_VPN_PROFILE, Activity.RESULT_OK, null); - } - - }); - dialog.setNegativeButton(android.R.string.cancel, + if (type == R.string.password) { + ((EditText)userpwlayout.findViewById(R.id.username)).setText(mSelectedProfile.mUsername); + ((EditText)userpwlayout.findViewById(R.id.password)).setText(mSelectedProfile.mPassword); + dialog.setView(userpwlayout); + } else { + dialog.setView(entry); + } + + AlertDialog.Builder builder = dialog.setPositiveButton(android.R.string.ok, + new OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + if (type == R.string.password) { + String pw = ((EditText) userpwlayout.findViewById(R.id.password)).getText().toString(); + String username = ((EditText) userpwlayout.findViewById(R.id.username)).getText().toString(); + mSelectedProfile.mUsername = username; + if (((CheckBox) userpwlayout.findViewById(R.id.save_password)).isChecked()) { + if (pw !=null && !("".equals(pw))) + mSelectedProfile.mPassword=pw; + } else { + mSelectedProfile.mPassword=null; + mSelectedProfile.mTransientPW = pw; + } + } else { + String pw = entry.getText().toString(); + mSelectedProfile.mTransientPCKS12PW = pw; + } + onActivityResult(START_VPN_PROFILE, Activity.RESULT_OK, null); + + } + + }); + dialog.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { diff --git a/src/de/blinkt/openvpn/VpnProfile.java b/src/de/blinkt/openvpn/VpnProfile.java index 03fcbc1b..dcca334c 100644 --- a/src/de/blinkt/openvpn/VpnProfile.java +++ b/src/de/blinkt/openvpn/VpnProfile.java @@ -659,9 +659,6 @@ public class VpnProfile implements Serializable{ if(mIPv4Address == null || cidrToIPAndNetmask(mIPv4Address) == null) return R.string.ipv4_format_error; } - if(isUserPWAuth() && !nonNull(mUsername)) { - return R.string.error_empty_username; - } if(!mUseDefaultRoute && getCustomRoutes()==null) return R.string.custom_route_format_error; @@ -757,10 +754,8 @@ public class VpnProfile implements Serializable{ } } - if(isUserPWAuth() && (mPassword.equals("") || mPassword == null)) { - if(mTransientPW==null) + if (isUserPWAuth() && !(nonNull(mUsername) && (nonNull(mPassword) || mTransientPW!=null))) { return R.string.password; - } return 0; } diff --git a/src/de/blinkt/openvpn/core/DeviceStateReceiver.java b/src/de/blinkt/openvpn/core/DeviceStateReceiver.java index 7d6e80db..b27ea00f 100644 --- a/src/de/blinkt/openvpn/core/DeviceStateReceiver.java +++ b/src/de/blinkt/openvpn/core/DeviceStateReceiver.java @@ -70,6 +70,7 @@ public class DeviceStateReceiver extends BroadcastReceiver implements ByteCountL screen = connectState.DISCONNECTED; OpenVPN.logInfo(R.string.screenoff_pause, OpenVpnService.humanReadableByteCount(TRAFFIC_LIMIT, false), TRAFFIC_WINDOW); + mManangement.pause(); } Log.i("OpenVPN", String.format("State: %s %s total %d last %d time %d",network.name(), screen.name(),windowtraffic/1024, -- cgit v1.2.3