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 | |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.HashMap; |
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; |
| 39 | import org.sflphone.account.AccountDetail; |
| 40 | import org.sflphone.account.AccountDetailAdvanced; |
| 41 | import org.sflphone.account.AccountDetailBasic; |
| 42 | import org.sflphone.account.AccountDetailSrtp; |
| 43 | import org.sflphone.account.AccountDetailTls; |
Alexandre Lision | 8b9d8e8 | 2013-10-04 09:21:27 -0400 | [diff] [blame] | 44 | import org.sflphone.client.AccountEditionActivity; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 45 | import org.sflphone.client.AccountWizard; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 46 | import org.sflphone.interfaces.AccountsInterface; |
| 47 | import org.sflphone.loaders.AccountsLoader; |
| 48 | import org.sflphone.loaders.LoaderConstants; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 49 | import org.sflphone.model.Account; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 50 | import org.sflphone.receivers.AccountsReceiver; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 51 | import org.sflphone.service.ConfigurationManagerCallback; |
| 52 | import org.sflphone.service.ISipService; |
| 53 | |
Alexandre Savard | 9a93f06 | 2012-09-11 10:42:43 -0400 | [diff] [blame] | 54 | import android.app.Activity; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 55 | import android.app.ListFragment; |
| 56 | import android.app.LoaderManager.LoaderCallbacks; |
Alexandre Savard | ae99293 | 2012-10-17 10:26:12 -0400 | [diff] [blame] | 57 | import android.content.Context; |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 58 | import android.content.Intent; |
Alexandre Savard | 4f11d7a | 2012-10-18 13:15:36 -0400 | [diff] [blame] | 59 | import android.content.IntentFilter; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 60 | import android.content.Loader; |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 61 | import android.graphics.Color; |
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 | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 65 | import android.view.Gravity; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 66 | import android.view.LayoutInflater; |
Alexandre Lision | a764c68 | 2013-09-09 10:02:07 -0400 | [diff] [blame] | 67 | import android.view.Menu; |
| 68 | import android.view.MenuInflater; |
| 69 | import android.view.MenuItem; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 70 | import android.view.View; |
| 71 | import android.view.ViewGroup; |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 72 | import android.view.ViewGroup.LayoutParams; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 73 | import android.widget.AdapterView; |
| 74 | import android.widget.AdapterView.OnItemClickListener; |
| 75 | import android.widget.BaseAdapter; |
| 76 | import android.widget.CheckBox; |
| 77 | import android.widget.CompoundButton; |
| 78 | import android.widget.CompoundButton.OnCheckedChangeListener; |
| 79 | import android.widget.TextView; |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 80 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 81 | public class AccountManagementFragment extends ListFragment implements LoaderCallbacks<ArrayList<Account>>, AccountsInterface { |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 82 | static final String TAG = "AccountManagementFragment"; |
Alexandre Savard | a6c0d99 | 2012-09-28 09:24:13 -0400 | [diff] [blame] | 83 | static final String DEFAULT_ACCOUNT_ID = "IP2IP"; |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 84 | static final int ACCOUNT_CREATE_REQUEST = 1; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 85 | static final int ACCOUNT_EDIT_REQUEST = 2; |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 86 | AccountsReceiver accountReceiver; |
| 87 | AccountsAdapter mAdapter; |
Alexandre Savard | ae99293 | 2012-10-17 10:26:12 -0400 | [diff] [blame] | 88 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 89 | private Callbacks mCallbacks = sDummyCallbacks; |
| 90 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 91 | |
| 92 | @Override |
| 93 | public ISipService getService() { |
| 94 | return null; |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | public interface Callbacks { |
| 99 | |
| 100 | public ISipService getService(); |
| 101 | |
| 102 | } |
Alexandre Savard | ae99293 | 2012-10-17 10:26:12 -0400 | [diff] [blame] | 103 | |
Emeric Vigier | 1f1ced3 | 2012-11-02 16:56:32 -0400 | [diff] [blame] | 104 | @Override |
| 105 | public void onAttach(Activity activity) { |
| 106 | super.onAttach(activity); |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 107 | if (!(activity instanceof Callbacks)) { |
| 108 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 109 | } |
| 110 | |
| 111 | mCallbacks = (Callbacks) activity; |
Emeric Vigier | 1f1ced3 | 2012-11-02 16:56:32 -0400 | [diff] [blame] | 112 | } |
Alexandre Savard | 12dc3ac | 2012-09-27 11:17:39 -0400 | [diff] [blame] | 113 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 114 | @Override |
| 115 | public void onDetach() { |
| 116 | super.onDetach(); |
| 117 | mCallbacks = sDummyCallbacks; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 118 | } |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 119 | |
| 120 | @Override |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 121 | public void onCreate(Bundle savedInstanceState) { |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 122 | super.onCreate(savedInstanceState); |
| 123 | |
Alexandre Savard | 9a93f06 | 2012-09-11 10:42:43 -0400 | [diff] [blame] | 124 | Log.i(TAG, "Create Account Management Fragment"); |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 125 | mAdapter = new AccountsAdapter(getActivity(), new ArrayList<Account>()); |
Alexandre Lision | a764c68 | 2013-09-09 10:02:07 -0400 | [diff] [blame] | 126 | this.setHasOptionsMenu(true); |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 127 | accountReceiver = new AccountsReceiver(this); |
| 128 | getLoaderManager().initLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
| 129 | } |
Alexandre Lision | a764c68 | 2013-09-09 10:02:07 -0400 | [diff] [blame] | 130 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 131 | @Override |
| 132 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
| 133 | View inflatedView = inflater.inflate(android.R.layout.list_content, parent, false); |
| 134 | setListAdapter(mAdapter); |
| 135 | return inflatedView; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void onViewCreated(View view, Bundle savedInstanceState) { |
| 140 | super.onViewCreated(view, savedInstanceState); |
| 141 | getListView().setOnItemClickListener(new OnItemClickListener() { |
| 142 | |
| 143 | @Override |
| 144 | public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) { |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 145 | launchAccountEditActivity(mAdapter.getItem(pos)); |
Emeric Vigier | 0038a61 | 2012-11-06 18:51:19 -0500 | [diff] [blame] | 146 | } |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 147 | }); |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 148 | |
| 149 | setEmptyText(getResources().getString(R.string.empty_account_list)); |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Alexandre Savard | 8b7d433 | 2012-09-30 20:02:11 -0400 | [diff] [blame] | 152 | @Override |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 153 | public void onPause() { |
| 154 | super.onPause(); |
| 155 | getActivity().unregisterReceiver(accountReceiver); |
Alexandre Savard | 8b7d433 | 2012-09-30 20:02:11 -0400 | [diff] [blame] | 156 | } |
| 157 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 158 | public void onResume() { |
| 159 | super.onResume(); |
| 160 | IntentFilter intentFilter2 = new IntentFilter(); |
| 161 | intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED); |
| 162 | intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED); |
| 163 | getActivity().registerReceiver(accountReceiver, intentFilter2); |
Alexandre Savard | 8b7d433 | 2012-09-30 20:02:11 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 166 | @SuppressWarnings("unchecked") |
| 167 | // No proper solution with HashMap runtime cast |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 168 | @Override |
| 169 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 170 | switch (requestCode) { |
| 171 | case ACCOUNT_CREATE_REQUEST: |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 172 | if (resultCode == AccountWizard.ACCOUNT_CREATED) { |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 173 | Bundle bundle = data.getExtras(); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 174 | HashMap<String, String> accountDetails = new HashMap<String, String>(); |
| 175 | accountDetails = (HashMap<String, String>) bundle.getSerializable(AccountDetail.TAG); |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 176 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 177 | createNewAccount(accountDetails); |
| 178 | } |
| 179 | break; |
| 180 | case ACCOUNT_EDIT_REQUEST: |
Alexandre Lision | 8b9d8e8 | 2013-10-04 09:21:27 -0400 | [diff] [blame] | 181 | if (resultCode == AccountEditionActivity.result.ACCOUNT_MODIFIED) { |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 182 | Bundle bundle = data.getExtras(); |
| 183 | String accountID = bundle.getString("AccountID"); |
| 184 | Log.i(TAG, "Update account settings for " + accountID); |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 185 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 186 | HashMap<String, String> accountDetails = new HashMap<String, String>(); |
| 187 | accountDetails = (HashMap<String, String>) bundle.getSerializable(AccountDetail.TAG); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 188 | setAccountDetails(accountID, accountDetails); |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 189 | |
Alexandre Lision | 8b9d8e8 | 2013-10-04 09:21:27 -0400 | [diff] [blame] | 190 | } else if (resultCode == AccountEditionActivity.result.ACCOUNT_DELETED) { |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 191 | Bundle bundle = data.getExtras(); |
| 192 | String accountID = bundle.getString("AccountID"); |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 193 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 194 | Log.i(TAG, "Remove account " + accountID); |
| 195 | deleteSelectedAccount(accountID); |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 196 | // Preference accountScreen = accountPreferenceHashMap.get(accountID); |
| 197 | // mRoot.removePreference(accountScreen); |
| 198 | // accountPreferenceHashMap.remove(accountID); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 199 | } else { |
| 200 | Log.i(TAG, "Edition canceled"); |
| 201 | } |
| 202 | break; |
| 203 | default: |
| 204 | break; |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 208 | private void createNewAccount(HashMap<String, String> accountDetails) { |
| 209 | try { |
| 210 | Log.i(TAG, "ADD ACCOUNT"); |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 211 | mCallbacks.getService().addAccount(accountDetails); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 212 | } catch (RemoteException e) { |
| 213 | Log.e(TAG, "Cannot call service method", e); |
| 214 | } |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 215 | } |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 216 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 217 | private void setAccountDetails(String accountID, HashMap<String, String> accountDetails) { |
| 218 | try { |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 219 | mCallbacks.getService().setAccountDetails(accountID, accountDetails); |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 220 | } catch (RemoteException e) { |
| 221 | Log.e(TAG, "Cannot call service method", e); |
| 222 | } |
| 223 | } |
alision | 73424b6 | 2013-04-26 11:49:18 -0400 | [diff] [blame] | 224 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 225 | @Override |
| 226 | public Loader<ArrayList<Account>> onCreateLoader(int id, Bundle args) { |
| 227 | AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService()); |
| 228 | l.forceLoad(); |
| 229 | return l; |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public void onLoadFinished(Loader<ArrayList<Account>> loader, ArrayList<Account> results) { |
| 234 | mAdapter.removeAll(); |
| 235 | mAdapter.addAll(results); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void onLoaderReset(Loader<ArrayList<Account>> arg0) { |
| 240 | // TODO Auto-generated method stub |
| 241 | |
| 242 | } |
| 243 | |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 244 | private void deleteSelectedAccount(String accountID) { |
| 245 | Log.i(TAG, "DeleteSelectedAccount"); |
| 246 | try { |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 247 | mCallbacks.getService().removeAccount(accountID); |
alision | a432515 | 2013-04-19 11:10:03 -0400 | [diff] [blame] | 248 | } catch (RemoteException e) { |
| 249 | Log.e(TAG, "Cannot call service method", e); |
| 250 | } |
| 251 | }; |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 252 | |
Alexandre Lision | a764c68 | 2013-09-09 10:02:07 -0400 | [diff] [blame] | 253 | @Override |
| 254 | public void onCreateOptionsMenu(Menu m, MenuInflater inf) { |
| 255 | super.onCreateOptionsMenu(m, inf); |
| 256 | inf.inflate(R.menu.account_creation, m); |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public boolean onOptionsItemSelected(MenuItem item) { |
| 261 | super.onOptionsItemSelected(item); |
| 262 | switch (item.getItemId()) { |
| 263 | case R.id.menuitem_create: |
| 264 | Intent intent = new Intent().setClass(getActivity(), AccountWizard.class); |
| 265 | startActivityForResult(intent, ACCOUNT_CREATE_REQUEST); |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | return true; |
| 270 | } |
| 271 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 272 | private void launchAccountEditActivity(Account acc) { |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 273 | Log.i(TAG, "Launch account edit activity"); |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 274 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 275 | Intent intent = new Intent().setClass(getActivity(), AccountEditionActivity.class); |
| 276 | Bundle bundle = new Bundle(); |
| 277 | bundle.putString("AccountID", acc.getAccountID()); |
| 278 | bundle.putStringArrayList(AccountDetailBasic.BUNDLE_TAG, acc.getBasicDetails().getValuesOnly()); |
| 279 | bundle.putStringArrayList(AccountDetailAdvanced.BUNDLE_TAG, acc.getAdvancedDetails().getValuesOnly()); |
| 280 | bundle.putStringArrayList(AccountDetailSrtp.BUNDLE_TAG, acc.getSrtpDetails().getValuesOnly()); |
| 281 | bundle.putStringArrayList(AccountDetailTls.BUNDLE_TAG, acc.getTlsDetails().getValuesOnly()); |
Alexandre Savard | 833616f | 2012-10-30 16:02:30 -0400 | [diff] [blame] | 282 | |
| 283 | intent.putExtras(bundle); |
| 284 | |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 285 | startActivityForResult(intent, ACCOUNT_EDIT_REQUEST); |
| 286 | } |
| 287 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 288 | @Override |
| 289 | public void accountsChanged() { |
| 290 | if (getActivity() != null) |
| 291 | getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this); |
Alexandre Savard | 52a7252 | 2012-09-27 16:40:13 -0400 | [diff] [blame] | 292 | } |
| 293 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 294 | @Override |
| 295 | public void accountStateChanged(Intent accountState) { |
| 296 | mAdapter.updateAccount(accountState); |
Alexandre Savard | 9a93f06 | 2012-09-11 10:42:43 -0400 | [diff] [blame] | 297 | } |
| 298 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 299 | /** |
| 300 | * |
| 301 | * Adapter for accounts List |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 302 | * |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 303 | * @author lisional |
Alexandre Lision | 40954dc | 2013-10-09 15:24:03 -0400 | [diff] [blame] | 304 | * |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 305 | */ |
| 306 | public class AccountsAdapter extends BaseAdapter { |
Alexandre Savard | 52a7252 | 2012-09-27 16:40:13 -0400 | [diff] [blame] | 307 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 308 | // private static final String TAG = AccountSelectionAdapter.class.getSimpleName(); |
Alexandre Savard | 7098201 | 2012-09-27 17:15:50 -0400 | [diff] [blame] | 309 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 310 | ArrayList<Account> accounts; |
| 311 | Context mContext; |
Alexandre Savard | 7098201 | 2012-09-27 17:15:50 -0400 | [diff] [blame] | 312 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 313 | public AccountsAdapter(Context cont, ArrayList<Account> newList) { |
| 314 | super(); |
| 315 | accounts = newList; |
| 316 | mContext = cont; |
Alexandre Savard | e990d05 | 2012-10-18 14:43:09 -0400 | [diff] [blame] | 317 | } |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 318 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 319 | @Override |
| 320 | public int getCount() { |
| 321 | return accounts.size(); |
| 322 | } |
Alexandre Savard | 7098201 | 2012-09-27 17:15:50 -0400 | [diff] [blame] | 323 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 324 | @Override |
| 325 | public Account getItem(int pos) { |
| 326 | return accounts.get(pos); |
| 327 | } |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 328 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 329 | @Override |
| 330 | public long getItemId(int pos) { |
| 331 | return 0; |
| 332 | } |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 333 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 334 | @Override |
| 335 | public View getView(final int pos, View convertView, ViewGroup parent) { |
| 336 | View rowView = convertView; |
| 337 | AccountView entryView = null; |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 338 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 339 | if (rowView == null) { |
| 340 | LayoutInflater inflater = LayoutInflater.from(mContext); |
| 341 | rowView = inflater.inflate(R.layout.item_account_pref, null); |
alision | d9e2944 | 2013-04-17 16:10:18 -0400 | [diff] [blame] | 342 | |
Alexandre Lision | 3ccccf0 | 2013-10-07 14:10:46 -0400 | [diff] [blame] | 343 | entryView = new AccountView(); |
| 344 | entryView.alias = (TextView) rowView.findViewById(R.id.account_alias); |
| 345 | entryView.host = (TextView) rowView.findViewById(R.id.account_host); |
| 346 | entryView.enabled = (CheckBox) rowView.findViewById(R.id.account_checked); |
| 347 | rowView.setTag(entryView); |
| 348 | } else { |
| 349 | entryView = (AccountView) rowView.getTag(); |
| 350 | } |
| 351 | |
| 352 | entryView.alias.setText(accounts.get(pos).getAlias()); |
| 353 | entryView.host.setText(accounts.get(pos).getHost() + " - " + accounts.get(pos).getRegistered_state()); |
| 354 | entryView.enabled.setChecked(accounts.get(pos).isEnabled()); |
| 355 | |
| 356 | entryView.enabled.setOnCheckedChangeListener(new OnCheckedChangeListener() { |
| 357 | |
| 358 | @Override |
| 359 | public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
| 360 | accounts.get(pos).setEnabled(isChecked); |
| 361 | |
| 362 | try { |
| 363 | mCallbacks.getService().setAccountDetails(accounts.get(pos).getAccountID(), accounts.get(pos).getDetails()); |
| 364 | } catch (RemoteException e) { |
| 365 | e.printStackTrace(); |
| 366 | } |
| 367 | } |
| 368 | }); |
| 369 | |
| 370 | return rowView; |
| 371 | } |
| 372 | |
| 373 | /********************* |
| 374 | * ViewHolder Pattern |
| 375 | *********************/ |
| 376 | public class AccountView { |
| 377 | public TextView alias; |
| 378 | public TextView host; |
| 379 | public CheckBox enabled; |
| 380 | } |
| 381 | |
| 382 | public void removeAll() { |
| 383 | accounts.clear(); |
| 384 | notifyDataSetChanged(); |
| 385 | |
| 386 | } |
| 387 | |
| 388 | public void addAll(ArrayList<Account> results) { |
| 389 | accounts.addAll(results); |
| 390 | notifyDataSetChanged(); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Modify state of specific account |
| 395 | * |
| 396 | * @param accountState |
| 397 | */ |
| 398 | public void updateAccount(Intent accountState) { |
| 399 | Log.i(TAG, "updateAccount"); |
| 400 | String id = accountState.getStringExtra("Account"); |
| 401 | String newState = accountState.getStringExtra("state"); |
| 402 | accountState.getStringExtra("Account"); |
| 403 | |
| 404 | for (Account a : accounts) { |
| 405 | if (a.getAccountID().contentEquals(id)) { |
| 406 | a.setRegistered_state(newState); |
| 407 | notifyDataSetChanged(); |
| 408 | return; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | } |
| 413 | |
Alexandre Savard | 6883811 | 2012-10-30 11:34:43 -0400 | [diff] [blame] | 414 | } |
Alexandre Savard | 2b370f0 | 2012-09-06 16:06:01 -0400 | [diff] [blame] | 415 | } |