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 | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 80 | |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 81 | Preference.OnPreferenceChangeListener changeNewAccountPreferenceListener = new Preference.OnPreferenceChangeListener() { |
| 82 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 83 | return true; |
| 84 | } |
| 85 | }; |
| 86 | |
Alexandre Savard | 20d0de0 | 2012-10-16 14:05:44 -0400 | [diff] [blame] | 87 | 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 Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 99 | 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 Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 114 | public AccountCreationActivity() |
| 115 | { |
| 116 | basicDetails = new AccountDetailBasic(); |
| 117 | advancedDetails = new AccountDetailAdvanced(); |
| 118 | srtpDetails = new AccountDetailSrtp(); |
| 119 | tlsDetails = new AccountDetailTls(); |
| 120 | } |
| 121 | |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 122 | private AlertDialog createAlertDialog() |
| 123 | { |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame^] | 124 | Activity ownerActivity = this; |
| 125 | AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity); |
| 126 | builder.setMessage("All parameters will be lost").setTitle("Account Creation") |
Alexandre Savard | fbf83ff | 2012-10-16 10:26:23 -0400 | [diff] [blame] | 127 | .setPositiveButton("Ok", new DialogInterface.OnClickListener() { |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 128 | public void onClick(DialogInterface dialog, int whichButton) { |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame^] | 129 | Activity activity = ((Dialog)dialog).getOwnerActivity(); |
| 130 | activity.finish(); |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 131 | } |
| 132 | }) |
| 133 | .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
| 134 | public void onClick(DialogInterface dialog, int whichButton) { |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame^] | 135 | /* Terminate with no action */ |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 136 | } |
| 137 | }); |
| 138 | |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame^] | 139 | AlertDialog alertDialog = builder.create(); |
| 140 | alertDialog.setOwnerActivity(ownerActivity); |
| 141 | |
| 142 | return alertDialog; |
Alexandre Savard | 44ec162 | 2012-10-16 10:00:48 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 145 | @Override |
| 146 | protected void onCreate(Bundle savedInstanceState) { |
| 147 | super.onCreate(savedInstanceState); |
| 148 | |
| 149 | addPreferencesFromResource(R.xml.account_creation_preferences); |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 150 | |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 151 | mPreferenceManager = getPreferenceManager(); |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 155 | 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 Savard | 20d0de0 | 2012-10-16 14:05:44 -0400 | [diff] [blame] | 169 | 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 Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 181 | @Override |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 182 | protected void onStart() { |
| 183 | super.onStart(); |
Alexandre Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 184 | |
Alexandre Savard | 20d0de0 | 2012-10-16 14:05:44 -0400 | [diff] [blame] | 185 | addPreferenceListener(basicDetails); |
| 186 | addPreferenceListener(advancedDetails); |
| 187 | addPreferenceListener(srtpDetails); |
| 188 | addPreferenceListener(tlsDetails); |
| 189 | |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 190 | 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 Savard | 5261f82 | 2012-10-05 15:23:56 -0400 | [diff] [blame] | 194 | } |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
| 198 | protected void onStop() { |
| 199 | super.onStop(); |
Alexandre Savard | fbf83ff | 2012-10-16 10:26:23 -0400 | [diff] [blame] | 200 | |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 201 | 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 Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame^] | 211 | AlertDialog dialog = createAlertDialog(); |
| 212 | dialog.show(); |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 213 | |
Alexandre Savard | d103a76 | 2012-10-16 15:05:50 -0400 | [diff] [blame^] | 214 | // super.onBackPressed(); |
Alexandre Savard | 88bc542 | 2012-10-16 11:17:23 -0400 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | private void createNewAccount() { |
| 218 | |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 219 | HashMap<String, String> accountDetails = new HashMap<String, String>(); |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 220 | |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 221 | 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 Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 253 | try { |
Alexandre Savard | f7f9e0b | 2012-10-15 17:24:07 -0400 | [diff] [blame] | 254 | Log.i(TAG, "ADD ACCOUNT"); |
Alexandre Savard | 8f27d16 | 2012-10-05 16:30:28 -0400 | [diff] [blame] | 255 | service.addAccount(accountDetails); |
| 256 | } catch (RemoteException e) { |
| 257 | Log.e(TAG, "Cannot call service method", e); |
| 258 | } |
Alexandre Savard | 3bbb479 | 2012-10-05 11:30:01 -0400 | [diff] [blame] | 259 | } |
Alexandre Savard | 53d2ddd | 2012-09-30 22:04:40 -0400 | [diff] [blame] | 260 | } |