OSDN Git Service

Add VPN settings classes to Settings app.
[android-x86/packages-apps-Settings.git] / src / com / android / settings / vpn / VpnEditor.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.app.AlertDialog;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.net.vpn.L2tpIpsecProfile;
25 import android.net.vpn.SingleServerProfile;
26 import android.net.vpn.VpnProfile;
27 import android.net.vpn.VpnType;
28 import android.os.Bundle;
29 import android.os.Parcelable;
30 import android.preference.EditTextPreference;
31 import android.preference.Preference;
32 import android.preference.PreferenceActivity;
33 import android.preference.PreferenceGroup;
34 import android.view.Menu;
35 import android.view.MenuItem;
36
37 /**
38  * The activity class for editing a new or existing VPN profile.
39  */
40 public class VpnEditor extends PreferenceActivity {
41     private static final String TAG = VpnEditor.class.getSimpleName();
42
43     private static final int MENU_SAVE = Menu.FIRST;
44     private static final int MENU_CANCEL = Menu.FIRST + 1;
45
46     private EditTextPreference mName;
47
48     private VpnProfileEditor mProfileEditor;
49
50     @Override
51     public void onCreate(Bundle savedInstanceState) {
52         super.onCreate(savedInstanceState);
53
54         // Loads the XML preferences file
55         addPreferencesFromResource(R.xml.vpn_edit);
56
57         mName = (EditTextPreference) findPreference("vpn_name");
58         mName.setOnPreferenceChangeListener(
59                 new Preference.OnPreferenceChangeListener() {
60                     public boolean onPreferenceChange(
61                             Preference pref, Object newValue) {
62                         setName((String) newValue);
63                         return true;
64                     }
65                 });
66
67         if (savedInstanceState == null) {
68             VpnProfile p = getIntent().getParcelableExtra(
69                     VpnSettings.KEY_VPN_PROFILE);
70             initViewFor(p);
71         }
72     }
73
74     @Override
75     public boolean onCreateOptionsMenu(Menu menu) {
76         super.onCreateOptionsMenu(menu);
77         menu.add(0, MENU_SAVE, 0, R.string.vpn_menu_save)
78             .setIcon(android.R.drawable.ic_menu_save);
79         menu.add(0, MENU_CANCEL, 0, R.string.vpn_menu_cancel)
80             .setIcon(android.R.drawable.ic_menu_close_clear_cancel);
81         return true;
82     }
83
84     @Override
85     public boolean onOptionsItemSelected(MenuItem item) {
86         switch (item.getItemId()) {
87             case MENU_SAVE:
88                 if (validateAndSetResult()) {
89                     finish();
90                 }
91                 return true;
92             case MENU_CANCEL:
93                 showCancellationConfirmDialog();
94                 return true;
95         }
96         return super.onOptionsItemSelected(item);
97     }
98
99     private void initViewFor(VpnProfile profile) {
100         VpnProfileEditor editor = getEditor(profile);
101         VpnType type = profile.getType();
102         PreferenceGroup subsettings = getPreferenceScreen();
103
104         setTitle(profile);
105         setName(profile.getName());
106
107         editor.loadPreferencesTo(subsettings);
108         mProfileEditor = editor;
109     }
110
111     private void setTitle(VpnProfile profile) {
112         if (Util.isNullOrEmpty(profile.getName())) {
113             setTitle(String.format(getString(R.string.vpn_edit_title_add),
114                     profile.getType().getDisplayName()));
115         } else {
116             setTitle(String.format(getString(R.string.vpn_edit_title_edit),
117                     profile.getType().getDisplayName()));
118         }
119     }
120
121     private void setName(String newName) {
122         newName = (newName == null) ? "" : newName.trim();
123         mName.setText(newName);
124         mName.setSummary(Util.isNullOrEmpty(newName)
125                 ? getString(R.string.vpn_name_summary)
126                 : newName);
127     }
128
129     /**
130      * Checks the validity of the inputs and set the profile as result if valid.
131      * @return true if the result is successfully set
132      */
133     private boolean validateAndSetResult() {
134         String errorMsg = null;
135         if (Util.isNullOrEmpty(mName.getText())) {
136             errorMsg = getString(R.string.vpn_error_name_empty);
137         } else {
138             errorMsg = mProfileEditor.validate(this);
139         }
140
141         if (errorMsg != null) {
142             Util.showErrorMessage(this, errorMsg);
143             return false;
144         }
145
146         setResult(mProfileEditor.getProfile());
147         return true;
148     }
149
150     private void setResult(VpnProfile p) {
151         p.setName(mName.getText());
152         p.setId(Util.base64Encode(p.getName().getBytes()));
153         Intent intent = new Intent(this, VpnSettings.class);
154         intent.putExtra(VpnSettings.KEY_VPN_PROFILE, (Parcelable) p);
155         setResult(RESULT_OK, intent);
156     }
157
158     private VpnProfileEditor getEditor(VpnProfile p) {
159         if (p instanceof L2tpIpsecProfile) {
160             return new L2tpIpsecEditor((L2tpIpsecProfile) p);
161         } else if (p instanceof SingleServerProfile) {
162             return new SingleServerEditor((SingleServerProfile) p);
163         } else {
164             throw new RuntimeException("Unknown profile type: " + p.getType());
165         }
166     }
167
168     private void showCancellationConfirmDialog() {
169         new AlertDialog.Builder(this)
170                 .setTitle(R.string.vpn_error_title)
171                 .setMessage(R.string.vpn_confirm_profile_cancellation)
172                 .setPositiveButton(R.string.vpn_yes_button,
173                         new DialogInterface.OnClickListener() {
174                             public void onClick(DialogInterface dialog, int w) {
175                                 finish();
176                             }
177                         })
178                 .setNegativeButton(R.string.vpn_mistake_button, null)
179                 .show();
180     }
181 }