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