blob: 4b19b7eaa9c63560e72af38387f679ca20254681 [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
35import java.util.ArrayList;
Alexandre Savard2b370f02012-09-06 16:06:01 -040036
Alexandre Lision064e1e02013-10-01 16:18:42 -040037import org.sflphone.R;
Alexandre Lision064e1e02013-10-01 16:18:42 -040038import org.sflphone.account.AccountDetailAdvanced;
39import org.sflphone.account.AccountDetailBasic;
40import org.sflphone.account.AccountDetailSrtp;
41import org.sflphone.account.AccountDetailTls;
Alexandre Lision8b9d8e82013-10-04 09:21:27 -040042import org.sflphone.client.AccountEditionActivity;
Alexandre Lision064e1e02013-10-01 16:18:42 -040043import org.sflphone.client.AccountWizard;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040044import org.sflphone.interfaces.AccountsInterface;
45import org.sflphone.loaders.AccountsLoader;
46import org.sflphone.loaders.LoaderConstants;
Alexandre Lision064e1e02013-10-01 16:18:42 -040047import org.sflphone.model.Account;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040048import org.sflphone.receivers.AccountsReceiver;
Alexandre Lision064e1e02013-10-01 16:18:42 -040049import org.sflphone.service.ConfigurationManagerCallback;
50import org.sflphone.service.ISipService;
51
Alexandre Savard9a93f062012-09-11 10:42:43 -040052import android.app.Activity;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040053import android.app.ListFragment;
54import android.app.LoaderManager.LoaderCallbacks;
Alexandre Savardae992932012-10-17 10:26:12 -040055import android.content.Context;
Alexandre Savard53d2ddd2012-09-30 22:04:40 -040056import android.content.Intent;
Alexandre Savard4f11d7a2012-10-18 13:15:36 -040057import android.content.IntentFilter;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040058import android.content.Loader;
alisionf76de3b2013-04-16 15:35:22 -040059import android.os.Bundle;
60import android.os.RemoteException;
Alexandre Savard2b370f02012-09-06 16:06:01 -040061import android.util.Log;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040062import android.view.LayoutInflater;
Alexandre Lisiona764c682013-09-09 10:02:07 -040063import android.view.Menu;
64import android.view.MenuInflater;
65import android.view.MenuItem;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040066import android.view.View;
67import android.view.ViewGroup;
68import android.widget.AdapterView;
69import android.widget.AdapterView.OnItemClickListener;
70import android.widget.BaseAdapter;
71import android.widget.CheckBox;
72import android.widget.CompoundButton;
73import android.widget.CompoundButton.OnCheckedChangeListener;
74import android.widget.TextView;
Alexandre Savard2b370f02012-09-06 16:06:01 -040075
Alexandre Lision3ccccf02013-10-07 14:10:46 -040076public class AccountManagementFragment extends ListFragment implements LoaderCallbacks<ArrayList<Account>>, AccountsInterface {
Alexandre Savard2b370f02012-09-06 16:06:01 -040077 static final String TAG = "AccountManagementFragment";
Alexandre Savarda6c0d992012-09-28 09:24:13 -040078 static final String DEFAULT_ACCOUNT_ID = "IP2IP";
Alexandre Savard53d2ddd2012-09-30 22:04:40 -040079 static final int ACCOUNT_CREATE_REQUEST = 1;
Alexandre Savard68838112012-10-30 11:34:43 -040080 static final int ACCOUNT_EDIT_REQUEST = 2;
Alexandre Lision3ccccf02013-10-07 14:10:46 -040081 AccountsReceiver accountReceiver;
82 AccountsAdapter mAdapter;
Alexandre Savardae992932012-10-17 10:26:12 -040083
Alexandre Lision3ccccf02013-10-07 14:10:46 -040084 private Callbacks mCallbacks = sDummyCallbacks;
85 private static Callbacks sDummyCallbacks = new Callbacks() {
86
87 @Override
88 public ISipService getService() {
89 return null;
90 }
91 };
92
93 public interface Callbacks {
94
95 public ISipService getService();
96
97 }
Alexandre Savardae992932012-10-17 10:26:12 -040098
Emeric Vigier1f1ced32012-11-02 16:56:32 -040099 @Override
100 public void onAttach(Activity activity) {
101 super.onAttach(activity);
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400102 if (!(activity instanceof Callbacks)) {
103 throw new IllegalStateException("Activity must implement fragment's callbacks.");
104 }
105
106 mCallbacks = (Callbacks) activity;
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400107 getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
Emeric Vigier1f1ced32012-11-02 16:56:32 -0400108 }
Alexandre Savard12dc3ac2012-09-27 11:17:39 -0400109
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400110 @Override
111 public void onDetach() {
112 super.onDetach();
113 mCallbacks = sDummyCallbacks;
alisiond9e29442013-04-17 16:10:18 -0400114 }
Alexandre Savard2b370f02012-09-06 16:06:01 -0400115
116 @Override
alisiond9e29442013-04-17 16:10:18 -0400117 public void onCreate(Bundle savedInstanceState) {
Alexandre Savard2b370f02012-09-06 16:06:01 -0400118 super.onCreate(savedInstanceState);
119
Alexandre Savard9a93f062012-09-11 10:42:43 -0400120 Log.i(TAG, "Create Account Management Fragment");
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400121 mAdapter = new AccountsAdapter(getActivity(), new ArrayList<Account>());
Alexandre Lisiona764c682013-09-09 10:02:07 -0400122 this.setHasOptionsMenu(true);
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400123 accountReceiver = new AccountsReceiver(this);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400124
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400125 }
Alexandre Lisiona764c682013-09-09 10:02:07 -0400126
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400127 @Override
128 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
129 View inflatedView = inflater.inflate(android.R.layout.list_content, parent, false);
130 setListAdapter(mAdapter);
131 return inflatedView;
132 }
133
134 @Override
135 public void onViewCreated(View view, Bundle savedInstanceState) {
136 super.onViewCreated(view, savedInstanceState);
137 getListView().setOnItemClickListener(new OnItemClickListener() {
138
139 @Override
140 public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400141 launchAccountEditActivity(mAdapter.getItem(pos));
Emeric Vigier0038a612012-11-06 18:51:19 -0500142 }
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400143 });
Alexandre Lision40954dc2013-10-09 15:24:03 -0400144
145 setEmptyText(getResources().getString(R.string.empty_account_list));
Alexandre Savard2b370f02012-09-06 16:06:01 -0400146 }
147
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400148 @Override
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400149 public void onPause() {
150 super.onPause();
151 getActivity().unregisterReceiver(accountReceiver);
Alexandre Savard8b7d4332012-09-30 20:02:11 -0400152 }
153
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400154 public void onResume() {
155 super.onResume();
156 IntentFilter intentFilter2 = new IntentFilter();
157 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNT_STATE_CHANGED);
158 intentFilter2.addAction(ConfigurationManagerCallback.ACCOUNTS_CHANGED);
159 getActivity().registerReceiver(accountReceiver, intentFilter2);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400160
161 getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
162
alisiona4325152013-04-19 11:10:03 -0400163 }
alision73424b62013-04-26 11:49:18 -0400164
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400165 @Override
166 public Loader<ArrayList<Account>> onCreateLoader(int id, Bundle args) {
167 AccountsLoader l = new AccountsLoader(getActivity(), mCallbacks.getService());
168 l.forceLoad();
169 return l;
170 }
171
172 @Override
173 public void onLoadFinished(Loader<ArrayList<Account>> loader, ArrayList<Account> results) {
174 mAdapter.removeAll();
175 mAdapter.addAll(results);
176 }
177
178 @Override
179 public void onLoaderReset(Loader<ArrayList<Account>> arg0) {
180 // TODO Auto-generated method stub
181
182 }
183
Alexandre Lisiona764c682013-09-09 10:02:07 -0400184 @Override
185 public void onCreateOptionsMenu(Menu m, MenuInflater inf) {
186 super.onCreateOptionsMenu(m, inf);
187 inf.inflate(R.menu.account_creation, m);
188 }
189
190 @Override
191 public boolean onOptionsItemSelected(MenuItem item) {
192 super.onOptionsItemSelected(item);
193 switch (item.getItemId()) {
194 case R.id.menuitem_create:
195 Intent intent = new Intent().setClass(getActivity(), AccountWizard.class);
196 startActivityForResult(intent, ACCOUNT_CREATE_REQUEST);
197 break;
198 }
199
200 return true;
201 }
202
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400203 private void launchAccountEditActivity(Account acc) {
Alexandre Savard68838112012-10-30 11:34:43 -0400204 Log.i(TAG, "Launch account edit activity");
Alexandre Savard833616f2012-10-30 16:02:30 -0400205
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400206 Intent intent = new Intent().setClass(getActivity(), AccountEditionActivity.class);
207 Bundle bundle = new Bundle();
208 bundle.putString("AccountID", acc.getAccountID());
209 bundle.putStringArrayList(AccountDetailBasic.BUNDLE_TAG, acc.getBasicDetails().getValuesOnly());
210 bundle.putStringArrayList(AccountDetailAdvanced.BUNDLE_TAG, acc.getAdvancedDetails().getValuesOnly());
211 bundle.putStringArrayList(AccountDetailSrtp.BUNDLE_TAG, acc.getSrtpDetails().getValuesOnly());
212 bundle.putStringArrayList(AccountDetailTls.BUNDLE_TAG, acc.getTlsDetails().getValuesOnly());
Alexandre Savard833616f2012-10-30 16:02:30 -0400213
214 intent.putExtras(bundle);
215
Alexandre Savard68838112012-10-30 11:34:43 -0400216 startActivityForResult(intent, ACCOUNT_EDIT_REQUEST);
217 }
218
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400219 @Override
220 public void accountsChanged() {
221 if (getActivity() != null)
222 getActivity().getLoaderManager().restartLoader(LoaderConstants.ACCOUNTS_LOADER, null, this);
Alexandre Savard52a72522012-09-27 16:40:13 -0400223 }
224
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400225 @Override
226 public void accountStateChanged(Intent accountState) {
227 mAdapter.updateAccount(accountState);
Alexandre Savard9a93f062012-09-11 10:42:43 -0400228 }
229
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400230 /**
231 *
232 * Adapter for accounts List
Alexandre Lision40954dc2013-10-09 15:24:03 -0400233 *
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400234 * @author lisional
Alexandre Lision40954dc2013-10-09 15:24:03 -0400235 *
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400236 */
237 public class AccountsAdapter extends BaseAdapter {
Alexandre Savard52a72522012-09-27 16:40:13 -0400238
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400239 // private static final String TAG = AccountSelectionAdapter.class.getSimpleName();
Alexandre Savard70982012012-09-27 17:15:50 -0400240
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400241 ArrayList<Account> accounts;
242 Context mContext;
Alexandre Savard70982012012-09-27 17:15:50 -0400243
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400244 public AccountsAdapter(Context cont, ArrayList<Account> newList) {
245 super();
246 accounts = newList;
247 mContext = cont;
Alexandre Savarde990d052012-10-18 14:43:09 -0400248 }
alisiond9e29442013-04-17 16:10:18 -0400249
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400250 @Override
251 public int getCount() {
252 return accounts.size();
253 }
Alexandre Savard70982012012-09-27 17:15:50 -0400254
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400255 @Override
256 public Account getItem(int pos) {
257 return accounts.get(pos);
258 }
Alexandre Savard68838112012-10-30 11:34:43 -0400259
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400260 @Override
261 public long getItemId(int pos) {
262 return 0;
263 }
Alexandre Savard68838112012-10-30 11:34:43 -0400264
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400265 @Override
266 public View getView(final int pos, View convertView, ViewGroup parent) {
267 View rowView = convertView;
268 AccountView entryView = null;
Alexandre Savard68838112012-10-30 11:34:43 -0400269
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400270 if (rowView == null) {
271 LayoutInflater inflater = LayoutInflater.from(mContext);
272 rowView = inflater.inflate(R.layout.item_account_pref, null);
alisiond9e29442013-04-17 16:10:18 -0400273
Alexandre Lision3ccccf02013-10-07 14:10:46 -0400274 entryView = new AccountView();
275 entryView.alias = (TextView) rowView.findViewById(R.id.account_alias);
276 entryView.host = (TextView) rowView.findViewById(R.id.account_host);
277 entryView.enabled = (CheckBox) rowView.findViewById(R.id.account_checked);
278 rowView.setTag(entryView);
279 } else {
280 entryView = (AccountView) rowView.getTag();
281 }
282
283 entryView.alias.setText(accounts.get(pos).getAlias());
284 entryView.host.setText(accounts.get(pos).getHost() + " - " + accounts.get(pos).getRegistered_state());
285 entryView.enabled.setChecked(accounts.get(pos).isEnabled());
286
287 entryView.enabled.setOnCheckedChangeListener(new OnCheckedChangeListener() {
288
289 @Override
290 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
291 accounts.get(pos).setEnabled(isChecked);
292
293 try {
294 mCallbacks.getService().setAccountDetails(accounts.get(pos).getAccountID(), accounts.get(pos).getDetails());
295 } catch (RemoteException e) {
296 e.printStackTrace();
297 }
298 }
299 });
300
301 return rowView;
302 }
303
304 /*********************
305 * ViewHolder Pattern
306 *********************/
307 public class AccountView {
308 public TextView alias;
309 public TextView host;
310 public CheckBox enabled;
311 }
312
313 public void removeAll() {
314 accounts.clear();
315 notifyDataSetChanged();
316
317 }
318
319 public void addAll(ArrayList<Account> results) {
320 accounts.addAll(results);
321 notifyDataSetChanged();
322 }
323
324 /**
325 * Modify state of specific account
326 *
327 * @param accountState
328 */
329 public void updateAccount(Intent accountState) {
330 Log.i(TAG, "updateAccount");
331 String id = accountState.getStringExtra("Account");
332 String newState = accountState.getStringExtra("state");
333 accountState.getStringExtra("Account");
334
335 for (Account a : accounts) {
336 if (a.getAccountID().contentEquals(id)) {
337 a.setRegistered_state(newState);
338 notifyDataSetChanged();
339 return;
340 }
341 }
342
343 }
344
Alexandre Savard68838112012-10-30 11:34:43 -0400345 }
Alexandre Savard2b370f02012-09-06 16:06:01 -0400346}