blob: 7a7eaa32bc7d13891a7b7d783eb219888e987621 [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 Lisiond16bad92013-10-09 17:16:20 -040010import org.sflphone.client.SFLPhonePreferenceActivity;
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 Lisiond16bad92013-10-09 17:16:20 -040019import android.util.Log;
Alexandre Lision7f0bba52013-10-16 14:43:11 -040020import android.view.KeyEvent;
alision5cfc35d2013-07-11 15:11:39 -040021import android.view.LayoutInflater;
22import android.view.View;
23import android.view.ViewGroup;
Alexandre Lision7f0bba52013-10-16 14:43:11 -040024import android.view.inputmethod.EditorInfo;
alision5cfc35d2013-07-11 15:11:39 -040025import android.widget.EditText;
Alexandre Lision7f0bba52013-10-16 14:43:11 -040026import android.widget.TextView;
27import android.widget.TextView.OnEditorActionListener;
alision5cfc35d2013-07-11 15:11:39 -040028
alision5cfc35d2013-07-11 15:11:39 -040029public 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 Lision7f0bba52013-10-16 14:43:11 -040042
Alexandre Lisiond16bad92013-10-09 17:16:20 -040043 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 }
alision5cfc35d2013-07-11 15:11:39 -040057
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 Lision7f0bba52013-10-16 14:43:11 -040072
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 });
alision5cfc35d2013-07-11 15:11:39 -040088 inflatedView.findViewById(R.id.create_button).setOnClickListener(new View.OnClickListener() {
89 @Override
90 public void onClick(View view) {
Alexandre Lisionf126dad2013-07-18 12:21:20 -040091 mAlias = mAliasView.getText().toString();
92 mHostname = mHostnameView.getText().toString();
93 mUsername = mUsernameView.getText().toString();
94 mPassword = mPasswordView.getText().toString();
alision5cfc35d2013-07-11 15:11:39 -040095 attemptCreation();
96 }
97 });
Alexandre Lision7f0bba52013-10-16 14:43:11 -040098
99 // inflatedView.findViewById(R.id.dev_account).setVisibility(View.GONE); // Hide this button in release apk
Alexandre Lisionf126dad2013-07-18 12:21:20 -0400100 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 Lision7f0bba52013-10-16 14:43:11 -0400115 mPassword = "sfl_u" + mUsername;
Alexandre Lisionf126dad2013-07-18 12:21:20 -0400116 attemptCreation();
117 }
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400118
Alexandre Lisionf126dad2013-07-18 12:21:20 -0400119 }
120 });
alision5cfc35d2013-07-11 15:11:39 -0400121
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 Lision7f0bba52013-10-16 14:43:11 -0400135
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400136 @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 }
alision5cfc35d2013-07-11 15:11:39 -0400145
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 Lisionf126dad2013-07-18 12:21:20 -0400157
alision5cfc35d2013-07-11 15:11:39 -0400158 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 Lision80f0a7e2013-09-26 12:27:51 -0400223 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);
alision5cfc35d2013-07-11 15:11:39 -0400226 accountDetails.put(AccountDetailAdvanced.CONFIG_STUN_SERVER, "");
227 accountDetails.put(AccountDetailAdvanced.CONFIG_PUBLISHED_SAMEAS_LOCAL, AccountDetailAdvanced.CONFIG_DEFAULT_PUBLISHED_SAMEAS_LOCAL);
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400228 accountDetails.put(AccountDetailAdvanced.CONFIG_RINGTONE_ENABLED, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400229 accountDetails.put(AccountDetailAdvanced.CONFIG_RINGTONE_PATH, "");
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400230 accountDetails.put(AccountDetailAdvanced.CONFIG_STUN_ENABLE, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400231
232 accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE, "");
233 accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_RTP_FALLBACK, "");
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400234 accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_ENABLE, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400235 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 Lision80f0a7e2013-09-26 12:27:51 -0400245 accountDetails.put(AccountDetailTls.CONFIG_TLS_ENABLE, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400246 accountDetails.put(AccountDetailTls.CONFIG_TLS_PASSWORD, "");
247 accountDetails.put(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE, "");
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400248
alision5cfc35d2013-07-11 15:11:39 -0400249 accountDetails.put(AccountDetailTls.CONFIG_TLS_SERVER_NAME, "");
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400250 accountDetails.put(AccountDetailTls.CONFIG_TLS_REQUIRE_CLIENT_CERTIFICATE, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400251 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 Lision7f0bba52013-10-16 14:43:11 -0400256
alision5cfc35d2013-07-11 15:11:39 -0400257 Bundle bundle = new Bundle();
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400258
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400259 createNewAccount(accountDetails);
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400260
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400261 Intent resultIntent = new Intent(getActivity(), SFLPhonePreferenceActivity.class);
alision5cfc35d2013-07-11 15:11:39 -0400262 getActivity().setResult(Activity.RESULT_OK, resultIntent);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400263 resultIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
264 startActivity(resultIntent);
alision5cfc35d2013-07-11 15:11:39 -0400265 getActivity().finish();
266
267 }
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400268
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400269 private void createNewAccount(HashMap<String, String> accountDetails) {
270 try {
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400271
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400272 mCallbacks.getService().addAccount(accountDetails);
273 } catch (RemoteException e) {
274 e.printStackTrace();
275 }
276 }
alision5cfc35d2013-07-11 15:11:39 -0400277
alision5cfc35d2013-07-11 15:11:39 -0400278}