blob: 388f2a1048dd50d2e29b7b0f867409f900160fd0 [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;
6import org.sflphone.account.AccountDetail;
7import org.sflphone.account.AccountDetailAdvanced;
8import org.sflphone.account.AccountDetailBasic;
9import org.sflphone.account.AccountDetailSrtp;
10import org.sflphone.account.AccountDetailTls;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040011import org.sflphone.client.SFLPhonePreferenceActivity;
12import org.sflphone.client.SFLPhonePreferenceActivity.PreferencesPagerAdapter;
13import org.sflphone.fragments.AccountManagementFragment.Callbacks;
14import org.sflphone.service.ISipService;
Alexandre Lision064e1e02013-10-01 16:18:42 -040015
alision5cfc35d2013-07-11 15:11:39 -040016import android.app.Activity;
alision5cfc35d2013-07-11 15:11:39 -040017import android.app.Fragment;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040018import android.content.ComponentName;
alision5cfc35d2013-07-11 15:11:39 -040019import android.content.Intent;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040020import android.content.ServiceConnection;
alision5cfc35d2013-07-11 15:11:39 -040021import android.os.Bundle;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040022import android.os.IBinder;
23import android.os.RemoteException;
alision5cfc35d2013-07-11 15:11:39 -040024import android.text.TextUtils;
Alexandre Lisiond16bad92013-10-09 17:16:20 -040025import android.util.Log;
alision5cfc35d2013-07-11 15:11:39 -040026import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.EditText;
alision5cfc35d2013-07-11 15:11:39 -040030
alision5cfc35d2013-07-11 15:11:39 -040031public 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 Lisiond16bad92013-10-09 17:16:20 -040044
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
alision5cfc35d2013-07-11 15:11:39 -040061
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 Lisionf126dad2013-07-18 12:21:20 -040079 mAlias = mAliasView.getText().toString();
80 mHostname = mHostnameView.getText().toString();
81 mUsername = mUsernameView.getText().toString();
82 mPassword = mPasswordView.getText().toString();
alision5cfc35d2013-07-11 15:11:39 -040083 attemptCreation();
84 }
85 });
Alexandre Lisionf126dad2013-07-18 12:21:20 -040086
Alexandre Lision8b9d8e82013-10-04 09:21:27 -040087// inflatedView.findViewById(R.id.dev_account).setVisibility(View.GONE); // Hide this button in release apk
Alexandre Lisionf126dad2013-07-18 12:21:20 -040088 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 });
alision5cfc35d2013-07-11 15:11:39 -0400109
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 Lisiond16bad92013-10-09 17:16:20 -0400123
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 }
alision5cfc35d2013-07-11 15:11:39 -0400133
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 Lisionf126dad2013-07-18 12:21:20 -0400145
alision5cfc35d2013-07-11 15:11:39 -0400146
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 Lision80f0a7e2013-09-26 12:27:51 -0400212 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);
alision5cfc35d2013-07-11 15:11:39 -0400215 accountDetails.put(AccountDetailAdvanced.CONFIG_STUN_SERVER, "");
216 accountDetails.put(AccountDetailAdvanced.CONFIG_PUBLISHED_SAMEAS_LOCAL, AccountDetailAdvanced.CONFIG_DEFAULT_PUBLISHED_SAMEAS_LOCAL);
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400217 accountDetails.put(AccountDetailAdvanced.CONFIG_RINGTONE_ENABLED, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400218 accountDetails.put(AccountDetailAdvanced.CONFIG_RINGTONE_PATH, "");
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400219 accountDetails.put(AccountDetailAdvanced.CONFIG_STUN_ENABLE, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400220
221 accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE, "");
222 accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_RTP_FALLBACK, "");
Alexandre Lision80f0a7e2013-09-26 12:27:51 -0400223 accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_ENABLE, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400224 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 Lision80f0a7e2013-09-26 12:27:51 -0400234 accountDetails.put(AccountDetailTls.CONFIG_TLS_ENABLE, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400235 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 Lision80f0a7e2013-09-26 12:27:51 -0400239 accountDetails.put(AccountDetailTls.CONFIG_TLS_REQUIRE_CLIENT_CERTIFICATE, AccountDetailAdvanced.FALSE_STR);
alision5cfc35d2013-07-11 15:11:39 -0400240 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 Lisiond16bad92013-10-09 17:16:20 -0400247
248
249 createNewAccount(accountDetails);
250
251 Intent resultIntent = new Intent(getActivity(), SFLPhonePreferenceActivity.class);
alision5cfc35d2013-07-11 15:11:39 -0400252 getActivity().setResult(Activity.RESULT_OK, resultIntent);
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400253 resultIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
254 startActivity(resultIntent);
alision5cfc35d2013-07-11 15:11:39 -0400255 getActivity().finish();
256
257 }
Alexandre Lisiond16bad92013-10-09 17:16:20 -0400258
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 }
alision5cfc35d2013-07-11 15:11:39 -0400267
alision5cfc35d2013-07-11 15:11:39 -0400268}