VpnSettings: switch to the new user interface.

Change-Id: I7158cd8f146677b2823cfa936792fd232a37ae28
This commit is contained in:
Chia-chi Yeh
2011-07-02 17:45:23 -07:00
parent e391a7690d
commit 7606a2f4f3
20 changed files with 3 additions and 2676 deletions

View File

@@ -19,7 +19,6 @@ package com.android.settings.accounts;
import com.android.settings.AccountPreference;
import com.android.settings.DialogCreatable;
import com.android.settings.R;
import com.android.settings.vpn.VpnTypeSelection;
import com.google.android.collect.Maps;
import android.accounts.Account;

View File

@@ -1,129 +0,0 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.vpn;
import com.android.settings.R;
import android.app.Dialog;
import android.content.Context;
import android.net.vpn.VpnManager;
import android.net.vpn.VpnProfile;
import android.net.vpn.VpnState;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import java.io.IOException;
/**
* A {@link VpnProfileActor} that provides an authentication view for users to
* input username and password before connecting to the VPN server.
*/
public class AuthenticationActor implements VpnProfileActor {
private static final String TAG = AuthenticationActor.class.getName();
private Context mContext;
private VpnProfile mProfile;
private VpnManager mVpnManager;
public AuthenticationActor(Context context, VpnProfile p) {
mContext = context;
mProfile = p;
mVpnManager = new VpnManager(context);
}
//@Override
public VpnProfile getProfile() {
return mProfile;
}
//@Override
public boolean isConnectDialogNeeded() {
return true;
}
//@Override
public String validateInputs(Dialog d) {
TextView usernameView = (TextView) d.findViewById(R.id.username_value);
TextView passwordView = (TextView) d.findViewById(R.id.password_value);
Context c = mContext;
if (TextUtils.isEmpty(usernameView.getText().toString())) {
return c.getString(R.string.vpn_a_username);
} else if (TextUtils.isEmpty(passwordView.getText().toString())) {
return c.getString(R.string.vpn_a_password);
} else {
return null;
}
}
//@Override
public void connect(Dialog d) {
TextView usernameView = (TextView) d.findViewById(R.id.username_value);
TextView passwordView = (TextView) d.findViewById(R.id.password_value);
CheckBox saveUsername = (CheckBox) d.findViewById(R.id.save_username);
try {
setSavedUsername(saveUsername.isChecked()
? usernameView.getText().toString()
: "");
} catch (IOException e) {
Log.e(TAG, "setSavedUsername()", e);
}
connect(usernameView.getText().toString(),
passwordView.getText().toString());
passwordView.setText("");
}
//@Override
public View createConnectView() {
View v = View.inflate(mContext, R.layout.vpn_connect_dialog_view, null);
TextView usernameView = (TextView) v.findViewById(R.id.username_value);
TextView passwordView = (TextView) v.findViewById(R.id.password_value);
CheckBox saveUsername = (CheckBox) v.findViewById(R.id.save_username);
String username = mProfile.getSavedUsername();
if (!TextUtils.isEmpty(username)) {
usernameView.setText(username);
saveUsername.setChecked(true);
passwordView.requestFocus();
}
return v;
}
protected Context getContext() {
return mContext;
}
private void connect(String username, String password) {
mVpnManager.connect(mProfile, username, password);
}
//@Override
public void disconnect() {
mVpnManager.disconnect();
}
private void setSavedUsername(String name) throws IOException {
if (!name.equals(mProfile.getSavedUsername())) {
mProfile.setSavedUsername(name);
VpnSettings.saveProfileToStorage(mProfile);
}
}
}

View File

