blob: eb51690275e18fc855bfdbc824c3d7c641f9111e [file] [log] [blame]
Alexandre Savard53d2ddd2012-09-30 22:04:40 -04001/*
2 * Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
3 *
4 * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Additional permission under GNU GPL version 3 section 7:
21 *
22 * If you modify this program, or any covered work, by linking or
23 * combining it with the OpenSSL project's OpenSSL library (or a
24 * modified version of that library), containing parts covered by the
25 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
26 * grants you additional permission to convey the resulting work.
27 * Corresponding Source for a non-source form of such a combination
28 * shall include the source code for the parts of OpenSSL used as well
29 * as that of the covered work.
30 */
31
32package com.savoirfairelinux.sflphone.client;
33
Alexandre Savardd103a762012-10-16 15:05:50 -040034import android.app.Activity;
Alexandre Savard44ec1622012-10-16 10:00:48 -040035import android.app.AlertDialog;
Alexandre Savardd103a762012-10-16 15:05:50 -040036import android.app.Dialog;
Alexandre Savard3bbb4792012-10-05 11:30:01 -040037import android.content.ComponentName;
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -040038import android.content.Context;
Alexandre Savard44ec1622012-10-16 10:00:48 -040039import android.content.DialogInterface;
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -040040import android.content.Intent;
Alexandre Savard3bbb4792012-10-05 11:30:01 -040041import android.content.ServiceConnection;
Alexandre Savard53d2ddd2012-09-30 22:04:40 -040042import android.os.Bundle;
Alexandre Savard3bbb4792012-10-05 11:30:01 -040043import android.os.IBinder;
Alexandre Savard8f27d162012-10-05 16:30:28 -040044import android.os.RemoteException;
Alexandre Savard5261f822012-10-05 15:23:56 -040045import android.preference.Preference;
Alexandre Savard53d2ddd2012-09-30 22:04:40 -040046import android.preference.PreferenceActivity;
Alexandre Savard3bbb4792012-10-05 11:30:01 -040047import android.preference.PreferenceManager;
48import android.preference.EditTextPreference;
Alexandre Savard20d0de02012-10-16 14:05:44 -040049import android.preference.CheckBoxPreference;
Alexandre Savard3bbb4792012-10-05 11:30:01 -040050import android.util.Log;
Alexandre Savard88bc5422012-10-16 11:17:23 -040051import android.view.Menu;
52import android.view.MenuItem;
53import android.view.Window;
Alexandre Savard53d2ddd2012-09-30 22:04:40 -040054
55import com.savoirfairelinux.sflphone.R;
Alexandre Savard3bbb4792012-10-05 11:30:01 -040056import com.savoirfairelinux.sflphone.service.SipService;
57import com.savoirfairelinux.sflphone.service.ISipService;
Alexandre Savard20d0de02012-10-16 14:05:44 -040058import com.savoirfairelinux.sflphone.utils.AccountDetail;
Alexandre Savard8f27d162012-10-05 16:30:28 -040059import com.savoirfairelinux.sflphone.utils.AccountDetailsHandler;
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -040060import com.savoirfairelinux.sflphone.utils.AccountDetailBasic;
61import com.savoirfairelinux.sflphone.utils.AccountDetailAdvanced;
62import com.savoirfairelinux.sflphone.utils.AccountDetailSrtp;
63import com.savoirfairelinux.sflphone.utils.AccountDetailTls;
Alexandre Savard53d2ddd2012-09-30 22:04:40 -040064
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -040065//import java.util.ArrayList;
66import java.util.EnumSet;
Alexandre Savard8f27d162012-10-05 16:30:28 -040067import java.util.HashMap;
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -040068import java.util.Set;
Alexandre Savard5261f822012-10-05 15:23:56 -040069
Alexandre Savard53d2ddd2012-09-30 22:04:40 -040070public class AccountCreationActivity extends PreferenceActivity
71{
Alexandre Savard3bbb4792012-10-05 11:30:01 -040072 static final String TAG = "SFLPhonePreferenceActivity";
73 private ISipService service;
74 private boolean mBound = false;
75 private PreferenceManager mPreferenceManager = null;
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -040076 private AccountDetailBasic basicDetails;
77 private AccountDetailAdvanced advancedDetails;
78 private AccountDetailSrtp srtpDetails;
79 private AccountDetailTls tlsDetails;
Alexandre Savardc64bfb42012-10-16 15:42:15 -040080 private MenuItem createAccountAction = null;
Alexandre Savard3bbb4792012-10-05 11:30:01 -040081
Alexandre Savard5261f822012-10-05 15:23:56 -040082 Preference.OnPreferenceChangeListener changeNewAccountPreferenceListener = new Preference.OnPreferenceChangeListener() {
83 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Savardc64bfb42012-10-16 15:42:15 -040084 preference.setSummary(getString(R.string.account_current_value_label) + (CharSequence)newValue);
85 AccountCreationActivity activity = (AccountCreationActivity)preference.getContext();
86 activity.validateAccountCreation();
Alexandre Savard5261f822012-10-05 15:23:56 -040087 return true;
88 }
89 };
90
Alexandre Savard20d0de02012-10-16 14:05:44 -040091 Preference.OnPreferenceChangeListener changeNewAccountTwoStateListener = new Preference.OnPreferenceChangeListener() {
92 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Savard20d0de02012-10-16 14:05:44 -040093 return true;
94 }
95 };
96
Alexandre Savard3bbb4792012-10-05 11:30:01 -040097 private ServiceConnection mConnection = new ServiceConnection() {
98 @Override
99 public void onServiceConnected(ComponentName className, IBinder binder) {
100 service = ISipService.Stub.asInterface(binder);
101 mBound = true;
102 Log.d(TAG, "Service connected");
103 }
104
105 @Override
106 public void onServiceDisconnected(ComponentName arg0) {
107 mBound = false;
108 Log.d(TAG, "Service disconnected");
109 }
110 };
111
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -0400112 public AccountCreationActivity()
113 {
114 basicDetails = new AccountDetailBasic();
115 advancedDetails = new AccountDetailAdvanced();
116 srtpDetails = new AccountDetailSrtp();
117 tlsDetails = new AccountDetailTls();
118 }
119
Alexandre Savard44ec1622012-10-16 10:00:48 -0400120 private AlertDialog createAlertDialog()
121 {
Alexandre Savardd103a762012-10-16 15:05:50 -0400122 Activity ownerActivity = this;
123 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
124 builder.setMessage("All parameters will be lost").setTitle("Account Creation")
Alexandre Savardfbf83ff2012-10-16 10:26:23 -0400125 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
Alexandre Savard44ec1622012-10-16 10:00:48 -0400126 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Savardd103a762012-10-16 15:05:50 -0400127 Activity activity = ((Dialog)dialog).getOwnerActivity();
128 activity.finish();
Alexandre Savard44ec1622012-10-16 10:00:48 -0400129 }
130 })
131 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
132 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Savardd103a762012-10-16 15:05:50 -0400133 /* Terminate with no action */
Alexandre Savard44ec1622012-10-16 10:00:48 -0400134 }
135 });
136
Alexandre Savardd103a762012-10-16 15:05:50 -0400137 AlertDialog alertDialog = builder.create();
138 alertDialog.setOwnerActivity(ownerActivity);
139
Alexandre Savardc64bfb42012-10-16 15:42:15 -0400140 return alertDialog;
141 }
142
143 public boolean validateAccountCreation()
144 {
145 createAccountAction.setEnabled(true);
146 return true;
Alexandre Savard44ec1622012-10-16 10:00:48 -0400147 }
148
Alexandre Savard53d2ddd2012-09-30 22:04:40 -0400149 @Override
150 protected void onCreate(Bundle savedInstanceState) {
151 super.onCreate(savedInstanceState);
Alexandre Savard53d2ddd2012-09-30 22:04:40 -0400152 addPreferencesFromResource(R.xml.account_creation_preferences);
Alexandre Savard8f27d162012-10-05 16:30:28 -0400153 mPreferenceManager = getPreferenceManager();
Alexandre Savard5261f822012-10-05 15:23:56 -0400154 }
155
156 @Override
Alexandre Savard88bc5422012-10-16 11:17:23 -0400157 public boolean onCreateOptionsMenu(Menu menu) {
Alexandre Savardc64bfb42012-10-16 15:42:15 -0400158 createAccountAction = menu.add("Create Account");
159 createAccountAction.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
160 createAccountAction.setEnabled(false);
Alexandre Savard88bc5422012-10-16 11:17:23 -0400161 return true;
162 }
163
164 @Override
165 public boolean onOptionsItemSelected(MenuItem item) {
Alexandre Savardc64bfb42012-10-16 15:42:15 -0400166 if(item.isEnabled()) {
167 createNewAccount();
168 finish();
169 }
Alexandre Savard88bc5422012-10-16 11:17:23 -0400170
171 return true;
172 }
173
Alexandre Savard20d0de02012-10-16 14:05:44 -0400174 private void addPreferenceListener(AccountDetail details) {
175 for(AccountDetail.PreferenceEntry p : details.getDetailValues()) {
176 Preference pref = mPreferenceManager.findPreference(p.mKey);
177 if(pref != null) {
178 Log.i(TAG, "FOUND " + p.mKey);
179 if(!p.isTwoState) {
180 pref.setOnPreferenceChangeListener(changeNewAccountPreferenceListener);
181 }
182 }
183 }
184 }
185
Alexandre Savard88bc5422012-10-16 11:17:23 -0400186 @Override
Alexandre Savard5261f822012-10-05 15:23:56 -0400187 protected void onStart() {
188 super.onStart();
Alexandre Savard5261f822012-10-05 15:23:56 -0400189
Alexandre Savard20d0de02012-10-16 14:05:44 -0400190 addPreferenceListener(basicDetails);
191 addPreferenceListener(advancedDetails);
192 addPreferenceListener(srtpDetails);
193 addPreferenceListener(tlsDetails);
194
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -0400195 if(!mBound) {
196 Log.i(TAG, "onStart: Binding service...");
197 Intent intent = new Intent(this, SipService.class);
198 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
Alexandre Savard5261f822012-10-05 15:23:56 -0400199 }
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400200 }
201
202 @Override
203 protected void onStop() {
204 super.onStop();
Alexandre Savardfbf83ff2012-10-16 10:26:23 -0400205
Alexandre Savard88bc5422012-10-16 11:17:23 -0400206 if(mBound) {
207 Log.i(TAG, "onStop: Unbinding service...");
208 unbindService(mConnection);
209 mBound = false;
Alexandre Savard94b0d092012-10-16 16:18:14 -0400210 }
Alexandre Savard88bc5422012-10-16 11:17:23 -0400211 }
212
213 @Override
214 public void onBackPressed() {
215
Alexandre Savardd103a762012-10-16 15:05:50 -0400216 AlertDialog dialog = createAlertDialog();
217 dialog.show();
Alexandre Savard88bc5422012-10-16 11:17:23 -0400218
Alexandre Savardd103a762012-10-16 15:05:50 -0400219 // super.onBackPressed();
Alexandre Savard88bc5422012-10-16 11:17:23 -0400220 }
221
Alexandre Savard94b0d092012-10-16 16:18:14 -0400222 private void updateAccountDetails(HashMap<String, String> accountDetails, AccountDetail det) {
223 for(AccountDetail.PreferenceEntry p : det.getDetailValues()) {
224 Preference pref = mPreferenceManager.findPreference(p.mKey);
225 if(pref != null) {
226 if(p.isTwoState) {
227 CheckBoxPreference boxPref = (CheckBoxPreference) pref;
228 accountDetails.put(p.mKey, boxPref.isChecked() ? "true" : "false");
229 }
230 else {
231 EditTextPreference textPref = (EditTextPreference) pref;
232 accountDetails.put(p.mKey, textPref.getText());
233 }
234 }
235 }
236 }
237
Alexandre Savard88bc5422012-10-16 11:17:23 -0400238 private void createNewAccount() {
239
Alexandre Savard8f27d162012-10-05 16:30:28 -0400240 HashMap<String, String> accountDetails = new HashMap<String, String>();
Alexandre Savard8f27d162012-10-05 16:30:28 -0400241
Alexandre Savard94b0d092012-10-16 16:18:14 -0400242 updateAccountDetails(accountDetails, basicDetails);
243 updateAccountDetails(accountDetails, advancedDetails);
244 updateAccountDetails(accountDetails, srtpDetails);
245 updateAccountDetails(accountDetails, tlsDetails);
246/*
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -0400247 for(String s : basicDetails.getDetailKeys()) {
248 EditTextPreference pref = (EditTextPreference) mPreferenceManager.findPreference(s);
249 if(pref != null) {
250 Log.i(TAG, "FOUND " + s + " " + pref.getText());
251 accountDetails.put(s, pref.getText());
252 }
253 }
254
255 for(String s : advancedDetails.getDetailKeys()) {
256 EditTextPreference pref = (EditTextPreference) mPreferenceManager.findPreference(s);
257 if(pref != null) {
258 Log.i(TAG, "FOUND " + s + " " + pref.getText());
259 accountDetails.put(s, pref.getText());
260 }
261 }
262
263 for(String s : srtpDetails.getDetailKeys()) {
264 EditTextPreference pref = (EditTextPreference) mPreferenceManager.findPreference(s);
265 if(pref != null) {
266 Log.i(TAG, "FOUND " + s + " " + pref.getText());
267 accountDetails.put(s, pref.getText());
268 }
269 }
270
271 for(String s : tlsDetails.getDetailKeys()) {
272 EditTextPreference pref = (EditTextPreference) mPreferenceManager.findPreference(s);
273 if(pref != null) {
274 Log.i(TAG, "FOUND " + s + " " + pref.getText());
275 accountDetails.put(s, pref.getText());
276 }
277 }
Alexandre Savard94b0d092012-10-16 16:18:14 -0400278*/
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -0400279
Alexandre Savard8f27d162012-10-05 16:30:28 -0400280 try {
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -0400281 Log.i(TAG, "ADD ACCOUNT");
Alexandre Savard8f27d162012-10-05 16:30:28 -0400282 service.addAccount(accountDetails);
283 } catch (RemoteException e) {
284 Log.e(TAG, "Cannot call service method", e);
285 }
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400286 }
Alexandre Savard53d2ddd2012-09-30 22:04:40 -0400287}