blob: cc722b893d4b82e5b6a678cc8fe5ba4229f054e6 [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 */
31
Alexandre Lision064e1e02013-10-01 16:18:42 -040032package org.sflphone.adapters;
alisiona4325152013-04-19 11:10:03 -040033
Alexandre Lision8a1bab82013-10-24 10:06:51 -040034import java.io.File;
alisiona4325152013-04-19 11:10:03 -040035import java.util.ArrayList;
alisiona4325152013-04-19 11:10:03 -040036
Alexandre Lisionf30ff852013-12-09 17:08:40 -050037import org.sflphone.R;
Alexandre Lision064e1e02013-10-01 16:18:42 -040038import org.sflphone.model.Account;
39
alisiona4325152013-04-19 11:10:03 -040040import android.content.Context;
alision58356b72013-06-03 17:13:36 -040041import android.content.Intent;
42import android.util.Log;
alisiona4325152013-04-19 11:10:03 -040043import android.view.LayoutInflater;
44import android.view.View;
45import android.view.ViewGroup;
46import android.widget.BaseAdapter;
Alexandre Lisiondd6623b2013-12-02 16:23:40 -050047import android.widget.ImageView;
alisiona4325152013-04-19 11:10:03 -040048import android.widget.TextView;
49
alisiona4325152013-04-19 11:10:03 -040050public class AccountSelectionAdapter extends BaseAdapter {
51
52 private static final String TAG = AccountSelectionAdapter.class.getSimpleName();
53
alision11e8e162013-05-28 10:33:14 -040054 ArrayList<Account> accounts;
alisiona4325152013-04-19 11:10:03 -040055 Context mContext;
alisiondf1dac92013-06-27 17:35:53 -040056 int selectedAccount = -1;
Alexandre Lision8a1bab82013-10-24 10:06:51 -040057 static final String DEFAULT_ACCOUNT_ID = "IP2IP";
alisiona4325152013-04-19 11:10:03 -040058
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040059 public AccountSelectionAdapter(Context cont, ArrayList<Account> newList) {
alisiona4325152013-04-19 11:10:03 -040060 super();
alision11e8e162013-05-28 10:33:14 -040061 accounts = newList;
alisiona4325152013-04-19 11:10:03 -040062 mContext = cont;
alisiona4325152013-04-19 11:10:03 -040063 }
64
65 @Override
66 public int getCount() {
alision11e8e162013-05-28 10:33:14 -040067 return accounts.size();
alisiona4325152013-04-19 11:10:03 -040068 }
69
70 @Override
alision11e8e162013-05-28 10:33:14 -040071 public Account getItem(int pos) {
72 return accounts.get(pos);
alisiona4325152013-04-19 11:10:03 -040073 }
74
75 @Override
76 public long getItemId(int pos) {
77 return 0;
78 }
79
80 @Override
81 public View getView(int pos, View convertView, ViewGroup parent) {
82 View rowView = convertView;
83 AccountView entryView = null;
84
85 if (rowView == null) {
86 LayoutInflater inflater = LayoutInflater.from(mContext);
87 rowView = inflater.inflate(R.layout.item_account, null);
88
89 entryView = new AccountView();
90 entryView.alias = (TextView) rowView.findViewById(R.id.account_alias);
91 entryView.host = (TextView) rowView.findViewById(R.id.account_host);
Alexandre Lisiondd6623b2013-12-02 16:23:40 -050092 entryView.select = (ImageView) rowView.findViewById(R.id.account_selected);
alisiona4325152013-04-19 11:10:03 -040093 rowView.setTag(entryView);
94 } else {
95 entryView = (AccountView) rowView.getTag();
96 }
97
alision11e8e162013-05-28 10:33:14 -040098 entryView.alias.setText(accounts.get(pos).getAlias());
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -050099
alision11e8e162013-05-28 10:33:14 -0400100 entryView.host.setText(accounts.get(pos).getHost() + " - " + accounts.get(pos).getRegistered_state());
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -0500101 // accManager.displayAccountDetails(accounts.get(pos), entryView);
alision11e8e162013-05-28 10:33:14 -0400102 if (pos == selectedAccount) {
Alexandre Lisiondd6623b2013-12-02 16:23:40 -0500103 entryView.select.setVisibility(View.VISIBLE);
104 } else {
105 entryView.select.setVisibility(View.GONE);
alisiona4325152013-04-19 11:10:03 -0400106 }
107
108 return rowView;
109 }
110
Alexandre Lisione796a872014-01-17 17:08:13 -0500111 public Account getAccount(String accountID) {
112 for(Account acc : accounts) {
113 if(acc.getAccountID().contentEquals(accountID))
114 return acc;
115 }
116 return null;
117 }
118
alisiona4325152013-04-19 11:10:03 -0400119 /*********************
120 * ViewHolder Pattern
121 *********************/
122 public class AccountView {
123 public TextView alias;
124 public TextView host;
Alexandre Lisiondd6623b2013-12-02 16:23:40 -0500125 public ImageView select;
alisiona4325152013-04-19 11:10:03 -0400126 }
127
alisiona4325152013-04-19 11:10:03 -0400128 public void setSelectedAccount(int pos) {
alision11e8e162013-05-28 10:33:14 -0400129 selectedAccount = pos;
alisiona4325152013-04-19 11:10:03 -0400130 }
131
alision11e8e162013-05-28 10:33:14 -0400132 public Account getSelectedAccount() {
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400133 if (selectedAccount == -1) {
alisiondf1dac92013-06-27 17:35:53 -0400134 return null;
135 }
alision11e8e162013-05-28 10:33:14 -0400136 return accounts.get(selectedAccount);
137 }
138
139 public void removeAll() {
140 accounts.clear();
141 notifyDataSetChanged();
142
143 }
144
145 public void addAll(ArrayList<Account> results) {
146 accounts.addAll(results);
147 notifyDataSetChanged();
alision371b77e2013-04-23 14:51:26 -0400148 }
149
alision58356b72013-06-03 17:13:36 -0400150 /**
151 * Modify state of specific account
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400152 *
alision58356b72013-06-03 17:13:36 -0400153 * @param accountState
154 */
155 public void updateAccount(Intent accountState) {
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400156 Log.i(TAG, "updateAccount");
alision58356b72013-06-03 17:13:36 -0400157 String id = accountState.getStringExtra("Account");
158 String newState = accountState.getStringExtra("state");
159 accountState.getStringExtra("Account");
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400160
161 for (Account a : accounts) {
162 if (a.getAccountID().contentEquals(id)) {
alision58356b72013-06-03 17:13:36 -0400163 a.setRegistered_state(newState);
164 notifyDataSetChanged();
165 return;
166 }
167 }
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400168
169 }
170
171 public String getAccountOrder() {
172 String result = DEFAULT_ACCOUNT_ID + File.separator;
173 String selectedID = accounts.get(selectedAccount).getAccountID();
174 result += selectedID + File.separator;
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -0500175
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400176 for (Account a : accounts) {
177 if (a.getAccountID().contentEquals(selectedID)) {
178 continue;
179 }
180 result += a.getAccountID() + File.separator;
181 }
Alexandre Lisionab0cc9f2013-12-12 11:27:25 -0500182
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400183 return result;
alision58356b72013-06-03 17:13:36 -0400184 }
185
alisiona4325152013-04-19 11:10:03 -0400186}