@@ -1,104 +0,0 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.vpn;
import com.android.settings.R;
import android.content.Context;
import android.net.vpn.L2tpProfile;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceGroup;
/**
* The class for editing {@link L2tpProfile}.
*/
class L2tpEditor extends VpnProfileEditor {
private CheckBoxPreference mSecret;
private SecretHandler mSecretHandler;
public L2tpEditor(L2tpProfile p) {
super(p);
}
@Override
protected void loadExtraPreferencesTo(PreferenceGroup subpanel) {
Context c = subpanel.getContext();
subpanel.addPreference(createSecretPreference(c));
subpanel.addPreference(createSecretStringPreference(c));
L2tpProfile profile = (L2tpProfile) getProfile();
}
@Override
public String validate() {
String result = super.validate();
if (!mSecret.isChecked()) return result;
return ((result != null) ? result : mSecretHandler.validate());
}
private Preference createSecretPreference(Context c) {
final L2tpProfile profile = (L2tpProfile) getProfile();
CheckBoxPreference secret = mSecret = new CheckBoxPreference(c);
boolean enabled = profile.isSecretEnabled();
setCheckBoxTitle(secret, R.string.vpn_l2tp_secret);
secret.setChecked(enabled);
setSecretSummary(secret, enabled);
secret.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(
Preference pref, Object newValue) {
boolean enabled = (Boolean) newValue;
profile.setSecretEnabled(enabled);
mSecretHandler.getPreference().setEnabled(enabled);
setSecretSummary(mSecret, enabled);
return true;
}
});
return secret;
}
private Preference createSecretStringPreference(Context c) {
SecretHandler sHandler = mSecretHandler = new SecretHandler(c,
R.string.vpn_l2tp_secret_string_title,
R.string.vpn_l2tp_secret) {
@Override
protected String getSecretFromProfile() {
return ((L2tpProfile) getProfile()).getSecretString();
}
@Override
protected void saveSecretToProfile(String secret) {
((L2tpProfile) getProfile()).setSecretString(secret);
}
};
Preference pref = sHandler.getPreference();
pref.setEnabled(mSecret.isChecked());
return pref;
}
private void setSecretSummary(CheckBoxPreference secret, boolean enabled) {
Context c = secret.getContext();
String formatString = c.getString(enabled
? R.string.vpn_is_enabled
: R.string.vpn_is_disabled);
secret.setSummary(String.format(
formatString, c.getString(R.string.vpn_l2tp_secret)));
}
}

View File

@@ -1,120 +0,0 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.vpn;
import com.android.settings.R;
import android.content.Context;
import android.net.vpn.L2tpIpsecProfile;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceGroup;
import android.security.Credentials;
import android.security.KeyStore;
import android.text.TextUtils;
/**
* The class for editing {@link L2tpIpsecProfile}.
*/
class L2tpIpsecEditor extends L2tpEditor {
private static final String TAG = L2tpIpsecEditor.class.getSimpleName();
private KeyStore mKeyStore = KeyStore.getInstance();
private ListPreference mUserCertificate;
private ListPreference mCaCertificate;
private L2tpIpsecProfile mProfile;
public L2tpIpsecEditor(L2tpIpsecProfile p) {
super(p);
mProfile = p;
}
@Override
protected void loadExtraPreferencesTo(PreferenceGroup subpanel) {
super.loadExtraPreferencesTo(subpanel);
Context c = subpanel.getContext();
subpanel.addPreference(createUserCertificatePreference(c));
subpanel.addPreference(createCaCertificatePreference(c));
}
@Override
public String validate() {
String result = super.validate();
if (result == null) {
result = validate(mUserCertificate, R.string.vpn_a_user_certificate);
}
if (result == null) {
result = validate(mCaCertificate, R.string.vpn_a_ca_certificate);
}
return result;
}
private Preference createUserCertificatePreference(Context c) {
mUserCertificate = createListPreference(c,
R.string.vpn_user_certificate_title,
mProfile.getUserCertificate(),
mKeyStore.saw(Credentials.USER_CERTIFICATE),
new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(
Preference pref, Object newValue) {
mProfile.setUserCertificate((String) newValue);
setSummary(pref, R.string.vpn_user_certificate,
(String) newValue);
return true;
}
});
setSummary(mUserCertificate, R.string.vpn_user_certificate,
mProfile.getUserCertificate());
return mUserCertificate;
}
private Preference createCaCertificatePreference(Context c) {
mCaCertificate = createListPreference(c,
R.string.vpn_ca_certificate_title,
mProfile.getCaCertificate(),
mKeyStore.saw(Credentials.CA_CERTIFICATE),
new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(
Preference pref, Object newValue) {
mProfile.setCaCertificate((String) newValue);
setSummary(pref, R.string.vpn_ca_certificate,
(String) newValue);
return true;
}
});
setSummary(mCaCertificate, R.string.vpn_ca_certificate,
mProfile.getCaCertificate());
return mCaCertificate;
}
private ListPreference createListPreference(Context c, int titleResId,
String text, String[] keys,
Preference.OnPreferenceChangeListener listener) {
ListPreference pref = new ListPreference(c);
pref.setTitle(titleResId);
pref.setDialogTitle(titleResId);
pref.setPersistent(true);
pref.setEntries(keys);
pref.setEntryValues(keys);
pref.setValue(text);
pref.setOnPreferenceChangeListener(listener);
return pref;
}
}

