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.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;
38 * The activity class for editing a new or existing VPN profile.
40 public class VpnEditor extends PreferenceActivity {
41 private static final String TAG = VpnEditor.class.getSimpleName();
43 private static final int MENU_SAVE = Menu.FIRST;
44 private static final int MENU_CANCEL = Menu.FIRST + 1;
46 private EditTextPreference mName;
48 private VpnProfileEditor mProfileEditor;
51 public void onCreate(Bundle savedInstanceState) {
52 super.onCreate(savedInstanceState);
54 // Loads the XML preferences file
55 addPreferencesFromResource(R.xml.vpn_edit);
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);
67 if (savedInstanceState == null) {
68 VpnProfile p = getIntent().getParcelableExtra(
69 VpnSettings.KEY_VPN_PROFILE);
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);
85 public boolean onOptionsItemSelected(MenuItem item) {
86 switch (item.getItemId()) {
88 if (validateAndSetResult()) {
93 showCancellationConfirmDialog();
96 return super.onOptionsItemSelected(item);
99 private void initViewFor(VpnProfile profile) {
100 VpnProfileEditor editor = getEditor(profile);
101 VpnType type = profile.getType();
102 PreferenceGroup subsettings = getPreferenceScreen();
105 setName(profile.getName());
107 editor.loadPreferencesTo(subsettings);
108 mProfileEditor = editor;
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()));
116 setTitle(String.format(getString(R.string.vpn_edit_title_edit),
117 profile.getType().getDisplayName()));
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)
130 * Checks the validity of the inputs and set the profile as result if valid.
131 * @return true if the result is successfully set
133 private boolean validateAndSetResult() {
134 String errorMsg = null;
135 if (Util.isNullOrEmpty(mName.getText())) {
136 errorMsg = getString(R.string.vpn_error_name_empty);
138 errorMsg = mProfileEditor.validate(this);
141 if (errorMsg != null) {
142 Util.showErrorMessage(this, errorMsg);
146 setResult(mProfileEditor.getProfile());
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);
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);
164 throw new RuntimeException("Unknown profile type: " + p.getType());
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) {
178 .setNegativeButton(R.string.vpn_mistake_button, null)