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

@@ -301,7 +301,7 @@
<activity android:name="Settings$VpnSettingsActivity"
android:theme="@android:style/Theme.Holo"
android:label="@string/vpn_settings_activity_title"
android:label="@string/vpn_settings_title"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
@@ -311,7 +311,7 @@
<category android:name="com.android.settings.SHORTCUT" />
</intent-filter>
<meta-data android:name="com.android.settings.FRAGMENT_CLASS"
android:value="com.android.settings.vpn.VpnSettings" />
android:value="com.android.settings.vpn2.VpnSettings" />
<meta-data android:name="com.android.settings.TOP_LEVEL_HEADER_ID"
android:resource="@id/wireless_settings" />
<meta-data android:name="com.android.settings.PARENT_FRAGMENT_TITLE"

View File

@@ -1,69 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dip">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/username_str"
android:layout_width="@dimen/vpn_connect_input_box_label_width"
android:layout_height="wrap_content"
android:textSize="@dimen/vpn_connect_normal_text_size"
android:gravity="right"
android:layout_marginRight="@dimen/vpn_connect_margin_right"
android:text="@string/vpn_username_colon" />
<EditText android:id="@+id/username_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="True"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/password_str"
android:layout_width="@dimen/vpn_connect_input_box_label_width"
android:layout_height="wrap_content"
android:textSize="@dimen/vpn_connect_normal_text_size"
android:gravity="right"
android:layout_marginRight="@dimen/vpn_connect_margin_right"
android:text="@string/vpn_password_colon" />
<EditText android:id="@+id/password_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:password="True"
android:singleLine="True"/>
</LinearLayout>
<CheckBox android:id="@+id/save_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="66dip"
android:text="@string/vpn_save_username" />
</LinearLayout>
</ScrollView>

View File

@@ -15,9 +15,6 @@
-->
<resources>
<dimen name="vpn_connect_margin_right">10sp</dimen>
<dimen name="vpn_connect_normal_text_size">16sp</dimen>
<dimen name="vpn_connect_input_box_label_width">90sp</dimen>
<dimen name="device_memory_usage_button_width">16dip</dimen>
<dimen name="device_memory_usage_button_height">32dip</dimen>
<dimen name="data_usage_chart_height">220dip</dimen>

View File