View File

@@ -1,68 +0,0 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.vpn;
import com.android.settings.R;
import android.content.Context;
import android.net.vpn.L2tpIpsecPskProfile;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceGroup;
/**
* The class for editing {@link L2tpIpsecPskProfile}.
*/
class L2tpIpsecPskEditor extends L2tpEditor {
private EditTextPreference mPresharedKey;
private SecretHandler mPskHandler;
public L2tpIpsecPskEditor(L2tpIpsecPskProfile p) {
super(p);
}
@Override
protected void loadExtraPreferencesTo(PreferenceGroup subpanel) {
Context c = subpanel.getContext();
subpanel.addPreference(createPresharedKeyPreference(c));
super.loadExtraPreferencesTo(subpanel);
}
@Override
public String validate() {
String result = super.validate();
return ((result != null) ? result : mPskHandler.validate());
}
private Preference createPresharedKeyPreference(Context c) {
SecretHandler pskHandler = mPskHandler = new SecretHandler(c,
R.string.vpn_ipsec_presharedkey_title,
R.string.vpn_ipsec_presharedkey) {
@Override
protected String getSecretFromProfile() {
return ((L2tpIpsecPskProfile) getProfile()).getPresharedKey();
}
@Override
protected void saveSecretToProfile(String secret) {
((L2tpIpsecPskProfile) getProfile()).setPresharedKey(secret);
}
};
return pskHandler.getPreference();
}
}

View File

@@ -1,73 +0,0 @@
/* * Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.vpn;
import com.android.settings.R;
import android.content.Context;
import android.net.vpn.PptpProfile;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceGroup;
/**
* The class for editing {@link PptpProfile}.
*/
class PptpEditor extends VpnProfileEditor {
private CheckBoxPreference mEncryption;
public PptpEditor(PptpProfile p) {
super(p);
}
@Override
protected void loadExtraPreferencesTo(PreferenceGroup subpanel) {
Context c = subpanel.getContext();
subpanel.addPreference(createEncryptionPreference(c));
PptpProfile profile = (PptpProfile) getProfile();
}
private Preference createEncryptionPreference(Context c) {
final PptpProfile profile = (PptpProfile) getProfile();
CheckBoxPreference encryption = mEncryption = new CheckBoxPreference(c);
boolean enabled = profile.isEncryptionEnabled();
setCheckBoxTitle(encryption, R.string.vpn_pptp_encryption_title);
encryption.setChecked(enabled);
setEncryptionSummary(encryption, enabled);
encryption.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(
Preference pref, Object newValue) {
boolean enabled = (Boolean) newValue;
profile.setEncryptionEnabled(enabled);
setEncryptionSummary(mEncryption, enabled);
return true;
}
});
return encryption;
}
private void setEncryptionSummary(CheckBoxPreference encryption,
boolean enabled) {
Context c = encryption.getContext();
String formatString = c.getString(enabled
? R.string.vpn_is_enabled
: R.string.vpn_is_disabled);
encryption.setSummary(String.format(
formatString, c.getString(R.string.vpn_pptp_encryption)));
}
}

View File

