blob: 69ec50cd8b7e5b4d52867e09607f82ace3fe788f [file] [log] [blame]
alision5cfc35d2013-07-11 15:11:39 -04001package com.savoirfairelinux.sflphone.fragments;
2
3import java.util.HashMap;
4
5import android.app.Activity;
6import android.app.AlertDialog;
7import android.app.Dialog;
8import android.app.Fragment;
9import android.content.DialogInterface;
10import android.content.Intent;
11import android.os.Bundle;
12import android.text.TextUtils;
13import android.view.LayoutInflater;
14import android.view.View;
15import android.view.ViewGroup;
16import android.widget.EditText;
17import android.widget.TextView;
18import android.widget.Toast;
19
20import com.savoirfairelinux.sflphone.R;
21import com.savoirfairelinux.sflphone.account.AccountDetail;
22import com.savoirfairelinux.sflphone.account.AccountDetailAdvanced;
23import com.savoirfairelinux.sflphone.account.AccountDetailBasic;
24import com.savoirfairelinux.sflphone.account.AccountDetailSrtp;
25import com.savoirfairelinux.sflphone.account.AccountDetailTls;
26
27public 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;
40
41 @Override
42 public void onCreate(Bundle savedInstanceState) {
43 super.onCreate(savedInstanceState);
44 // mAdapter = new HistoryAdapter(getActivity(),new ArrayList<HashMap<String, String>>());
45 }
46
47 @Override
48 public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
49 View inflatedView = inflater.inflate(R.layout.frag_account_creation, parent, false);
50
51 mAliasView = (EditText) inflatedView.findViewById(R.id.alias);
52 mHostnameView = (EditText) inflatedView.findViewById(R.id.hostname);
53 mUsernameView = (EditText) inflatedView.findViewById(R.id.username);
54 mPasswordView = (EditText) inflatedView.findViewById(R.id.password);
55 inflatedView.findViewById(R.id.create_button).setOnClickListener(new View.OnClickListener() {
56 @Override
57 public void onClick(View view) {
58 attemptCreation();
59 }
60 });
61
62 return inflatedView;
63 }
64
65 @Override
66 public void onResume() {
67 super.onResume();
68 }
69
70 @Override
71 public void onStart() {
72 super.onStart();
73
74 }
75
76 /**
77 * Attempts to sign in or register the account specified by the login form. If there are form errors (invalid email, missing fields, etc.), the
78 * errors are presented and no actual login attempt is made.
79 */
80 public void attemptCreation() {
81
82 // Reset errors.
83 mAliasView.setError(null);
84 mPasswordView.setError(null);
85
86 // Store values at the time of the login attempt.
87 mAlias = mAliasView.getText().toString();
88 mHostname = mHostnameView.getText().toString();
89 mUsername = mUsernameView.getText().toString();
90 mPassword = mPasswordView.getText().toString();
91
92 boolean cancel = false;
93 View focusView = null;
94
95 // Check for a valid password.
96 if (TextUtils.isEmpty(mPassword)) {
97 mPasswordView.setError(getString(R.string.error_field_required));
98 focusView = mPasswordView;
99 cancel = true;
100 }
101
102 if (TextUtils.isEmpty(mUsername)) {
103 mUsernameView.setError(getString(R.string.error_field_required));
104 focusView = mUsernameView;
105 cancel = true;
106 }
107
108 if (TextUtils.isEmpty(mHostname)) {
109 mHostnameView.setError(getString(R.string.error_field_required));
110 focusView = mHostnameView;
111 cancel = true;
112 }
113
114 // Check for a valid email address.
115 if (TextUtils.isEmpty(mAlias)) {
116 mAliasView.setError(getString(R.string.error_field_required));
117 focusView = mAliasView;
118 cancel = true;
119 }
120
121 if (cancel) {
122 // There was an error; don't attempt login and focus the first
123 // form field with an error.
124 focusView.requestFocus();
125 } else {
126 // Show a progress spinner, and kick off a background task to
127 // perform the user login attempt.
128 initCreation();
129
130 }
131 }
132
133 private void initCreation() {
134
135 HashMap<String, String> accountDetails = new HashMap<String, String>();
136
137 accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_TYPE, AccountDetailBasic.CONFIG_ACCOUNT_DEFAULT_TYPE);
138 accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_ALIAS, mAlias);
139 accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_HOSTNAME, mHostname);
140 accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_USERNAME, mUsername);
141 accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD, mPassword);
142 accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_ROUTESET, "");
143 accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_REALM, AccountDetailBasic.CONFIG_ACCOUNT_DEFAULT_REALM);
144 accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_ENABLE, AccountDetailBasic.CONFIG_ACCOUNT_DEFAULT_ENABLE);
145 accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_PASSWORD, mPassword);
146 accountDetails.put(AccountDetailBasic.CONFIG_ACCOUNT_USERAGENT, AccountDetailBasic.CONFIG_ACCOUNT_DEFAULT_USERAGENT);
147
148 accountDetails.put(AccountDetailAdvanced.CONFIG_LOCAL_PORT, AccountDetailAdvanced.CONFIG_DEFAULT_LOCAL_PORT);
149 accountDetails.put(AccountDetailAdvanced.CONFIG_LOCAL_INTERFACE, AccountDetailAdvanced.CONFIG_DEFAULT_INTERFACE);
150 accountDetails.put(AccountDetailAdvanced.CONFIG_PUBLISHED_PORT, AccountDetailAdvanced.CONFIG_DEFAULT_PUBLISHED_PORT);
151 accountDetails.put(AccountDetailAdvanced.CONFIG_PUBLISHED_ADDRESS, AccountDetailAdvanced.CONFIG_DEFAULT_ADDRESS);
152 accountDetails.put(AccountDetailAdvanced.CONFIG_ACCOUNT_REGISTRATION_EXPIRE, AccountDetailAdvanced.CONFIG_DEFAULT_REGISTRATION_EXPIRE);
153 accountDetails.put(AccountDetailAdvanced.CONFIG_STUN_SERVER, "");
154 accountDetails.put(AccountDetailAdvanced.CONFIG_ACCOUNT_REGISTRATION_STATUS, "");
155 accountDetails.put(AccountDetailAdvanced.CONFIG_ACCOUNT_REGISTRATION_STATE_CODE, "");
156 accountDetails.put(AccountDetailAdvanced.CONFIG_ACCOUNT_REGISTRATION_STATE_DESC, "");
157 accountDetails.put(AccountDetailAdvanced.CONFIG_ACCOUNT_AUTOANSWER, "");
158 accountDetails.put(AccountDetailAdvanced.CONFIG_ACCOUNT_DTMF_TYPE, "");
159 accountDetails.put(AccountDetailAdvanced.CONFIG_KEEP_ALIVE_ENABLED, "");
160 accountDetails.put(AccountDetailAdvanced.CONFIG_STUN_SERVER, "");
161 accountDetails.put(AccountDetailAdvanced.CONFIG_PUBLISHED_SAMEAS_LOCAL, AccountDetailAdvanced.CONFIG_DEFAULT_PUBLISHED_SAMEAS_LOCAL);
162 accountDetails.put(AccountDetailAdvanced.CONFIG_RINGTONE_ENABLED, "false");
163 accountDetails.put(AccountDetailAdvanced.CONFIG_RINGTONE_PATH, "");
164 accountDetails.put(AccountDetailAdvanced.CONFIG_STUN_ENABLE, "false");
165
166 accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE, "");
167 accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_RTP_FALLBACK, "");
168 accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_ENABLE, "");
169 accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE, "");
170 accountDetails.put(AccountDetailSrtp.CONFIG_ZRTP_DISPLAY_SAS, "");
171 accountDetails.put(AccountDetailSrtp.CONFIG_ZRTP_DISPLAY_SAS_ONCE, "");
172 accountDetails.put(AccountDetailSrtp.CONFIG_SRTP_KEY_EXCHANGE, "");
173 accountDetails.put(AccountDetailSrtp.CONFIG_ZRTP_HELLO_HASH, "");
174 accountDetails.put(AccountDetailSrtp.CONFIG_ZRTP_NOT_SUPP_WARNING, "");
175
176 accountDetails.put(AccountDetailTls.CONFIG_TLS_CIPHERS, "");
177 accountDetails.put(AccountDetailTls.CONFIG_TLS_LISTENER_PORT, "");
178 accountDetails.put(AccountDetailTls.CONFIG_TLS_METHOD, "");
179 accountDetails.put(AccountDetailTls.CONFIG_TLS_ENABLE, "");
180 accountDetails.put(AccountDetailTls.CONFIG_TLS_PASSWORD, "");
181 accountDetails.put(AccountDetailTls.CONFIG_TLS_PRIVATE_KEY_FILE, "");
182
183 accountDetails.put(AccountDetailTls.CONFIG_TLS_SERVER_NAME, "");
184 accountDetails.put(AccountDetailTls.CONFIG_TLS_REQUIRE_CLIENT_CERTIFICATE, "false");
185 accountDetails.put(AccountDetailTls.CONFIG_TLS_LISTENER_PORT, "");
186 accountDetails.put(AccountDetailTls.CONFIG_TLS_VERIFY_CLIENT, "");
187 accountDetails.put(AccountDetailTls.CONFIG_TLS_CERTIFICATE_FILE, "");
188 accountDetails.put(AccountDetailTls.CONFIG_TLS_CA_LIST_FILE, "");
189 accountDetails.put(AccountDetailTls.CONFIG_TLS_VERIFY_SERVER, "");
190
191 Bundle bundle = new Bundle();
192 bundle.putSerializable(AccountDetail.TAG, accountDetails);
193 Intent resultIntent = new Intent();
194 resultIntent.putExtras(bundle);
195
196 getActivity().setResult(Activity.RESULT_OK, resultIntent);
197 getActivity().finish();
198
199 }
200
201 private AlertDialog createCancelDialog() {
202 Activity ownerActivity = getActivity();
203 AlertDialog.Builder builder = new AlertDialog.Builder(ownerActivity);
204 builder.setMessage("All parameters will be lost").setTitle("Account Creation").setPositiveButton("Ok", new DialogInterface.OnClickListener() {
205 public void onClick(DialogInterface dialog, int whichButton) {
206 Activity activity = ((Dialog) dialog).getOwnerActivity();
207 activity.finish();
208 }
209 }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
210 public void onClick(DialogInterface dialog, int whichButton) {
211 /* Terminate with no action */
212 }
213 });
214
215 AlertDialog alertDialog = builder.create();
216 alertDialog.setOwnerActivity(ownerActivity);
217
218 return alertDialog;
219 }
220
221}