OSDN Git Service

3ab0b909d06b925ecf156a3a3cf291143a9f0e4c
[android-x86/packages-apps-Settings.git] / src / com / android / settings / vpn / VpnEditor.java
1 /*
2  * Copyright (C) 2009 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 import com.android.settings.SettingsPreferenceFragment;
21
22 import android.app.Activity;
23 import android.app.AlertDialog;
24 import android.app.Dialog;
25 import android.content.DialogInterface;
26 import android.content.Intent;
27 import android.net.vpn.L2tpIpsecProfile;
28 import android.net.vpn.L2tpIpsecPskProfile;
29 import android.net.vpn.L2tpProfile;
30 import android.net.vpn.PptpProfile;
31 import android.net.vpn.VpnProfile;
32 import android.os.Bundle;
33 import android.os.Parcel;
34 import android.os.Parcelable;
35 import android.text.TextUtils;
36 import android.util.Log;
37 import android.view.Menu;
38 import android.view.MenuInflater;
39 import android.view.MenuItem;
40
41 /**
42  * The activity class for editing a new or existing VPN profile.
43  */
44 public class VpnEditor extends SettingsPreferenceFragment {
45     private static final int MENU_SAVE = Menu.FIRST;
46     private static final int MENU_CANCEL = Menu.FIRST + 1;
47     private static final int CONFIRM_DIALOG_ID = 0;
48     private static final String KEY_PROFILE = "profile";
49     private static final String KEY_ORIGINAL_PROFILE_NAME = "orig_profile_name";
50
51     private VpnProfileEditor mProfileEditor;
52     private boolean mAddingProfile;
53     private byte[] mOriginalProfileData;
54
55     @Override
56     public void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58
59         // Loads the XML preferences file
60         addPreferencesFromResource(R.xml.vpn_edit);
61     }
62
63     @Override
64     public void onActivityCreated(Bundle savedInstanceState) {
65         super.onActivityCreated(savedInstanceState);
66
67         VpnProfile p;
68         if (savedInstanceState != null) {
69             p = (VpnProfile)savedInstanceState.getParcelable(KEY_PROFILE);
70         } else {
71             p = (VpnProfile)getArguments().getParcelable(VpnSettings.KEY_VPN_PROFILE);
72             if (p == null) {
73                 p = getActivity().getIntent().getParcelableExtra(VpnSettings.KEY_VPN_PROFILE);
74             }
75         }
76
77         mProfileEditor = getEditor(p);
78         mAddingProfile = TextUtils.isEmpty(p.getName());
79
80         initViewFor(p);
81
82         Parcel parcel = Parcel.obtain();
83         p.writeToParcel(parcel, 0);
84         mOriginalProfileData = parcel.marshall();
85
86         registerForContextMenu(getListView());
87         setHasOptionsMenu(true);
88     }
89
90     @Override
91     public synchronized void onSaveInstanceState(Bundle outState) {
92         if (mProfileEditor == null) return;
93
94         outState.putParcelable(KEY_PROFILE, getProfile());
95     }
96
97     @Override
98     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
99         super.onCreateOptionsMenu(menu, inflater);
100         menu.add(0, MENU_SAVE, 0, R.string.vpn_menu_done)
101             .setIcon(android.R.drawable.ic_menu_save);
102         menu.add(0, MENU_CANCEL, 0,
103                 mAddingProfile ? R.string.vpn_menu_cancel
104                                : R.string.vpn_menu_revert)
105             .setIcon(android.R.drawable.ic_menu_close_clear_cancel);
106     }
107
108     @Override
109     public boolean onOptionsItemSelected(MenuItem item) {
110         switch (item.getItemId()) {
111             case MENU_SAVE:
112                 if (validateAndSetResult()) finishFragment();
113                 return true;
114
115             case MENU_CANCEL:
116                 if (profileChanged()) {
117                     showDialog(CONFIRM_DIALOG_ID);
118                 } else {
119                     finishFragment();
120                 }
121                 return true;
122         }
123         return super.onOptionsItemSelected(item);
124     }
125
126     /*
127     @Override
128     public boolean onKeyDown(int keyCode, KeyEvent event) {
129         switch (keyCode) {
130             case KeyEvent.KEYCODE_BACK:
131                 if (validateAndSetResult()) finish();
132                 return true;
133         }
134         return super.onKeyDown(keyCode, event);
135     }*/
136
137     private void initViewFor(VpnProfile profile) {
138         setTitle(profile);
139         mProfileEditor.loadPreferencesTo(getPreferenceScreen());
140     }
141
142     private void setTitle(VpnProfile profile) {
143         final Activity activity = getActivity();
144         String formatString = mAddingProfile
145                 ? activity.getString(R.string.vpn_edit_title_add)
146                 : activity.getString(R.string.vpn_edit_title_edit);
147         activity.setTitle(String.format(formatString,
148                 profile.getType().getDisplayName()));
149     }
150
151     /**
152      * Checks the validity of the inputs and set the profile as result if valid.
153      * @return true if the result is successfully set
154      */
155     private boolean validateAndSetResult() {
156         String errorMsg = mProfileEditor.validate();
157
158         if (errorMsg != null) {
159             Util.showErrorMessage(getActivity(), errorMsg);
160             return false;
161         }
162
163         if (profileChanged()) setResult(getProfile());
164         return true;
165     }
166
167     private void setResult(VpnProfile p) {
168         Intent intent = new Intent(getActivity(), VpnSettings.class);
169         intent.putExtra(VpnSettings.KEY_VPN_PROFILE, (Parcelable) p);
170         setResult(Activity.RESULT_OK, intent);
171     }
172
173     private VpnProfileEditor getEditor(VpnProfile p) {
174         switch (p.getType()) {
175             case L2TP_IPSEC:
176                 return new L2tpIpsecEditor((L2tpIpsecProfile) p);
177
178             case L2TP_IPSEC_PSK:
179                 return new L2tpIpsecPskEditor((L2tpIpsecPskProfile) p);
180
181             case L2TP:
182                 return new L2tpEditor((L2tpProfile) p);
183
184             case PPTP:
185                 return new PptpEditor((PptpProfile) p);
186
187             default:
188                 return new VpnProfileEditor(p);
189         }
190     }
191
192
193     @Override
194     public Dialog onCreateDialog(int id) {
195         if (id == CONFIRM_DIALOG_ID) {
196             return new AlertDialog.Builder(getActivity())
197                     .setTitle(android.R.string.dialog_alert_title)
198                     .setIcon(android.R.drawable.ic_dialog_alert)
199                     .setMessage(mAddingProfile
200                             ? R.string.vpn_confirm_add_profile_cancellation
201                             : R.string.vpn_confirm_edit_profile_cancellation)
202                     .setPositiveButton(R.string.vpn_yes_button,
203                             new DialogInterface.OnClickListener() {
204                                 public void onClick(DialogInterface dialog, int w) {
205                                     finishFragment();
206                                 }
207                             })
208                     .setNegativeButton(R.string.vpn_mistake_button, null)
209                     .create();
210         }
211
212         return super.onCreateDialog(id);
213     }
214
215     /*
216     @Override
217     public void onPrepareDialog(int id, Dialog dialog) {
218         super.onPrepareDialog(id, dialog);
219
220         if (id == CONFIRM_DIALOG_ID) {
221             ((AlertDialog)dialog).setMessage(mAddingProfile
222                     ? getString(R.string.vpn_confirm_add_profile_cancellation)
223                     : getString(R.string.vpn_confirm_edit_profile_cancellation));
224         }
225     }*/
226
227     private VpnProfile getProfile() {
228         return mProfileEditor.getProfile();
229     }
230
231     private boolean profileChanged() {
232         Parcel newParcel = Parcel.obtain();
233         getProfile().writeToParcel(newParcel, 0);
234         byte[] newData = newParcel.marshall();
235         if (mOriginalProfileData.length == newData.length) {
236             for (int i = 0, n = mOriginalProfileData.length; i < n; i++) {
237                 if (mOriginalProfileData[i] != newData[i]) return true;
238             }
239             return false;
240         }
241         return true;
242     }
243 }