Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 1 | /* |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 2 | * Copyright (C) 2004-2013 Savoir-Faire Linux Inc. |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 3 | * |
| 4 | * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com> |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 5 | * Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | * |
| 21 | * Additional permission under GNU GPL version 3 section 7: |
| 22 | * |
| 23 | * If you modify this program, or any covered work, by linking or |
| 24 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 25 | * modified version of that library), containing parts covered by the |
| 26 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 27 | * grants you additional permission to convey the resulting work. |
| 28 | * Corresponding Source for a non-source form of such a combination |
| 29 | * shall include the source code for the parts of OpenSSL used as well |
| 30 | * as that of the covered work. |
| 31 | */ |
| 32 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 33 | package org.sflphone.fragments; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 34 | |
Alexandre Lision | 4cf7870 | 2013-10-16 13:43:23 -0400 | [diff] [blame^] | 35 | import java.io.File; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 36 | import java.util.ArrayList; |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 37 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 38 | import org.sflphone.R; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 39 | import org.sflphone.account.AccountDetailAdvanced; |
| 40 | import org.sflphone.account.AccountDetailBasic; |
| 41 | import org.sflphone.account.AccountDetailSrtp; |
| 42 | import org.sflphone.account.AccountDetailTls; |
Alexandre Lision | 8b9d8e8 | 2013-10-04 09:21:27 -0400 | [diff] [blame] | 43 | import org.sflphone.client.AccountEditionActivity; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 44 | import org.sflphone.client.AccountWizard; |
Alexandre Lision | 4cf7870 | 2013-10-16 13:43:23 -0400 | [diff] [blame^] | 45 | import org.sflphone.dragsortlv.DragSortListView; |
| 46 | import org.sflphone.dragsortlv.DragSortListView.RemoveListener; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 47 | import org.sflphone.interfaces.AccountsInterface; |
| 48 | import org.sflphone.loaders.AccountsLoader; |
| 49 | import org.sflphone.loaders.LoaderConstants; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 50 | import org.sflphone.model.Account; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 51 | import org.sflphone.receivers.AccountsReceiver; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 52 | import org.sflphone.service.ConfigurationManagerCallback; |
| 53 | import org.sflphone.service.ISipService; |
| 54 | |
Alexandre Savard | 9a93f06 | 2012-09-11 10:42:43 -0400 | [diff] [blame] | 55 | import android.app.Activity; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 56 | import android.app.ListFragment; |
| 57 | import android.app.LoaderManager.LoaderCallbacks; |
Alexandre Savard | ae99293 | 2012-10-17 10:26:12 -0400 | [diff] [blame] | 58 | import android.content.Context; |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 59 | import android.content.Intent; |
Alexandre Savard | 4f11d7a | 2012-10-18 13:15:36 -0400 | [diff] [blame] | 60 | import android.content.IntentFilter; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 61 | import android.content.Loader; |
alision | f76de3b | 2013-04-16 15:35:22 -0400 | [diff] [blame] | 62 | import android.os.Bundle; |
| 63 | import android.os.RemoteException; |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 64 | import android.util.Log; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 65 | import android.view.LayoutInflater; |
Alexandre Lision | a764c68 | 2013-09-09 10:02:07 -0400 | [diff] [blame] | 66 | import android.view.Menu; |
| 67 | import android.view.MenuInflater; |
| 68 | import android.view.MenuItem; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 69 | import android.view.View; |
| 70 | import android.view.ViewGroup; |
| 71 | import android.widget.AdapterView; |
| 72 | import android.widget.AdapterView.OnItemClickListener; |
| 73 | import android.widget.BaseAdapter; |
| 74 | import android.widget.CheckBox; |
| 75 | import android.widget.CompoundButton; |
| 76 | import android.widget.CompoundButton.OnCheckedChangeListener; |
| 77 | import android.widget.TextView; |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 78 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 79 | public class AccountManagementFragment extends ListFragment implements LoaderCallbacks<ArrayList<Account>>, AccountsInterface { |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 80 | static final String TAG = "AccountManagementFragment"; |
Alexandre Savard | a6c0d99 | 2012-09-28 09:24:13 -0400 | [diff] [blame] | 81 | static final String DEFAULT_ACCOUNT_ID = "IP2IP"; |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 82 | static final int ACCOUNT_CREATE_REQUEST = 1; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 83 | static final int ACCOUNT_EDIT_REQUEST = 2; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 84 | AccountsReceiver accountReceiver; |
| 85 | AccountsAdapter mAdapter; |
Alexandre Savard | ae99293 | 2012-10-17 10:26:12 -0400 | [diff] [blame] | 86 | |
Alexandre Lision | 4cf7870 | 2013-10-16 13:43:23 -0400 | [diff] [blame^] | 87 | private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() { |
| 88 | @Override |
| 89 | public void drop(int from, int to) { |
| 90 | if (from != to) { |
| 91 | // DragSortListView list = getListView(); |
| 92 | Account item = mAdapter.getItem(from); |
| 93 | mAdapter.remove(item); |
| 94 | mAdapter.insert(item, to); |
| 95 | // list.moveCheckState(from, to); |
| 96 | try { |
| 97 | mCallbacks.getService().setAccountOrder(mAdapter.generateAccountOrder()); |
| 98 | } catch (RemoteException e) { |
| 99 | e.printStackTrace(); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | }; |
| 106 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 107 | private Callbacks mCallbacks = sDummyCallbacks; |
| 108 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 109 | |
| 110 | @Override |
| 111 | public ISipService getService() { |
| 112 | return null; |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | public interface Callbacks { |
| 117 | |
| 118 | public ISipService getService(); |
| 119 | |
| 120 | } |
Alexandre Savard | ae99293 | 2012-10-17 10:26:12 -0400 | [diff] [blame] | 121 | |
Emeric Vigier | 1f1ced3 | 2012-11-02 16:56:32 -0400 | [diff] [blame] | 122 | @Override |
| 123 | public void onAttach(Activity activity) { |
| 124 | super.onAttach(activity); |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 125 | if (!(activity instanceof Callbacks)) { |
| 126 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 127 | } |
| 128 | |
| 129 | mCallbacks = (Callbacks) activity; |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 130 | getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
Emeric Vigier | 1f1ced3 | 2012-11-02 16:56:32 -0400 | [diff] [blame] | 131 | } |
Alexandre Savard | 12dc3ac | 2012-09-27 11:17:39 -0400 | [diff] [blame] | 132 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 133 | @Override |
| 134 | public void onDetach() { |
| 135 | super.onDetach(); |
| 136 | mCallbacks = sDummyCallbacks; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 137 | } |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 138 | |
| 139 | @Override |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 140 | public void onCreate(Bundle savedInstanceState) { |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 141 | super.onCreate(savedInstanceState); |
| 142 | |
Alexandre Savard | 9a93f06 | 2012-09-11 10:42:43 -0400 | [diff] [blame] | 143 | Log.i(TAG, "Create Account Management Fragment"); |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 144 | mAdapter = new AccountsAdapter(getActivity(), new ArrayList<Account>()); |
Alexandre Lision | a764c68 | 2013-09-09 10:02:07 -0400 | [diff] [blame] | 145 | this.setHasOptionsMenu(true); |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 146 | accountReceiver = new AccountsReceiver(this); |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 147 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 148 | } |
Alexandre Lision | a764c68 | 2013-09-09 10:02:07 -0400 | [diff] [blame] | 149 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 150 | @Override |
| 151 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
Alexandre Lision | 4cf7870 | 2013-10-16 13:43:23 -0400 | [diff] [blame^] | 152 | View inflatedView = inflater.inflate(R.layout.frag_accounts_list, parent, false); |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 153 | setListAdapter(mAdapter); |
Alexandre Lision | 4cf7870 | 2013-10-16 13:43:23 -0400 | [diff] [blame^] | 154 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 155 | return inflatedView; |
| 156 | } |
| 157 | |
| 158 | @Override |
Alexandre Lision | 4cf7870 | 2013-10-16 13:43:23 -0400 | [diff] [blame^] | 159 | public DragSortListView getListView() { |
| 160 | return (DragSortListView) super.getListView(); |
| 161 | } |
| 162 | |
| 163 | @Override |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 164 | public void onViewCreated(View view, Bundle savedInstanceState) { |
| 165 | super.onViewCreated(view, savedInstanceState); |
Alexandre Lision | 4cf7870 | 2013-10-16 13:43:23 -0400 | [diff] [blame^] | 166 | |
| 167 | DragSortListView list = getListView(); |
| 168 | list.setDropListener(onDrop); |
| 169 | list.setOnItemClickListener(new OnItemClickListener() { |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 170 | |
| 171 | @Override |
| 172 | public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 173 | launchAccountEditActivity(mAdapter.getItem(pos)); |
Emeric Vigier | 0038a61 | 2012-11-06 18:51:19 -0500 | [diff] [blame] | 174 | } |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 175 | }); |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 176 | |
Alexandre Lision | 4cf7870 | 2013-10-16 13:43:23 -0400 | [diff] [blame^] | 177 | list.setEmptyView(view.findViewById(android.R.id.empty)); |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 178 | } |
| 179 | |
Alexandre Savard | 8b7d433 | 2012-09-30 20:02:11 -0400 | [diff] [blame] | 180 | @Override |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 181 | public void onPause() { |
| 182 | super.onPause(); |
| 183 | getActivity().unregisterReceiver(accountReceiver); |
Alexandre Savard | 8b7d433 | 2012-09-30 20:02:11 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 186 | public void onResume() { |
| 187 | super.onResume(); |
| 188 | IntentFilter intentFilter2 = new IntentFilter(); |
| 189 | intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED); |
| 190 | intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED); |
| 191 | getActivity().registerReceiver(accountReceiver, intentFilter2); |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 192 | getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
Alexandre Lision | 4cf7870 | 2013-10-16 13:43:23 -0400 | [diff] [blame^] | 193 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 194 | } |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 195 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 196 | @Override |
| 197 | public Loader<ArrayList<Account>> onCreateLoader(int id, Bundle args) { |
| 198 | AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService()); |
Alexandre Lision | 4cf7870 | 2013-10-16 13:43:23 -0400 | [diff] [blame^] | 199 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 200 | l.forceLoad(); |
Alexandre Lision | 4cf7870 | 2013-10-16 13:43:23 -0400 | [diff] [blame^] | 201 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 202 | return l; |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void onLoadFinished(Loader<ArrayList<Account>> loader, ArrayList<Account> results) { |
| 207 | mAdapter.removeAll(); |
| 208 | mAdapter.addAll(results); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void onLoaderReset(Loader<ArrayList<Account>> arg0) { |
| 213 | // TODO Auto-generated method stub |
| 214 | |
| 215 | } |
| 216 | |
Alexandre Lision | a764c68 | 2013-09-09 10:02:07 -0400 | [diff] [blame] | 217 | @Override |
| 218 | public void onCreateOptionsMenu(Menu m, MenuInflater inf) { |
| 219 | super.onCreateOptionsMenu(m, inf); |
| 220 | inf.inflate(R.menu.account_creation, m); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public boolean onOptionsItemSelected(MenuItem item) { |
| 225 | super.onOptionsItemSelected(item); |
| 226 | switch (item.getItemId()) { |
| 227 | case R.id.menuitem_create: |
| 228 | Intent intent = new Intent().setClass(getActivity(), AccountWizard.class); |
| 229 | startActivityForResult(intent, ACCOUNT_CREATE_REQUEST); |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | return true; |
| 234 | } |
| 235 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 236 | private void launchAccountEditActivity(Account acc) { |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 237 | Log.i(TAG, "Launch account edit activity"); |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 238 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 239 | Intent intent = new Intent().setClass(getActivity(), AccountEditionActivity.class); |
| 240 | Bundle bundle = new Bundle(); |
| 241 | bundle.putString("AccountID", acc.getAccountID()); |
Alexandre Lision | afd40e4 | 2013-10-15 13:48:37 -0400 | [diff] [blame] | 242 | bundle.putSerializable(AccountDetailBasic.BUNDLE_TAG, acc.getBasicDetails().getDetailsHashMap()); |
| 243 | bundle.putSerializable(AccountDetailAdvanced.BUNDLE_TAG, acc.getAdvancedDetails().getDetailsHashMap()); |
| 244 | bundle.putSerializable(AccountDetailSrtp.BUNDLE_TAG, acc.getSrtpDetails().getDetailsHashMap()); |
| 245 | bundle.putSerializable(AccountDetailTls.BUNDLE_TAG, acc.getTlsDetails().getDetailsHashMap()); |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 246 | |
| 247 | intent.putExtras(bundle); |
| 248 | |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 249 | startActivityForResult(intent, ACCOUNT_EDIT_REQUEST); |
| 250 | } |
| 251 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 252 | @Override |
| 253 | public void accountsChanged() { |
| 254 | if (getActivity() != null) |
| 255 | getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
Alexandre Savard | 52a7252 | 2012-09-27 16:40:13 -0400 | [diff] [blame] | 256 | } |
| 257 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 258 | @Override |
| 259 | public void accountStateChanged(Intent accountState) { |
| 260 | mAdapter.updateAccount(accountState); |
Alexandre Savard | 9a93f06 | 2012-09-11 10:42:43 -0400 | [diff] [blame] | 261 | } |
| 262 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 263 | /** |
| 264 | * |
| 265 | * Adapter for accounts List |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 266 | * |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 267 | * @author lisional |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 268 | * |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 269 | */ |
| 270 | public class AccountsAdapter extends BaseAdapter { |
Alexandre Savard | 52a7252 | 2012-09-27 16:40:13 -0400 | [diff] [blame] | 271 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 272 | // private static final String TAG = AccountSelectionAdapter.class.getSimpleName(); |
Alexandre Savard | 7098201 | 2012-09-27 17:15:50 -0400 | [diff] [blame] | 273 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 274 | ArrayList<Account> accounts; |
| 275 | Context mContext; |
Alexandre Savard | 7098201 | 2012-09-27 17:15:50 -0400 | [diff] [blame] | 276 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 277 | public AccountsAdapter(Context cont, ArrayList<Account> newList) { |
| 278 | super(); |
| 279 | accounts = newList; |
| 280 | mContext = cont; |
Alexandre Savard | e990d05 | 2012-10-18 14:43:09 -0400 | [diff] [blame] | 281 | } |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 282 | |
Alexandre Lision | 4cf7870 | 2013-10-16 13:43:23 -0400 | [diff] [blame^] | 283 | public void insert(Account item, int to) { |
| 284 | accounts.add(to, item); |
| 285 | notifyDataSetChanged(); |
| 286 | } |
| 287 | |
| 288 | public void remove(Account item) { |
| 289 | accounts.remove(item); |
| 290 | notifyDataSetChanged(); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public boolean hasStableIds(){ |
| 295 | return true; |
| 296 | } |
| 297 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 298 | @Override |
| 299 | public int getCount() { |
| 300 | return accounts.size(); |
| 301 | } |
Alexandre Savard | 7098201 | 2012-09-27 17:15:50 -0400 | [diff] [blame] | 302 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 303 | @Override |
| 304 | public Account getItem(int pos) { |
| 305 | return accounts.get(pos); |
| 306 | } |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 307 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 308 | @Override |
| 309 | public long getItemId(int pos) { |
| 310 | return 0; |
| 311 | } |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 312 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 313 | @Override |
| 314 | public View getView(final int pos, View convertView, ViewGroup parent) { |
| 315 | View rowView = convertView; |
| 316 | AccountView entryView = null; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 317 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 318 | if (rowView == null) { |
| 319 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 320 | rowView = inflater.inflate(R.layout.item_account_pref, null); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 321 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 322 | entryView = new AccountView(); |
| 323 | entryView.alias = (TextView) rowView.findViewById(R.id.account_alias); |
| 324 | entryView.host = (TextView) rowView.findViewById(R.id.account_host); |
| 325 | entryView.enabled = (CheckBox) rowView.findViewById(R.id.account_checked); |
| 326 | rowView.setTag(entryView); |
| 327 | } else { |
| 328 | entryView = (AccountView) rowView.getTag(); |
| 329 | } |
| 330 | |
| 331 | entryView.alias.setText(accounts.get(pos).getAlias()); |
| 332 | entryView.host.setText(accounts.get(pos).getHost() + " - " + accounts.get(pos).getRegistered_state()); |
| 333 | entryView.enabled.setChecked(accounts.get(pos).isEnabled()); |
| 334 | |
| 335 | entryView.enabled.setOnCheckedChangeListener(new OnCheckedChangeListener() { |
| 336 | |
| 337 | @Override |
| 338 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 339 | accounts.get(pos).setEnabled(isChecked); |
| 340 | |
| 341 | try { |
| 342 | mCallbacks.getService().setAccountDetails(accounts.get(pos).getAccountID(), accounts.get(pos).getDetails()); |
| 343 | } catch (RemoteException e) { |
| 344 | e.printStackTrace(); |
| 345 | } |
| 346 | } |
| 347 | }); |
| 348 | |
| 349 | return rowView; |
| 350 | } |
| 351 | |
| 352 | /********************* |
| 353 | * ViewHolder Pattern |
| 354 | *********************/ |
| 355 | public class AccountView { |
| 356 | public TextView alias; |
| 357 | public TextView host; |
| 358 | public CheckBox enabled; |
| 359 | } |
| 360 | |
| 361 | public void removeAll() { |
| 362 | accounts.clear(); |
| 363 | notifyDataSetChanged(); |
| 364 | |
| 365 | } |
| 366 | |
| 367 | public void addAll(ArrayList<Account> results) { |
| 368 | accounts.addAll(results); |
| 369 | notifyDataSetChanged(); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * Modify state of specific account |
| 374 | * |
| 375 | * @param accountState |
| 376 | */ |
| 377 | public void updateAccount(Intent accountState) { |
| 378 | Log.i(TAG, "updateAccount"); |
| 379 | String id = accountState.getStringExtra("Account"); |
| 380 | String newState = accountState.getStringExtra("state"); |
| 381 | accountState.getStringExtra("Account"); |
| 382 | |
| 383 | for (Account a : accounts) { |
| 384 | if (a.getAccountID().contentEquals(id)) { |
| 385 | a.setRegistered_state(newState); |
| 386 | notifyDataSetChanged(); |
| 387 | return; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | } |
Alexandre Lision | 4cf7870 | 2013-10-16 13:43:23 -0400 | [diff] [blame^] | 392 | |
| 393 | private String generateAccountOrder() { |
| 394 | String result = DEFAULT_ACCOUNT_ID + File.separator; |
| 395 | for (Account a : accounts) { |
| 396 | result += a.getAccountID() + File.separator; |
| 397 | } |
| 398 | return result; |
| 399 | } |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 400 | |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 401 | } |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 402 | } |