blob: ce260f00980405bcc641b001878c0ffd7234a532 [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 Savardd0aa7912012-10-16 17:46:50 -040065import java.util.ArrayList;
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -040066import 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 Savardd0aa7912012-10-16 17:46:50 -040081 private ArrayList<String> requiredFields = null;
Alexandre Savard3bbb4792012-10-05 11:30:01 -040082
Alexandre Savard5261f822012-10-05 15:23:56 -040083 Preference.OnPreferenceChangeListener changeNewAccountPreferenceListener = new Preference.OnPreferenceChangeListener() {
84 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Savardc64bfb42012-10-16 15:42:15 -040085 preference.setSummary(getString(R.string.account_current_value_label) + (CharSequence)newValue);
Alexandre Savard5261f822012-10-05 15:23:56 -040086 return true;
87 }
88 };
89
Alexandre Savard20d0de02012-10-16 14:05:44 -040090 Preference.OnPreferenceChangeListener changeNewAccountTwoStateListener = new Preference.OnPreferenceChangeListener() {
91 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Savard20d0de02012-10-16 14:05:44 -040092 return true;
93 }
94 };
95
Alexandre Savard3bbb4792012-10-05 11:30:01 -040096 private ServiceConnection mConnection = new ServiceConnection() {
97 @Override
98 public void onServiceConnected(ComponentName className, IBinder binder) {
99 service = ISipService.Stub.asInterface(binder);
100 mBound = true;
101 Log.d(TAG, "Service connected");
102 }
103
104 @Override
105 public void onServiceDisconnected(ComponentName arg0) {
106 mBound = false;
107 Log.d(TAG, "Service disconnected");
108 }
109 };
110
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -0400111 public AccountCreationActivity()
112 {
113 basicDetails = new AccountDetailBasic();
114 advancedDetails = new AccountDetailAdvanced();
115 srtpDetails = new AccountDetailSrtp();
116 tlsDetails = new AccountDetailTls();
Alexandre Savardd0aa7912012-10-16 17:46:50 -0400117
118 requiredFields = new ArrayList<String>();
119
120 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_ALIAS);
121 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_HOSTNAME);
122 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME);
123 requiredFields.add(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD);
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -0400124 }
125
Alexandre Savard44ec1622012-10-16 10:00:48 -0400126 private AlertDialog createAlertDialog()
127 {
Alexandre Savardd103a762012-10-16 15:05:50 -0400128 Activity ownerActivity = this;
129 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
130 builder.setMessage("All parameters will be lost").setTitle("Account Creation")
Alexandre Savardfbf83ff2012-10-16 10:26:23 -0400131 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
Alexandre Savard44ec1622012-10-16 10:00:48 -0400132 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Savardd103a762012-10-16 15:05:50 -0400133 Activity activity = ((Dialog)dialog).getOwnerActivity();
134 activity.finish();
Alexandre Savard44ec1622012-10-16 10:00:48 -0400135 }
136 })
137 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
138 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Savardd103a762012-10-16 15:05:50 -0400139 /* Terminate with no action */
Alexandre Savard44ec1622012-10-16 10:00:48 -0400140 }
141 });
142
Alexandre Savardd103a762012-10-16 15:05:50 -0400143 AlertDialog alertDialog = builder.create();
144 alertDialog.setOwnerActivity(ownerActivity);
145
Alexandre Savardc64bfb42012-10-16 15:42:15 -0400146 return alertDialog;
147 }
148
Alexandre Savardd0aa7912012-10-16 17:46:50 -0400149 private AlertDialog createCouldNotValidateDialog(ArrayList<String> missingValue)
Alexandre Savardc64bfb42012-10-16 15:42:15 -0400150 {
Alexandre Savardd0aa7912012-10-16 17:46:50 -0400151 String message = "The following parameters are missing:";
152
153 for(String s : missingValue)
154 message += "\n - " + s;
155
156 Activity ownerActivity = this;
157 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
158 builder.setMessage(message).setTitle("Missing Parameters")
159 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
160 public void onClick(DialogInterface dialog, int whichButton) {
161 /* Nothing to be done */
162 }
163 });
164
165 AlertDialog alertDialog = builder.create();
166 return alertDialog;
167 }
168
169 public boolean validateAccountCreation(ArrayList<String> missingValue)
170 {
171 boolean valid = true;
172
173 for(String s : requiredFields) {
174 EditTextPreference pref = (EditTextPreference)mPreferenceManager.findPreference(s);
175 Log.i(TAG, "Looking for " + s);
176 if(pref.getText().isEmpty()) {
177 Log.i(TAG, " INVALIDATED " + s + " " + pref.getText() + ";");
178 valid = false;
179 missingValue.add(pref.getTitle().toString());
180 }
181 }
182
183 return valid;
Alexandre Savard44ec1622012-10-16 10:00:48 -0400184 }
185
Alexandre Savard53d2ddd2012-09-30 22:04:40 -0400186 @Override
187 protected void onCreate(Bundle savedInstanceState) {
188 super.onCreate(savedInstanceState);
Alexandre Savard53d2ddd2012-09-30 22:04:40 -0400189 addPreferencesFromResource(R.xml.account_creation_preferences);
Alexandre Savard8f27d162012-10-05 16:30:28 -0400190 mPreferenceManager = getPreferenceManager();
Alexandre Savard5261f822012-10-05 15:23:56 -0400191 }
192
193 @Override
Alexandre Savard88bc5422012-10-16 11:17:23 -0400194 public boolean onCreateOptionsMenu(Menu menu) {
Alexandre Savardc64bfb42012-10-16 15:42:15 -0400195 createAccountAction = menu.add("Create Account");
196 createAccountAction.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
Alexandre Savard88bc5422012-10-16 11:17:23 -0400197 return true;
198 }
199
200 @Override
201 public boolean onOptionsItemSelected(MenuItem item) {
Alexandre Savardd0aa7912012-10-16 17:46:50 -0400202 ArrayList<String> missingValue = new ArrayList<String>();
203 if(validateAccountCreation(missingValue)) {
Alexandre Savardc64bfb42012-10-16 15:42:15 -0400204 createNewAccount();
205 finish();
206 }
Alexandre Savardd0aa7912012-10-16 17:46:50 -0400207 else {
208 AlertDialog dialog = createCouldNotValidateDialog(missingValue);
209 dialog.show();
210 }
Alexandre Savard88bc5422012-10-16 11:17:23 -0400211
212 return true;
213 }
214
Alexandre Savard20d0de02012-10-16 14:05:44 -0400215 private void addPreferenceListener(AccountDetail details) {
216 for(AccountDetail.PreferenceEntry p : details.getDetailValues()) {
217 Preference pref = mPreferenceManager.findPreference(p.mKey);
218 if(pref != null) {
219 Log.i(TAG, "FOUND " + p.mKey);
220 if(!p.isTwoState) {
221 pref.setOnPreferenceChangeListener(changeNewAccountPreferenceListener);
222 }
223 }
224 }
225 }
226
Alexandre Savard88bc5422012-10-16 11:17:23 -0400227 @Override
Alexandre Savard5261f822012-10-05 15:23:56 -0400228 protected void onStart() {
229 super.onStart();
Alexandre Savard5261f822012-10-05 15:23:56 -0400230
Alexandre Savard20d0de02012-10-16 14:05:44 -0400231 addPreferenceListener(basicDetails);
232 addPreferenceListener(advancedDetails);
233 addPreferenceListener(srtpDetails);
234 addPreferenceListener(tlsDetails);
235
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -0400236 if(!mBound) {
237 Log.i(TAG, "onStart: Binding service...");
238 Intent intent = new Intent(this, SipService.class);
239 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
Alexandre Savard5261f822012-10-05 15:23:56 -0400240 }
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400241 }
242
243 @Override
244 protected void onStop() {
245 super.onStop();
Alexandre Savardfbf83ff2012-10-16 10:26:23 -0400246
Alexandre Savard88bc5422012-10-16 11:17:23 -0400247 if(mBound) {
248 Log.i(TAG, "onStop: Unbinding service...");
249 unbindService(mConnection);
250 mBound = false;
Alexandre Savard94b0d092012-10-16 16:18:14 -0400251 }
Alexandre Savard88bc5422012-10-16 11:17:23 -0400252 }
253
254 @Override
255 public void onBackPressed() {
256
Alexandre Savardd103a762012-10-16 15:05:50 -0400257 AlertDialog dialog = createAlertDialog();
258 dialog.show();
Alexandre Savard88bc5422012-10-16 11:17:23 -0400259 }
260
Alexandre Savard94b0d092012-10-16 16:18:14 -0400261 private void updateAccountDetails(HashMap<String, String> accountDetails, AccountDetail det) {
262 for(AccountDetail.PreferenceEntry p : det.getDetailValues()) {
263 Preference pref = mPreferenceManager.findPreference(p.mKey);
264 if(pref != null) {
265 if(p.isTwoState) {
266 CheckBoxPreference boxPref = (CheckBoxPreference) pref;
267 accountDetails.put(p.mKey, boxPref.isChecked() ? "true" : "false");
268 }
269 else {
270 EditTextPreference textPref = (EditTextPreference) pref;
271 accountDetails.put(p.mKey, textPref.getText());
272 }
273 }
274 }
275 }
276
Alexandre Savard88bc5422012-10-16 11:17:23 -0400277 private void createNewAccount() {
278
Alexandre Savard8f27d162012-10-05 16:30:28 -0400279 HashMap<String, String> accountDetails = new HashMap<String, String>();
Alexandre Savard8f27d162012-10-05 16:30:28 -0400280
Alexandre Savard94b0d092012-10-16 16:18:14 -0400281 updateAccountDetails(accountDetails, basicDetails);
282 updateAccountDetails(accountDetails, advancedDetails);
283 updateAccountDetails(accountDetails, srtpDetails);
284 updateAccountDetails(accountDetails, tlsDetails);
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -0400285
Alexandre Savard8f27d162012-10-05 16:30:28 -0400286 try {
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -0400287 Log.i(TAG, "ADD ACCOUNT");
Alexandre Savard8f27d162012-10-05 16:30:28 -0400288 service.addAccount(accountDetails);
289 } catch (RemoteException e) {
290 Log.e(TAG, "Cannot call service method", e);
291 }
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400292 }
Alexandre Savard53d2ddd2012-09-30 22:04:40 -0400293}