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 | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 31 | package org.sflphone.fragments; |
| 32 | |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 33 | import org.sflphone.R; |
| 34 | import org.sflphone.account.AccountDetail; |
Alexandre Lision | 338e7cf | 2014-01-13 11:55:29 -0500 | [diff] [blame] | 35 | import org.sflphone.account.AccountDetailBasic; |
Alexandre Lision | 059da9d | 2013-10-22 11:17:25 -0400 | [diff] [blame] | 36 | import org.sflphone.model.Account; |
Alexandre Lision | 59daaaf | 2013-12-10 10:41:46 -0500 | [diff] [blame] | 37 | import org.sflphone.views.PasswordPreference; |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 38 | |
| 39 | import android.app.Activity; |
| 40 | import android.os.Bundle; |
| 41 | import android.preference.CheckBoxPreference; |
| 42 | import android.preference.EditTextPreference; |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 43 | import android.preference.Preference; |
| 44 | import android.preference.Preference.OnPreferenceChangeListener; |
| 45 | import android.preference.PreferenceFragment; |
| 46 | import android.util.Log; |
| 47 | |
Alexandre Lision | 8907c1c | 2013-11-12 11:40:14 -0500 | [diff] [blame] | 48 | public class GeneralAccountFragment extends PreferenceFragment { |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 49 | |
Alexandre Lision | 8907c1c | 2013-11-12 11:40:14 -0500 | [diff] [blame] | 50 | private static final String TAG = GeneralAccountFragment.class.getSimpleName(); |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 51 | private Callbacks mCallbacks = sDummyCallbacks; |
| 52 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 53 | |
| 54 | @Override |
Alexandre Lision | 059da9d | 2013-10-22 11:17:25 -0400 | [diff] [blame] | 55 | public Account getAccount() { |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 56 | return null; |
| 57 | } |
| 58 | |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | public interface Callbacks { |
| 62 | |
Alexandre Lision | 059da9d | 2013-10-22 11:17:25 -0400 | [diff] [blame] | 63 | public Account getAccount(); |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 64 | |
| 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 Lision | 059da9d | 2013-10-22 11:17:25 -0400 | [diff] [blame] | 76 | |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 77 | @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 Lision | c48c36a | 2013-11-12 16:26:10 -0500 | [diff] [blame] | 88 | addPreferencesFromResource(R.xml.account_general_prefs); |
Alexandre Lision | 059da9d | 2013-10-22 11:17:25 -0400 | [diff] [blame] | 89 | setPreferenceDetails(mCallbacks.getAccount().getBasicDetails()); |
Alexandre Lision | 059da9d | 2013-10-22 11:17:25 -0400 | [diff] [blame] | 90 | addPreferenceListener(mCallbacks.getAccount().getBasicDetails(), changeBasicPreferenceListener); |
Alexandre Lision | 451f2a8 | 2013-11-12 12:55:55 -0500 | [diff] [blame] | 91 | |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 92 | } |
| 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 Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 99 | if (!p.isTwoState) { |
| 100 | ((EditTextPreference) pref).setText(p.mValue); |
Alexandre Lision | 59daaaf | 2013-12-10 10:41:46 -0500 | [diff] [blame] | 101 | if (pref instanceof PasswordPreference) { |
| 102 | String tmp = new String(); |
| 103 | for (int i = 0; i < p.mValue.length(); ++i) { |
| 104 | tmp += "*"; |
Alexandre Lision | 48cf8a7 | 2013-12-11 15:00:43 -0500 | [diff] [blame] | 105 | |
Alexandre Lision | 59daaaf | 2013-12-10 10:41:46 -0500 | [diff] [blame] | 106 | } |
| 107 | pref.setSummary(tmp); |
| 108 | } else { |
| 109 | pref.setSummary(p.mValue); |
| 110 | } |
Alexandre Lision | 451f2a8 | 2013-11-12 12:55:55 -0500 | [diff] [blame] | 111 | } else { |
| 112 | ((CheckBoxPreference) pref).setChecked(p.isChecked()); |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 113 | } |
| 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 Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 125 | pref.setOnPreferenceChangeListener(listener); |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 126 | } 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 Lision | 48cf8a7 | 2013-12-11 15:00:43 -0500 | [diff] [blame] | 135 | |
| 136 | Log.i(TAG, "Changing preference value:" + newValue); |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 137 | if (preference instanceof CheckBoxPreference) { |
Alexandre Lision | 338e7cf | 2014-01-13 11:55:29 -0500 | [diff] [blame] | 138 | mCallbacks.getAccount().getBasicDetails().setDetailString(preference.getKey(), newValue.toString()); |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 139 | } else { |
Alexandre Lision | 48cf8a7 | 2013-12-11 15:00:43 -0500 | [diff] [blame] | 140 | if (preference instanceof PasswordPreference) { |
| 141 | String tmp = new String(); |
| 142 | for (int i = 0; i < ((String) newValue).length(); ++i) { |
| 143 | tmp += "*"; |
Alexandre Lision | 48cf8a7 | 2013-12-11 15:00:43 -0500 | [diff] [blame] | 144 | } |
Alexandre Lision | 338e7cf | 2014-01-13 11:55:29 -0500 | [diff] [blame] | 145 | mCallbacks.getAccount().getCredentials().get(0).setDetailString(preference.getKey(), newValue.toString()); |
Alexandre Lision | 48cf8a7 | 2013-12-11 15:00:43 -0500 | [diff] [blame] | 146 | preference.setSummary(tmp); |
Alexandre Lision | 338e7cf | 2014-01-13 11:55:29 -0500 | [diff] [blame] | 147 | } else if(preference.getKey().contentEquals(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME)) { |
| 148 | mCallbacks.getAccount().getCredentials().get(0).setDetailString(preference.getKey(), newValue.toString()); |
Alexandre Lision | 48cf8a7 | 2013-12-11 15:00:43 -0500 | [diff] [blame] | 149 | preference.setSummary((CharSequence) newValue); |
Alexandre Lision | 338e7cf | 2014-01-13 11:55:29 -0500 | [diff] [blame] | 150 | } else { |
| 151 | preference.setSummary((CharSequence) newValue); |
| 152 | } |
Alexandre Lision | 48cf8a7 | 2013-12-11 15:00:43 -0500 | [diff] [blame] | 153 | |
Alexandre Lision | 338e7cf | 2014-01-13 11:55:29 -0500 | [diff] [blame] | 154 | mCallbacks.getAccount().getBasicDetails().setDetailString(preference.getKey(), newValue.toString()); |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 155 | } |
Alexandre Lision | 48cf8a7 | 2013-12-11 15:00:43 -0500 | [diff] [blame] | 156 | mCallbacks.getAccount().notifyObservers(); |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | }; |
| 160 | |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 161 | } |