212
|
1 |
package com.beem.project.beem.ui; |
|
2 |
|
325
|
3 |
import java.util.regex.Pattern; |
|
4 |
|
212
|
5 |
import org.jivesoftware.smack.AccountManager; |
|
6 |
import org.jivesoftware.smack.ConnectionConfiguration; |
|
7 |
import org.jivesoftware.smack.XMPPConnection; |
|
8 |
import org.jivesoftware.smack.XMPPException; |
|
9 |
import org.jivesoftware.smack.proxy.ProxyInfo; |
325
|
10 |
import org.jivesoftware.smack.util.StringUtils; |
212
|
11 |
|
|
12 |
import android.app.Activity; |
|
13 |
import android.app.AlertDialog; |
|
14 |
import android.content.DialogInterface; |
|
15 |
import android.content.SharedPreferences; |
|
16 |
import android.os.Bundle; |
|
17 |
import android.util.Log; |
|
18 |
import android.view.View; |
|
19 |
import android.widget.Button; |
|
20 |
import android.widget.EditText; |
325
|
21 |
import android.widget.TextView; |
212
|
22 |
import android.widget.Toast; |
|
23 |
|
|
24 |
import com.beem.project.beem.R; |
|
25 |
|
|
26 |
/** |
325
|
27 |
* This class represents an activity which allows the user to create an account on the XMPP server saved in settings |
212
|
28 |
* @author dasilvj |
|
29 |
*/ |
|
30 |
public class CreateAccount extends Activity { |
|
31 |
|
|
32 |
private final static boolean DEFAULT_BOOLEAN_VALUE = false; |
325
|
33 |
private final static String DEFAULT_STRING_VALUE = ""; |
|
34 |
private final static int DEFAULT_INT_VALUE = 0; |
212
|
35 |
|
325
|
36 |
private final static String DEFAULT_XMPP_PORT = "5222"; |
212
|
37 |
|
325
|
38 |
private final static int NOTIFICATION_DURATION = Toast.LENGTH_SHORT; |
212
|
39 |
|
325
|
40 |
private SharedPreferences settings = null; |
|
41 |
private Button createAccountButton = null; |
256
|
42 |
|
|
43 |
/** |
|
44 |
* {@inheritDoc} |
|
45 |
*/ |
|
46 |
@Override |
|
47 |
public void onCreate(Bundle savedInstanceState) { |
|
48 |
super.onCreate(savedInstanceState); |
|
49 |
setContentView(R.layout.create_account); |
|
50 |
initCreateAccountButton(); |
|
51 |
settings = getSharedPreferences(getString(R.string.settings_filename), MODE_PRIVATE); |
|
52 |
} |
|
53 |
|
212
|
54 |
/** |
|
55 |
* Create an account on the XMPP server specified in settings |
|
56 |
*/ |
256
|
57 |
private boolean createAccount(String username, String password) { |
212
|
58 |
XMPPConnection xmppConnection = null; |
|
59 |
ConnectionConfiguration connectionConfiguration = null; |
|
60 |
|
|
61 |
if (getRegisteredProxy() != null) { |
325
|
62 |
connectionConfiguration = new ConnectionConfiguration(getXMPPServer(), 5222, getRegisteredProxy()); |
212
|
63 |
} else { |
325
|
64 |
connectionConfiguration = new ConnectionConfiguration(getXMPPServer(), 5222); |
212
|
65 |
} |
|
66 |
if (getRegisteredXMPPTLSUse()) |
|
67 |
connectionConfiguration.setSecurityMode(ConnectionConfiguration.SecurityMode.required); |
|
68 |
|
|
69 |
xmppConnection = new XMPPConnection(connectionConfiguration); |
|
70 |
try { |
|
71 |
xmppConnection.connect(); |
|
72 |
AccountManager accountManager = new AccountManager(xmppConnection); |
|
73 |
accountManager.createAccount(username, password); |
|
74 |
Toast toast = Toast.makeText(getApplicationContext(), "Account " + username + " " |
256
|
75 |
+ getString(R.string.create_account_successfull_after), NOTIFICATION_DURATION); |
212
|
76 |
toast.show(); |
|
77 |
} catch (XMPPException e) { |
|
78 |
Log.e(getString(R.string.create_account_tag), e.getMessage()); |
|
79 |
createErrorDialog(e.getMessage()); |
256
|
80 |
return false; |
212
|
81 |
} |
|
82 |
xmppConnection.disconnect(); |
256
|
83 |
return true; |
212
|
84 |
} |
|
85 |
|
|
86 |
/** |
|
87 |
* Create a dialog containing an error message |
325
|
88 |
* @param errMsg the error message |
212
|
89 |
*/ |
|
90 |
private void createErrorDialog(String errMsg) { |
|
91 |
Log.v(getString(R.string.create_account_tag), errMsg); |
|
92 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
|
93 |
builder.setTitle(R.string.create_account_err_dialog_title).setMessage(errMsg).setCancelable(false); |
|
94 |
builder.setNeutralButton(R.string.create_account_close_dialog_button, new DialogInterface.OnClickListener() { |
|
95 |
|
|
96 |
@Override |
|
97 |
public void onClick(DialogInterface dialog, int which) { |
|
98 |
dialog.cancel(); |
|
99 |
} |
|
100 |
}); |
|
101 |
AlertDialog settingsErrDialog = builder.create(); |
|
102 |
settingsErrDialog.show(); |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* Retrive proxy informations from the preferences |
|
107 |
* @return Registered proxy informations |
|
108 |
*/ |
|
109 |
private ProxyInfo getRegisteredProxy() { |
|
110 |
if (getRegisteredProxyUse()) { |
|
111 |
ProxyInfo proxyInfo = new ProxyInfo(getRegisteredProxyType(), getRegisteredProxyServer(), |
256
|
112 |
getRegisteredProxyPort(), getRegisteredProxyUsername(), getRegisteredProxyPassword()); |
212
|
113 |
return (proxyInfo); |
|
114 |
} |
|
115 |
return (null); |
|
116 |
} |
|
117 |
|
|
118 |
/** |
|
119 |
* Retrieve proxy password from the preferences |
|
120 |
* @return Registered proxy password |
|
121 |
*/ |
|
122 |
private String getRegisteredProxyPassword() { |
|
123 |
return (settings.getString(getString(R.string.settings_key_proxy_password), DEFAULT_STRING_VALUE)); |
|
124 |
} |
|
125 |
|
|
126 |
/** |
|
127 |
* Retrieve proxy port from the preferences |
|
128 |
* @return Registered proxy port |
|
129 |
*/ |
|
130 |
private int getRegisteredProxyPort() { |
|
131 |
return (Integer.parseInt(settings.getString(getString(R.string.settings_key_proxy_port), DEFAULT_STRING_VALUE))); |
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* Retrieve proxy server from the preferences |
|
136 |
* @return Registered proxy server |
|
137 |
*/ |
|
138 |
private String getRegisteredProxyServer() { |
|
139 |
return (settings.getString(getString(R.string.settings_key_proxy_server), DEFAULT_STRING_VALUE)); |
|
140 |
} |
|
141 |
|
|
142 |
/** |
|
143 |
* Retrieve proxy type from the preferences |
|
144 |
* @return Registered proxy type |
|
145 |
*/ |
|
146 |
private ProxyInfo.ProxyType getRegisteredProxyType() { |
|
147 |
switch (settings.getInt(getString(R.string.settings_key_proxy_type), DEFAULT_INT_VALUE)) { |
256
|
148 |
case 0: |
|
149 |
return ProxyInfo.ProxyType.HTTP; |
|
150 |
case 1: |
|
151 |
return ProxyInfo.ProxyType.SOCKS4; |
|
152 |
case 2: |
|
153 |
Log.i(getString(R.string.create_account_tag), "SOCKS5 PROXY"); |
|
154 |
return ProxyInfo.ProxyType.SOCKS5; |
|
155 |
default: |
|
156 |
return ProxyInfo.ProxyType.NONE; |
212
|
157 |
} |
|
158 |
} |
|
159 |
|
|
160 |
/** |
|
161 |
* Retrieve proxy use from the preferences |
|
162 |
* @return Registered proxy use |
|
163 |
*/ |
|
164 |
private boolean getRegisteredProxyUse() { |
|
165 |
return (settings.getBoolean(getString(R.string.settings_key_proxy_use), DEFAULT_BOOLEAN_VALUE)); |
|
166 |
} |
|
167 |
|
|
168 |
/** |
|
169 |
* Retrieve proxy username from the preferences |
|
170 |
* @return Registered proxy username |
|
171 |
*/ |
|
172 |
private String getRegisteredProxyUsername() { |
|
173 |
return (settings.getString(getString(R.string.settings_key_proxy_username), DEFAULT_STRING_VALUE)); |
|
174 |
} |
|
175 |
|
|
176 |
/** |
|
177 |
* Retrieve xmpp server from the preferences |
|
178 |
* @return Registered xmpp server |
|
179 |
*/ |
325
|
180 |
private String getXMPPServer() { |
|
181 |
TextView xmppServerTextView = (TextView) findViewById(R.id.create_account_username); |
|
182 |
String xmppServer = StringUtils.parseServer(xmppServerTextView.getText().toString()); |
|
183 |
return xmppServer; |
212
|
184 |
} |
|
185 |
|
|
186 |
/** |
|
187 |
* Retrieve TLS use from the preferences |
|
188 |
* @return Registered TLS use |
|
189 |
*/ |
|
190 |
private boolean getRegisteredXMPPTLSUse() { |
|
191 |
return (settings.getBoolean(getString(R.string.settings_key_xmpp_tls_use), DEFAULT_BOOLEAN_VALUE)); |
|
192 |
} |
|
193 |
|
|
194 |
/** |
325
|
195 |
* Check if the fields password and confirm password match |
|
196 |
* @return return true if password & confirm password fields match, else false |
|
197 |
*/ |
|
198 |
private boolean checkPasswords() { |
|
199 |
final String passwordFieldValue = ((EditText) findViewById(R.id.create_account_password)).getText().toString(); |
|
200 |
final String passwordConfirmFielddValue = ((EditText) findViewById(R.id.create_account_confirm_password)) |
|
201 |
.getText().toString(); |
|
202 |
|
|
203 |
if (passwordFieldValue.equals(passwordConfirmFielddValue)) |
|
204 |
return (true); |
|
205 |
return (false); |
|
206 |
} |
|
207 |
|
|
208 |
private boolean checkEmail() { |
|
209 |
String email = ((TextView) findViewById(R.id.create_account_username)).getText().toString(); |
|
210 |
return (Pattern.matches("[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+.)+[a-zA-Z]{2,4}", email)); |
|
211 |
} |
|
212 |
|
|
213 |
/** |
212
|
214 |
* Initialize the "Create this account" button which allows the user to create an account |
|
215 |
*/ |
|
216 |
private void initCreateAccountButton() { |
|
217 |
createAccountButton = (Button) findViewById(R.id.create_account_button); |
|
218 |
createAccountButton.setOnClickListener(new View.OnClickListener() { |
|
219 |
public void onClick(View v) { |
|
220 |
String usernameFieldValue = ((EditText) findViewById(R.id.create_account_username)).getText() |
325
|
221 |
.toString(); |
212
|
222 |
String passwordFieldValue = ((EditText) findViewById(R.id.create_account_password)).getText() |
325
|
223 |
.toString(); |
|
224 |
String username = StringUtils.parseName(usernameFieldValue); |
|
225 |
if (!checkEmail()) |
|
226 |
createErrorDialog(getString(R.string.create_account_err_username)); |
|
227 |
else if (!checkPasswords()) |
|
228 |
createErrorDialog(getString(R.string.create_account_err_passwords)); |
|
229 |
else { |
|
230 |
if (createAccount(username, passwordFieldValue)) |
|
231 |
finish(); |
|
232 |
} |
212
|
233 |
|
|
234 |
} |
|
235 |
}); |
256
|
236 |
Button createAccountLoginButton = (Button) findViewById(R.id.create_account_login_button); |
|
237 |
createAccountLoginButton.setOnClickListener(new View.OnClickListener() { |
|
238 |
public void onClick(View v) { |
|
239 |
String usernameFieldValue = ((EditText) findViewById(R.id.create_account_username)).getText() |
325
|
240 |
.toString(); |
|
241 |
String username = StringUtils.parseName(usernameFieldValue); |
256
|
242 |
String passwordFieldValue = ((EditText) findViewById(R.id.create_account_password)).getText() |
325
|
243 |
.toString(); |
|
244 |
if (!checkEmail()) |
|
245 |
createErrorDialog(getString(R.string.create_account_err_username)); |
|
246 |
else if (!checkPasswords()) |
256
|
247 |
createErrorDialog(getString(R.string.create_account_err_passwords)); |
|
248 |
else { |
325
|
249 |
if (createAccount(username, passwordFieldValue)) { |
256
|
250 |
SharedPreferences.Editor settingsEditor = settings.edit(); |
326
|
251 |
settingsEditor.putString(getString(R.string.settings_key_account_username), usernameFieldValue); |
256
|
252 |
settingsEditor.putString(getString(R.string.settings_key_account_password), passwordFieldValue); |
|
253 |
settingsEditor.commit(); |
|
254 |
finish(); |
|
255 |
} |
|
256 |
} |
|
257 |
} |
|
258 |
}); |
325
|
259 |
} |
212
|
260 |
} |