@@ -1,138 +0,0 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.vpn;
import com.android.settings.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
class Util {
static void showShortToastMessage(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
static void showShortToastMessage(Context context, int messageId) {
Toast.makeText(context, messageId, Toast.LENGTH_SHORT).show();
}
static void showLongToastMessage(Context context, String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
static void showLongToastMessage(Context context, int messageId) {
Toast.makeText(context, messageId, Toast.LENGTH_LONG).show();
}
static void showErrorMessage(Context c, String message) {
createErrorDialog(c, message, null).show();
}
static void showErrorMessage(Context c, String message,
DialogInterface.OnClickListener listener) {
createErrorDialog(c, message, listener).show();
}
static void deleteFile(String path) {
deleteFile(new File(path));
}
static void deleteFile(String path, boolean toDeleteSelf) {
deleteFile(new File(path), toDeleteSelf);
}
static void deleteFile(File f) {
deleteFile(f, true);
}
static void deleteFile(File f, boolean toDeleteSelf) {
if (f.isDirectory()) {
for (File child : f.listFiles()) deleteFile(child, true);
}
if (toDeleteSelf) f.delete();
}
static boolean isFileOrEmptyDirectory(String path) {
File f = new File(path);
if (!f.isDirectory()) return true;
String[] list = f.list();
return ((list == null) || (list.length == 0));
}
static boolean copyFiles(String sourcePath , String targetPath)
throws IOException {
return copyFiles(new File(sourcePath), new File(targetPath));
}
// returns false if sourceLocation is the same as the targetLocation
static boolean copyFiles(File sourceLocation , File targetLocation)
throws IOException {
if (sourceLocation.equals(targetLocation)) return false;
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists()) {
targetLocation.mkdir();
}
String[] children = sourceLocation.list();
for (int i=0; i<children.length; i++) {
copyFiles(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]));
}
} else if (sourceLocation.exists()) {
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
return true;
}
private static AlertDialog createErrorDialog(Context c, String message,
DialogInterface.OnClickListener okListener) {
AlertDialog.Builder b = new AlertDialog.Builder(c)
.setTitle(android.R.string.dialog_alert_title)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage(message);
if (okListener != null) {
b.setPositiveButton(R.string.vpn_back_button, okListener);
} else {
b.setPositiveButton(android.R.string.ok, null);
}
return b.create();
}
private Util() {
}
}

View File

