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 17package com.android.settings.vpn; 18 19import com.android.settings.R; 20 21import android.content.Context; 22import android.net.vpn.L2tpProfile; 23import android.preference.CheckBoxPreference; 24import android.preference.EditTextPreference; 25import android.preference.Preference; 26import android.preference.PreferenceGroup; 27 28/** 29 * The class for editing {@link L2tpProfile}. 30 */ 31class L2tpEditor extends VpnProfileEditor { 32 private CheckBoxPreference mSecret; 33 private SecretHandler mSecretHandler; 34 35 public L2tpEditor(L2tpProfile p) { 36 super(p); 37 } 38 39 @Override 40 protected void loadExtraPreferencesTo(PreferenceGroup subpanel) { 41 Context c = subpanel.getContext(); 42 subpanel.addPreference(createSecretPreference(c)); 43 subpanel.addPreference(createSecretStringPreference(c)); 44 45 L2tpProfile profile = (L2tpProfile) getProfile(); 46 } 47 48 @Override 49 public String validate() { 50 String result = super.validate(); 51 if (!mSecret.isChecked()) return result; 52 53 return ((result != null) ? result : mSecretHandler.validate()); 54 } 55 56 private Preference createSecretPreference(Context c) { 57 final L2tpProfile profile = (L2tpProfile) getProfile(); 58 CheckBoxPreference secret = mSecret = new CheckBoxPreference(c); 59 boolean enabled = profile.isSecretEnabled(); 60 setCheckBoxTitle(secret, R.string.vpn_l2tp_secret); 61 secret.setChecked(enabled); 62 setSecretSummary(secret, enabled); 63 secret.setOnPreferenceChangeListener( 64 new Preference.OnPreferenceChangeListener() { 65 public boolean onPreferenceChange( 66 Preference pref, Object newValue) { 67 boolean enabled = (Boolean) newValue; 68 profile.setSecretEnabled(enabled); 69 mSecretHandler.getPreference().setEnabled(enabled); 70 setSecretSummary(mSecret, enabled); 71 return true; 72 } 73 }); 74 return secret; 75 } 76 77 private Preference createSecretStringPreference(Context c) { 78 SecretHandler sHandler = mSecretHandler = new SecretHandler(c, 79 R.string.vpn_l2tp_secret_string_title, 80 R.string.vpn_l2tp_secret) { 81 @Override 82 protected String getSecretFromProfile() { 83 return ((L2tpProfile) getProfile()).getSecretString(); 84 } 85 86 @Override 87 protected void saveSecretToProfile(String secret) { 88 ((L2tpProfile) getProfile()).setSecretString(secret); 89 } 90 }; 91 Preference pref = sHandler.getPreference(); 92 pref.setEnabled(mSecret.isChecked()); 93 return pref; 94 } 95 96 private void setSecretSummary(CheckBoxPreference secret, boolean enabled) { 97 Context c = secret.getContext(); 98 String formatString = c.getString(enabled 99 ? R.string.vpn_is_enabled 100 : R.string.vpn_is_disabled); 101 secret.setSummary(String.format( 102 formatString, c.getString(R.string.vpn_l2tp_secret))); 103 } 104} 105