2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.settings.vpn;
19 import com.android.settings.R;
21 import android.content.Context;
22 import android.net.vpn.L2tpIpsecProfile;
23 import android.preference.EditTextPreference;
24 import android.preference.ListPreference;
25 import android.preference.Preference;
26 import android.preference.PreferenceGroup;
27 import android.security.Keystore;
30 * The class for editing {@link L2tpIpsecProfile}.
32 class L2tpIpsecEditor extends SingleServerEditor {
33 private static final String TAG = L2tpIpsecEditor.class.getSimpleName();
35 private ListPreference mUserCertificate;
36 private ListPreference mCaCertificate;
37 private ListPreference mUserkey;
39 private L2tpIpsecProfile mProfile;
41 public L2tpIpsecEditor(L2tpIpsecProfile p) {
47 public void loadPreferencesTo(PreferenceGroup subsettings) {
48 super.loadPreferencesTo(subsettings);
49 Context c = subsettings.getContext();
50 subsettings.addPreference(createUserkeyPreference(c));
51 subsettings.addPreference(createUserCertificatePreference(c));
52 subsettings.addPreference(createCaCertificatePreference(c));
53 subsettings.addPreference(createDomainSufficesPreference(c));
57 public String validate(Context c) {
58 String result = super.validate(c);
61 } else if (mProfile.isCustomized()) {
63 } else if (Util.isNullOrEmpty(mUserkey.getValue())) {
64 return c.getString(R.string.vpn_error_userkey_not_selected);
65 } else if (Util.isNullOrEmpty(mUserCertificate.getValue())) {
66 return c.getString(R.string.vpn_error_user_certificate_not_selected);
67 } else if (Util.isNullOrEmpty(mCaCertificate.getValue())) {
68 return c.getString(R.string.vpn_error_ca_certificate_not_selected);
74 private Preference createUserCertificatePreference(Context c) {
75 mUserCertificate = createListPreference(c,
76 R.string.vpn_user_certificate_title,
77 mProfile.getUserCertificate(),
78 Keystore.getInstance().getAllCertificateKeys(),
79 new Preference.OnPreferenceChangeListener() {
80 public boolean onPreferenceChange(
81 Preference pref, Object newValue) {
82 mProfile.setUserCertificate((String) newValue);
83 return onPreferenceChangeCommon(pref, newValue);
86 return mUserCertificate;
89 private Preference createCaCertificatePreference(Context c) {
90 mCaCertificate = createListPreference(c,
91 R.string.vpn_ca_certificate_title,
92 mProfile.getCaCertificate(),
93 Keystore.getInstance().getAllCertificateKeys(),
94 new Preference.OnPreferenceChangeListener() {
95 public boolean onPreferenceChange(
96 Preference pref, Object newValue) {
97 mProfile.setCaCertificate((String) newValue);
98 return onPreferenceChangeCommon(pref, newValue);
101 return mCaCertificate;
104 private Preference createUserkeyPreference(Context c) {
105 mUserkey = createListPreference(c,
106 R.string.vpn_userkey_title,
107 mProfile.getUserkey(),
108 Keystore.getInstance().getAllUserkeyKeys(),
109 new Preference.OnPreferenceChangeListener() {
110 public boolean onPreferenceChange(
111 Preference pref, Object newValue) {
112 mProfile.setUserkey((String) newValue);
113 return onPreferenceChangeCommon(pref, newValue);
119 private ListPreference createListPreference(Context c, int titleResId,
120 String text, String[] keys,
121 Preference.OnPreferenceChangeListener listener) {
122 ListPreference pref = new ListPreference(c);
123 pref.setTitle(titleResId);
124 pref.setDialogTitle(titleResId);
125 pref.setPersistent(true);
126 pref.setEntries(keys);
127 pref.setEntryValues(keys);
129 pref.setSummary(checkNull(text, c));
130 pref.setOnPreferenceChangeListener(listener);
134 private boolean onPreferenceChangeCommon(Preference pref, Object newValue) {
135 pref.setSummary(checkNull(newValue.toString(), pref.getContext()));