Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 1 | package org.sflphone.fragments; |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 2 | |
| 3 | import java.util.HashMap; |
| 4 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 5 | import org.sflphone.R; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 6 | import org.sflphone.account.AccountDetailAdvanced; |
| 7 | import org.sflphone.account.AccountDetailBasic; |
| 8 | import org.sflphone.account.AccountDetailSrtp; |
| 9 | import org.sflphone.account.AccountDetailTls; |
Alexandre Lision | 4ab5397 | 2013-11-04 16:59:18 -0500 | [diff] [blame] | 10 | import org.sflphone.client.SettingsActivity; |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 11 | import org.sflphone.service.ISipService; |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 12 | |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 13 | import android.app.Activity; |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 14 | import android.app.Fragment; |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 15 | import android.content.Intent; |
| 16 | import android.os.Bundle; |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 17 | import android.os.RemoteException; |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 18 | import android.text.TextUtils; |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 19 | import android.view.KeyEvent; |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 20 | import android.view.LayoutInflater; |
| 21 | import android.view.View; |
| 22 | import android.view.ViewGroup; |
| 23 | import android.widget.EditText; |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 24 | import android.widget.TextView; |
| 25 | import android.widget.TextView.OnEditorActionListener; |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 26 | |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 27 | public class AccountCreationFragment extends Fragment { |
| 28 | |
| 29 | // Values for email and password at the time of the login attempt. |
| 30 | private String mAlias; |
| 31 | private String mHostname; |
| 32 | private String mUsername; |
| 33 | private String mPassword; |
| 34 | |
| 35 | // UI references. |
| 36 | private EditText mAliasView; |
| 37 | private EditText mHostnameView; |
| 38 | private EditText mUsernameView; |
| 39 | private EditText mPasswordView; |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 40 | |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 41 | private Callbacks mCallbacks = sDummyCallbacks; |
| 42 | private static Callbacks sDummyCallbacks = new Callbacks() { |
| 43 | |
| 44 | @Override |
| 45 | public ISipService getService() { |
| 46 | return null; |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | public interface Callbacks { |
| 51 | |
| 52 | public ISipService getService(); |
| 53 | |
| 54 | } |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 55 | |
| 56 | @Override |
| 57 | public void onCreate(Bundle savedInstanceState) { |
| 58 | super.onCreate(savedInstanceState); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { |
| 63 | View inflatedView = inflater.inflate(R.layout.frag_account_creation, parent, false); |
| 64 | |
| 65 | mAliasView = (EditText) inflatedView.findViewById(R.id.alias); |
| 66 | mHostnameView = (EditText) inflatedView.findViewById(R.id.hostname); |
| 67 | mUsernameView = (EditText) inflatedView.findViewById(R.id.username); |
| 68 | mPasswordView = (EditText) inflatedView.findViewById(R.id.password); |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 69 | |
| 70 | mPasswordView.setOnEditorActionListener(new OnEditorActionListener() { |
| 71 | |
| 72 | @Override |
| 73 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
| 74 | // if(actionId == EditorInfo.IME_ACTION_GO || event.getAction() == KeyEvent.KEYCODE_ENTER){ |
| 75 | mAlias = mAliasView.getText().toString(); |
| 76 | mHostname = mHostnameView.getText().toString(); |
| 77 | mUsername = mUsernameView.getText().toString(); |
| 78 | mPassword = mPasswordView.getText().toString(); |
| 79 | attemptCreation(); |
| 80 | // } |
| 81 | |
| 82 | return true; |
| 83 | } |
| 84 | }); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 85 | inflatedView.findViewById(R.id.create_button).setOnClickListener(new View.OnClickListener() { |
| 86 | @Override |
| 87 | public void onClick(View view) { |
Alexandre Lision | f126dad | 2013-07-18 12:21:20 -0400 | [diff] [blame] | 88 | mAlias = mAliasView.getText().toString(); |
| 89 | mHostname = mHostnameView.getText().toString(); |
| 90 | mUsername = mUsernameView.getText().toString(); |
| 91 | mPassword = mPasswordView.getText().toString(); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 92 | attemptCreation(); |
| 93 | } |
| 94 | }); |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 95 | |
Alexandre Lision | 5221499 | 2013-10-28 17:41:23 -0400 | [diff] [blame] | 96 | // inflatedView.findViewById(R.id.dev_account).setVisibility(View.GONE); // Hide this button in release apk |
Alexandre Lision | f126dad | 2013-07-18 12:21:20 -0400 | [diff] [blame] | 97 | inflatedView.findViewById(R.id.dev_account).setOnClickListener(new View.OnClickListener() { |
| 98 | @Override |
| 99 | public void onClick(View view) { |
| 100 | createDevAccount(); |
| 101 | } |
| 102 | |
| 103 | private void createDevAccount() { |
| 104 | mUsername = mUsernameView.getText().toString(); |
| 105 | if (TextUtils.isEmpty(mUsername)) { |
| 106 | mUsernameView.setError(getString(R.string.error_field_required)); |
| 107 | mUsernameView.requestFocus(); |
| 108 | return; |
| 109 | } else { |
| 110 | mAlias = mUsername; |
| 111 | mHostname = "192.95.9.63"; |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 112 | mPassword = "sfl_u" + mUsername; |
Alexandre Lision | f126dad | 2013-07-18 12:21:20 -0400 | [diff] [blame] | 113 | attemptCreation(); |
| 114 | } |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 115 | |
Alexandre Lision | f126dad | 2013-07-18 12:21:20 -0400 | [diff] [blame] | 116 | } |
| 117 | }); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 118 | |
| 119 | return inflatedView; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void onResume() { |
| 124 | super.onResume(); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onStart() { |
| 129 | super.onStart(); |
| 130 | |
| 131 | } |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 132 | |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 133 | @Override |
| 134 | public void onAttach(Activity activity) { |
| 135 | super.onAttach(activity); |
| 136 | if (!(activity instanceof Callbacks)) { |
| 137 | throw new IllegalStateException("Activity must implement fragment's callbacks."); |
| 138 | } |
| 139 | |
| 140 | mCallbacks = (Callbacks) activity; |
| 141 | } |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * Attempts to sign in or register the account specified by the login form. If there are form errors (invalid email, missing fields, etc.), the |
| 145 | * errors are presented and no actual login attempt is made. |
| 146 | */ |
| 147 | public void attemptCreation() { |
| 148 | |
| 149 | // Reset errors. |
| 150 | mAliasView.setError(null); |
| 151 | mPasswordView.setError(null); |
| 152 | |
| 153 | // Store values at the time of the login attempt. |
Alexandre Lision | f126dad | 2013-07-18 12:21:20 -0400 | [diff] [blame] | 154 | |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 155 | boolean cancel = false; |
| 156 | View focusView = null; |
| 157 | |
| 158 | // Check for a valid password. |
| 159 | if (TextUtils.isEmpty(mPassword)) { |
| 160 | mPasswordView.setError(getString(R.string.error_field_required)); |
| 161 | focusView = mPasswordView; |
| 162 | cancel = true; |
| 163 | } |
| 164 | |
| 165 | if (TextUtils.isEmpty(mUsername)) { |
| 166 | mUsernameView.setError(getString(R.string.error_field_required)); |
| 167 | focusView = mUsernameView; |
| 168 | cancel = true; |
| 169 | } |
| 170 | |
| 171 | if (TextUtils.isEmpty(mHostname)) { |
| 172 | mHostnameView.setError(getString(R.string.error_field_required)); |
| 173 | focusView = mHostnameView; |
| 174 | cancel = true; |
| 175 | } |
| 176 | |
| 177 | // Check for a valid email address. |
| 178 | if (TextUtils.isEmpty(mAlias)) { |
| 179 | mAliasView.setError(getString(R.string.error_field_required)); |
| 180 | focusView = mAliasView; |
| 181 | cancel = true; |
| 182 | } |
| 183 | |
| 184 | if (cancel) { |
| 185 | // There was an error; don't attempt login and focus the first |
| 186 | // form field with an error. |
| 187 | focusView.requestFocus(); |
| 188 | } else { |
| 189 | // Show a progress spinner, and kick off a background task to |
| 190 | // perform the user login attempt. |
| 191 | initCreation(); |
| 192 | |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | private void initCreation() { |
| 197 | |
| 198 | HashMap<String, String> accountDetails = new HashMap<String, String>(); |
| 199 | |
| 200 | accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_TYPE, AccountDetailBasic.CONFIG_ACCOUNT_DEFAULT_TYPE); |
| 201 | accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_ALIAS, mAlias); |
| 202 | accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_HOSTNAME, mHostname); |
| 203 | accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME, mUsername); |
| 204 | accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD, mPassword); |
| 205 | accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_ROUTESET, ""); |
| 206 | accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_REALM, AccountDetailBasic.CONFIG_ACCOUNT_DEFAULT_REALM); |
| 207 | accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_ENABLE, AccountDetailBasic.CONFIG_ACCOUNT_DEFAULT_ENABLE); |
| 208 | accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD, mPassword); |
| 209 | accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_USERAGENT, AccountDetailBasic.CONFIG_ACCOUNT_DEFAULT_USERAGENT); |
| 210 | |
| 211 | accountDetails.put(AccountDetailAdvanced.CONFIG_LOCAL_PORT, AccountDetailAdvanced.CONFIG_DEFAULT_LOCAL_PORT); |
| 212 | accountDetails.put(AccountDetailAdvanced.CONFIG_LOCAL_INTERFACE, AccountDetailAdvanced.CONFIG_DEFAULT_INTERFACE); |
| 213 | accountDetails.put(AccountDetailAdvanced.CONFIG_PUBLISHED_PORT, AccountDetailAdvanced.CONFIG_DEFAULT_PUBLISHED_PORT); |
| 214 | accountDetails.put(AccountDetailAdvanced.CONFIG_PUBLISHED_ADDRESS, AccountDetailAdvanced.CONFIG_DEFAULT_ADDRESS); |
| 215 | accountDetails.put(AccountDetailAdvanced.CONFIG_ACCOUNT_REGISTRATION_EXPIRE, AccountDetailAdvanced.CONFIG_DEFAULT_REGISTRATION_EXPIRE); |
| 216 | accountDetails.put(AccountDetailAdvanced.CONFIG_STUN_SERVER, ""); |
| 217 | accountDetails.put(AccountDetailAdvanced.CONFIG_ACCOUNT_REGISTRATION_STATUS, ""); |
| 218 | accountDetails.put(AccountDetailAdvanced.CONFIG_ACCOUNT_REGISTRATION_STATE_CODE, ""); |
| 219 | accountDetails.put(AccountDetailAdvanced.CONFIG_ACCOUNT_REGISTRATION_STATE_DESC, ""); |
Alexandre Lision | 80f0a7e | 2013-09-26 12:27:51 -0400 | [diff] [blame] | 220 | accountDetails.put(AccountDetailAdvanced.CONFIG_ACCOUNT_AUTOANSWER, AccountDetailAdvanced.FALSE_STR); |
| 221 | accountDetails.put(AccountDetailAdvanced.CONFIG_ACCOUNT_DTMF_TYPE, AccountDetailAdvanced.CONFIG_DEFAULT_DTMF_TYPE); |
| 222 | accountDetails.put(AccountDetailAdvanced.CONFIG_KEEP_ALIVE_ENABLED, AccountDetailAdvanced.FALSE_STR); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 223 | accountDetails.put(AccountDetailAdvanced.CONFIG_STUN_SERVER, ""); |
| 224 | accountDetails.put(AccountDetailAdvanced.CONFIG_PUBLISHED_SAMEAS_LOCAL, AccountDetailAdvanced.CONFIG_DEFAULT_PUBLISHED_SAMEAS_LOCAL); |
Alexandre Lision | 80f0a7e | 2013-09-26 12:27:51 -0400 | [diff] [blame] | 225 | accountDetails.put(AccountDetailAdvanced.CONFIG_RINGTONE_ENABLED, AccountDetailAdvanced.FALSE_STR); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 226 | accountDetails.put(AccountDetailAdvanced.CONFIG_RINGTONE_PATH, ""); |
Alexandre Lision | 80f0a7e | 2013-09-26 12:27:51 -0400 | [diff] [blame] | 227 | accountDetails.put(AccountDetailAdvanced.CONFIG_STUN_ENABLE, AccountDetailAdvanced.FALSE_STR); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 228 | |
| 229 | accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE, ""); |
| 230 | accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_RTP_FALLBACK, ""); |
Alexandre Lision | 80f0a7e | 2013-09-26 12:27:51 -0400 | [diff] [blame] | 231 | accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_ENABLE, AccountDetailAdvanced.FALSE_STR); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 232 | accountDetails.put(AccountDetailSrtp.CONFIG_ZRTP_DISPLAY_SAS, ""); |
| 233 | accountDetails.put(AccountDetailSrtp.CONFIG_ZRTP_DISPLAY_SAS_ONCE, ""); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 234 | accountDetails.put(AccountDetailSrtp.CONFIG_ZRTP_HELLO_HASH, ""); |
| 235 | accountDetails.put(AccountDetailSrtp.CONFIG_ZRTP_NOT_SUPP_WARNING, ""); |
| 236 | |
| 237 | accountDetails.put(AccountDetailTls.CONFIG_TLS_CIPHERS, ""); |
| 238 | accountDetails.put(AccountDetailTls.CONFIG_TLS_LISTENER_PORT, ""); |
| 239 | accountDetails.put(AccountDetailTls.CONFIG_TLS_METHOD, ""); |
Alexandre Lision | 80f0a7e | 2013-09-26 12:27:51 -0400 | [diff] [blame] | 240 | accountDetails.put(AccountDetailTls.CONFIG_TLS_ENABLE, AccountDetailAdvanced.FALSE_STR); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 241 | accountDetails.put(AccountDetailTls.CONFIG_TLS_PASSWORD, ""); |
| 242 | accountDetails.put(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE, ""); |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 243 | |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 244 | accountDetails.put(AccountDetailTls.CONFIG_TLS_SERVER_NAME, ""); |
Alexandre Lision | 80f0a7e | 2013-09-26 12:27:51 -0400 | [diff] [blame] | 245 | accountDetails.put(AccountDetailTls.CONFIG_TLS_REQUIRE_CLIENT_CERTIFICATE, AccountDetailAdvanced.FALSE_STR); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 246 | accountDetails.put(AccountDetailTls.CONFIG_TLS_LISTENER_PORT, ""); |
| 247 | accountDetails.put(AccountDetailTls.CONFIG_TLS_VERIFY_CLIENT, ""); |
| 248 | accountDetails.put(AccountDetailTls.CONFIG_TLS_CERTIFICATE_FILE, ""); |
| 249 | accountDetails.put(AccountDetailTls.CONFIG_TLS_CA_LIST_FILE, ""); |
| 250 | accountDetails.put(AccountDetailTls.CONFIG_TLS_VERIFY_SERVER, ""); |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 251 | |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 252 | createNewAccount(accountDetails); |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 253 | |
Alexandre Lision | 4ab5397 | 2013-11-04 16:59:18 -0500 | [diff] [blame] | 254 | Intent resultIntent = new Intent(getActivity(), SettingsActivity.class); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 255 | getActivity().setResult(Activity.RESULT_OK, resultIntent); |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 256 | resultIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); |
| 257 | startActivity(resultIntent); |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 258 | getActivity().finish(); |
| 259 | |
| 260 | } |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 261 | |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 262 | private void createNewAccount(HashMap<String, String> accountDetails) { |
| 263 | try { |
Alexandre Lision | 7f0bba5 | 2013-10-16 14:43:11 -0400 | [diff] [blame] | 264 | |
Alexandre Lision | d16bad9 | 2013-10-09 17:16:20 -0400 | [diff] [blame] | 265 | mCallbacks.getService().addAccount(accountDetails); |
| 266 | } catch (RemoteException e) { |
| 267 | e.printStackTrace(); |
| 268 | } |
| 269 | } |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 270 | |
alision | 5cfc35d | 2013-07-11 15:11:39 -0400 | [diff] [blame] | 271 | } |