blob: d8aa528226e9ef55d4cce1fdb0c7100e0f74be53 [file] [log] [blame]
Alexandre Lision3cefec22013-11-14 17:26:35 -05001/*
2 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
Alexandre Lision3cefec22013-11-14 17:26:35 -05005 *
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 */
31
32package org.sflphone.account;
33
34import java.util.ArrayList;
35import java.util.HashMap;
36
Alexandre Lision3cefec22013-11-14 17:26:35 -050037import org.sflphone.model.Account;
38import org.sflphone.views.CredentialsPreference;
39
40import android.content.Context;
41import android.os.Bundle;
42import android.preference.Preference;
43import android.preference.Preference.OnPreferenceChangeListener;
44import android.preference.PreferenceScreen;
45
46public class CredentialsManager {
47
48 PreferenceScreen mScreen;
49 public static final String CURRENT_CRED = "current_cred";
50 public static final String NEW_CRED = "new_cred";
51 private Context mContext;
52 private Account mAccount;
53
54
55
56 public void onCreate(Context cont, PreferenceScreen preferenceScreen, Account acc) {
57 mContext = cont;
58 mScreen = preferenceScreen;
59 mAccount = acc;
60 }
61
62 public void reloadCredentials() {
63 removeAllCredentials();
64 addAllCredentials();
65 }
66
67 public void setAddCredentialListener() {
68 mScreen.findPreference("Add.credentials").setOnPreferenceChangeListener(addCredentialListener);
69 }
70
71 public void setEditCredentialListener() {
72 mScreen.findPreference("Add.credentials").setOnPreferenceChangeListener(addCredentialListener);
73 }
74
75 private void addAllCredentials() {
76 ArrayList<AccountCredentials> credentials = mAccount.getCredentials();
77 for (AccountCredentials cred : credentials) {
78 CredentialsPreference toAdd = new CredentialsPreference(mContext, null);
79 toAdd.setKey("credential");
80 toAdd.setTitle(cred.getDetailString(AccountCredentials.CONFIG_ACCOUNT_USERNAME));
81 toAdd.setSummary(cred.getDetailString(AccountCredentials.CONFIG_ACCOUNT_REALM));
82 toAdd.getExtras().putSerializable(CURRENT_CRED, cred.getDetailsHashMap());
83 toAdd.setOnPreferenceChangeListener(editCredentialListener);
84 toAdd.setIcon(null);
85 mScreen.addPreference(toAdd);
86 }
87
88 }
89
90 private void removeAllCredentials() {
91 Preference toRemove = mScreen.findPreference("credential");
92 while (mScreen.findPreference("credential") != null) {
93 mScreen.removePreference(toRemove);
94 toRemove = mScreen.findPreference("credential");
95 }
96 }
97
98 private OnPreferenceChangeListener editCredentialListener = new OnPreferenceChangeListener() {
99
100 @Override
101 public boolean onPreferenceChange(Preference preference, Object newValue) {
102
103 // We need the old and new value to correctly edit the list of credentials
104 Bundle result = (Bundle) newValue;
105 mAccount.removeCredential(new AccountCredentials((HashMap<String, String>) result.get(CURRENT_CRED)));
106
107 if(result.get(NEW_CRED) != null){
108 // There is a new value for this credentials it means it has been edited (otherwise deleted)
109 mAccount.addCredential(new AccountCredentials((HashMap<String, String>) result.get(NEW_CRED)));
110 }
111 mAccount.notifyObservers();
112 reloadCredentials();
113 return false;
114 }
115 };
116
117 private OnPreferenceChangeListener addCredentialListener = new OnPreferenceChangeListener() {
118
119 @Override
120 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Lisionafff1cc2013-11-22 11:57:33 -0500121 mAccount.addCredential((AccountCredentials) newValue);
Alexandre Lision3cefec22013-11-14 17:26:35 -0500122 mAccount.notifyObservers();
123 reloadCredentials();
124 return false;
125 }
126 };
127
128
129
130}