@@ -2953,137 +2953,10 @@ found in the list of installed applications.</string>
<string name="gadget_toggle_wifi">Updating Wi-Fi setting</string>
<string name="gadget_toggle_bluetooth">Updating Bluetooth setting</string>
<string name="vpn_settings_activity_title">VPN settings</string>
<!-- In VPN connect dialog, for inputing username and password -->
<string name="vpn_username_colon">Username:</string>
<string name="vpn_password_colon">Password:</string>
<string name="vpn_a_username">a username</string>
<string name="vpn_a_password">a password</string>
<!-- In VPN connect dialog where user may check to remember the username entered -->
<string name="vpn_save_username">Remember username</string>
<string name="vpn_connect_button">Connect</string>
<string name="vpn_yes_button">Yes</string>
<string name="vpn_no_button">No</string>
<string name="vpn_back_button">Back</string>
<string name="vpn_mistake_button">No</string>
<string name="vpn_menu_done">Save</string>
<string name="vpn_menu_cancel">Cancel</string>
<string name="vpn_menu_revert">Revert</string>
<string name="vpn_menu_connect">Connect to network</string>
<string name="vpn_menu_disconnect">Disconnect from network</string>
<!-- VPN error dialog messages -->
<string name="vpn_error_miss_entering">You must enter <xliff:g id="code">%s</xliff:g>.</string>
<string name="vpn_error_miss_selecting">You must select <xliff:g id="option">%s</xliff:g>.</string>
<string name="vpn_error_duplicate_name">The VPN name \'<xliff:g id="name" example="Home Network">%s</xliff:g>\' already exists. Find another name.</string>
<string name="vpn_confirm_profile_deletion">Are you sure you want to delete this VPN?</string>
<string name="vpn_confirm_add_profile_cancellation">Are you sure you don\'t want to create this profile?</string>
<string name="vpn_confirm_edit_profile_cancellation">Are you sure you want to discard the changes made to this profile?</string>
<string name="vpn_confirm_reconnect">Unable to connect to the network. Do you want to try again?</string>
<string name="vpn_reconnect_from_lost">Connection lost. Do you want to connect again?</string>
<string name="vpn_unknown_server_dialog_msg">Server name cannot be resolved. Do you want to check your server name setting?</string>
<string name="vpn_challenge_error_dialog_msg">Challenge error. Do you want to check your secret setting?</string>
<string name="vpn_secret_not_set_dialog_msg">One or more secrets are missing in this VPN configuration. Do you want to check your secret setting?</string>
<string name="vpn_auth_error_dialog_msg">The username or password you entered is incorrect. Do you want to try again?</string>
<string name="vpn_remote_hung_up_error_dialog_msg">Server hung up. The username or password you entered could be incorrect. Do you want to try again?</string>
<string name="vpn_remote_ppp_hung_up_error_dialog_msg">Server hung up. It is possible that you are behind a firewall that prevents you from connecting to the server. Do you want to try again?</string>
<string name="vpn_ppp_negotiation_failed_dialog_msg">Server negotiation failed. The server may not agree with your encryption option. Do you want to check your encryption setting?</string>
<!-- VPN type selection activity title -->
<string name="vpn_type_title">Add VPN</string>
<!-- "Add VPN" preference title -->
<string name="vpn_add_new_vpn">Add VPN</string>
<!-- VPN details screen title -->
<string name="vpn_details_title">VPN details</string>
<!-- VPN profile editor title when adding a new profile -->
<string name="vpn_edit_title_add">Add <xliff:g id="name">%s</xliff:g> VPN</string>
<!-- VPN profile editor title when editing an existing profile -->
<string name="vpn_edit_title_edit"><xliff:g id="name">%s</xliff:g> details</string>
<!-- Preference group title for a list of VPN profiles -->
<string name="vpns">VPNs</string>
<!-- Preference summary text when VPN is connecting -->
<string name="vpn_connecting">Connecting...</string>
<!-- Preference summary text when VPN is disconnecting -->
<string name="vpn_disconnecting">Disconnecting...</string>
<!-- Preference summary text when VPN is connected -->
<string name="vpn_connected">Connected</string>
<!-- Preference summary text when VPN is not connected -->
<string name="vpn_connect_hint">Connect to network</string>
<!-- Name of a VPN profile -->
<string name="vpn_a_name">a VPN name</string>
<!-- Toast message shown when a profile is added -->
<string name="vpn_profile_added">\'<xliff:g id="name">%s</xliff:g>\' is added</string>
<!-- Toast message shown when changes of a profile is saved -->
<string name="vpn_profile_replaced">Changes are made to \'<xliff:g id="name">%s</xliff:g>\'</string>
<!-- Preference title -->
<string name="vpn_user_certificate_title">Set user certificate</string>
<!-- Complete term -->
<string name="vpn_user_certificate">User certificate</string>
<string name="vpn_a_user_certificate">a user certificate</string>
<!-- Preference title -->
<string name="vpn_ca_certificate_title">Set CA certificate</string>
<!-- Complete term -->
<string name="vpn_ca_certificate">Certificate authority (CA) certificate</string>
<string name="vpn_a_ca_certificate">a CA certificate</string>
<!-- Preference title -->
<string name="vpn_l2tp_secret_string_title">Set L2TP secret</string>
<!-- Complete term -->
<string name="vpn_a_l2tp_secret">an L2TP secret</string>
<string name="vpn_pptp_encryption_title">encryption</string>
<string name="vpn_pptp_encryption">PPTP encryption</string>
<!-- Preference title -->
<string name="vpn_ipsec_presharedkey_title">Set IPSec pre-shared key</string>
<!-- Complete term -->
<string name="vpn_ipsec_presharedkey">IPSec pre-shared key</string>
<string name="vpn_a_ipsec_presharedkey">an IPSec pre-shared key</string>
<!-- Preference title -->
<string name="vpn_vpn_server_title">Set VPN server</string>
<!-- Complete term -->
<string name="vpn_vpn_server">VPN server</string>
<string name="vpn_a_vpn_server">a VPN server</string>
<!-- Dialog title for setting VPN server name -->
<string name="vpn_vpn_server_dialog_title">VPN server name</string>
<!-- Preference title -->
<string name="vpn_dns_search_list_title">DNS search domains</string>
<!-- Complete term -->
<string name="vpn_dns_search_list">DNS search domains</string>
<!-- Summary text to hint that the value is set -->
<string name="vpn_field_is_set"><xliff:g id="value">%s</xliff:g> is set</string>
<!-- Summary text to hint that the value is not set -->
<string name="vpn_field_not_set"><xliff:g id="value">%s</xliff:g> not set</string>
<!-- Summary text to hint that the value is not set but it's not required-->
<string name="vpn_field_not_set_optional"><xliff:g id="value">%s</xliff:g> not set (optional)</string>
<!-- CheckBoxPreference title to enable something -->
<string name="vpn_enable_field">Enable <xliff:g id="option">%s</xliff:g></string>
<!-- CheckBoxPreference title to disable something -->
<string name="vpn_disable_field">Disable <xliff:g id="option">%s</xliff:g></string>
<!-- CheckBoxPreference summary to hint that something is enabled -->
<string name="vpn_is_enabled"><xliff:g id="option">%s</xliff:g> is enabled</string>
<!-- CheckBoxPreference summary to hint that something is disabled -->
<string name="vpn_is_disabled"><xliff:g id="option">%s</xliff:g> is disabled</string>
<!-- Title of preference to enter the VPN settings activity -->
<string name="vpn_settings_title">VPN settings</string>
<!-- Summary of preference to enter the VPN settings activity -->
<string name="vpn_settings_summary">Set up &amp; manage Virtual Private Networks (VPNs)</string>
<!-- A secret edit field's grayed out value when it has not been modified -->
<string name="vpn_secret_unchanged">(unchanged)</string>
<!-- A secret edit field's grayed out value when it has not been set -->
<string name="vpn_secret_not_set">(not set)</string>
<!-- Title of preference group for credential storage settings [CHAR LIMIT=30] -->
<string name="credentials_title">Credential storage</string>

