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