blob: afb5c562a3f77a30ecfd082bd413cc0837eb9308 [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 * If you own a pjsip commercial license you can also redistribute it
11 * and/or modify it under the terms of the GNU Lesser General Public License
12 * as an android library.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
Alexandre Lision3b7148e2013-11-13 17:23:06 -050022package org.sflphone.account;
23
24import java.util.ArrayList;
25import java.util.HashMap;
26
Alexandre Lision3b7148e2013-11-13 17:23:06 -050027import android.util.Log;
28
29public class AccountCredentials implements AccountDetail {
30
31 private static final String TAG = AccountCredentials.class.getSimpleName();
32
33 public static final String CONFIG_ACCOUNT_USERNAME = "Account.username";
34 public static final String CONFIG_ACCOUNT_PASSWORD = "Account.password";
35 public static final String CONFIG_ACCOUNT_REALM = "Account.realm";
Alexandre Lision3b7148e2013-11-13 17:23:06 -050036
37 private ArrayList<AccountDetail.PreferenceEntry> privateArray;
38
39 public static ArrayList<AccountDetail.PreferenceEntry> getPreferenceEntries() {
40 ArrayList<AccountDetail.PreferenceEntry> preference = new ArrayList<AccountDetail.PreferenceEntry>();
41
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -050042 preference.add(new PreferenceEntry(CONFIG_ACCOUNT_USERNAME));
43 preference.add(new PreferenceEntry(CONFIG_ACCOUNT_PASSWORD));
44 preference.add(new PreferenceEntry(CONFIG_ACCOUNT_REALM));
Alexandre Lision3b7148e2013-11-13 17:23:06 -050045
46 return preference;
47 }
48
49 public AccountCredentials() {
50 privateArray = getPreferenceEntries();
51 }
52
53 public AccountCredentials(HashMap<String, String> pref) {
54 privateArray = getPreferenceEntries();
55
56 for (AccountDetail.PreferenceEntry p : privateArray) {
57 p.mValue = pref.get(p.mKey);
58 }
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -050059
Alexandre Lision3b7148e2013-11-13 17:23:06 -050060 }
61
62 public AccountCredentials(ArrayList<String> pref) {
63 privateArray = getPreferenceEntries();
64
65 if (pref.size() != privateArray.size()) {
66 Log.i(TAG, "Error list are not of equal size");
67 } else {
68 int index = 0;
69 for (String s : pref) {
70 privateArray.get(index).mValue = s;
71 index++;
72 }
73 }
74 }
75
76 public ArrayList<AccountDetail.PreferenceEntry> getDetailValues() {
77 return privateArray;
78 }
79
80 public ArrayList<String> getValuesOnly() {
81 ArrayList<String> valueList = new ArrayList<String>();
82
83 for (AccountDetail.PreferenceEntry p : privateArray) {
Alexandre Lision3b7148e2013-11-13 17:23:06 -050084 valueList.add(p.mValue);
85 }
86
87 return valueList;
88 }
89
90 public HashMap<String, String> getDetailsHashMap() {
91 HashMap<String, String> map = new HashMap<String, String>();
92
93 for (AccountDetail.PreferenceEntry p : privateArray) {
94 map.put(p.mKey, p.mValue);
95 }
96
97 return map;
98 }
99
100 public String getDetailString(String key) {
101 String value = "";
102
103 for (AccountDetail.PreferenceEntry p : privateArray) {
104 if (p.mKey.equals(key)) {
105 value = p.mValue;
106 return value;
107 }
108 }
109 return value;
110 }
111
112 public void setDetailString(String key, String newValue) {
113 for (int i = 0; i < privateArray.size(); ++i) {
114 PreferenceEntry p = privateArray.get(i);
115 if (p.mKey.equals(key)) {
116 privateArray.get(i).mValue = newValue;
117 }
118 }
119
120 }
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -0500121
Alexandre Lision3cefec22013-11-14 17:26:35 -0500122 @Override
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -0500123 public boolean equals(Object other) {
124 if (other instanceof AccountCredentials)
125 return ((AccountCredentials) other).getDetailsHashMap().get(CONFIG_ACCOUNT_USERNAME)
126 .contentEquals(getDetailString(CONFIG_ACCOUNT_USERNAME))
127 && ((AccountCredentials) other).getDetailsHashMap().get(CONFIG_ACCOUNT_PASSWORD)
128 .contentEquals(getDetailString(CONFIG_ACCOUNT_PASSWORD))
129 && ((AccountCredentials) other).getDetailsHashMap().get(CONFIG_ACCOUNT_REALM)
130 .contentEquals(getDetailString(CONFIG_ACCOUNT_REALM));
131
Alexandre Lision3cefec22013-11-14 17:26:35 -0500132 return false;
133 }
Alexandre Lision3b7148e2013-11-13 17:23:06 -0500134
135}