blob: 8602a751ab80d9532ab1e7dbb0f7ee21ae8a0a22 [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 Lision3ccccf02013-10-07 14:10:46 -040045import org.sflphone.interfaces.AccountsInterface;
46import org.sflphone.loaders.AccountsLoader;
47import org.sflphone.loaders.LoaderConstants;
Alexandre Lision064e1e02013-10-01 16:18:42 -040048import org.sflphone.model.Account;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040049import org.sflphone.receivers.AccountsReceiver;
Alexandre Lision064e1e02013-10-01 16:18:42 -040050import org.sflphone.service.ConfigurationManagerCallback;
51import org.sflphone.service.ISipService;
Alexandre Lision7f0bba52013-10-16 14:43:11 -040052import org.sflphone.views.dragsortlv.DragSortListView;
Alexandre Lision064e1e02013-10-01 16:18:42 -040053
Alexandre Savard9a93f062012-09-11 10:42:43 -040054import android.app.Activity;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040055import android.app.ListFragment;
56import android.app.LoaderManager.LoaderCallbacks;
Alexandre Savardae992932012-10-17 10:26:12 -040057import android.content.Context;
Alexandre Savard53d2ddd2012-09-30 22:04:40 -040058import android.content.Intent;
Alexandre Savard4f11d7a2012-10-18 13:15:36 -040059import android.content.IntentFilter;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040060import android.content.Loader;
alisionf76de3b2013-04-16 15:35:22 -040061import android.os.Bundle;
62import android.os.RemoteException;
Alexandre Savard2b370f02012-09-06 16:06:01 -040063import android.util.Log;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040064import android.view.LayoutInflater;
Alexandre Lisiona764c682013-09-09 10:02:07 -040065import android.view.Menu;
66import android.view.MenuInflater;
67import android.view.MenuItem;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040068import android.view.View;
69import android.view.ViewGroup;
70import android.widget.AdapterView;
71import android.widget.AdapterView.OnItemClickListener;
72import android.widget.BaseAdapter;
73import android.widget.CheckBox;
74import android.widget.CompoundButton;
75import android.widget.CompoundButton.OnCheckedChangeListener;
76import android.widget.TextView;
Alexandre Savard2b370f02012-09-06 16:06:01 -040077
Alexandre Lision3ccccf02013-10-07 14:10:46 -040078public class AccountManagementFragment extends ListFragment implements LoaderCallbacks<ArrayList<Account>>, AccountsInterface {
Alexandre Savard2b370f02012-09-06 16:06:01 -040079 static final String TAG = "AccountManagementFragment";
Alexandre Savarda6c0d992012-09-28 09:24:13 -040080 static final String DEFAULT_ACCOUNT_ID = "IP2IP";
Alexandre Savard53d2ddd2012-09-30 22:04:40 -040081 static final int ACCOUNT_CREATE_REQUEST = 1;
Alexandre Savard68838112012-10-30 11:34:43 -040082 static final int ACCOUNT_EDIT_REQUEST = 2;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040083 AccountsReceiver accountReceiver;
84 AccountsAdapter mAdapter;
Alexandre Savardae992932012-10-17 10:26:12 -040085
Alexandre Lision4cf78702013-10-16 13:43:23 -040086 private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
87 @Override
88 public void drop(int from, int to) {
89 if (from != to) {
Alexandre Lision4cf78702013-10-16 13:43:23 -040090 Account item = mAdapter.getItem(from);
91 mAdapter.remove(item);
92 mAdapter.insert(item, to);
Alexandre Lision4cf78702013-10-16 13:43:23 -040093 try {
94 mCallbacks.getService().setAccountOrder(mAdapter.generateAccountOrder());
95 } catch (RemoteException e) {
96 e.printStackTrace();
97 }
98 }
99 }
100
101
102 };
103
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400104 private Callbacks mCallbacks = sDummyCallbacks;
105 private static Callbacks sDummyCallbacks = new Callbacks() {
106
107 @Override
108 public ISipService getService() {
109 return null;
110 }
111 };
112
113 public interface Callbacks {
114
115 public ISipService getService();
116
117 }
Alexandre Savardae992932012-10-17 10:26:12 -0400118
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400119 @Override
120 public void onAttach(Activity activity) {
121 super.onAttach(activity);
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400122 if (!(activity instanceof Callbacks)) {
123 throw new IllegalStateException("Activity must implement fragment's callbacks.");
124 }
125
126 mCallbacks = (Callbacks) activity;
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400127 getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400128 }
Alexandre Savard12dc3ac2012-09-27 11:17:39 -0400129
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400130 @Override
131 public void onDetach() {
132 super.onDetach();
133 mCallbacks = sDummyCallbacks;
alisiond9e29442013-04-17 16:10:18 -0400134 }
Alexandre Savard2b370f02012-09-06 16:06:01 -0400135
136 @Override
alisiond9e29442013-04-17 16:10:18 -0400137 public void onCreate(Bundle savedInstanceState) {
Alexandre Savard2b370f02012-09-06 16:06:01 -0400138 super.onCreate(savedInstanceState);
139
Alexandre Savard9a93f062012-09-11 10:42:43 -0400140 Log.i(TAG, "Create Account Management Fragment");
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400141 mAdapter = new AccountsAdapter(getActivity(), new ArrayList<Account>());
Alexandre Lisiona764c682013-09-09 10:02:07 -0400142 this.setHasOptionsMenu(true);
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400143 accountReceiver = new AccountsReceiver(this);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400144
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400145 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400146
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400147 @Override
148 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
Alexandre Lision4cf78702013-10-16 13:43:23 -0400149 View inflatedView = inflater.inflate(R.layout.frag_accounts_list, parent, false);
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400150 setListAdapter(mAdapter);
Alexandre Lision4cf78702013-10-16 13:43:23 -0400151
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400152 return inflatedView;
153 }
154
155 @Override
Alexandre Lision4cf78702013-10-16 13:43:23 -0400156 public DragSortListView getListView() {
157 return (DragSortListView) super.getListView();
158 }
159
160 @Override
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400161 public void onViewCreated(View view, Bundle savedInstanceState) {
162 super.onViewCreated(view, savedInstanceState);
Alexandre Lision4cf78702013-10-16 13:43:23 -0400163
164 DragSortListView list = getListView();
165 list.setDropListener(onDrop);
166 list.setOnItemClickListener(new OnItemClickListener() {
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400167
168 @Override
169 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400170 launchAccountEditActivity(mAdapter.getItem(pos));
Emeric Vigier0038a612012-11-06 18:51:19 -0500171 }
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400172 });
Alexandre Lision40954dc2013-10-09 15:24:03 -0400173
Alexandre Lision4cf78702013-10-16 13:43:23 -0400174 list.setEmptyView(view.findViewById(android.R.id.empty));
Alexandre Savard2b370f02012-09-06 16:06:01 -0400175 }
176
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400177 @Override
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400178 public void onPause() {
179 super.onPause();
180 getActivity().unregisterReceiver(accountReceiver);
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400181 }
182
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400183 public void onResume() {
184 super.onResume();
185 IntentFilter intentFilter2 = new IntentFilter();
186 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
187 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
188 getActivity().registerReceiver(accountReceiver, intentFilter2);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400189 getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
Alexandre Lision4cf78702013-10-16 13:43:23 -0400190
alisiona4325152013-04-19 11:10:03 -0400191 }
alision73424b62013-04-26 11:49:18 -0400192
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400193 @Override
194 public Loader<ArrayList<Account>> onCreateLoader(int id, Bundle args) {
195 AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService());
Alexandre Lision4cf78702013-10-16 13:43:23 -0400196
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400197 l.forceLoad();
Alexandre Lision4cf78702013-10-16 13:43:23 -0400198
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400199 return l;
200 }
201
202 @Override
203 public void onLoadFinished(Loader<ArrayList<Account>> loader, ArrayList<Account> results) {
204 mAdapter.removeAll();
205 mAdapter.addAll(results);
206 }
207
208 @Override
209 public void onLoaderReset(Loader<ArrayList<Account>> arg0) {
210 // TODO Auto-generated method stub
211
212 }
213
Alexandre Lisiona764c682013-09-09 10:02:07 -0400214 @Override
215 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
216 super.onCreateOptionsMenu(m, inf);
217 inf.inflate(R.menu.account_creation, m);
218 }
219
220 @Override
221 public boolean onOptionsItemSelected(MenuItem item) {
222 super.onOptionsItemSelected(item);
223 switch (item.getItemId()) {
224 case R.id.menuitem_create:
225 Intent intent = new Intent().setClass(getActivity(), AccountWizard.class);
226 startActivityForResult(intent, ACCOUNT_CREATE_REQUEST);
227 break;
228 }
229
230 return true;
231 }
232
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400233 private void launchAccountEditActivity(Account acc) {
Alexandre Savard68838112012-10-30 11:34:43 -0400234 Log.i(TAG, "Launch account edit activity");
Alexandre Savard833616f2012-10-30 16:02:30 -0400235
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400236 Intent intent = new Intent().setClass(getActivity(), AccountEditionActivity.class);
237 Bundle bundle = new Bundle();
238 bundle.putString("AccountID", acc.getAccountID());
Alexandre Lisionafd40e42013-10-15 13:48:37 -0400239 bundle.putSerializable(AccountDetailBasic.BUNDLE_TAG, acc.getBasicDetails().getDetailsHashMap());
240 bundle.putSerializable(AccountDetailAdvanced.BUNDLE_TAG, acc.getAdvancedDetails().getDetailsHashMap());
241 bundle.putSerializable(AccountDetailSrtp.BUNDLE_TAG, acc.getSrtpDetails().getDetailsHashMap());
242 bundle.putSerializable(AccountDetailTls.BUNDLE_TAG, acc.getTlsDetails().getDetailsHashMap());
Alexandre Savard833616f2012-10-30 16:02:30 -0400243
244 intent.putExtras(bundle);
245
Alexandre Savard68838112012-10-30 11:34:43 -0400246 startActivityForResult(intent, ACCOUNT_EDIT_REQUEST);
247 }
248
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400249 @Override
250 public void accountsChanged() {
251 if (getActivity() != null)
252 getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
Alexandre Savard52a72522012-09-27 16:40:13 -0400253 }
254
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400255 @Override
256 public void accountStateChanged(Intent accountState) {
257 mAdapter.updateAccount(accountState);
Alexandre Savard9a93f062012-09-11 10:42:43 -0400258 }
259
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400260 /**
261 *
262 * Adapter for accounts List
Alexandre Lision40954dc2013-10-09 15:24:03 -0400263 *
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400264 * @author lisional
Alexandre Lision40954dc2013-10-09 15:24:03 -0400265 *
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400266 */
267 public class AccountsAdapter extends BaseAdapter {
Alexandre Savard52a72522012-09-27 16:40:13 -0400268
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400269 // private static final String TAG = AccountSelectionAdapter.class.getSimpleName();
Alexandre Savard70982012012-09-27 17:15:50 -0400270
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400271 ArrayList<Account> accounts;
272 Context mContext;
Alexandre Savard70982012012-09-27 17:15:50 -0400273
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400274 public AccountsAdapter(Context cont, ArrayList<Account> newList) {
275 super();
276 accounts = newList;
277 mContext = cont;
Alexandre Savarde990d052012-10-18 14:43:09 -0400278 }
alisiond9e29442013-04-17 16:10:18 -0400279
Alexandre Lision4cf78702013-10-16 13:43:23 -0400280 public void insert(Account item, int to) {
281 accounts.add(to, item);
282 notifyDataSetChanged();
283 }
284
285 public void remove(Account item) {
286 accounts.remove(item);
287 notifyDataSetChanged();
288 }
289
290 @Override
291 public boolean hasStableIds(){
292 return true;
293 }
294
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400295 @Override
296 public int getCount() {
297 return accounts.size();
298 }
Alexandre Savard70982012012-09-27 17:15:50 -0400299
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400300 @Override
301 public Account getItem(int pos) {
302 return accounts.get(pos);
303 }
Alexandre Savard68838112012-10-30 11:34:43 -0400304
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400305 @Override
306 public long getItemId(int pos) {
307 return 0;
308 }
Alexandre Savard68838112012-10-30 11:34:43 -0400309
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400310 @Override
311 public View getView(final int pos, View convertView, ViewGroup parent) {
312 View rowView = convertView;
313 AccountView entryView = null;
Alexandre Savard68838112012-10-30 11:34:43 -0400314
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400315 if (rowView == null) {
316 LayoutInflater inflater = LayoutInflater.from(mContext);
317 rowView = inflater.inflate(R.layout.item_account_pref, null);
alisiond9e29442013-04-17 16:10:18 -0400318
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400319 entryView = new AccountView();
320 entryView.alias = (TextView) rowView.findViewById(R.id.account_alias);
321 entryView.host = (TextView) rowView.findViewById(R.id.account_host);
322 entryView.enabled = (CheckBox) rowView.findViewById(R.id.account_checked);
323 rowView.setTag(entryView);
324 } else {
325 entryView = (AccountView) rowView.getTag();
326 }
327
328 entryView.alias.setText(accounts.get(pos).getAlias());
329 entryView.host.setText(accounts.get(pos).getHost() + " - " + accounts.get(pos).getRegistered_state());
330 entryView.enabled.setChecked(accounts.get(pos).isEnabled());
331
332 entryView.enabled.setOnCheckedChangeListener(new OnCheckedChangeListener() {
333
334 @Override
335 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
336 accounts.get(pos).setEnabled(isChecked);
337
338 try {
339 mCallbacks.getService().setAccountDetails(accounts.get(pos).getAccountID(), accounts.get(pos).getDetails());
340 } catch (RemoteException e) {
341 e.printStackTrace();
342 }
343 }
344 });
345
346 return rowView;
347 }
348
349 /*********************
350 * ViewHolder Pattern
351 *********************/
352 public class AccountView {
353 public TextView alias;
354 public TextView host;
355 public CheckBox enabled;
356 }
357
358 public void removeAll() {
359 accounts.clear();
360 notifyDataSetChanged();
361
362 }
363
364 public void addAll(ArrayList<Account> results) {
365 accounts.addAll(results);
366 notifyDataSetChanged();
367 }
368
369 /**
370 * Modify state of specific account
371 *
372 * @param accountState
373 */
374 public void updateAccount(Intent accountState) {
375 Log.i(TAG, "updateAccount");
376 String id = accountState.getStringExtra("Account");
377 String newState = accountState.getStringExtra("state");
378 accountState.getStringExtra("Account");
379
380 for (Account a : accounts) {
381 if (a.getAccountID().contentEquals(id)) {
382 a.setRegistered_state(newState);
383 notifyDataSetChanged();
384 return;
385 }
386 }
387
388 }
Alexandre Lision4cf78702013-10-16 13:43:23 -0400389
390 private String generateAccountOrder() {
391 String result = DEFAULT_ACCOUNT_ID + File.separator;
392 for (Account a : accounts) {
393 result += a.getAccountID() + File.separator;
394 }
395 return result;
396 }
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400397
Alexandre Savard68838112012-10-30 11:34:43 -0400398 }
Alexandre Savard2b370f02012-09-06 16:06:01 -0400399}