@@ -1,261 +0,0 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.vpn;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.vpn.L2tpIpsecProfile;
import android.net.vpn.L2tpIpsecPskProfile;
import android.net.vpn.L2tpProfile;
import android.net.vpn.PptpProfile;
import android.net.vpn.VpnManager;
import android.net.vpn.VpnProfile;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.PreferenceActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
/**
* The activity class for editing a new or existing VPN profile.
*/
public class VpnEditor extends SettingsPreferenceFragment {
private static final int MENU_SAVE = Menu.FIRST;
private static final int MENU_CANCEL = Menu.FIRST + 1;
private static final int CONFIRM_DIALOG_ID = 0;
private static final String KEY_PROFILE = "profile";
private static final String KEY_ORIGINAL_PROFILE_NAME = "orig_profile_name";
private VpnManager mVpnManager;
private VpnProfileEditor mProfileEditor;
private boolean mAddingProfile;
private byte[] mOriginalProfileData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Loads the XML preferences file
addPreferencesFromResource(R.xml.vpn_edit);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mVpnManager = new VpnManager(getActivity());
VpnProfile p;
if (savedInstanceState != null) {
p = (VpnProfile)savedInstanceState.getParcelable(KEY_PROFILE);
} else {
p = (VpnProfile)getArguments().getParcelable(VpnSettings.KEY_VPN_PROFILE);
if (p == null) {
p = getActivity().getIntent().getParcelableExtra(VpnSettings.KEY_VPN_PROFILE);
}
}
Parcel parcel = Parcel.obtain();
p.writeToParcel(parcel, 0);
mOriginalProfileData = parcel.marshall();
parcel.setDataPosition(0);
VpnProfile profile = (VpnProfile) VpnProfile.CREATOR.createFromParcel(parcel);
mProfileEditor = getEditor(profile);
mAddingProfile = TextUtils.isEmpty(profile.getName());
initViewFor(profile);
registerForContextMenu(getListView());
setHasOptionsMenu(true);
}
@Override
public synchronized void onSaveInstanceState(Bundle outState) {
if (mProfileEditor == null) return;
outState.putParcelable(KEY_PROFILE, getProfile());
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.add(0, MENU_SAVE, 0, R.string.vpn_menu_done)
.setIcon(android.R.drawable.ic_menu_save);
menu.add(0, MENU_CANCEL, 0,
mAddingProfile ? R.string.vpn_menu_cancel
: R.string.vpn_menu_revert)
.setIcon(android.R.drawable.ic_menu_close_clear_cancel);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_SAVE:
Intent resultIntent = validateAndGetResult();
if (resultIntent != null) {
PreferenceActivity activity =
(PreferenceActivity) getActivity();
if (!mVpnManager.isIdle()) {
Toast.makeText(activity, R.string.service_busy,
Toast.LENGTH_SHORT).show();
} else {
activity.finishPreferencePanel(this,
Activity.RESULT_OK, resultIntent);
}
}
return true;
case MENU_CANCEL:
if (profileChanged()) {
showDialog(CONFIRM_DIALOG_ID);
} else {
finishFragment();
}
return true;
}
return super.onOptionsItemSelected(item);
}
/*
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (validateAndSetResult()) finish();
return true;
}
return super.onKeyDown(keyCode, event);
}*/
private void initViewFor(VpnProfile profile) {
mProfileEditor.loadPreferencesTo(getPreferenceScreen());
}
/* package */static String getTitle(Context context, VpnProfile profile, boolean adding) {
String formatString = adding
? context.getString(R.string.vpn_edit_title_add)
: context.getString(R.string.vpn_edit_title_edit);
return String.format(formatString,
profile.getType().getDisplayName());
}
/**
* Checks the validity of the inputs and set the profile as result if valid.
* @return true if the result is successfully set
*/
private Intent validateAndGetResult() {
String errorMsg = mProfileEditor.validate();
if (errorMsg != null) {
Util.showErrorMessage(getActivity(), errorMsg);
return null;
}
if (profileChanged()) {
return getResult(getProfile());
}
return null;
}
private Intent getResult(VpnProfile p) {
Intent intent = new Intent(getActivity(), VpnSettings.class);
intent.putExtra(VpnSettings.KEY_VPN_PROFILE, (Parcelable) p);
return intent;
}
private VpnProfileEditor getEditor(VpnProfile p) {
switch (p.getType()) {
case L2TP_IPSEC:
return new L2tpIpsecEditor((L2tpIpsecProfile) p);
case L2TP_IPSEC_PSK:
return new L2tpIpsecPskEditor((L2tpIpsecPskProfile) p);
case L2TP:
return new L2tpEditor((L2tpProfile) p);
case PPTP:
return new PptpEditor((PptpProfile) p);
default:
return new VpnProfileEditor(p);
}
}
@Override
public Dialog onCreateDialog(int id) {
if (id == CONFIRM_DIALOG_ID) {
return new AlertDialog.Builder(getActivity())
.setTitle(android.R.string.dialog_alert_title)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage(mAddingProfile
? R.string.vpn_confirm_add_profile_cancellation
: R.string.vpn_confirm_edit_profile_cancellation)
.setPositiveButton(R.string.vpn_yes_button,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int w) {
finishFragment();
}
})
.setNegativeButton(R.string.vpn_mistake_button, null)
.create();
}
return super.onCreateDialog(id);
}
/*
@Override
public void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
if (id == CONFIRM_DIALOG_ID) {
((AlertDialog)dialog).setMessage(mAddingProfile
? getString(R.string.vpn_confirm_add_profile_cancellation)
: getString(R.string.vpn_confirm_edit_profile_cancellation));
}
}*/
private VpnProfile getProfile() {
return mProfileEditor.getProfile();
}
private boolean profileChanged() {
Parcel newParcel = Parcel.obtain();
getProfile().writeToParcel(newParcel, 0);
byte[] newData = newParcel.marshall();
if (mOriginalProfileData.length == newData.length) {
for (int i = 0, n = mOriginalProfileData.length; i < n; i++) {
if (mOriginalProfileData[i] != newData[i]) return true;
}
return false;
}
return true;
}
}

View File

@@ -1,57 +0,0 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.vpn;
import android.app.Dialog;
import android.net.vpn.VpnProfile;
import android.view.View;
/**
* The interface to act on a {@link VpnProfile}.
*/
public interface VpnProfileActor {
VpnProfile getProfile();
/**
* Returns true if a connect dialog is needed before establishing a
* connection.
*/
boolean isConnectDialogNeeded();
/**
* Creates the view in the connect dialog.
*/
View createConnectView();
/**
* Validates the inputs in the dialog.
* @param dialog the connect dialog
* @return an error message if the inputs are not valid
*/
String validateInputs(Dialog dialog);
/**
* Establishes a VPN connection.
* @param dialog the connect dialog
*/
void connect(Dialog dialog);
/**
* Tears down the connection.
*/
void disconnect();
}

