blob: 4c1574b99564817335274102e1f033729eed98cf [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 Lisionafd40e42013-10-15 13:48:37 -040031package org.sflphone.fragments;
32
Alexandre Lisionafd40e42013-10-15 13:48:37 -040033import org.sflphone.R;
34import org.sflphone.account.AccountDetail;
Alexandre Lision338e7cf2014-01-13 11:55:29 -050035import org.sflphone.account.AccountDetailBasic;
Alexandre Lision059da9d2013-10-22 11:17:25 -040036import org.sflphone.model.Account;
Alexandre Lision59daaaf2013-12-10 10:41:46 -050037import org.sflphone.views.PasswordPreference;
Alexandre Lisionafd40e42013-10-15 13:48:37 -040038
39import android.app.Activity;
40import android.os.Bundle;
41import android.preference.CheckBoxPreference;
42import android.preference.EditTextPreference;
Alexandre Lisionafd40e42013-10-15 13:48:37 -040043import android.preference.Preference;
44import android.preference.Preference.OnPreferenceChangeListener;
45import android.preference.PreferenceFragment;
46import android.util.Log;
47
Alexandre Lision8907c1c2013-11-12 11:40:14 -050048public class GeneralAccountFragment extends PreferenceFragment {
Alexandre Lisionafd40e42013-10-15 13:48:37 -040049
Alexandre Lision8907c1c2013-11-12 11:40:14 -050050 private static final String TAG = GeneralAccountFragment.class.getSimpleName();
Alexandre Lisionafd40e42013-10-15 13:48:37 -040051 private Callbacks mCallbacks = sDummyCallbacks;
52 private static Callbacks sDummyCallbacks = new Callbacks() {
53
54 @Override
Alexandre Lision059da9d2013-10-22 11:17:25 -040055 public Account getAccount() {
Alexandre Lisionafd40e42013-10-15 13:48:37 -040056 return null;
57 }
58
Alexandre Lisionafd40e42013-10-15 13:48:37 -040059 };
60
61 public interface Callbacks {
62
Alexandre Lision059da9d2013-10-22 11:17:25 -040063 public Account getAccount();
Alexandre Lisionafd40e42013-10-15 13:48:37 -040064
65 }
66
67 @Override
68 public void onAttach(Activity activity) {
69 super.onAttach(activity);
70 if (!(activity instanceof Callbacks)) {
71 throw new IllegalStateException("Activity must implement fragment's callbacks.");
72 }
73
74 mCallbacks = (Callbacks) activity;
75 }
Alexandre Lision059da9d2013-10-22 11:17:25 -040076
Alexandre Lisionafd40e42013-10-15 13:48:37 -040077 @Override
78 public void onDetach() {
79 super.onDetach();
80 mCallbacks = sDummyCallbacks;
81 }
82
83 @Override
84 public void onCreate(Bundle savedInstanceState) {
85 super.onCreate(savedInstanceState);
86
87 // Load the preferences from an XML resource
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050088 addPreferencesFromResource(R.xml.account_general_prefs);
Alexandre Lision059da9d2013-10-22 11:17:25 -040089 setPreferenceDetails(mCallbacks.getAccount().getBasicDetails());
Alexandre Lision059da9d2013-10-22 11:17:25 -040090 addPreferenceListener(mCallbacks.getAccount().getBasicDetails(), changeBasicPreferenceListener);
Alexandre Lision451f2a82013-11-12 12:55:55 -050091
Alexandre Lisionafd40e42013-10-15 13:48:37 -040092 }
93
94 private void setPreferenceDetails(AccountDetail details) {
95 for (AccountDetail.PreferenceEntry p : details.getDetailValues()) {
96 Log.i(TAG, "setPreferenceDetails: pref " + p.mKey + " value " + p.mValue);
97 Preference pref = findPreference(p.mKey);
98 if (pref != null) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -040099 if (!p.isTwoState) {
100 ((EditTextPreference) pref).setText(p.mValue);
Alexandre Lision59daaaf2013-12-10 10:41:46 -0500101 if (pref instanceof PasswordPreference) {
102 String tmp = new String();
103 for (int i = 0; i < p.mValue.length(); ++i) {
104 tmp += "*";
Alexandre Lision48cf8a72013-12-11 15:00:43 -0500105
Alexandre Lision59daaaf2013-12-10 10:41:46 -0500106 }
107 pref.setSummary(tmp);
108 } else {
109 pref.setSummary(p.mValue);
110 }
Alexandre Lision451f2a82013-11-12 12:55:55 -0500111 } else {
112 ((CheckBoxPreference) pref).setChecked(p.isChecked());
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400113 }
114 } else {
115 Log.w(TAG, "pref not found");
116 }
117 }
118 }
119
120 private void addPreferenceListener(AccountDetail details, OnPreferenceChangeListener listener) {
121 for (AccountDetail.PreferenceEntry p : details.getDetailValues()) {
122 Log.i(TAG, "addPreferenceListener: pref " + p.mKey + p.mValue);
123 Preference pref = findPreference(p.mKey);
124 if (pref != null) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400125 pref.setOnPreferenceChangeListener(listener);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400126 } else {
127 Log.w(TAG, "addPreferenceListener: pref not found");
128 }
129 }
130 }
131
132 Preference.OnPreferenceChangeListener changeBasicPreferenceListener = new Preference.OnPreferenceChangeListener() {
133 @Override
134 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Lision48cf8a72013-12-11 15:00:43 -0500135
136 Log.i(TAG, "Changing preference value:" + newValue);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400137 if (preference instanceof CheckBoxPreference) {
Alexandre Lision338e7cf2014-01-13 11:55:29 -0500138 mCallbacks.getAccount().getBasicDetails().setDetailString(preference.getKey(), newValue.toString());
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400139 } else {
Alexandre Lision48cf8a72013-12-11 15:00:43 -0500140 if (preference instanceof PasswordPreference) {
141 String tmp = new String();
142 for (int i = 0; i < ((String) newValue).length(); ++i) {
143 tmp += "*";
Alexandre Lision48cf8a72013-12-11 15:00:43 -0500144 }
Alexandre Lision338e7cf2014-01-13 11:55:29 -0500145 mCallbacks.getAccount().getCredentials().get(0).setDetailString(preference.getKey(), newValue.toString());
Alexandre Lision48cf8a72013-12-11 15:00:43 -0500146 preference.setSummary(tmp);
Alexandre Lision338e7cf2014-01-13 11:55:29 -0500147 } else if(preference.getKey().contentEquals(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME)) {
148 mCallbacks.getAccount().getCredentials().get(0).setDetailString(preference.getKey(), newValue.toString());
Alexandre Lision48cf8a72013-12-11 15:00:43 -0500149 preference.setSummary((CharSequence) newValue);
Alexandre Lision338e7cf2014-01-13 11:55:29 -0500150 } else {
151 preference.setSummary((CharSequence) newValue);
152 }
Alexandre Lision48cf8a72013-12-11 15:00:43 -0500153
Alexandre Lision338e7cf2014-01-13 11:55:29 -0500154 mCallbacks.getAccount().getBasicDetails().setDetailString(preference.getKey(), newValue.toString());
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400155 }
Alexandre Lision48cf8a72013-12-11 15:00:43 -0500156 mCallbacks.getAccount().notifyObservers();
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400157 return true;
158 }
159 };
160
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400161}