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