View File

@@ -1,254 +0,0 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.vpn;
import com.android.settings.R;
import android.content.Context;
import android.net.vpn.VpnProfile;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceGroup;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
/**
* The common class for editing {@link VpnProfile}.
*/
class VpnProfileEditor {
private static final String KEY_VPN_NAME = "vpn_name";
private EditTextPreference mName;
private EditTextPreference mServerName;
private EditTextPreference mDomainSuffices;
private VpnProfile mProfile;
public VpnProfileEditor(VpnProfile p) {
mProfile = p;
}
//@Override
public VpnProfile getProfile() {
return mProfile;
}
/**
* Adds the preferences to the panel. Subclasses should override
* {@link #loadExtraPreferencesTo(PreferenceGroup)} instead of this method.
*/
public void loadPreferencesTo(PreferenceGroup subpanel) {
Context c = subpanel.getContext();
mName = (EditTextPreference) subpanel.findPreference(KEY_VPN_NAME);
mName.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(
Preference pref, Object newValue) {
setName((String) newValue);
return true;
}
});
setName(getProfile().getName());
mName.getEditText().setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
subpanel.addPreference(createServerNamePreference(c));
loadExtraPreferencesTo(subpanel);
subpanel.addPreference(createDomainSufficesPreference(c));
}
/**
* Adds the extra preferences to the panel. Subclasses should add
* additional preferences in this method.
*/
protected void loadExtraPreferencesTo(PreferenceGroup subpanel) {
}
/**
* Validates the inputs in the preferences.
*
* @return an error message that is ready to be displayed in a dialog; or
* null if all the inputs are valid
*/
public String validate() {
String result = validate(mName, R.string.vpn_a_name);
return ((result != null)
? result
: validate(mServerName, R.string.vpn_a_vpn_server));
}
/**
* Creates a preference for users to input domain suffices.
*/
protected EditTextPreference createDomainSufficesPreference(Context c) {
EditTextPreference pref = mDomainSuffices = createEditTextPreference(c,
R.string.vpn_dns_search_list_title,
R.string.vpn_dns_search_list,
mProfile.getDomainSuffices(),
new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(
Preference pref, Object newValue) {
String v = ((String) newValue).trim();
mProfile.setDomainSuffices(v);
setSummary(pref, R.string.vpn_dns_search_list, v, false);
return true;
}
});
pref.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_URI);
return pref;
}
private Preference createServerNamePreference(Context c) {
EditTextPreference pref = mServerName = createEditTextPreference(c,
R.string.vpn_vpn_server_title,
R.string.vpn_vpn_server,
mProfile.getServerName(),
new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(
Preference pref, Object newValue) {
String v = ((String) newValue).trim();
mProfile.setServerName(v);
setSummary(pref, R.string.vpn_vpn_server, v);
return true;
}
});
pref.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_URI);
return pref;
}
protected EditTextPreference createEditTextPreference(Context c, int titleId,
int prefNameId, String value,
Preference.OnPreferenceChangeListener listener) {
EditTextPreference pref = new EditTextPreference(c);
pref.setTitle(titleId);
pref.setDialogTitle(titleId);
setSummary(pref, prefNameId, value);
pref.setText(value);
pref.setPersistent(true);
pref.setOnPreferenceChangeListener(listener);
return pref;
}
protected String validate(Preference pref, int fieldNameId) {
Context c = pref.getContext();
String value = (pref instanceof EditTextPreference)
? ((EditTextPreference) pref).getText()
: ((ListPreference) pref).getValue();
String formatString = (pref instanceof EditTextPreference)
? c.getString(R.string.vpn_error_miss_entering)
: c.getString(R.string.vpn_error_miss_selecting);
return (TextUtils.isEmpty(value)
? String.format(formatString, c.getString(fieldNameId))
: null);
}
protected void setSummary(Preference pref, int fieldNameId, String v) {
setSummary(pref, fieldNameId, v, true);
}
protected void setSummary(Preference pref, int fieldNameId, String v,
boolean required) {
Context c = pref.getContext();
String formatString = required
? c.getString(R.string.vpn_field_not_set)
: c.getString(R.string.vpn_field_not_set_optional);
pref.setSummary(TextUtils.isEmpty(v)
? String.format(formatString, c.getString(fieldNameId))
: v);
}
protected void setCheckBoxTitle(CheckBoxPreference pref, int fieldNameId) {
Context c = pref.getContext();
String formatString = c.getString(R.string.vpn_enable_field);
pref.setTitle(String.format(formatString, c.getString(fieldNameId)));
}
private void setName(String newName) {
newName = (newName == null) ? "" : newName.trim();
mName.setText(newName);
getProfile().setName(newName);
setSummary(mName, R.string.vpn_name, newName);
}
// Secret is tricky to handle because empty field may mean "not set" or
// "unchanged". This class hides that logic from callers.
protected static abstract class SecretHandler {
private EditTextPreference mPref;
private int mFieldNameId;
private boolean mHadSecret;
protected SecretHandler(Context c, int titleId, int fieldNameId) {
String value = getSecretFromProfile();
mHadSecret = !TextUtils.isEmpty(value);
mFieldNameId = fieldNameId;
EditTextPreference pref = mPref = new EditTextPreference(c);
pref.setTitle(titleId);
pref.setDialogTitle(titleId);
pref.getEditText().setInputType(
InputType.TYPE_TEXT_VARIATION_PASSWORD);
pref.getEditText().setTransformationMethod(
new PasswordTransformationMethod());
pref.setText("");
pref.getEditText().setHint(mHadSecret
? R.string.vpn_secret_unchanged
: R.string.vpn_secret_not_set);
setSecretSummary(value);
pref.setPersistent(true);
saveSecretToProfile("");
pref.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(
Preference pref, Object newValue) {
saveSecretToProfile((String) newValue);
setSecretSummary((String) newValue);
return true;
}
});
}
protected EditTextPreference getPreference() {
return mPref;
}
protected String validate() {
Context c = mPref.getContext();
String value = mPref.getText();
return ((TextUtils.isEmpty(value) && !mHadSecret)
? String.format(
c.getString(R.string.vpn_error_miss_entering),
c.getString(mFieldNameId))
: null);
}
private void setSecretSummary(String value) {
EditTextPreference pref = mPref;
Context c = pref.getContext();
String formatString = (TextUtils.isEmpty(value) && !mHadSecret)
? c.getString(R.string.vpn_field_not_set)
: c.getString(R.string.vpn_field_is_set);
pref.setSummary(
String.format(formatString, c.getString(mFieldNameId)));
}
protected abstract String getSecretFromProfile();
protected abstract void saveSecretToProfile(String secret);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,77 +0,0 @@
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.vpn;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import android.app.Activity;
import android.content.Intent;
import android.net.vpn.VpnManager;
import android.net.vpn.VpnType;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import java.util.HashMap;
import java.util.Map;
/**
* The activity to select a VPN type.
*/
public class VpnTypeSelection extends SettingsPreferenceFragment {
private Map<String, VpnType> mTypeMap = new HashMap<String, VpnType>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.vpn_type);
initTypeList();
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen ps, Preference pref) {
((PreferenceActivity)getActivity())
.finishPreferencePanel(this, Activity.RESULT_OK,
getResultIntent(mTypeMap.get(pref.getTitle().toString())));
return true;
}
private void initTypeList() {
PreferenceScreen root = getPreferenceScreen();
final Activity activity = getActivity();
for (VpnType t : VpnManager.getSupportedVpnTypes()) {
String displayName = t.getDisplayName();
String message = String.format(
activity.getString(R.string.vpn_edit_title_add), displayName);
mTypeMap.put(message, t);
Preference pref = new Preference(activity);
pref.setTitle(message);
pref.setSummary(t.getDescriptionId());
root.addPreference(pref);
}
}
private Intent getResultIntent(VpnType type) {
Intent intent = new Intent(getActivity(), VpnSettings.class);
intent.putExtra(VpnSettings.KEY_VPN_TYPE, type.toString());
return intent;
}
}