View File

@@ -1,26 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings">
<EditTextPreference
android:key="vpn_name"
android:title="@string/vpn_name"
android:dialogTitle="@string/vpn_name"
android:singleLine="true"/>
</PreferenceScreen>

View File

@@ -1,36 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/vpn_settings_activity_title">
<PreferenceScreen android:key="add_new_vpn"
android:title="@string/vpn_add_new_vpn">
<!--intent
android:action="android.intent.action.MAIN"
android:targetPackage="com.android.settings.vpn"
android:targetClass="com.android.settings.vpn.VpnEditor" /-->
</PreferenceScreen>
<!--CheckBoxPreference android:key="installer_enabled"
android:defaultValue="false"
android:title="@string/installer_enabled"
android:summaryOn="@string/installer_enabled_summary_on"
android:summaryOff="@string/installer_enabled_summary_off" /-->
<PreferenceCategory android:key="vpn_list" android:title="@string/vpns">
</PreferenceCategory>
</PreferenceScreen>

View File

@@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"
android:title="@string/vpn_type_title">
</PreferenceScreen>

View File

@@ -34,7 +34,7 @@
android:persistent="false" />
<PreferenceScreen
android:fragment="com.android.settings.vpn.VpnSettings"
android:fragment="com.android.settings.vpn2.VpnSettings"
android:key="vpn_settings"
android:title="@string/vpn_settings_title"
android:summary="@string/vpn_settings_summary" >

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;
}
}