blob: 62b3f2876ae73826288ba4d4861f6318636abdc9 [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.AccountDetailBasic;
Alexandre Lision4ab53972013-11-04 16:59:18 -05007import org.sflphone.client.SettingsActivity;
Alexandre Lisiond16bad92013-10-09 17:16:20 -04008import org.sflphone.service.ISipService;
Alexandre Lision064e1e02013-10-01 16:18:42 -04009
alision5cfc35d2013-07-11 15:11:39 -040010import android.app.Activity;
alision5cfc35d2013-07-11 15:11:39 -040011import android.app.Fragment;
alision5cfc35d2013-07-11 15:11:39 -040012import android.content.Intent;
13import android.os.Bundle;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040014import android.os.RemoteException;
alision5cfc35d2013-07-11 15:11:39 -040015import android.text.TextUtils;
Alexandre Lision7f0bba52013-10-16 14:43:11 -040016import android.view.KeyEvent;
alision5cfc35d2013-07-11 15:11:39 -040017import android.view.LayoutInflater;
18import android.view.View;
19import android.view.ViewGroup;
20import android.widget.EditText;
Alexandre Lision7f0bba52013-10-16 14:43:11 -040021import android.widget.TextView;
22import android.widget.TextView.OnEditorActionListener;
Alexandre Lision681d0292013-11-22 12:47:34 -050023import android.widget.Toast;
alision5cfc35d2013-07-11 15:11:39 -040024
alision5cfc35d2013-07-11 15:11:39 -040025public class AccountCreationFragment extends Fragment {
26
27 // Values for email and password at the time of the login attempt.
28 private String mAlias;
29 private String mHostname;
30 private String mUsername;
31 private String mPassword;
32
33 // UI references.
34 private EditText mAliasView;
35 private EditText mHostnameView;
36 private EditText mUsernameView;
37 private EditText mPasswordView;
Alexandre Lision7f0bba52013-10-16 14:43:11 -040038
Alexandre Lisiond16bad92013-10-09 17:16:20 -040039 private Callbacks mCallbacks = sDummyCallbacks;
40 private static Callbacks sDummyCallbacks = new Callbacks() {
41
42 @Override
43 public ISipService getService() {
44 return null;
45 }
46 };
47
48 public interface Callbacks {
49
50 public ISipService getService();
51
52 }
alision5cfc35d2013-07-11 15:11:39 -040053
54 @Override
55 public void onCreate(Bundle savedInstanceState) {
56 super.onCreate(savedInstanceState);
alision5cfc35d2013-07-11 15:11:39 -040057 }
58
59 @Override
60 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
61 View inflatedView = inflater.inflate(R.layout.frag_account_creation, parent, false);
62
63 mAliasView = (EditText) inflatedView.findViewById(R.id.alias);
64 mHostnameView = (EditText) inflatedView.findViewById(R.id.hostname);
65 mUsernameView = (EditText) inflatedView.findViewById(R.id.username);
66 mPasswordView = (EditText) inflatedView.findViewById(R.id.password);
Alexandre Lision7f0bba52013-10-16 14:43:11 -040067
68 mPasswordView.setOnEditorActionListener(new OnEditorActionListener() {
69
70 @Override
71 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
72 // if(actionId == EditorInfo.IME_ACTION_GO || event.getAction() == KeyEvent.KEYCODE_ENTER){
73 mAlias = mAliasView.getText().toString();
74 mHostname = mHostnameView.getText().toString();
75 mUsername = mUsernameView.getText().toString();
76 mPassword = mPasswordView.getText().toString();
77 attemptCreation();
78 // }
79
80 return true;
81 }
82 });
alision5cfc35d2013-07-11 15:11:39 -040083 inflatedView.findViewById(R.id.create_button).setOnClickListener(new View.OnClickListener() {
84 @Override
85 public void onClick(View view) {
Alexandre Lisionf126dad2013-07-18 12:21:20 -040086 mAlias = mAliasView.getText().toString();
87 mHostname = mHostnameView.getText().toString();
88 mUsername = mUsernameView.getText().toString();
89 mPassword = mPasswordView.getText().toString();
alision5cfc35d2013-07-11 15:11:39 -040090 attemptCreation();
91 }
92 });
Alexandre Lision7f0bba52013-10-16 14:43:11 -040093
Alexandre Lision681d0292013-11-22 12:47:34 -050094 inflatedView.findViewById(R.id.dev_account).setVisibility(View.GONE); // Hide this button in release apk
Alexandre Lisionf126dad2013-07-18 12:21:20 -040095 inflatedView.findViewById(R.id.dev_account).setOnClickListener(new View.OnClickListener() {
96 @Override
97 public void onClick(View view) {
98 createDevAccount();
99 }
100
101 private void createDevAccount() {
102 mUsername = mUsernameView.getText().toString();
103 if (TextUtils.isEmpty(mUsername)) {
104 mUsernameView.setError(getString(R.string.error_field_required));
105 mUsernameView.requestFocus();
106 return;
107 } else {
108 mAlias = mUsername;
109 mHostname = "192.95.9.63";
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400110 mPassword = "sfl_u" + mUsername;
Alexandre Lisionf126dad2013-07-18 12:21:20 -0400111 attemptCreation();
112 }
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400113
Alexandre Lisionf126dad2013-07-18 12:21:20 -0400114 }
115 });
alision5cfc35d2013-07-11 15:11:39 -0400116
117 return inflatedView;
118 }
119
120 @Override
121 public void onResume() {
122 super.onResume();
123 }
124
125 @Override
126 public void onStart() {
127 super.onStart();
128
129 }
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400130
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400131 @Override
132 public void onAttach(Activity activity) {
133 super.onAttach(activity);
134 if (!(activity instanceof Callbacks)) {
135 throw new IllegalStateException("Activity must implement fragment's callbacks.");
136 }
137
138 mCallbacks = (Callbacks) activity;
139 }
alision5cfc35d2013-07-11 15:11:39 -0400140
141 /**
142 * Attempts to sign in or register the account specified by the login form. If there are form errors (invalid email, missing fields, etc.), the
143 * errors are presented and no actual login attempt is made.
144 */
145 public void attemptCreation() {
146
147 // Reset errors.
148 mAliasView.setError(null);
149 mPasswordView.setError(null);
150
151 // Store values at the time of the login attempt.
Alexandre Lisionf126dad2013-07-18 12:21:20 -0400152
alision5cfc35d2013-07-11 15:11:39 -0400153 boolean cancel = false;
154 View focusView = null;
155
156 // Check for a valid password.
157 if (TextUtils.isEmpty(mPassword)) {
158 mPasswordView.setError(getString(R.string.error_field_required));
159 focusView = mPasswordView;
160 cancel = true;
161 }
162
163 if (TextUtils.isEmpty(mUsername)) {
164 mUsernameView.setError(getString(R.string.error_field_required));
165 focusView = mUsernameView;
166 cancel = true;
167 }
168
169 if (TextUtils.isEmpty(mHostname)) {
170 mHostnameView.setError(getString(R.string.error_field_required));
171 focusView = mHostnameView;
172 cancel = true;
173 }
174
175 // Check for a valid email address.
176 if (TextUtils.isEmpty(mAlias)) {
177 mAliasView.setError(getString(R.string.error_field_required));
178 focusView = mAliasView;
179 cancel = true;
180 }
181
182 if (cancel) {
183 // There was an error; don't attempt login and focus the first
184 // form field with an error.
185 focusView.requestFocus();
186 } else {
187 // Show a progress spinner, and kick off a background task to
188 // perform the user login attempt.
189 initCreation();
190
191 }
192 }
193
194 private void initCreation() {
Alexandre Lision681d0292013-11-22 12:47:34 -0500195
Alexandre Lision451f2a82013-11-12 12:55:55 -0500196 try {
197 HashMap<String, String> accountDetails = (HashMap<String, String>) mCallbacks.getService().getAccountTemplate();
198 accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_ALIAS, mAlias);
199 accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_HOSTNAME, mHostname);
200 accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME, mUsername);
201 accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD, mPassword);
Alexandre Lision681d0292013-11-22 12:47:34 -0500202
Alexandre Lision451f2a82013-11-12 12:55:55 -0500203 createNewAccount(accountDetails);
alision5cfc35d2013-07-11 15:11:39 -0400204
Alexandre Lision451f2a82013-11-12 12:55:55 -0500205 } catch (RemoteException e) {
206 Toast.makeText(getActivity(), "Error creating account", Toast.LENGTH_SHORT).show();
207 e.printStackTrace();
208 }
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400209
Alexandre Lision4ab53972013-11-04 16:59:18 -0500210 Intent resultIntent = new Intent(getActivity(), SettingsActivity.class);
alision5cfc35d2013-07-11 15:11:39 -0400211 getActivity().setResult(Activity.RESULT_OK, resultIntent);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400212 resultIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
213 startActivity(resultIntent);
alision5cfc35d2013-07-11 15:11:39 -0400214 getActivity().finish();
215
216 }
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400217
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400218 private void createNewAccount(HashMap<String, String> accountDetails) {
219 try {
Alexandre Lision7f0bba52013-10-16 14:43:11 -0400220
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400221 mCallbacks.getService().addAccount(accountDetails);
222 } catch (RemoteException e) {
223 e.printStackTrace();
224 }
225 }
alision5cfc35d2013-07-11 15:11:39 -0400226
alision5cfc35d2013-07-11 15:11:39 -0400227}