blob: 9f90d945fb206844874d806e640416508a85ac26 [file] [log] [blame]
Alexandre Lision064e1e02013-10-01 16:18:42 -04001package org.sflphone.adapters;
alisiona4325152013-04-19 11:10:03 -04002
Alexandre Lision8a1bab82013-10-24 10:06:51 -04003import java.io.File;
alisiona4325152013-04-19 11:10:03 -04004import java.util.ArrayList;
alisiona4325152013-04-19 11:10:03 -04005
Alexandre Lision064e1e02013-10-01 16:18:42 -04006import org.sflphone.model.Account;
7
alisiona4325152013-04-19 11:10:03 -04008import android.content.Context;
alision58356b72013-06-03 17:13:36 -04009import android.content.Intent;
10import android.util.Log;
alisiona4325152013-04-19 11:10:03 -040011import android.view.LayoutInflater;
12import android.view.View;
13import android.view.ViewGroup;
14import android.widget.BaseAdapter;
15import android.widget.RadioButton;
16import android.widget.TextView;
17
Alexandre Lision064e1e02013-10-01 16:18:42 -040018import org.sflphone.R;
alisiona4325152013-04-19 11:10:03 -040019
20public class AccountSelectionAdapter extends BaseAdapter {
21
22 private static final String TAG = AccountSelectionAdapter.class.getSimpleName();
23
alision11e8e162013-05-28 10:33:14 -040024 ArrayList<Account> accounts;
alisiona4325152013-04-19 11:10:03 -040025 Context mContext;
alisiondf1dac92013-06-27 17:35:53 -040026 int selectedAccount = -1;
Alexandre Lision8a1bab82013-10-24 10:06:51 -040027 static final String DEFAULT_ACCOUNT_ID = "IP2IP";
alisiona4325152013-04-19 11:10:03 -040028
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040029 public AccountSelectionAdapter(Context cont, ArrayList<Account> newList) {
alisiona4325152013-04-19 11:10:03 -040030 super();
alision11e8e162013-05-28 10:33:14 -040031 accounts = newList;
alisiona4325152013-04-19 11:10:03 -040032 mContext = cont;
alisiona4325152013-04-19 11:10:03 -040033 }
34
35 @Override
36 public int getCount() {
alision11e8e162013-05-28 10:33:14 -040037 return accounts.size();
alisiona4325152013-04-19 11:10:03 -040038 }
39
40 @Override
alision11e8e162013-05-28 10:33:14 -040041 public Account getItem(int pos) {
42 return accounts.get(pos);
alisiona4325152013-04-19 11:10:03 -040043 }
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
alision11e8e162013-05-28 10:33:14 -040068 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) {
alisiona4325152013-04-19 11:10:03 -040072 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
alisiona4325152013-04-19 11:10:03 -040087 public void setSelectedAccount(int pos) {
alision11e8e162013-05-28 10:33:14 -040088 selectedAccount = pos;
alisiona4325152013-04-19 11:10:03 -040089 }
90
alision11e8e162013-05-28 10:33:14 -040091 public Account getSelectedAccount() {
Alexandre Lision8a1bab82013-10-24 10:06:51 -040092 if (selectedAccount == -1) {
alisiondf1dac92013-06-27 17:35:53 -040093 return null;
94 }
alision11e8e162013-05-28 10:33:14 -040095 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();
alision371b77e2013-04-23 14:51:26 -0400107 }
108
alision58356b72013-06-03 17:13:36 -0400109 /**
110 * Modify state of specific account
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400111 *
alision58356b72013-06-03 17:13:36 -0400112 * @param accountState
113 */
114 public void updateAccount(Intent accountState) {
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400115 Log.i(TAG, "updateAccount");
alision58356b72013-06-03 17:13:36 -0400116 String id = accountState.getStringExtra("Account");
117 String newState = accountState.getStringExtra("state");
118 accountState.getStringExtra("Account");
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400119
120 for (Account a : accounts) {
121 if (a.getAccountID().contentEquals(id)) {
alision58356b72013-06-03 17:13:36 -0400122 a.setRegistered_state(newState);
123 notifyDataSetChanged();
124 return;
125 }
126 }
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400127
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;
alision58356b72013-06-03 17:13:36 -0400134
Alexandre Lision8a1bab82013-10-24 10:06:51 -0400135 for (Account a : accounts) {
136 if (a.getAccountID().contentEquals(selectedID)) {
137 continue;
138 }
139 result += a.getAccountID() + File.separator;
140 }
141
142 return result;
alision58356b72013-06-03 17:13:36 -0400143 }
144
alisiona4325152013-04-19 11:10:03 -0400145}