blob: 71810157a5664a33252db8cf0973b77d0e942f4c [file] [log] [blame]
Alexandre Lision064e1e02013-10-01 16:18:42 -04001package org.sflphone.fragments;
alision5cfc35d2013-07-11 15:11:39 -04002
3import java.util.HashMap;
4
Alexandre Lision064e1e02013-10-01 16:18:42 -04005import org.sflphone.R;
Alexandre Lision064e1e02013-10-01 16:18:42 -04006import org.sflphone.account.AccountDetailAdvanced;
7import org.sflphone.account.AccountDetailBasic;
8import org.sflphone.account.AccountDetailSrtp;
9import org.sflphone.account.AccountDetailTls;
Alexandre Lision4ab53972013-11-04 16:59:18 -050010import org.sflphone.client.SettingsActivity;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040011import org.sflphone.service.ISipService;
Alexandre Lision064e1e02013-10-01 16:18:42 -040012
alision5cfc35d2013-07-11 15:11:39 -040013import android.app.Activity;
alision5cfc35d2013-07-11 15:11:39 -040014import android.app.Fragment;
alision5cfc35d2013-07-11 15:11:39 -040015import android.content.Intent;
16import android.os.Bundle;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040017import android.os.RemoteException;
alision5cfc35d2013-07-11 15:11:39 -040018import android.text.TextUtils;
Alexandre Lision7f0bba52013-10-16 14:43:11 -040019import android.view.KeyEvent;
alision5cfc35d2013-07-11 15:11:39 -040020import android.view.LayoutInflater;
21import android.view.View;
22import android.view.ViewGroup;
23import android.widget.EditText;
Alexandre Lision7f0bba52013-10-16 14:43:11 -040024import android.widget.TextView;
25import android.widget.TextView.OnEditorActionListener;
alision5cfc35d2013-07-11 15:11:39 -040026
alision5cfc35d2013-07-11 15:11:39 -040027public 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 Lision7f0bba52013-10-16 14:43:11 -040040
Alexandre Lisiond16bad92013-10-09 17:16:20 -040041 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 }
alision5cfc35d2013-07-11 15:11:39 -040055
56 @Override
57 public void onCreate(Bundle savedInstanceState) {
58 super.onCreate(savedInstanceState);
alision5cfc35d2013-07-11 15:11:39 -040059 }
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 Lision7f0bba52013-10-16 14:43:11 -040069
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 });
alision5cfc35d2013-07-11 15:11:39 -040085 inflatedView.findViewById(R.id.create_button).setOnClickListener(new View.OnClickListener() {
86 @Override
87 public void onClick(View view) {
Alexandre Lisionf126dad2013-07-18 12:21:20 -040088 mAlias = mAliasView.getText().toString();
89 mHostname = mHostnameView.getText().toString();
90 mUsername = mUsernameView.getText().toString();
91 mPassword = mPasswordView.getText().toString();
alision5cfc35d2013-07-11 15:11:39 -040092 attemptCreation();
93 }
94 });
Alexandre Lision7f0bba52013-10-16 14:43:11 -040095
Alexandre Lision52214992013-10-28 17:41:23 -040096// inflatedView.findViewById(R.id.dev_account).setVisibility(View.GONE); // Hide this button in release apk
Alexandre Lisionf126dad2013-07-18 12:21:20 -040097 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 Lision7f0bba52013-10-16 14:43:11 -0400112 mPassword = "sfl_u" + mUsername;
Alexandre Lisionf126dad2013-07-18 12:21:20 -0400113 attemptCreation();
114 }
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400115
Alexandre Lisionf126dad2013-07-18 12:21:20 -0400116 }
117 });
alision5cfc35d2013-07-11 15:11:39 -0400118
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 Lision7f0bba52013-10-16 14:43:11 -0400132
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400133 @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 }
alision5cfc35d2013-07-11 15:11:39 -0400142
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 Lisionf126dad2013-07-18 12:21:20 -0400154
alision5cfc35d2013-07-11 15:11:39 -0400155 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 Lision80f0a7e2013-09-26 12:27:51 -0400220 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);
alision5cfc35d2013-07-11 15:11:39 -0400223 accountDetails.put(AccountDetailAdvanced.CONFIG_STUN_SERVER, "");
224 accountDetails.put(AccountDetailAdvanced.CONFIG_PUBLISHED_SAMEAS_LOCAL, AccountDetailAdvanced.CONFIG_DEFAULT_PUBLISHED_SAMEAS_LOCAL);
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400225 accountDetails.put(AccountDetailAdvanced.CONFIG_RINGTONE_ENABLED, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400226 accountDetails.put(AccountDetailAdvanced.CONFIG_RINGTONE_PATH, "");
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400227 accountDetails.put(AccountDetailAdvanced.CONFIG_STUN_ENABLE, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400228
229 accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE, "");
230 accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_RTP_FALLBACK, "");
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400231 accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_ENABLE, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400232 accountDetails.put(AccountDetailSrtp.CONFIG_ZRTP_DISPLAY_SAS, "");
233 accountDetails.put(AccountDetailSrtp.CONFIG_ZRTP_DISPLAY_SAS_ONCE, "");
alision5cfc35d2013-07-11 15:11:39 -0400234 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 Lision80f0a7e2013-09-26 12:27:51 -0400240 accountDetails.put(AccountDetailTls.CONFIG_TLS_ENABLE, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400241 accountDetails.put(AccountDetailTls.CONFIG_TLS_PASSWORD, "");
242 accountDetails.put(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE, "");
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400243
alision5cfc35d2013-07-11 15:11:39 -0400244 accountDetails.put(AccountDetailTls.CONFIG_TLS_SERVER_NAME, "");
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400245 accountDetails.put(AccountDetailTls.CONFIG_TLS_REQUIRE_CLIENT_CERTIFICATE, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400246 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 Lision7f0bba52013-10-16 14:43:11 -0400251
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400252 createNewAccount(accountDetails);
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400253
Alexandre Lision4ab53972013-11-04 16:59:18 -0500254 Intent resultIntent = new Intent(getActivity(), SettingsActivity.class);
alision5cfc35d2013-07-11 15:11:39 -0400255 getActivity().setResult(Activity.RESULT_OK, resultIntent);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400256 resultIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
257 startActivity(resultIntent);
alision5cfc35d2013-07-11 15:11:39 -0400258 getActivity().finish();
259
260 }
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400261
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400262 private void createNewAccount(HashMap<String, String> accountDetails) {
263 try {
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400264
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400265 mCallbacks.getService().addAccount(accountDetails);
266 } catch (RemoteException e) {
267 e.printStackTrace();
268 }
269 }
alision5cfc35d2013-07-11 15:11:39 -0400270
alision5cfc35d2013-07-11 15:11:39 -0400271}