blob: 7d3a2c7327be26957535461693cb2c9b30aafcaf [file] [log] [blame]
Alexandre Lisiona8b78722013-12-13 10:18:33 -05001/*
2 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
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 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 Lision059da9d2013-10-22 11:17:25 -040035import org.sflphone.model.Account;
Alexandre Lision59daaaf2013-12-10 10:41:46 -050036import org.sflphone.views.PasswordPreference;
Alexandre Lisionafd40e42013-10-15 13:48:37 -040037
38import android.app.Activity;
39import android.os.Bundle;
40import android.preference.CheckBoxPreference;
41import android.preference.EditTextPreference;
Alexandre Lisionafd40e42013-10-15 13:48:37 -040042import android.preference.Preference;
43import android.preference.Preference.OnPreferenceChangeListener;
44import android.preference.PreferenceFragment;
45import android.util.Log;
46
Alexandre Lision8907c1c2013-11-12 11:40:14 -050047public class GeneralAccountFragment extends PreferenceFragment {
Alexandre Lisionafd40e42013-10-15 13:48:37 -040048
Alexandre Lision8907c1c2013-11-12 11:40:14 -050049 private static final String TAG = GeneralAccountFragment.class.getSimpleName();
Alexandre Lisionafd40e42013-10-15 13:48:37 -040050 private Callbacks mCallbacks = sDummyCallbacks;
51 private static Callbacks sDummyCallbacks = new Callbacks() {
52
53 @Override
Alexandre Lision059da9d2013-10-22 11:17:25 -040054 public Account getAccount() {
Alexandre Lisionafd40e42013-10-15 13:48:37 -040055 return null;
56 }
57
Alexandre Lisionafd40e42013-10-15 13:48:37 -040058 };
59
60 public interface Callbacks {
61
Alexandre Lision059da9d2013-10-22 11:17:25 -040062 public Account getAccount();
Alexandre Lisionafd40e42013-10-15 13:48:37 -040063
64 }
65
66 @Override
67 public void onAttach(Activity activity) {
68 super.onAttach(activity);
69 if (!(activity instanceof Callbacks)) {
70 throw new IllegalStateException("Activity must implement fragment's callbacks.");
71 }
72
73 mCallbacks = (Callbacks) activity;
74 }
Alexandre Lision059da9d2013-10-22 11:17:25 -040075
Alexandre Lisionafd40e42013-10-15 13:48:37 -040076 @Override
77 public void onDetach() {
78 super.onDetach();
79 mCallbacks = sDummyCallbacks;
80 }
81
82 @Override
83 public void onCreate(Bundle savedInstanceState) {
84 super.onCreate(savedInstanceState);
85
86 // Load the preferences from an XML resource
Alexandre Lisionc48c36a2013-11-12 16:26:10 -050087 addPreferencesFromResource(R.xml.account_general_prefs);
Alexandre Lision059da9d2013-10-22 11:17:25 -040088 setPreferenceDetails(mCallbacks.getAccount().getBasicDetails());
Alexandre Lision059da9d2013-10-22 11:17:25 -040089 addPreferenceListener(mCallbacks.getAccount().getBasicDetails(), changeBasicPreferenceListener);
Alexandre Lision451f2a82013-11-12 12:55:55 -050090
Alexandre Lisionafd40e42013-10-15 13:48:37 -040091 }
92
93 private void setPreferenceDetails(AccountDetail details) {
94 for (AccountDetail.PreferenceEntry p : details.getDetailValues()) {
95 Log.i(TAG, "setPreferenceDetails: pref " + p.mKey + " value " + p.mValue);
96 Preference pref = findPreference(p.mKey);
97 if (pref != null) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -040098 if (!p.isTwoState) {
99 ((EditTextPreference) pref).setText(p.mValue);
Alexandre Lision59daaaf2013-12-10 10:41:46 -0500100 if (pref instanceof PasswordPreference) {
101 String tmp = new String();
102 for (int i = 0; i < p.mValue.length(); ++i) {
103 tmp += "*";
Alexandre Lision48cf8a72013-12-11 15:00:43 -0500104
Alexandre Lision59daaaf2013-12-10 10:41:46 -0500105 }
106 pref.setSummary(tmp);
107 } else {
108 pref.setSummary(p.mValue);
109 }
Alexandre Lision451f2a82013-11-12 12:55:55 -0500110 } else {
111 ((CheckBoxPreference) pref).setChecked(p.isChecked());
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400112 }
113 } else {
114 Log.w(TAG, "pref not found");
115 }
116 }
117 }
118
119 private void addPreferenceListener(AccountDetail details, OnPreferenceChangeListener listener) {
120 for (AccountDetail.PreferenceEntry p : details.getDetailValues()) {
121 Log.i(TAG, "addPreferenceListener: pref " + p.mKey + p.mValue);
122 Preference pref = findPreference(p.mKey);
123 if (pref != null) {
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400124 pref.setOnPreferenceChangeListener(listener);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400125 } else {
126 Log.w(TAG, "addPreferenceListener: pref not found");
127 }
128 }
129 }
130
131 Preference.OnPreferenceChangeListener changeBasicPreferenceListener = new Preference.OnPreferenceChangeListener() {
132 @Override
133 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Lision48cf8a72013-12-11 15:00:43 -0500134
135 Log.i(TAG, "Changing preference value:" + newValue);
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400136 if (preference instanceof CheckBoxPreference) {
Alexandre Lision451f2a82013-11-12 12:55:55 -0500137 mCallbacks.getAccount().getBasicDetails().setDetailString(preference.getKey(), ((Boolean) newValue).toString());
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400138 } else {
Alexandre Lision48cf8a72013-12-11 15:00:43 -0500139 if (preference instanceof PasswordPreference) {
140 String tmp = new String();
141 for (int i = 0; i < ((String) newValue).length(); ++i) {
142 tmp += "*";
Alexandre Lision3cefec22013-11-14 17:26:35 -0500143
Alexandre Lision48cf8a72013-12-11 15:00:43 -0500144 }
145 preference.setSummary(tmp);
146 } else
147 preference.setSummary((CharSequence) newValue);
148
149 mCallbacks.getAccount().getBasicDetails().setDetailString(preference.getKey(), ((CharSequence) newValue).toString());
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400150 }
Alexandre Lision48cf8a72013-12-11 15:00:43 -0500151 mCallbacks.getAccount().notifyObservers();
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400152 return true;
153 }
154 };
155
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400156}