Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 3 | * |
| 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | * Additional permission under GNU GPL version 3 section 7: |
| 21 | * |
| 22 | * If you modify this program, or any covered work, by linking or |
| 23 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 24 | * modified version of that library), containing parts covered by the |
| 25 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 26 | * grants you additional permission to convey the resulting work. |
| 27 | * Corresponding Source for a non-source form of such a combination |
| 28 | * shall include the source code for the parts of OpenSSL used as well |
| 29 | * as that of the covered work. |
| 30 | */ |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 31 | package org.sflphone.fragments; |
| 32 | |
| 33 | import java.net.NetworkInterface; |
| 34 | import java.net.SocketException; |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.Enumeration; |
| 37 | |
| 38 | import org.sflphone.R; |
| 39 | import org.sflphone.account.AccountDetail; |
| 40 | import org.sflphone.account.AccountDetailAdvanced; |
| 41 | import org.sflphone.model.Account; |
| 42 | |
| 43 | import android.app.Activity; |
| 44 | import android.os.Bundle; |
| 45 | import android.preference.CheckBoxPreference; |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 46 | import android.preference.ListPreference; |
| 47 | import android.preference.Preference; |
| 48 | import android.preference.Preference.OnPreferenceChangeListener; |
| 49 | import android.preference.PreferenceFragment; |
| 50 | import android.util.Log; |
| 51 | |
| 52 | public class AdvancedAccountFragment extends PreferenceFragment { |
| 53 | |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 54 | private static final String TAG = AdvancedAccountFragment.class.getSimpleName(); |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 55 | |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 56 | private Callbacks mCallbacks = sDummyCallbacks; |
| 57 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 58 | |
| 59 | @Override |
| 60 | public Account getAccount() { |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | }; |
| 65 | |
| 66 | public interface Callbacks { |
| 67 | |
| 68 | public Account getAccount(); |
| 69 | |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void onAttach(Activity activity) { |
| 74 | super.onAttach(activity); |
| 75 | if (!(activity instanceof Callbacks)) { |
| 76 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 77 | } |
| 78 | |
| 79 | mCallbacks = (Callbacks) activity; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onDetach() { |
| 84 | super.onDetach(); |
| 85 | mCallbacks = sDummyCallbacks; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void onCreate(Bundle savedInstanceState) { |
| 90 | super.onCreate(savedInstanceState); |
| 91 | |
| 92 | // Load the preferences from an XML resource |
| 93 | addPreferencesFromResource(R.xml.account_advanced_prefs); |
| 94 | setPreferenceDetails(mCallbacks.getAccount().getAdvancedDetails()); |
| 95 | addPreferenceListener(mCallbacks.getAccount().getAdvancedDetails(), changeAdvancedPreferenceListener); |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 96 | |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | private void setPreferenceDetails(AccountDetail details) { |
| 100 | for (AccountDetail.PreferenceEntry p : details.getDetailValues()) { |
| 101 | Log.i(TAG, "setPreferenceDetails: pref " + p.mKey + " value " + p.mValue); |
| 102 | Preference pref = findPreference(p.mKey); |
| 103 | if (pref != null) { |
| 104 | if (p.mKey == AccountDetailAdvanced.CONFIG_LOCAL_INTERFACE) { |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 105 | ArrayList<CharSequence> entries = getNetworkInterfaces(); |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 106 | CharSequence[] display = new CharSequence[entries.size()]; |
| 107 | entries.toArray(display); |
| 108 | ((ListPreference) pref).setEntries(display); |
| 109 | ((ListPreference) pref).setEntryValues(display); |
| 110 | pref.setSummary(p.mValue); |
| 111 | continue; |
| 112 | } |
| 113 | if (!p.isTwoState) { |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 114 | pref.setSummary(p.mValue); |
Alexandre Lision | 3cefec2 | 2013-11-14 17:26:35 -0500 | [diff] [blame] | 115 | } else if (pref.getKey().contentEquals("STUN.enable")) { |
| 116 | ((CheckBoxPreference) pref).setChecked(p.mValue.contentEquals("true")); |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 117 | findPreference("STUN.server").setEnabled(p.mValue.contentEquals("true")); |
Alexandre Lision | 3cefec2 | 2013-11-14 17:26:35 -0500 | [diff] [blame] | 118 | } else if (pref.getKey().contentEquals("Account.publishedSameAsLocal")) { |
| 119 | ((CheckBoxPreference) pref).setChecked(p.mValue.contentEquals("true")); |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 120 | findPreference("Account.publishedPort").setEnabled(!p.mValue.contentEquals("true")); |
| 121 | findPreference("Account.publishedAddress").setEnabled(!p.mValue.contentEquals("true")); |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 122 | } |
| 123 | } else { |
| 124 | Log.w(TAG, "pref not found"); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 129 | private ArrayList<CharSequence> getNetworkInterfaces() { |
| 130 | ArrayList<CharSequence> result = new ArrayList<CharSequence>(); |
Alexandre Lision | 3cefec2 | 2013-11-14 17:26:35 -0500 | [diff] [blame] | 131 | |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 132 | result.add("default"); |
| 133 | try { |
| 134 | |
| 135 | for (Enumeration<NetworkInterface> list = NetworkInterface.getNetworkInterfaces(); list.hasMoreElements();) { |
| 136 | NetworkInterface i = list.nextElement(); |
| 137 | if (i.isUp()) |
| 138 | result.add(i.getDisplayName()); |
| 139 | } |
| 140 | } catch (SocketException e) { |
| 141 | Log.e(TAG, e.toString()); |
| 142 | } |
| 143 | return result; |
| 144 | } |
| 145 | |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 146 | private void addPreferenceListener(AccountDetail details, OnPreferenceChangeListener listener) { |
| 147 | for (AccountDetail.PreferenceEntry p : details.getDetailValues()) { |
| 148 | Log.i(TAG, "addPreferenceListener: pref " + p.mKey + p.mValue); |
| 149 | Preference pref = findPreference(p.mKey); |
| 150 | if (pref != null) { |
| 151 | |
| 152 | pref.setOnPreferenceChangeListener(listener); |
| 153 | |
| 154 | } else { |
| 155 | Log.w(TAG, "addPreferenceListener: pref not found"); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | Preference.OnPreferenceChangeListener changeAdvancedPreferenceListener = new Preference.OnPreferenceChangeListener() { |
| 161 | @Override |
| 162 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
Alexandre Lision | 3cefec2 | 2013-11-14 17:26:35 -0500 | [diff] [blame] | 163 | |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 164 | if (preference instanceof CheckBoxPreference) { |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 165 | mCallbacks.getAccount().getAdvancedDetails().setDetailString(preference.getKey(), ((Boolean) newValue).toString()); |
| 166 | if (preference.getKey().contentEquals("STUN.enable")) { |
| 167 | findPreference("STUN.server").setEnabled((Boolean) newValue); |
| 168 | } else if (preference.getKey().contentEquals("Account.publishedSameAsLocal")) { |
| 169 | findPreference("Account.publishedPort").setEnabled(!(Boolean) newValue); |
| 170 | findPreference("Account.publishedAddress").setEnabled(!(Boolean) newValue); |
| 171 | } |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 172 | } else { |
| 173 | preference.setSummary((CharSequence) newValue); |
Alexandre Lision | a365099 | 2013-11-13 14:19:35 -0500 | [diff] [blame] | 174 | Log.i(TAG, "Changing" + preference.getKey() + " value:" + newValue); |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 175 | mCallbacks.getAccount().getAdvancedDetails().setDetailString(preference.getKey(), ((CharSequence) newValue).toString()); |
| 176 | } |
Alexandre Lision | 68ee0ac | 2013-12-06 15:06:19 -0500 | [diff] [blame] | 177 | |
| 178 | mCallbacks.getAccount().notifyObservers(); |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | }; |
| 182 | |
Alexandre Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 183 | } |