OSDN Git Service

Add VPN settings classes to Settings app.
[android-x86/packages-apps-Settings.git] / src / com / android / settings / vpn / L2tpIpsecEditor.java
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.settings.vpn;
18
19 import com.android.settings.R;
20
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;
28
29 /**
30  * The class for editing {@link L2tpIpsecProfile}.
31  */
32 class L2tpIpsecEditor extends SingleServerEditor {
33     private static final String TAG = L2tpIpsecEditor.class.getSimpleName();
34
35     private ListPreference mUserCertificate;
36     private ListPreference mCaCertificate;
37     private ListPreference mUserkey;
38
39     private L2tpIpsecProfile mProfile;
40
41     public L2tpIpsecEditor(L2tpIpsecProfile p) {
42         super(p);
43         mProfile = p;
44     }
45
46     //@Override
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));
54     }
55
56     //@Override
57     public String validate(Context c) {
58         String result = super.validate(c);
59         if (result != null) {
60             return result;
61         } else if (mProfile.isCustomized()) {
62             return null;
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);
69         } else {
70             return null;
71         }
72     }
73
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);
84                     }
85                 });
86         return mUserCertificate;
87     }
88
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);
99                     }
100                 });
101         return mCaCertificate;
102     }
103
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);
114                     }
115                 });
116         return mUserkey;
117     }
118
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);
128         pref.setValue(text);
129         pref.setSummary(checkNull(text, c));
130         pref.setOnPreferenceChangeListener(listener);
131         return pref;
132     }
133
134     private boolean onPreferenceChangeCommon(Preference pref, Object newValue) {
135         pref.setSummary(checkNull(newValue.toString(), pref.getContext()));
136         return true;
137     }
138 }