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