blob: 555c4c8562233d851447c5df2434652b80eec851 [file] [log] [blame]
Alexandre Lisiona8b78722013-12-13 10:18:33 -05001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Alexandre Lisiona8b78722013-12-13 10:18:33 -05003 *
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 Lisionc48c36a2013-11-12 16:26:10 -050031package org.sflphone.fragments;
32
33import java.net.NetworkInterface;
34import java.net.SocketException;
35import java.util.ArrayList;
36import java.util.Enumeration;
37
38import org.sflphone.R;
39import org.sflphone.account.AccountDetail;
40import org.sflphone.account.AccountDetailAdvanced;
41import org.sflphone.model.Account;
42
43import android.app.Activity;
44import android.os.Bundle;
45import android.preference.CheckBoxPreference;
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050046import android.preference.ListPreference;
47import android.preference.Preference;
48import android.preference.Preference.OnPreferenceChangeListener;
49import android.preference.PreferenceFragment;
50import android.util.Log;
51
52public class AdvancedAccountFragment extends PreferenceFragment {
53
Alexandre Lisiona3650992013-11-13 14:19:35 -050054 private static final String TAG = AdvancedAccountFragment.class.getSimpleName();
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050055
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050056 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 Lisiona3650992013-11-13 14:19:35 -050096
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050097 }
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 Lisiona3650992013-11-13 14:19:35 -0500105 ArrayList<CharSequence> entries = getNetworkInterfaces();
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500106 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 Lisionc48c36a2013-11-12 16:26:10 -0500114 pref.setSummary(p.mValue);
Alexandre Lision3cefec22013-11-14 17:26:35 -0500115 } else if (pref.getKey().contentEquals("STUN.enable")) {
116 ((CheckBoxPreference) pref).setChecked(p.mValue.contentEquals("true"));
Alexandre Lisiona3650992013-11-13 14:19:35 -0500117 findPreference("STUN.server").setEnabled(p.mValue.contentEquals("true"));
Alexandre Lision3cefec22013-11-14 17:26:35 -0500118 } else if (pref.getKey().contentEquals("Account.publishedSameAsLocal")) {
119 ((CheckBoxPreference) pref).setChecked(p.mValue.contentEquals("true"));
Alexandre Lisiona3650992013-11-13 14:19:35 -0500120 findPreference("Account.publishedPort").setEnabled(!p.mValue.contentEquals("true"));
121 findPreference("Account.publishedAddress").setEnabled(!p.mValue.contentEquals("true"));
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500122 }
123 } else {
124 Log.w(TAG, "pref not found");
125 }
126 }
127 }
128
Alexandre Lisiona3650992013-11-13 14:19:35 -0500129 private ArrayList<CharSequence> getNetworkInterfaces() {
130 ArrayList<CharSequence> result = new ArrayList<CharSequence>();
Alexandre Lision3cefec22013-11-14 17:26:35 -0500131
Alexandre Lisiona3650992013-11-13 14:19:35 -0500132 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 Lisionc48c36a2013-11-12 16:26:10 -0500146 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 Lision3cefec22013-11-14 17:26:35 -0500163
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500164 if (preference instanceof CheckBoxPreference) {
Alexandre Lisiona3650992013-11-13 14:19:35 -0500165 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 Lisionc48c36a2013-11-12 16:26:10 -0500172 } else {
173 preference.setSummary((CharSequence) newValue);
Alexandre Lisiona3650992013-11-13 14:19:35 -0500174 Log.i(TAG, "Changing" + preference.getKey() + " value:" + newValue);
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500175 mCallbacks.getAccount().getAdvancedDetails().setDetailString(preference.getKey(), ((CharSequence) newValue).toString());
176 }
Alexandre Lision68ee0ac2013-12-06 15:06:19 -0500177
178 mCallbacks.getAccount().notifyObservers();
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500179 return true;
180 }
181 };
182
Alexandre Lisionc48c36a2013-11-12 16:26:10 -0500183}