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 | */ |
| 31 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 32 | package org.sflphone.adapters; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 33 | |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 34 | import java.io.File; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 35 | import java.util.ArrayList; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 36 | |
Alexandre Lision | f30ff85 | 2013-12-09 17:08:40 -0500 | [diff] [blame] | 37 | import org.sflphone.R; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 38 | import org.sflphone.model.Account; |
| 39 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 40 | import android.content.Context; |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 41 | import android.content.Intent; |
| 42 | import android.util.Log; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 43 | import android.view.LayoutInflater; |
| 44 | import android.view.View; |
| 45 | import android.view.ViewGroup; |
| 46 | import android.widget.BaseAdapter; |
Alexandre Lision | dd6623b | 2013-12-02 16:23:40 -0500 | [diff] [blame] | 47 | import android.widget.ImageView; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 48 | import android.widget.TextView; |
| 49 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 50 | public class AccountSelectionAdapter extends BaseAdapter { |
| 51 | |
| 52 | private static final String TAG = AccountSelectionAdapter.class.getSimpleName(); |
| 53 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 54 | ArrayList<Account> accounts; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 55 | Context mContext; |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 56 | int selectedAccount = -1; |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 57 | static final String DEFAULT_ACCOUNT_ID = "IP2IP"; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 58 | |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 59 | public AccountSelectionAdapter(Context cont, ArrayList<Account> newList) { |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 60 | super(); |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 61 | accounts = newList; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 62 | mContext = cont; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public int getCount() { |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 67 | return accounts.size(); |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 71 | public Account getItem(int pos) { |
| 72 | return accounts.get(pos); |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 73 | } |
| 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 Lision | dd6623b | 2013-12-02 16:23:40 -0500 | [diff] [blame] | 92 | entryView.select = (ImageView) rowView.findViewById(R.id.account_selected); |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 93 | rowView.setTag(entryView); |
| 94 | } else { |
| 95 | entryView = (AccountView) rowView.getTag(); |
| 96 | } |
| 97 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 98 | entryView.alias.setText(accounts.get(pos).getAlias()); |
Alexandre Lision | ab0cc9f | 2013-12-12 11:27:25 -0500 | [diff] [blame] | 99 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 100 | entryView.host.setText(accounts.get(pos).getHost() + " - " + accounts.get(pos).getRegistered_state()); |
Alexandre Lision | ab0cc9f | 2013-12-12 11:27:25 -0500 | [diff] [blame] | 101 | // accManager.displayAccountDetails(accounts.get(pos), entryView); |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 102 | if (pos == selectedAccount) { |
Alexandre Lision | dd6623b | 2013-12-02 16:23:40 -0500 | [diff] [blame] | 103 | entryView.select.setVisibility(View.VISIBLE); |
| 104 | } else { |
| 105 | entryView.select.setVisibility(View.GONE); |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | return rowView; |
| 109 | } |
| 110 | |
Alexandre Lision | e796a87 | 2014-01-17 17:08:13 -0500 | [diff] [blame] | 111 | 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 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 119 | /********************* |
| 120 | * ViewHolder Pattern |
| 121 | *********************/ |
| 122 | public class AccountView { |
| 123 | public TextView alias; |
| 124 | public TextView host; |
Alexandre Lision | dd6623b | 2013-12-02 16:23:40 -0500 | [diff] [blame] | 125 | public ImageView select; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 126 | } |
| 127 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 128 | public void setSelectedAccount(int pos) { |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 129 | selectedAccount = pos; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 130 | } |
| 131 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 132 | public Account getSelectedAccount() { |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 133 | if (selectedAccount == -1) { |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 134 | return null; |
| 135 | } |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 136 | 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(); |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 148 | } |
| 149 | |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 150 | /** |
| 151 | * Modify state of specific account |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 152 | */ |
Alexandre Lision | 48b49eb | 2014-02-11 13:37:33 -0500 | [diff] [blame] | 153 | public void updateAccount(String accoundID, String state, int code) { |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 154 | Log.i(TAG, "updateAccount"); |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 155 | |
| 156 | for (Account a : accounts) { |
Alexandre Lision | 48b49eb | 2014-02-11 13:37:33 -0500 | [diff] [blame] | 157 | if (a.getAccountID().contentEquals(accoundID)) { |
| 158 | a.setRegistered_state(state); |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 159 | notifyDataSetChanged(); |
| 160 | return; |
| 161 | } |
| 162 | } |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | public String getAccountOrder() { |
| 166 | String result = DEFAULT_ACCOUNT_ID + File.separator; |
| 167 | String selectedID = accounts.get(selectedAccount).getAccountID(); |
| 168 | result += selectedID + File.separator; |
Alexandre Lision | ab0cc9f | 2013-12-12 11:27:25 -0500 | [diff] [blame] | 169 | |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 170 | for (Account a : accounts) { |
| 171 | if (a.getAccountID().contentEquals(selectedID)) { |
| 172 | continue; |
| 173 | } |
| 174 | result += a.getAccountID() + File.separator; |
| 175 | } |
Alexandre Lision | ab0cc9f | 2013-12-12 11:27:25 -0500 | [diff] [blame] | 176 | |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 177 | return result; |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 178 | } |
| 179 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 180 | } |