Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 32 | package com.savoirfairelinux.sflphone.client; |
| 33 | |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame] | 34 | import android.app.Activity; |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 35 | import android.app.AlertDialog; |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame] | 36 | import android.app.Dialog; |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 37 | import android.content.ComponentName; |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 38 | import android.content.Context; |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 39 | import android.content.DialogInterface; |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 40 | import android.content.Intent; |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 41 | import android.content.ServiceConnection; |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 42 | import android.os.Bundle; |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 43 | import android.os.IBinder; |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 44 | import android.os.RemoteException; |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 45 | import android.preference.Preference; |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 46 | import android.preference.PreferenceActivity; |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 47 | import android.preference.PreferenceManager; |
| 48 | import android.preference.EditTextPreference; |
Alexandre Savard | 20d0de0 | 2012-10-16 14:05:44 -0400 | [diff] [blame] | 49 | import android.preference.CheckBoxPreference; |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 50 | import android.util.Log; |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 51 | import android.view.Menu; |
| 52 | import android.view.MenuItem; |
| 53 | import android.view.Window; |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 54 | |
| 55 | import com.savoirfairelinux.sflphone.R; |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 56 | import com.savoirfairelinux.sflphone.service.SipService; |
| 57 | import com.savoirfairelinux.sflphone.service.ISipService; |
Alexandre Savard | 20d0de0 | 2012-10-16 14:05:44 -0400 | [diff] [blame] | 58 | import com.savoirfairelinux.sflphone.utils.AccountDetail; |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 59 | import com.savoirfairelinux.sflphone.utils.AccountDetailsHandler; |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 60 | import com.savoirfairelinux.sflphone.utils.AccountDetailBasic; |
| 61 | import com.savoirfairelinux.sflphone.utils.AccountDetailAdvanced; |
| 62 | import com.savoirfairelinux.sflphone.utils.AccountDetailSrtp; |
| 63 | import com.savoirfairelinux.sflphone.utils.AccountDetailTls; |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 64 | |
Alexandre Savard | d0aa791 | 2012-10-16 17:46:50 -0400 | [diff] [blame] | 65 | import java.util.ArrayList; |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 66 | import java.util.EnumSet; |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 67 | import java.util.HashMap; |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 68 | import java.util.Set; |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 69 | |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 70 | public class AccountCreationActivity extends PreferenceActivity |
| 71 | { |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 72 | static final String TAG = "SFLPhonePreferenceActivity"; |
| 73 | private ISipService service; |
| 74 | private boolean mBound = false; |
| 75 | private PreferenceManager mPreferenceManager = null; |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 76 | private AccountDetailBasic basicDetails; |
| 77 | private AccountDetailAdvanced advancedDetails; |
| 78 | private AccountDetailSrtp srtpDetails; |
| 79 | private AccountDetailTls tlsDetails; |
Alexandre Savard | c64bfb4 | 2012-10-16 15:42:15 -0400 | [diff] [blame] | 80 | private MenuItem createAccountAction = null; |
Alexandre Savard | d0aa791 | 2012-10-16 17:46:50 -0400 | [diff] [blame] | 81 | private ArrayList<String> requiredFields = null; |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 82 | |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 83 | Preference.OnPreferenceChangeListener changeNewAccountPreferenceListener = new Preference.OnPreferenceChangeListener() { |
| 84 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
Alexandre Savard | c64bfb4 | 2012-10-16 15:42:15 -0400 | [diff] [blame] | 85 | preference.setSummary(getString(R.string.account_current_value_label) + (CharSequence)newValue); |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 86 | return true; |
| 87 | } |
| 88 | }; |
| 89 | |
Alexandre Savard | 20d0de0 | 2012-10-16 14:05:44 -0400 | [diff] [blame] | 90 | Preference.OnPreferenceChangeListener changeNewAccountTwoStateListener = new Preference.OnPreferenceChangeListener() { |
| 91 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
Alexandre Savard | 20d0de0 | 2012-10-16 14:05:44 -0400 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | }; |
| 95 | |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 96 | 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 Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 111 | public AccountCreationActivity() |
| 112 | { |
| 113 | basicDetails = new AccountDetailBasic(); |
| 114 | advancedDetails = new AccountDetailAdvanced(); |
| 115 | srtpDetails = new AccountDetailSrtp(); |
| 116 | tlsDetails = new AccountDetailTls(); |
Alexandre Savard | d0aa791 | 2012-10-16 17:46:50 -0400 | [diff] [blame] | 117 | |
| 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 Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 126 | private AlertDialog createAlertDialog() |
| 127 | { |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame] | 128 | Activity ownerActivity = this; |
| 129 | AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity); |
| 130 | builder.setMessage("All parameters will be lost").setTitle("Account Creation") |
Alexandre Savard | fbf83ff | 2012-10-16 10:26:23 -0400 | [diff] [blame] | 131 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 132 | public void onClick(DialogInterface dialog, int whichButton) { |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame] | 133 | Activity activity = ((Dialog)dialog).getOwnerActivity(); |
| 134 | activity.finish(); |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 135 | } |
| 136 | }) |
| 137 | .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
| 138 | public void onClick(DialogInterface dialog, int whichButton) { |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame] | 139 | /* Terminate with no action */ |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 140 | } |
| 141 | }); |
| 142 | |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame] | 143 | AlertDialog alertDialog = builder.create(); |
| 144 | alertDialog.setOwnerActivity(ownerActivity); |
| 145 | |
Alexandre Savard | c64bfb4 | 2012-10-16 15:42:15 -0400 | [diff] [blame] | 146 | return alertDialog; |
| 147 | } |
| 148 | |
Alexandre Savard | d0aa791 | 2012-10-16 17:46:50 -0400 | [diff] [blame] | 149 | private AlertDialog createCouldNotValidateDialog(ArrayList<String> missingValue) |
Alexandre Savard | c64bfb4 | 2012-10-16 15:42:15 -0400 | [diff] [blame] | 150 | { |
Alexandre Savard | d0aa791 | 2012-10-16 17:46:50 -0400 | [diff] [blame] | 151 | 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 Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 186 | @Override |
| 187 | protected void onCreate(Bundle savedInstanceState) { |
| 188 | super.onCreate(savedInstanceState); |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 189 | addPreferencesFromResource(R.xml.account_creation_preferences); |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 190 | mPreferenceManager = getPreferenceManager(); |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 194 | public boolean onCreateOptionsMenu(Menu menu) { |
Alexandre Savard | c64bfb4 | 2012-10-16 15:42:15 -0400 | [diff] [blame] | 195 | createAccountAction = menu.add("Create Account"); |
| 196 | createAccountAction.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 197 | return true; |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public boolean onOptionsItemSelected(MenuItem item) { |
Alexandre Savard | d0aa791 | 2012-10-16 17:46:50 -0400 | [diff] [blame] | 202 | ArrayList<String> missingValue = new ArrayList<String>(); |
| 203 | if(validateAccountCreation(missingValue)) { |
Alexandre Savard | c64bfb4 | 2012-10-16 15:42:15 -0400 | [diff] [blame] | 204 | createNewAccount(); |
| 205 | finish(); |
| 206 | } |
Alexandre Savard | d0aa791 | 2012-10-16 17:46:50 -0400 | [diff] [blame] | 207 | else { |
| 208 | AlertDialog dialog = createCouldNotValidateDialog(missingValue); |
| 209 | dialog.show(); |
| 210 | } |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 211 | |
| 212 | return true; |
| 213 | } |
| 214 | |
Alexandre Savard | 20d0de0 | 2012-10-16 14:05:44 -0400 | [diff] [blame] | 215 | private void addPreferenceListener(AccountDetail details) { |
| 216 | for(AccountDetail.PreferenceEntry p : details.getDetailValues()) { |
| 217 | Preference pref = mPreferenceManager.findPreference(p.mKey); |
| 218 | if(pref != null) { |
Alexandre Savard | 20d0de0 | 2012-10-16 14:05:44 -0400 | [diff] [blame] | 219 | if(!p.isTwoState) { |
| 220 | pref.setOnPreferenceChangeListener(changeNewAccountPreferenceListener); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 226 | @Override |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 227 | protected void onStart() { |
| 228 | super.onStart(); |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 229 | |
Alexandre Savard | 20d0de0 | 2012-10-16 14:05:44 -0400 | [diff] [blame] | 230 | addPreferenceListener(basicDetails); |
| 231 | addPreferenceListener(advancedDetails); |
| 232 | addPreferenceListener(srtpDetails); |
| 233 | addPreferenceListener(tlsDetails); |
| 234 | |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 235 | if(!mBound) { |
| 236 | Log.i(TAG, "onStart: Binding service..."); |
| 237 | Intent intent = new Intent(this, SipService.class); |
| 238 | bindService(intent, mConnection, Context.BIND_AUTO_CREATE); |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 239 | } |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
| 243 | protected void onStop() { |
| 244 | super.onStop(); |
Alexandre Savard | fbf83ff | 2012-10-16 10:26:23 -0400 | [diff] [blame] | 245 | |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 246 | if(mBound) { |
| 247 | Log.i(TAG, "onStop: Unbinding service..."); |
| 248 | unbindService(mConnection); |
| 249 | mBound = false; |
Alexandre Savard | 94b0d09 | 2012-10-16 16:18:14 -0400 | [diff] [blame] | 250 | } |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public void onBackPressed() { |
| 255 | |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame] | 256 | AlertDialog dialog = createAlertDialog(); |
| 257 | dialog.show(); |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 258 | } |
| 259 | |
Alexandre Savard | 94b0d09 | 2012-10-16 16:18:14 -0400 | [diff] [blame] | 260 | private void updateAccountDetails(HashMap<String, String> accountDetails, AccountDetail det) { |
| 261 | for(AccountDetail.PreferenceEntry p : det.getDetailValues()) { |
| 262 | Preference pref = mPreferenceManager.findPreference(p.mKey); |
| 263 | if(pref != null) { |
| 264 | if(p.isTwoState) { |
| 265 | CheckBoxPreference boxPref = (CheckBoxPreference) pref; |
| 266 | accountDetails.put(p.mKey, boxPref.isChecked() ? "true" : "false"); |
| 267 | } |
| 268 | else { |
| 269 | EditTextPreference textPref = (EditTextPreference) pref; |
| 270 | accountDetails.put(p.mKey, textPref.getText()); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 276 | private void createNewAccount() { |
| 277 | |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 278 | HashMap<String, String> accountDetails = new HashMap<String, String>(); |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 279 | |
Alexandre Savard | 94b0d09 | 2012-10-16 16:18:14 -0400 | [diff] [blame] | 280 | updateAccountDetails(accountDetails, basicDetails); |
| 281 | updateAccountDetails(accountDetails, advancedDetails); |
| 282 | updateAccountDetails(accountDetails, srtpDetails); |
| 283 | updateAccountDetails(accountDetails, tlsDetails); |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 284 | |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 285 | try { |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 286 | Log.i(TAG, "ADD ACCOUNT"); |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 287 | service.addAccount(accountDetails); |
| 288 | } catch (RemoteException e) { |
| 289 | Log.e(TAG, "Cannot call service method", e); |
| 290 | } |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 291 | } |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 292 | } |