blob: ee627cec785052ccfe1356d00095a7407eacc236 [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 Lision064e1e02013-10-01 16:18:42 -040039import org.sflphone.account.AccountDetailAdvanced;
40import org.sflphone.account.AccountDetailBasic;
41import org.sflphone.account.AccountDetailSrtp;
42import org.sflphone.account.AccountDetailTls;
Alexandre Lision8b9d8e82013-10-04 09:21:27 -040043import org.sflphone.client.AccountEditionActivity;
Alexandre Lision064e1e02013-10-01 16:18:42 -040044import org.sflphone.client.AccountWizard;
Alexandre Lision4cf78702013-10-16 13:43:23 -040045import org.sflphone.dragsortlv.DragSortListView;
46import org.sflphone.dragsortlv.DragSortListView.RemoveListener;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040047import org.sflphone.interfaces.AccountsInterface;
48import org.sflphone.loaders.AccountsLoader;
49import org.sflphone.loaders.LoaderConstants;
Alexandre Lision064e1e02013-10-01 16:18:42 -040050import org.sflphone.model.Account;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040051import org.sflphone.receivers.AccountsReceiver;
Alexandre Lision064e1e02013-10-01 16:18:42 -040052import org.sflphone.service.ConfigurationManagerCallback;
53import org.sflphone.service.ISipService;
54
Alexandre Savard9a93f062012-09-11 10:42:43 -040055import android.app.Activity;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040056import android.app.ListFragment;
57import android.app.LoaderManager.LoaderCallbacks;
Alexandre Savardae992932012-10-17 10:26:12 -040058import android.content.Context;
Alexandre Savard53d2ddd2012-09-30 22:04:40 -040059import android.content.Intent;
Alexandre Savard4f11d7a2012-10-18 13:15:36 -040060import android.content.IntentFilter;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040061import android.content.Loader;
alisionf76de3b2013-04-16 15:35:22 -040062import android.os.Bundle;
63import android.os.RemoteException;
Alexandre Savard2b370f02012-09-06 16:06:01 -040064import android.util.Log;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040065import android.view.LayoutInflater;
Alexandre Lisiona764c682013-09-09 10:02:07 -040066import android.view.Menu;
67import android.view.MenuInflater;
68import android.view.MenuItem;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040069import android.view.View;
70import android.view.ViewGroup;
71import android.widget.AdapterView;
72import android.widget.AdapterView.OnItemClickListener;
73import android.widget.BaseAdapter;
74import android.widget.CheckBox;
75import android.widget.CompoundButton;
76import android.widget.CompoundButton.OnCheckedChangeListener;
77import android.widget.TextView;
Alexandre Savard2b370f02012-09-06 16:06:01 -040078
Alexandre Lision3ccccf02013-10-07 14:10:46 -040079public class AccountManagementFragment extends ListFragment implements LoaderCallbacks<ArrayList<Account>>, AccountsInterface {
Alexandre Savard2b370f02012-09-06 16:06:01 -040080 static final String TAG = "AccountManagementFragment";
Alexandre Savarda6c0d992012-09-28 09:24:13 -040081 static final String DEFAULT_ACCOUNT_ID = "IP2IP";
Alexandre Savard53d2ddd2012-09-30 22:04:40 -040082 static final int ACCOUNT_CREATE_REQUEST = 1;
Alexandre Savard68838112012-10-30 11:34:43 -040083 static final int ACCOUNT_EDIT_REQUEST = 2;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040084 AccountsReceiver accountReceiver;
85 AccountsAdapter mAdapter;
Alexandre Savardae992932012-10-17 10:26:12 -040086
Alexandre Lision4cf78702013-10-16 13:43:23 -040087 private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
88 @Override
89 public void drop(int from, int to) {
90 if (from != to) {
91// DragSortListView list = getListView();
92 Account item = mAdapter.getItem(from);
93 mAdapter.remove(item);
94 mAdapter.insert(item, to);
95// list.moveCheckState(from, to);
96 try {
97 mCallbacks.getService().setAccountOrder(mAdapter.generateAccountOrder());
98 } catch (RemoteException e) {
99 e.printStackTrace();
100 }
101 }
102 }
103
104
105 };
106
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400107 private Callbacks mCallbacks = sDummyCallbacks;
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 Savardae992932012-10-17 10:26:12 -0400121
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400122 @Override
123 public void onAttach(Activity activity) {
124 super.onAttach(activity);
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400125 if (!(activity instanceof Callbacks)) {
126 throw new IllegalStateException("Activity must implement fragment's callbacks.");
127 }
128
129 mCallbacks = (Callbacks) activity;
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400130 getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400131 }
Alexandre Savard12dc3ac2012-09-27 11:17:39 -0400132
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400133 @Override
134 public void onDetach() {
135 super.onDetach();
136 mCallbacks = sDummyCallbacks;
alisiond9e29442013-04-17 16:10:18 -0400137 }
Alexandre Savard2b370f02012-09-06 16:06:01 -0400138
139 @Override
alisiond9e29442013-04-17 16:10:18 -0400140 public void onCreate(Bundle savedInstanceState) {
Alexandre Savard2b370f02012-09-06 16:06:01 -0400141 super.onCreate(savedInstanceState);
142
Alexandre Savard9a93f062012-09-11 10:42:43 -0400143 Log.i(TAG, "Create Account Management Fragment");
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400144 mAdapter = new AccountsAdapter(getActivity(), new ArrayList<Account>());
Alexandre Lisiona764c682013-09-09 10:02:07 -0400145 this.setHasOptionsMenu(true);
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400146 accountReceiver = new AccountsReceiver(this);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400147
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400148 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400149
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400150 @Override
151 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
Alexandre Lision4cf78702013-10-16 13:43:23 -0400152 View inflatedView = inflater.inflate(R.layout.frag_accounts_list, parent, false);
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400153 setListAdapter(mAdapter);
Alexandre Lision4cf78702013-10-16 13:43:23 -0400154
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400155 return inflatedView;
156 }
157
158 @Override
Alexandre Lision4cf78702013-10-16 13:43:23 -0400159 public DragSortListView getListView() {
160 return (DragSortListView) super.getListView();
161 }
162
163 @Override
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400164 public void onViewCreated(View view, Bundle savedInstanceState) {
165 super.onViewCreated(view, savedInstanceState);
Alexandre Lision4cf78702013-10-16 13:43:23 -0400166
167 DragSortListView list = getListView();
168 list.setDropListener(onDrop);
169 list.setOnItemClickListener(new OnItemClickListener() {
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400170
171 @Override
172 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400173 launchAccountEditActivity(mAdapter.getItem(pos));
Emeric Vigier0038a612012-11-06 18:51:19 -0500174 }
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400175 });
Alexandre Lision40954dc2013-10-09 15:24:03 -0400176
Alexandre Lision4cf78702013-10-16 13:43:23 -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
197 public Loader<ArrayList<Account>> onCreateLoader(int id, Bundle args) {
198 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 Lision4cf78702013-10-16 13:43:23 -0400201
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400202 return l;
203 }
204
205 @Override
206 public void onLoadFinished(Loader<ArrayList<Account>> loader, ArrayList<Account> results) {
207 mAdapter.removeAll();
208 mAdapter.addAll(results);
209 }
210
211 @Override
212 public void onLoaderReset(Loader<ArrayList<Account>> arg0) {
213 // TODO Auto-generated method stub
214
215 }
216
Alexandre Lisiona764c682013-09-09 10:02:07 -0400217 @Override
218 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
219 super.onCreateOptionsMenu(m, inf);
220 inf.inflate(R.menu.account_creation, m);
221 }
222
223 @Override
224 public boolean onOptionsItemSelected(MenuItem item) {
225 super.onOptionsItemSelected(item);
226 switch (item.getItemId()) {
227 case R.id.menuitem_create:
228 Intent intent = new Intent().setClass(getActivity(), AccountWizard.class);
229 startActivityForResult(intent, ACCOUNT_CREATE_REQUEST);
230 break;
231 }
232
233 return true;
234 }
235
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400236 private void launchAccountEditActivity(Account acc) {
Alexandre Savard68838112012-10-30 11:34:43 -0400237 Log.i(TAG, "Launch account edit activity");
Alexandre Savard833616f2012-10-30 16:02:30 -0400238
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400239 Intent intent = new Intent().setClass(getActivity(), AccountEditionActivity.class);
240 Bundle bundle = new Bundle();
241 bundle.putString("AccountID", acc.getAccountID());
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400242 bundle.putSerializable(AccountDetailBasic.BUNDLE_TAG, acc.getBasicDetails().getDetailsHashMap());
243 bundle.putSerializable(AccountDetailAdvanced.BUNDLE_TAG, acc.getAdvancedDetails().getDetailsHashMap());
244 bundle.putSerializable(AccountDetailSrtp.BUNDLE_TAG, acc.getSrtpDetails().getDetailsHashMap());
245 bundle.putSerializable(AccountDetailTls.BUNDLE_TAG, acc.getTlsDetails().getDetailsHashMap());
Alexandre Savard833616f2012-10-30 16:02:30 -0400246
247 intent.putExtras(bundle);
248
Alexandre Savard68838112012-10-30 11:34:43 -0400249 startActivityForResult(intent, ACCOUNT_EDIT_REQUEST);
250 }
251
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400252 @Override
253 public void accountsChanged() {
254 if (getActivity() != null)
255 getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
Alexandre Savard52a72522012-09-27 16:40:13 -0400256 }
257
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400258 @Override
259 public void accountStateChanged(Intent accountState) {
260 mAdapter.updateAccount(accountState);
Alexandre Savard9a93f062012-09-11 10:42:43 -0400261 }
262
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400263 /**
264 *
265 * Adapter for accounts List
Alexandre Lision40954dc2013-10-09 15:24:03 -0400266 *
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400267 * @author lisional
Alexandre Lision40954dc2013-10-09 15:24:03 -0400268 *
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400269 */
270 public class AccountsAdapter extends BaseAdapter {
Alexandre Savard52a72522012-09-27 16:40:13 -0400271
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400272 // private static final String TAG = AccountSelectionAdapter.class.getSimpleName();
Alexandre Savard70982012012-09-27 17:15:50 -0400273
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400274 ArrayList<Account> accounts;
275 Context mContext;
Alexandre Savard70982012012-09-27 17:15:50 -0400276
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400277 public AccountsAdapter(Context cont, ArrayList<Account> newList) {
278 super();
279 accounts = newList;
280 mContext = cont;
Alexandre Savarde990d052012-10-18 14:43:09 -0400281 }
alisiond9e29442013-04-17 16:10:18 -0400282
Alexandre Lision4cf78702013-10-16 13:43:23 -0400283 public void insert(Account item, int to) {
284 accounts.add(to, item);
285 notifyDataSetChanged();
286 }
287
288 public void remove(Account item) {
289 accounts.remove(item);
290 notifyDataSetChanged();
291 }
292
293 @Override
294 public boolean hasStableIds(){
295 return true;
296 }
297
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400298 @Override
299 public int getCount() {
300 return accounts.size();
301 }
Alexandre Savard70982012012-09-27 17:15:50 -0400302
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400303 @Override
304 public Account getItem(int pos) {
305 return accounts.get(pos);
306 }
Alexandre Savard68838112012-10-30 11:34:43 -0400307
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400308 @Override
309 public long getItemId(int pos) {
310 return 0;
311 }
Alexandre Savard68838112012-10-30 11:34:43 -0400312
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400313 @Override
314 public View getView(final int pos, View convertView, ViewGroup parent) {
315 View rowView = convertView;
316 AccountView entryView = null;
Alexandre Savard68838112012-10-30 11:34:43 -0400317
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400318 if (rowView == null) {
319 LayoutInflater inflater = LayoutInflater.from(mContext);
320 rowView = inflater.inflate(R.layout.item_account_pref, null);
alisiond9e29442013-04-17 16:10:18 -0400321
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400322 entryView = new AccountView();
323 entryView.alias = (TextView) rowView.findViewById(R.id.account_alias);
324 entryView.host = (TextView) rowView.findViewById(R.id.account_host);
325 entryView.enabled = (CheckBox) rowView.findViewById(R.id.account_checked);
326 rowView.setTag(entryView);
327 } else {
328 entryView = (AccountView) rowView.getTag();
329 }
330
331 entryView.alias.setText(accounts.get(pos).getAlias());
332 entryView.host.setText(accounts.get(pos).getHost() + " - " + accounts.get(pos).getRegistered_state());
333 entryView.enabled.setChecked(accounts.get(pos).isEnabled());
334
335 entryView.enabled.setOnCheckedChangeListener(new OnCheckedChangeListener() {
336
337 @Override
338 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
339 accounts.get(pos).setEnabled(isChecked);
340
341 try {
342 mCallbacks.getService().setAccountDetails(accounts.get(pos).getAccountID(), accounts.get(pos).getDetails());
343 } catch (RemoteException e) {
344 e.printStackTrace();
345 }
346 }
347 });
348
349 return rowView;
350 }
351
352 /*********************
353 * ViewHolder Pattern
354 *********************/
355 public class AccountView {
356 public TextView alias;
357 public TextView host;
358 public CheckBox enabled;
359 }
360
361 public void removeAll() {
362 accounts.clear();
363 notifyDataSetChanged();
364
365 }
366
367 public void addAll(ArrayList<Account> results) {
368 accounts.addAll(results);
369 notifyDataSetChanged();
370 }
371
372 /**
373 * Modify state of specific account
374 *
375 * @param accountState
376 */
377 public void updateAccount(Intent accountState) {
378 Log.i(TAG, "updateAccount");
379 String id = accountState.getStringExtra("Account");
380 String newState = accountState.getStringExtra("state");
381 accountState.getStringExtra("Account");
382
383 for (Account a : accounts) {
384 if (a.getAccountID().contentEquals(id)) {
385 a.setRegistered_state(newState);
386 notifyDataSetChanged();
387 return;
388 }
389 }
390
391 }
Alexandre Lision4cf78702013-10-16 13:43:23 -0400392
393 private String generateAccountOrder() {
394 String result = DEFAULT_ACCOUNT_ID + File.separator;
395 for (Account a : accounts) {
396 result += a.getAccountID() + File.separator;
397 }
398 return result;
399 }
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400400
Alexandre Savard68838112012-10-30 11:34:43 -0400401 }
Alexandre Savard2b370f02012-09-06 16:06:01 -0400402}