blob: b9b5665d6614c02622a0657a0ced06b18240545f [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 Savard3bbb4792012-10-05 11:30:01 -040080
Alexandre Savard5261f822012-10-05 15:23:56 -040081 Preference.OnPreferenceChangeListener changeNewAccountPreferenceListener = new Preference.OnPreferenceChangeListener() {
82 public boolean onPreferenceChange(Preference preference, Object newValue) {
Alexandre Savard5261f822012-10-05 15:23:56 -040083 return true;
84 }
85 };
86
Alexandre Savard20d0de02012-10-16 14:05:44 -040087 Preference.OnPreferenceChangeListener changeNewAccountTwoStateListener = new Preference.OnPreferenceChangeListener() {
88 public boolean onPreferenceChange(Preference preference, Object newValue) {
89 if(((CheckBoxPreference)preference).isChecked()) {
90 preference.setSummary(getString(R.string.account_current_value_label) + "enabled");
91 }
92 else {
93 preference.setSummary(getString(R.string.account_current_value_label) + "disabled");
94 }
95 return true;
96 }
97 };
98
Alexandre Savard3bbb4792012-10-05 11:30:01 -040099 private ServiceConnection mConnection = new ServiceConnection() {
100 @Override
101 public void onServiceConnected(ComponentName className, IBinder binder) {
102 service = ISipService.Stub.asInterface(binder);
103 mBound = true;
104 Log.d(TAG, "Service connected");
105 }
106
107 @Override
108 public void onServiceDisconnected(ComponentName arg0) {
109 mBound = false;
110 Log.d(TAG, "Service disconnected");
111 }
112 };
113
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -0400114 public AccountCreationActivity()
115 {
116 basicDetails = new AccountDetailBasic();
117 advancedDetails = new AccountDetailAdvanced();
118 srtpDetails = new AccountDetailSrtp();
119 tlsDetails = new AccountDetailTls();
120 }
121
Alexandre Savard44ec1622012-10-16 10:00:48 -0400122 private AlertDialog createAlertDialog()
123 {
Alexandre Savardd103a762012-10-16 15:05:50 -0400124 Activity ownerActivity = this;
125 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
126 builder.setMessage("All parameters will be lost").setTitle("Account Creation")
Alexandre Savardfbf83ff2012-10-16 10:26:23 -0400127 .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
Alexandre Savard44ec1622012-10-16 10:00:48 -0400128 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Savardd103a762012-10-16 15:05:50 -0400129 Activity activity = ((Dialog)dialog).getOwnerActivity();
130 activity.finish();
Alexandre Savard44ec1622012-10-16 10:00:48 -0400131 }
132 })
133 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
134 public void onClick(DialogInterface dialog, int whichButton) {
Alexandre Savardd103a762012-10-16 15:05:50 -0400135 /* Terminate with no action */
Alexandre Savard44ec1622012-10-16 10:00:48 -0400136 }
137 });
138
Alexandre Savardd103a762012-10-16 15:05:50 -0400139 AlertDialog alertDialog = builder.create();
140 alertDialog.setOwnerActivity(ownerActivity);
141
142 return alertDialog;
Alexandre Savard44ec1622012-10-16 10:00:48 -0400143 }
144
Alexandre Savard53d2ddd2012-09-30 22:04:40 -0400145 @Override
146 protected void onCreate(Bundle savedInstanceState) {
147 super.onCreate(savedInstanceState);
148
149 addPreferencesFromResource(R.xml.account_creation_preferences);
Alexandre Savard5261f822012-10-05 15:23:56 -0400150
Alexandre Savard8f27d162012-10-05 16:30:28 -0400151 mPreferenceManager = getPreferenceManager();
Alexandre Savard5261f822012-10-05 15:23:56 -0400152 }
153
154 @Override
Alexandre Savard88bc5422012-10-16 11:17:23 -0400155 public boolean onCreateOptionsMenu(Menu menu) {
156 MenuItem actionItem = menu.add("Create Account");
157 actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
158 return true;
159 }
160
161 @Override
162 public boolean onOptionsItemSelected(MenuItem item) {
163 createNewAccount();
164 finish();
165
166 return true;
167 }
168
Alexandre Savard20d0de02012-10-16 14:05:44 -0400169 private void addPreferenceListener(AccountDetail details) {
170 for(AccountDetail.PreferenceEntry p : details.getDetailValues()) {
171 Preference pref = mPreferenceManager.findPreference(p.mKey);
172 if(pref != null) {
173 Log.i(TAG, "FOUND " + p.mKey);
174 if(!p.isTwoState) {
175 pref.setOnPreferenceChangeListener(changeNewAccountPreferenceListener);
176 }
177 }
178 }
179 }
180
Alexandre Savard88bc5422012-10-16 11:17:23 -0400181 @Override
Alexandre Savard5261f822012-10-05 15:23:56 -0400182 protected void onStart() {
183 super.onStart();
Alexandre Savard5261f822012-10-05 15:23:56 -0400184
Alexandre Savard20d0de02012-10-16 14:05:44 -0400185 addPreferenceListener(basicDetails);
186 addPreferenceListener(advancedDetails);
187 addPreferenceListener(srtpDetails);
188 addPreferenceListener(tlsDetails);
189
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -0400190 if(!mBound) {
191 Log.i(TAG, "onStart: Binding service...");
192 Intent intent = new Intent(this, SipService.class);
193 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
Alexandre Savard5261f822012-10-05 15:23:56 -0400194 }
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400195 }
196
197 @Override
198 protected void onStop() {
199 super.onStop();
Alexandre Savardfbf83ff2012-10-16 10:26:23 -0400200
Alexandre Savard88bc5422012-10-16 11:17:23 -0400201 if(mBound) {
202 Log.i(TAG, "onStop: Unbinding service...");
203 unbindService(mConnection);
204 mBound = false;
205 }
206 }
207
208 @Override
209 public void onBackPressed() {
210
Alexandre Savardd103a762012-10-16 15:05:50 -0400211 AlertDialog dialog = createAlertDialog();
212 dialog.show();
Alexandre Savard88bc5422012-10-16 11:17:23 -0400213
Alexandre Savardd103a762012-10-16 15:05:50 -0400214 // super.onBackPressed();
Alexandre Savard88bc5422012-10-16 11:17:23 -0400215 }
216
217 private void createNewAccount() {
218
Alexandre Savard8f27d162012-10-05 16:30:28 -0400219 HashMap<String, String> accountDetails = new HashMap<String, String>();
Alexandre Savard8f27d162012-10-05 16:30:28 -0400220
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -0400221 for(String s : basicDetails.getDetailKeys()) {
222 EditTextPreference pref = (EditTextPreference) mPreferenceManager.findPreference(s);
223 if(pref != null) {
224 Log.i(TAG, "FOUND " + s + " " + pref.getText());
225 accountDetails.put(s, pref.getText());
226 }
227 }
228
229 for(String s : advancedDetails.getDetailKeys()) {
230 EditTextPreference pref = (EditTextPreference) mPreferenceManager.findPreference(s);
231 if(pref != null) {
232 Log.i(TAG, "FOUND " + s + " " + pref.getText());
233 accountDetails.put(s, pref.getText());
234 }
235 }
236
237 for(String s : srtpDetails.getDetailKeys()) {
238 EditTextPreference pref = (EditTextPreference) mPreferenceManager.findPreference(s);
239 if(pref != null) {
240 Log.i(TAG, "FOUND " + s + " " + pref.getText());
241 accountDetails.put(s, pref.getText());
242 }
243 }
244
245 for(String s : tlsDetails.getDetailKeys()) {
246 EditTextPreference pref = (EditTextPreference) mPreferenceManager.findPreference(s);
247 if(pref != null) {
248 Log.i(TAG, "FOUND " + s + " " + pref.getText());
249 accountDetails.put(s, pref.getText());
250 }
251 }
252
Alexandre Savard8f27d162012-10-05 16:30:28 -0400253 try {
Alexandre Savardf7f9e0b2012-10-15 17:24:07 -0400254 Log.i(TAG, "ADD ACCOUNT");
Alexandre Savard8f27d162012-10-05 16:30:28 -0400255 service.addAccount(accountDetails);
256 } catch (RemoteException e) {
257 Log.e(TAG, "Cannot call service method", e);
258 }
Alexandre Savard3bbb4792012-10-05 11:30:01 -0400259 }
Alexandre Savard53d2ddd2012-09-30 22:04:40 -0400260}