blob: 5384653b294f635fa3b305b6cd86eb278f768470 [file] [log] [blame]
Alexandre Savard2b370f02012-09-06 16:06:01 -04001/*
alision5cfc35d2013-07-11 15:11:39 -04002 * Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
Alexandre Savard2b370f02012-09-06 16:06:01 -04003 *
4 * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
alision5cfc35d2013-07-11 15:11:39 -04005 * Alexandre Lision <alexandre.lision@savoirfairelinux.com>
Alexandre Savard2b370f02012-09-06 16:06:01 -04006 *
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 Lision064e1e02013-10-01 16:18:42 -040033package org.sflphone.fragments;
alisionf76de3b2013-04-16 15:35:22 -040034
Alexandre Lision4cf78702013-10-16 13:43:23 -040035import java.io.File;
alisionf76de3b2013-04-16 15:35:22 -040036import java.util.ArrayList;
Alexandre Savard2b370f02012-09-06 16:06:01 -040037
Alexandre Lision064e1e02013-10-01 16:18:42 -040038import org.sflphone.R;
Alexandre Lision8b9d8e82013-10-04 09:21:27 -040039import org.sflphone.client.AccountEditionActivity;
Alexandre Lision064e1e02013-10-01 16:18:42 -040040import org.sflphone.client.AccountWizard;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040041import org.sflphone.interfaces.AccountsInterface;
42import org.sflphone.loaders.AccountsLoader;
43import org.sflphone.loaders.LoaderConstants;
Alexandre Lision064e1e02013-10-01 16:18:42 -040044import org.sflphone.model.Account;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040045import org.sflphone.receivers.AccountsReceiver;
Alexandre Lision064e1e02013-10-01 16:18:42 -040046import org.sflphone.service.ConfigurationManagerCallback;
47import org.sflphone.service.ISipService;
Alexandre Lision7f0bba52013-10-16 14:43:11 -040048import org.sflphone.views.dragsortlv.DragSortListView;
Alexandre Lision064e1e02013-10-01 16:18:42 -040049
Alexandre Savard9a93f062012-09-11 10:42:43 -040050import android.app.Activity;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040051import android.app.ListFragment;
52import android.app.LoaderManager.LoaderCallbacks;
Alexandre Savardae992932012-10-17 10:26:12 -040053import android.content.Context;
Alexandre Savard53d2ddd2012-09-30 22:04:40 -040054import android.content.Intent;
Alexandre Savard4f11d7a2012-10-18 13:15:36 -040055import android.content.IntentFilter;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040056import android.content.Loader;
alisionf76de3b2013-04-16 15:35:22 -040057import android.os.Bundle;
58import android.os.RemoteException;
Alexandre Savard2b370f02012-09-06 16:06:01 -040059import android.util.Log;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040060import android.view.LayoutInflater;
Alexandre Lisiona764c682013-09-09 10:02:07 -040061import android.view.Menu;
62import android.view.MenuInflater;
63import android.view.MenuItem;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040064import android.view.View;
Alexandre Lision059da9d2013-10-22 11:17:25 -040065import android.view.View.OnClickListener;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040066import android.view.ViewGroup;
67import android.widget.AdapterView;
68import android.widget.AdapterView.OnItemClickListener;
69import android.widget.BaseAdapter;
70import android.widget.CheckBox;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040071import android.widget.TextView;
Alexandre Savard2b370f02012-09-06 16:06:01 -040072
Alexandre Lisiond2e9e062013-10-21 16:27:33 -040073public class AccountManagementFragment extends ListFragment implements LoaderCallbacks<Bundle>, AccountsInterface {
Alexandre Savard2b370f02012-09-06 16:06:01 -040074 static final String TAG = "AccountManagementFragment";
Alexandre Savarda6c0d992012-09-28 09:24:13 -040075 static final String DEFAULT_ACCOUNT_ID = "IP2IP";
Alexandre Savard53d2ddd2012-09-30 22:04:40 -040076 static final int ACCOUNT_CREATE_REQUEST = 1;
Alexandre Savard68838112012-10-30 11:34:43 -040077 static final int ACCOUNT_EDIT_REQUEST = 2;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040078 AccountsReceiver accountReceiver;
79 AccountsAdapter mAdapter;
Alexandre Savardae992932012-10-17 10:26:12 -040080
Alexandre Lision4cf78702013-10-16 13:43:23 -040081 private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
82 @Override
83 public void drop(int from, int to) {
84 if (from != to) {
Alexandre Lision4cf78702013-10-16 13:43:23 -040085 Account item = mAdapter.getItem(from);
86 mAdapter.remove(item);
87 mAdapter.insert(item, to);
Alexandre Lision4cf78702013-10-16 13:43:23 -040088 try {
89 mCallbacks.getService().setAccountOrder(mAdapter.generateAccountOrder());
90 } catch (RemoteException e) {
91 e.printStackTrace();
92 }
93 }
94 }
95
Alexandre Lision4cf78702013-10-16 13:43:23 -040096 };
97
Alexandre Lision3ccccf02013-10-07 14:10:46 -040098 private Callbacks mCallbacks = sDummyCallbacks;
Alexandre Lision059da9d2013-10-22 11:17:25 -040099 private Account ip2ip;
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400100 private static Callbacks sDummyCallbacks = new Callbacks() {
101
102 @Override
103 public ISipService getService() {
104 return null;
105 }
106 };
107
108 public interface Callbacks {
109
110 public ISipService getService();
111
112 }
Alexandre Savardae992932012-10-17 10:26:12 -0400113
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400114 @Override
115 public void onAttach(Activity activity) {
116 super.onAttach(activity);
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400117 if (!(activity instanceof Callbacks)) {
118 throw new IllegalStateException("Activity must implement fragment's callbacks.");
119 }
120
121 mCallbacks = (Callbacks) activity;
Alexandre Lision059da9d2013-10-22 11:17:25 -0400122 getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400123 }
Alexandre Savard12dc3ac2012-09-27 11:17:39 -0400124
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400125 @Override
126 public void onDetach() {
127 super.onDetach();
128 mCallbacks = sDummyCallbacks;
alisiond9e29442013-04-17 16:10:18 -0400129 }
Alexandre Savard2b370f02012-09-06 16:06:01 -0400130
131 @Override
alisiond9e29442013-04-17 16:10:18 -0400132 public void onCreate(Bundle savedInstanceState) {
Alexandre Savard2b370f02012-09-06 16:06:01 -0400133 super.onCreate(savedInstanceState);
134
Alexandre Savard9a93f062012-09-11 10:42:43 -0400135 Log.i(TAG, "Create Account Management Fragment");
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400136 mAdapter = new AccountsAdapter(getActivity(), new ArrayList<Account>());
Alexandre Lisiona764c682013-09-09 10:02:07 -0400137 this.setHasOptionsMenu(true);
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400138 accountReceiver = new AccountsReceiver(this);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400139
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400140 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400141
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400142 @Override
143 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
Alexandre Lision4cf78702013-10-16 13:43:23 -0400144 View inflatedView = inflater.inflate(R.layout.frag_accounts_list, parent, false);
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400145 setListAdapter(mAdapter);
Alexandre Lision4cf78702013-10-16 13:43:23 -0400146
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400147 return inflatedView;
148 }
149
150 @Override
Alexandre Lision4cf78702013-10-16 13:43:23 -0400151 public DragSortListView getListView() {
152 return (DragSortListView) super.getListView();
153 }
154
155 @Override
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400156 public void onViewCreated(View view, Bundle savedInstanceState) {
157 super.onViewCreated(view, savedInstanceState);
Alexandre Lision4cf78702013-10-16 13:43:23 -0400158
159 DragSortListView list = getListView();
160 list.setDropListener(onDrop);
161 list.setOnItemClickListener(new OnItemClickListener() {
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400162
163 @Override
164 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400165 launchAccountEditActivity(mAdapter.getItem(pos));
Emeric Vigier0038a612012-11-06 18:51:19 -0500166 }
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400167 });
Alexandre Lision059da9d2013-10-22 11:17:25 -0400168
169 getView().findViewById(R.id.layer).setOnClickListener(new OnClickListener() {
170
171 @Override
172 public void onClick(View v) {
173 launchAccountEditActivity(ip2ip);
174 }
175 });
Alexandre Lision40954dc2013-10-09 15:24:03 -0400176
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400177 list.setEmptyView(view.findViewById(android.R.id.empty));
Alexandre Savard2b370f02012-09-06 16:06:01 -0400178 }
179
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400180 @Override
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400181 public void onPause() {
182 super.onPause();
183 getActivity().unregisterReceiver(accountReceiver);
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400184 }
185
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400186 public void onResume() {
187 super.onResume();
188 IntentFilter intentFilter2 = new IntentFilter();
189 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
190 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
191 getActivity().registerReceiver(accountReceiver, intentFilter2);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400192 getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
Alexandre Lision4cf78702013-10-16 13:43:23 -0400193
alisiona4325152013-04-19 11:10:03 -0400194 }
alision73424b62013-04-26 11:49:18 -0400195
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400196 @Override
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400197 public Loader<Bundle> onCreateLoader(int id, Bundle args) {
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400198 AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService());
Alexandre Lision4cf78702013-10-16 13:43:23 -0400199
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400200 l.forceLoad();
Alexandre Lision059da9d2013-10-22 11:17:25 -0400201
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400202 return l;
203 }
204
205 @Override
Alexandre Lision059da9d2013-10-22 11:17:25 -0400206 public void onLoadFinished(Loader<Bundle> loader, Bundle results) {
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400207 mAdapter.removeAll();
Alexandre Lision059da9d2013-10-22 11:17:25 -0400208 ArrayList<Account> tmp = results.getParcelableArrayList(AccountsLoader.ACCOUNTS);
209 ip2ip = results.getParcelable(AccountsLoader.ACCOUNT_IP2IP);
210 mAdapter.addAll(tmp);
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400211 }
212
213 @Override
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400214 public void onLoaderReset(Loader<Bundle> arg0) {
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400215 // TODO Auto-generated method stub
216
217 }
218
Alexandre Lisiona764c682013-09-09 10:02:07 -0400219 @Override
220 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
221 super.onCreateOptionsMenu(m, inf);
222 inf.inflate(R.menu.account_creation, m);
223 }
224
225 @Override
226 public boolean onOptionsItemSelected(MenuItem item) {
227 super.onOptionsItemSelected(item);
228 switch (item.getItemId()) {
229 case R.id.menuitem_create:
230 Intent intent = new Intent().setClass(getActivity(), AccountWizard.class);
231 startActivityForResult(intent, ACCOUNT_CREATE_REQUEST);
232 break;
233 }
234
235 return true;
236 }
237
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400238 private void launchAccountEditActivity(Account acc) {
Alexandre Savard68838112012-10-30 11:34:43 -0400239 Log.i(TAG, "Launch account edit activity");
Alexandre Savard833616f2012-10-30 16:02:30 -0400240
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400241 Intent intent = new Intent().setClass(getActivity(), AccountEditionActivity.class);
242 Bundle bundle = new Bundle();
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400243 bundle.putParcelable("account", acc);
Alexandre Lision059da9d2013-10-22 11:17:25 -0400244
Alexandre Savard833616f2012-10-30 16:02:30 -0400245 intent.putExtras(bundle);
246
Alexandre Savard68838112012-10-30 11:34:43 -0400247 startActivityForResult(intent, ACCOUNT_EDIT_REQUEST);
248 }
249
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400250 @Override
251 public void accountsChanged() {
252 if (getActivity() != null)
253 getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
Alexandre Savard52a72522012-09-27 16:40:13 -0400254 }
255
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400256 @Override
257 public void accountStateChanged(Intent accountState) {
258 mAdapter.updateAccount(accountState);
Alexandre Savard9a93f062012-09-11 10:42:43 -0400259 }
260
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400261 /**
262 *
263 * Adapter for accounts List
Alexandre Lision40954dc2013-10-09 15:24:03 -0400264 *
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400265 * @author lisional
Alexandre Lision40954dc2013-10-09 15:24:03 -0400266 *
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400267 */
268 public class AccountsAdapter extends BaseAdapter {
Alexandre Savard52a72522012-09-27 16:40:13 -0400269
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400270 // private static final String TAG = AccountSelectionAdapter.class.getSimpleName();
Alexandre Savard70982012012-09-27 17:15:50 -0400271
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400272 ArrayList<Account> accounts;
273 Context mContext;
Alexandre Savard70982012012-09-27 17:15:50 -0400274
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400275 public AccountsAdapter(Context cont, ArrayList<Account> newList) {
276 super();
277 accounts = newList;
278 mContext = cont;
Alexandre Savarde990d052012-10-18 14:43:09 -0400279 }
alisiond9e29442013-04-17 16:10:18 -0400280
Alexandre Lision4cf78702013-10-16 13:43:23 -0400281 public void insert(Account item, int to) {
282 accounts.add(to, item);
283 notifyDataSetChanged();
284 }
285
286 public void remove(Account item) {
287 accounts.remove(item);
288 notifyDataSetChanged();
289 }
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400290
Alexandre Lision4cf78702013-10-16 13:43:23 -0400291 @Override
Alexandre Lisiond2e9e062013-10-21 16:27:33 -0400292 public boolean hasStableIds() {
Alexandre Lision4cf78702013-10-16 13:43:23 -0400293 return true;
294 }
295
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400296 @Override
297 public int getCount() {
298 return accounts.size();
299 }
Alexandre Savard70982012012-09-27 17:15:50 -0400300
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400301 @Override
302 public Account getItem(int pos) {
303 return accounts.get(pos);
304 }
Alexandre Savard68838112012-10-30 11:34:43 -0400305
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400306 @Override
307 public long getItemId(int pos) {
308 return 0;
309 }
Alexandre Savard68838112012-10-30 11:34:43 -0400310
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400311 @Override
312 public View getView(final int pos, View convertView, ViewGroup parent) {
313 View rowView = convertView;
314 AccountView entryView = null;
Alexandre Savard68838112012-10-30 11:34:43 -0400315
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400316 if (rowView == null) {
317 LayoutInflater inflater = LayoutInflater.from(mContext);
318 rowView = inflater.inflate(R.layout.item_account_pref, null);
alisiond9e29442013-04-17 16:10:18 -0400319
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400320 entryView = new AccountView();
321 entryView.alias = (TextView) rowView.findViewById(R.id.account_alias);
322 entryView.host = (TextView) rowView.findViewById(R.id.account_host);
323 entryView.enabled = (CheckBox) rowView.findViewById(R.id.account_checked);
324 rowView.setTag(entryView);
325 } else {
326 entryView = (AccountView) rowView.getTag();
327 }
328
Alexandre Lision059da9d2013-10-22 11:17:25 -0400329 final Account item = accounts.get(pos);
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400330 entryView.alias.setText(accounts.get(pos).getAlias());
Alexandre Lision059da9d2013-10-22 11:17:25 -0400331 entryView.host.setText(item.getHost() + " - " + item.getRegistered_state());
332 entryView.enabled.setChecked(item.isEnabled());
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400333
Alexandre Lision059da9d2013-10-22 11:17:25 -0400334 entryView.enabled.setOnClickListener(new OnClickListener() {
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400335
336 @Override
Alexandre Lision059da9d2013-10-22 11:17:25 -0400337 public void onClick(View v) {
338 item.setEnabled(!item.isEnabled());
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400339
340 try {
Alexandre Lision059da9d2013-10-22 11:17:25 -0400341 mCallbacks.getService().setAccountDetails(item.getAccountID(), item.getDetails());
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400342 } catch (RemoteException e) {
343 e.printStackTrace();
344 }
345 }
346 });
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) {
377 Log.i(TAG, "updateAccount");
378 String id = accountState.getStringExtra("Account");
379 String newState = accountState.getStringExtra("state");
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400380
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 Lisiond2e9e062013-10-21 16:27:33 -0400390
Alexandre Lision4cf78702013-10-16 13:43:23 -0400391 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 Lision3ccccf02013-10-07 14:10:46 -0400398
Alexandre Savard68838112012-10-30 11:34:43 -0400399 }
Alexandre Savard2b370f02012-09-06 16:06:01 -0400400}