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