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