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