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