Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 1 | package org.sflphone.adapters; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 2 | |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 3 | import java.io.File; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 4 | import java.util.ArrayList; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 5 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 6 | import org.sflphone.model.Account; |
| 7 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 8 | import android.content.Context; |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 9 | import android.content.Intent; |
| 10 | import android.util.Log; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 11 | import android.view.LayoutInflater; |
| 12 | import android.view.View; |
| 13 | import android.view.ViewGroup; |
| 14 | import android.widget.BaseAdapter; |
| 15 | import android.widget.RadioButton; |
| 16 | import android.widget.TextView; |
| 17 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 18 | import org.sflphone.R; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 19 | |
| 20 | public class AccountSelectionAdapter extends BaseAdapter { |
| 21 | |
| 22 | private static final String TAG = AccountSelectionAdapter.class.getSimpleName(); |
| 23 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 24 | ArrayList<Account> accounts; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 25 | Context mContext; |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 26 | int selectedAccount = -1; |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 27 | static final String DEFAULT_ACCOUNT_ID = "IP2IP"; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 28 | |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 29 | public AccountSelectionAdapter(Context cont, ArrayList<Account> newList) { |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 30 | super(); |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 31 | accounts = newList; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 32 | mContext = cont; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public int getCount() { |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 37 | return accounts.size(); |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | @Override |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 41 | public Account getItem(int pos) { |
| 42 | return accounts.get(pos); |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public long getItemId(int pos) { |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public View getView(int pos, View convertView, ViewGroup parent) { |
| 52 | View rowView = convertView; |
| 53 | AccountView entryView = null; |
| 54 | |
| 55 | if (rowView == null) { |
| 56 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 57 | rowView = inflater.inflate(R.layout.item_account, null); |
| 58 | |
| 59 | entryView = new AccountView(); |
| 60 | entryView.alias = (TextView) rowView.findViewById(R.id.account_alias); |
| 61 | entryView.host = (TextView) rowView.findViewById(R.id.account_host); |
| 62 | entryView.select = (RadioButton) rowView.findViewById(R.id.account_checked); |
| 63 | rowView.setTag(entryView); |
| 64 | } else { |
| 65 | entryView = (AccountView) rowView.getTag(); |
| 66 | } |
| 67 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 68 | entryView.alias.setText(accounts.get(pos).getAlias()); |
| 69 | entryView.host.setText(accounts.get(pos).getHost() + " - " + accounts.get(pos).getRegistered_state()); |
| 70 | // accManager.displayAccountDetails(accounts.get(pos), entryView); |
| 71 | if (pos == selectedAccount) { |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 72 | entryView.select.setChecked(true); |
| 73 | } |
| 74 | |
| 75 | return rowView; |
| 76 | } |
| 77 | |
| 78 | /********************* |
| 79 | * ViewHolder Pattern |
| 80 | *********************/ |
| 81 | public class AccountView { |
| 82 | public TextView alias; |
| 83 | public TextView host; |
| 84 | public RadioButton select; |
| 85 | } |
| 86 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 87 | public void setSelectedAccount(int pos) { |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 88 | selectedAccount = pos; |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 89 | } |
| 90 | |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 91 | public Account getSelectedAccount() { |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 92 | if (selectedAccount == -1) { |
alision | df1dac9 | 2013-06-27 17:35:53 -0400 | [diff] [blame] | 93 | return null; |
| 94 | } |
alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 95 | return accounts.get(selectedAccount); |
| 96 | } |
| 97 | |
| 98 | public void removeAll() { |
| 99 | accounts.clear(); |
| 100 | notifyDataSetChanged(); |
| 101 | |
| 102 | } |
| 103 | |
| 104 | public void addAll(ArrayList<Account> results) { |
| 105 | accounts.addAll(results); |
| 106 | notifyDataSetChanged(); |
alision | 371b77e | 2013-04-23 14:51:26 -0400 | [diff] [blame] | 107 | } |
| 108 | |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 109 | /** |
| 110 | * Modify state of specific account |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 111 | * |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 112 | * @param accountState |
| 113 | */ |
| 114 | public void updateAccount(Intent accountState) { |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 115 | Log.i(TAG, "updateAccount"); |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 116 | String id = accountState.getStringExtra("Account"); |
| 117 | String newState = accountState.getStringExtra("state"); |
| 118 | accountState.getStringExtra("Account"); |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 119 | |
| 120 | for (Account a : accounts) { |
| 121 | if (a.getAccountID().contentEquals(id)) { |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 122 | a.setRegistered_state(newState); |
| 123 | notifyDataSetChanged(); |
| 124 | return; |
| 125 | } |
| 126 | } |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 127 | |
| 128 | } |
| 129 | |
| 130 | public String getAccountOrder() { |
| 131 | String result = DEFAULT_ACCOUNT_ID + File.separator; |
| 132 | String selectedID = accounts.get(selectedAccount).getAccountID(); |
| 133 | result += selectedID + File.separator; |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 134 | |
Alexandre Lision | 8a1bab8 | 2013-10-24 10:06:51 -0400 | [diff] [blame] | 135 | for (Account a : accounts) { |
| 136 | if (a.getAccountID().contentEquals(selectedID)) { |
| 137 | continue; |
| 138 | } |
| 139 | result += a.getAccountID() + File.separator; |
| 140 | } |
| 141 | |
| 142 | return result; |
alision | 58356b7 | 2013-06-03 17:13:36 -0400 | [diff] [blame] | 143 | } |
| 144 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 145 | } |