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 | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 65 | //import java.util.ArrayList; |
| 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 | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 81 | |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 82 | Preference.OnPreferenceChangeListener changeNewAccountPreferenceListener = new Preference.OnPreferenceChangeListener() { |
| 83 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
Alexandre Savard | c64bfb4 | 2012-10-16 15:42:15 -0400 | [diff] [blame] | 84 | preference.setSummary(getString(R.string.account_current_value_label) + (CharSequence)newValue); |
| 85 | AccountCreationActivity activity = (AccountCreationActivity)preference.getContext(); |
| 86 | activity.validateAccountCreation(); |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 87 | return true; |
| 88 | } |
| 89 | }; |
| 90 | |
Alexandre Savard | 20d0de0 | 2012-10-16 14:05:44 -0400 | [diff] [blame] | 91 | Preference.OnPreferenceChangeListener changeNewAccountTwoStateListener = new Preference.OnPreferenceChangeListener() { |
| 92 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
Alexandre Savard | 20d0de0 | 2012-10-16 14:05:44 -0400 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | }; |
| 96 | |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 97 | 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 Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 112 | public AccountCreationActivity() |
| 113 | { |
| 114 | basicDetails = new AccountDetailBasic(); |
| 115 | advancedDetails = new AccountDetailAdvanced(); |
| 116 | srtpDetails = new AccountDetailSrtp(); |
| 117 | tlsDetails = new AccountDetailTls(); |
| 118 | } |
| 119 | |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 120 | private AlertDialog createAlertDialog() |
| 121 | { |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame] | 122 | Activity ownerActivity = this; |
| 123 | AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity); |
| 124 | builder.setMessage("All parameters will be lost").setTitle("Account Creation") |
Alexandre Savard | fbf83ff | 2012-10-16 10:26:23 -0400 | [diff] [blame] | 125 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 126 | public void onClick(DialogInterface dialog, int whichButton) { |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame] | 127 | Activity activity = ((Dialog)dialog).getOwnerActivity(); |
| 128 | activity.finish(); |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 129 | } |
| 130 | }) |
| 131 | .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
| 132 | public void onClick(DialogInterface dialog, int whichButton) { |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame] | 133 | /* Terminate with no action */ |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 134 | } |
| 135 | }); |
| 136 | |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame] | 137 | AlertDialog alertDialog = builder.create(); |
| 138 | alertDialog.setOwnerActivity(ownerActivity); |
| 139 | |
Alexandre Savard | c64bfb4 | 2012-10-16 15:42:15 -0400 | [diff] [blame] | 140 | return alertDialog; |
| 141 | } |
| 142 | |
| 143 | public boolean validateAccountCreation() |
| 144 | { |
| 145 | createAccountAction.setEnabled(true); |
| 146 | return true; |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 149 | @Override |
| 150 | protected void onCreate(Bundle savedInstanceState) { |
| 151 | super.onCreate(savedInstanceState); |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 152 | addPreferencesFromResource(R.xml.account_creation_preferences); |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 153 | mPreferenceManager = getPreferenceManager(); |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 157 | public boolean onCreateOptionsMenu(Menu menu) { |
Alexandre Savard | c64bfb4 | 2012-10-16 15:42:15 -0400 | [diff] [blame] | 158 | createAccountAction = menu.add("Create Account"); |
| 159 | createAccountAction.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); |
| 160 | createAccountAction.setEnabled(false); |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public boolean onOptionsItemSelected(MenuItem item) { |
Alexandre Savard | c64bfb4 | 2012-10-16 15:42:15 -0400 | [diff] [blame] | 166 | if(item.isEnabled()) { |
| 167 | createNewAccount(); |
| 168 | finish(); |
| 169 | } |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 170 | |
| 171 | return true; |
| 172 | } |
| 173 | |
Alexandre Savard | 20d0de0 | 2012-10-16 14:05:44 -0400 | [diff] [blame] | 174 | 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 Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 186 | @Override |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 187 | protected void onStart() { |
| 188 | super.onStart(); |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 189 | |
Alexandre Savard | 20d0de0 | 2012-10-16 14:05:44 -0400 | [diff] [blame] | 190 | addPreferenceListener(basicDetails); |
| 191 | addPreferenceListener(advancedDetails); |
| 192 | addPreferenceListener(srtpDetails); |
| 193 | addPreferenceListener(tlsDetails); |
| 194 | |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 195 | 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 Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 199 | } |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
| 203 | protected void onStop() { |
| 204 | super.onStop(); |
Alexandre Savard | fbf83ff | 2012-10-16 10:26:23 -0400 | [diff] [blame] | 205 | |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 206 | if(mBound) { |
| 207 | Log.i(TAG, "onStop: Unbinding service..."); |
| 208 | unbindService(mConnection); |
| 209 | mBound = false; |
Alexandre Savard | 94b0d09 | 2012-10-16 16:18:14 -0400 | [diff] [blame^] | 210 | } |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void onBackPressed() { |
| 215 | |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame] | 216 | AlertDialog dialog = createAlertDialog(); |
| 217 | dialog.show(); |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 218 | |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame] | 219 | // super.onBackPressed(); |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 220 | } |
| 221 | |
Alexandre Savard | 94b0d09 | 2012-10-16 16:18:14 -0400 | [diff] [blame^] | 222 | 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 Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 238 | private void createNewAccount() { |
| 239 | |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 240 | HashMap<String, String> accountDetails = new HashMap<String, String>(); |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 241 | |
Alexandre Savard | 94b0d09 | 2012-10-16 16:18:14 -0400 | [diff] [blame^] | 242 | updateAccountDetails(accountDetails, basicDetails); |
| 243 | updateAccountDetails(accountDetails, advancedDetails); |
| 244 | updateAccountDetails(accountDetails, srtpDetails); |
| 245 | updateAccountDetails(accountDetails, tlsDetails); |
| 246 | /* |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 247 | 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 Savard | 94b0d09 | 2012-10-16 16:18:14 -0400 | [diff] [blame^] | 278 | */ |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 279 | |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 280 | try { |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 281 | Log.i(TAG, "ADD ACCOUNT"); |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 282 | service.addAccount(accountDetails); |
| 283 | } catch (RemoteException e) { |
| 284 | Log.e(TAG, "Cannot call service method", e); |
| 285 | } |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 286 | } |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 287 | } |