OSDN Git Service

VpnSettings: generate arguments for PPTP and L2TP IPSec VPNs.
authorChia-chi Yeh <chiachi@android.com>
Wed, 29 Jun 2011 22:29:28 +0000 (15:29 -0700)
committerChia-chi Yeh <chiachi@android.com>
Wed, 29 Jun 2011 22:29:28 +0000 (15:29 -0700)
Change-Id: Ia58afa2f6f6d4b1b208bf6b893b8b7712154bdbf

src/com/android/settings/vpn2/VpnDialog.java
src/com/android/settings/vpn2/VpnSettings.java

index b3e417b..92ad362 100644 (file)
@@ -36,15 +36,15 @@ import android.widget.Spinner;
 import android.widget.TextView;
 
 class VpnDialog extends AlertDialog implements TextWatcher, OnItemSelectedListener {
-    private static final String DUMMY = "\r\r\r\r";
+    private static final String DUMMY = "\r";
 
     private static String getDummy(String secret) {
         return secret.isEmpty() ? "" : DUMMY;
     }
 
-    private static String getSecret(TextView dummy) {
-        String secret = dummy.getText().toString();
-        return DUMMY.equals(secret) ? "" : secret;
+    private static String getSecret(String oldSecret, TextView view) {
+        String newSecret = view.getText().toString();
+        return DUMMY.equals(newSecret) ? oldSecret : newSecret;
     }
 
     private final KeyStore mKeyStore = KeyStore.getInstance();
@@ -116,7 +116,7 @@ class VpnDialog extends AlertDialog implements TextWatcher, OnItemSelectedListen
         mIpsecSecret.setText(getDummy(mProfile.ipsecSecret));
         loadCertificates(mIpsecUserCert, Credentials.USER_CERTIFICATE,
                 0, mProfile.ipsecUserCert);
-        loadCertificates(mIpsecUserCert, Credentials.CA_CERTIFICATE,
+        loadCertificates(mIpsecCaCert, Credentials.CA_CERTIFICATE,
                 R.string.vpn_no_ca_cert, mProfile.ipsecCaCert);
         mSaveLogin.setChecked(mProfile.saveLogin);
 
@@ -186,7 +186,7 @@ class VpnDialog extends AlertDialog implements TextWatcher, OnItemSelectedListen
         if (parent == mType) {
             changeType(position);
         }
-        getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(validate(false));
+        getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(validate(mEditing));
     }
 
     @Override
@@ -252,24 +252,24 @@ class VpnDialog extends AlertDialog implements TextWatcher, OnItemSelectedListen
     private void loadCertificates(Spinner spinner, String prefix, int firstId, String selected) {
         Context context = getContext();
         String first = (firstId == 0) ? "" : context.getString(firstId);
-        String[] certs = mKeyStore.saw(prefix);
+        String[] certificates = mKeyStore.saw(prefix);
 
-        if (certs == null || certs.length == 0) {
-            certs = new String[] {first};
+        if (certificates == null || certificates.length == 0) {
+            certificates = new String[] {first};
         } else {
-            String[] array = new String[certs.length + 1];
+            String[] array = new String[certificates.length + 1];
             array[0] = first;
-            System.arraycopy(certs, 0, array, 1, certs.length);
-            certs = array;
+            System.arraycopy(certificates, 0, array, 1, certificates.length);
+            certificates = array;
         }
 
         ArrayAdapter<String> adapter = new ArrayAdapter<String>(
-                context, android.R.layout.simple_spinner_item, certs);
+                context, android.R.layout.simple_spinner_item, certificates);
         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         spinner.setAdapter(adapter);
 
-        for (int i = 1; i < certs.length; ++i) {
-            if (certs[i].equals(selected)) {
+        for (int i = 1; i < certificates.length; ++i) {
+            if (certificates[i].equals(selected)) {
                 spinner.setSelection(i);
                 break;
             }
@@ -287,7 +287,7 @@ class VpnDialog extends AlertDialog implements TextWatcher, OnItemSelectedListen
         profile.type = mType.getSelectedItemPosition();
         profile.server = mServer.getText().toString().trim();
         profile.username = mUsername.getText().toString();
-        profile.password = getSecret(mPassword);
+        profile.password = getSecret(mProfile.password, mPassword);
         profile.domains = mDomains.getText().toString().trim();
         profile.routes = mRoutes.getText().toString().trim();
 
@@ -298,14 +298,14 @@ class VpnDialog extends AlertDialog implements TextWatcher, OnItemSelectedListen
                 break;
 
             case VpnProfile.TYPE_L2TP_IPSEC_PSK:
-                profile.l2tpSecret = getSecret(mL2tpSecret);
+                profile.l2tpSecret = getSecret(mProfile.l2tpSecret, mL2tpSecret);
                 // fall through
             case VpnProfile.TYPE_IPSEC_XAUTH_PSK:
-                profile.ipsecSecret = getSecret(mIpsecSecret);
+                profile.ipsecSecret = getSecret(mProfile.ipsecSecret, mIpsecSecret);
                 break;
 
             case VpnProfile.TYPE_L2TP_IPSEC_RSA:
-                profile.l2tpSecret = getSecret(mL2tpSecret);
+                profile.l2tpSecret = getSecret(mProfile.l2tpSecret, mL2tpSecret);
                 // fall through
             case VpnProfile.TYPE_IPSEC_XAUTH_RSA:
                 if (mIpsecCaCert.getSelectedItemPosition() != 0) {
index 6662dd9..56fb983 100644 (file)
@@ -20,9 +20,11 @@ import com.android.settings.R;
 
 import android.content.Context;
 import android.content.DialogInterface;
+import android.net.IConnectivityManager;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
+import android.os.ServiceManager;
 import android.preference.Preference;
 import android.preference.PreferenceGroup;
 import android.security.Credentials;
@@ -57,8 +59,11 @@ public class VpnSettings extends SettingsPreferenceFragment implements
 
     private HashMap<String, VpnPreference> mPreferences;
     private VpnDialog mDialog;
+
+    private Handler mUpdater;
+
+    // The key of the profile for the current ContextMenu.
     private String mSelectedKey;
-    private Handler mHandler;
 
     @Override
     public void onCreate(Bundle savedState) {
@@ -145,10 +150,10 @@ public class VpnSettings extends SettingsPreferenceFragment implements
         }
 
         // Start monitoring.
-        if (mHandler == null) {
-            mHandler = new Handler(this);
+        if (mUpdater == null) {
+            mUpdater = new Handler(this);
         }
-        mHandler.sendEmptyMessage(0);
+        mUpdater.sendEmptyMessage(0);
 
         // Register for context menu. Hmmm, getListView() is hidden?
         registerForContextMenu(getListView());
@@ -194,7 +199,7 @@ public class VpnSettings extends SettingsPreferenceFragment implements
 
             // If we are not editing, connect!
             if (!mDialog.isEditing()) {
-                connect(profile.key);
+                connect(profile);
             }
         }
     }
@@ -274,19 +279,74 @@ public class VpnSettings extends SettingsPreferenceFragment implements
 
     @Override
     public boolean handleMessage(Message message) {
-        mHandler.removeMessages(0);
+        mUpdater.removeMessages(0);
 
         if (isResumed()) {
 
 
 
 
-            mHandler.sendEmptyMessageDelayed(0, 1000);
+            mUpdater.sendEmptyMessageDelayed(0, 1000);
         }
         return true;
     }
 
-    private void connect(String key) {
+    private static IConnectivityManager getService() {
+        return IConnectivityManager.Stub.asInterface(
+                ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
+    }
+
+    private void connect(VpnProfile profile) {
+        String[] racoon = null;
+        switch (profile.type) {
+            case VpnProfile.TYPE_L2TP_IPSEC_PSK:
+                racoon = new String[] {
+                    profile.server, "1701", profile.ipsecSecret,
+                };
+                break;
+            case VpnProfile.TYPE_L2TP_IPSEC_RSA:
+                racoon = new String[] {
+                    profile.server, "1701",
+                    Credentials.USER_PRIVATE_KEY + profile.ipsecUserCert,
+                    Credentials.USER_CERTIFICATE + profile.ipsecUserCert,
+                    Credentials.CA_CERTIFICATE + profile.ipsecCaCert,
+                };
+                break;
+            case VpnProfile.TYPE_IPSEC_XAUTH_PSK:
+                break;
+            case VpnProfile.TYPE_IPSEC_XAUTH_RSA:
+                break;
+            case VpnProfile.TYPE_IPSEC_HYBRID_RSA:
+                break;
+        }
+
+        String[] mtpd = null;
+        switch (profile.type) {
+            case VpnProfile.TYPE_PPTP:
+                mtpd = new String[] {
+                    "pptp", profile.server, "1723",
+                    "name", profile.username, "password", profile.password,
+                    "linkname", "vpn", "refuse-eap", "nodefaultroute",
+                    "usepeerdns", "idle", "1800", "mtu", "1400", "mru", "1400",
+                    (profile.mppe ? "+mppe" : "nomppe"),
+                };
+                break;
+            case VpnProfile.TYPE_L2TP_IPSEC_PSK:
+            case VpnProfile.TYPE_L2TP_IPSEC_RSA:
+                mtpd = new String[] {
+                    "l2tp", profile.server, "1701", profile.l2tpSecret,
+                    "name", profile.username, "password", profile.password,
+                    "linkname", "vpn", "refuse-eap", "nodefaultroute",
+                    "usepeerdns", "idle", "1800", "mtu", "1400", "mru", "1400",
+                };
+                break;
+        }
+
+        try {
+//            getService().doLegacyVpn(racoon, mtpd);
+        } catch (Exception e) {
+            Log.e(TAG, "connect", e);
+        }
     }
 
     private void disconnect(String key) {
@@ -331,7 +391,7 @@ public class VpnSettings extends SettingsPreferenceFragment implements
 
         @Override
         public int compareTo(Preference preference) {
-            int result = 1;
+            int result = -1;
             if (preference instanceof VpnPreference) {
                 VpnPreference another = (VpnPreference) preference;