SHOW:
|
|
- or go back to the newest paste.
1 | import android.animation.Animator; | |
2 | import android.animation.AnimatorListenerAdapter; | |
3 | import android.annotation.TargetApi; | |
4 | import android.app.AlertDialog; | |
5 | import android.content.DialogInterface; | |
6 | - | // Keep track of the login task to ensure we can cancel it if requested. |
6 | + | import android.content.SharedPreferences; |
7 | import android.os.AsyncTask; | |
8 | import android.os.Build; | |
9 | import android.os.Bundle; | |
10 | import android.support.v7.app.ActionBarActivity; | |
11 | import android.text.TextUtils; | |
12 | import android.view.KeyEvent; | |
13 | import android.view.View; | |
14 | import android.view.inputmethod.EditorInfo; | |
15 | import android.widget.AutoCompleteTextView; | |
16 | import android.widget.CheckBox; | |
17 | import android.widget.EditText; | |
18 | import android.widget.TextView; | |
19 | import android.widget.Toast; | |
20 | ||
21 | /** | |
22 | * A login screen that offers login via username/password | |
23 | */ | |
24 | public class LoginActivity extends ActionBarActivity | |
25 | { | |
26 | // Keep track of the login task to ensure we can cancel it if requested. | |
27 | private UserLoginTask mAuthTask = null; | |
28 | ||
29 | // UI references. | |
30 | private AutoCompleteTextView mNickView; | |
31 | private EditText mPasswordView; | |
32 | private View mProgressView; | |
33 | private View mScrollView; | |
34 | private View mSignInButton; | |
35 | private View mRegisterButton; | |
36 | private AlertDialog mAlertDialog; | |
37 | private EditText mMailEditText; | |
38 | ||
39 | private String mPassword; | |
40 | private String mNickname; | |
41 | private static final String SHARED_PREFERENCES = "SHRD_PREF"; | |
42 | private static final String USER_NICKNAME = "USR_NICK"; | |
43 | private static final String USER_PASSWORD = "USR_PWD"; | |
44 | ||
45 | @Override | |
46 | protected void onCreate(Bundle savedInstanceState) | |
47 | { | |
48 | super.onCreate(savedInstanceState); | |
49 | setContentView(R.layout.activity_login); | |
50 | ||
51 | findViews(); | |
52 | ||
53 | if (isCredentialsStoredYet()) | |
54 | { | |
55 | attemptLoginOrRegister(UserTasks.LOGIN); | |
56 | } | |
57 | setListeners(); | |
58 | } | |
59 | ||
60 | private void findViews() | |
61 | { | |
62 | // Find the buttons. | |
63 | mSignInButton = findViewById(R.id.activity_login_sign_in_button); | |
64 | mRegisterButton = findViewById(R.id.activity_login_register_button); | |
65 | ||
66 | // Set up the login form. | |
67 | mNickView = (AutoCompleteTextView) findViewById(R.id.activity_login_nick); | |
68 | mProgressView = findViewById(R.id.activity_login_progress); | |
69 | mScrollView = findViewById(R.id.activity_login_scrollview); | |
70 | mPasswordView = (EditText) findViewById(R.id.activity_login_password); | |
71 | } | |
72 | ||
73 | private AlertDialog.Builder buildDialog(String mailString) | |
74 | { | |
75 | final AlertDialog.Builder alertDialogBuilder = | |
76 | new AlertDialog.Builder(LoginActivity.this); | |
77 | - | // after debug and place it inside setError below |
77 | + | |
78 | alertDialogBuilder.setTitle("Insert mail"); | |
79 | - | Toast.makeText(LoginActivity.this, "Error", Toast.LENGTH_SHORT).show(); |
79 | + | |
80 | // Set an EditText view to get user input | |
81 | mMailEditText = new EditText(LoginActivity.this); | |
82 | if (mailString != null) | |
83 | mMailEditText.setText(mailString); | |
84 | alertDialogBuilder.setView(mMailEditText); | |
85 | alertDialogBuilder.setPositiveButton( | |
86 | "Ok", new DialogInterface.OnClickListener() | |
87 | { | |
88 | public void onClick(DialogInterface dialog, int whichButton) | |
89 | { | |
90 | String email = mMailEditText.getText().toString(); | |
91 | if (!TextUtils.isEmpty(email) && !isEmailValid( | |
92 | email)) | |
93 | { | |
94 | String s = getString( | |
95 | R.string | |
96 | .activity_login_error_invalid_email); //TODO delete this reference | |
97 | // after debug and place it inside setError below | |
98 | mMailEditText.setError(s); | |
99 | Toast.makeText(LoginActivity.this, "Error", Toast.LENGTH_SHORT) | |
100 | .show(); | |
101 | } | |
102 | else | |
103 | { | |
104 | attemptLoginOrRegister(UserTasks.REGISTER, email); | |
105 | } | |
106 | } | |
107 | }); | |
108 | alertDialogBuilder.setNegativeButton( | |
109 | "Cancel", new DialogInterface.OnClickListener() | |
110 | { | |
111 | public void onClick(DialogInterface dialog, int whichButton) | |
112 | { | |
113 | dialog.cancel(); | |
114 | } | |
115 | }); | |
116 | ||
117 | mAlertDialog = alertDialogBuilder.create(); | |
118 | return alertDialogBuilder; | |
119 | } | |
120 | ||
121 | private void setListeners() | |
122 | { | |
123 | mPasswordView.setOnEditorActionListener( | |
124 | new TextView.OnEditorActionListener() | |
125 | { | |
126 | @Override | |
127 | public boolean onEditorAction( | |
128 | TextView textView, int id, KeyEvent keyEvent) | |
129 | { | |
130 | if (id == R.id.activity_login_imeaction | |
131 | || id == EditorInfo.IME_NULL) | |
132 | { | |
133 | attemptLoginOrRegister(UserTasks.LOGIN); | |
134 | return true; | |
135 | } | |
136 | return false; | |
137 | } | |
138 | }); | |
139 | ||
140 | mSignInButton.setOnClickListener( | |
141 | new View.OnClickListener() | |
142 | { | |
143 | @Override | |
144 | public void onClick(View view) | |
145 | { | |
146 | attemptLoginOrRegister(UserTasks.LOGIN); | |
147 | } | |
148 | }); | |
149 | ||
150 | mRegisterButton.setOnClickListener( | |
151 | new View.OnClickListener() | |
152 | { | |
153 | @Override | |
154 | public void onClick(View v) | |
155 | { | |
156 | buildDialog(null).show(); | |
157 | } | |
158 | }); | |
159 | } | |
160 | ||
161 | ||
162 | /* Check if the user has already stored its credentials before | |
163 | */ | |
164 | private boolean isCredentialsStoredYet() | |
165 | { | |
166 | SharedPreferences sp = getSharedPreferences( | |
167 | SHARED_PREFERENCES, MODE_PRIVATE); | |
168 | ||
169 | mPassword = sp.getString(USER_PASSWORD, null); | |
170 | mNickname = sp.getString(USER_NICKNAME, null); | |
171 | ||
172 | return mPassword != null && mNickname != null; | |
173 | } | |
174 | ||
175 | ||
176 | /* | |
177 | * Attempts to sign in or register with the account specified by the login | |
178 | * form. | |
179 | * If there are form errors (invalid email, missing fields, etc.), the | |
180 | * errors are presented and no actual login attempt is made. | |
181 | */ | |
182 | private void attemptLoginOrRegister(UserTasks taskType, String... params) | |
183 | { | |
184 | if (mAuthTask != null) return; | |
185 | ||
186 | // Reset errors. | |
187 | mNickView.setError(null); | |
188 | mPasswordView.setError(null); | |
189 | ||
190 | // Check if the user was already stored | |
191 | if (mNickname == null || mPassword == null) | |
192 | { | |
193 | mNickname = mNickView.getText().toString(); | |
194 | mPassword = mPasswordView.getText().toString(); | |
195 | } | |
196 | ||
197 | // Store values at the time of the login attempt. | |
198 | boolean cancel = false; | |
199 | View focusView = null; | |
200 | ||
201 | // Check for a valid password, if the user entered one. | |
202 | if (!TextUtils.isEmpty(mPassword) && !isPasswordValid(mPassword)) | |
203 | { | |
204 | mPasswordView.setError( | |
205 | getResources().getString( | |
206 | R.string.activity_login_error_invalid_password)); | |
207 | focusView = mPasswordView; | |
208 | cancel = true; | |
209 | } | |
210 | ||
211 | // Check for a valid nick. | |
212 | if (TextUtils.isEmpty(mNickname)) | |
213 | { | |
214 | mNickView | |
215 | .setError( | |
216 | getString(R.string.activity_login_error_field_required)); | |
217 | focusView = mNickView; | |
218 | cancel = true; | |
219 | } | |
220 | else if (!isNickValid(mNickname)) | |
221 | { | |
222 | mNickView | |
223 | .setError(getString(R.string.activity_login_error_invalid_nickname)); | |
224 | focusView = mNickView; | |
225 | cancel = true; | |
226 | } | |
227 | ||
228 | if (cancel) | |
229 | { | |
230 | // There was an error; don't attempt login and focus the first | |
231 | // form field with an error. | |
232 | focusView.requestFocus(); | |
233 | } | |
234 | else | |
235 | { | |
236 | // Show a progress spinner, and kick off a background task to | |
237 | // perform the user login attempt. | |
238 | showProgress(true); | |
239 | CheckBox checkBox = (CheckBox) findViewById( | |
240 | R.id | |
241 | .activity_login_remember_checkBox); | |
242 | if (checkBox.isChecked()) | |
243 | { storeCredentials(); } | |
244 | mAuthTask = new UserLoginTask(mNickname, mPassword, taskType); | |
245 | mAuthTask.execute(params); | |
246 | } | |
247 | } | |
248 | ||
249 | private void storeCredentials() | |
250 | { | |
251 | SharedPreferences.Editor editor = getSharedPreferences | |
252 | (SHARED_PREFERENCES, MODE_PRIVATE).edit(); | |
253 | editor.putString(USER_NICKNAME, mNickname); | |
254 | editor.putString(USER_PASSWORD, mPassword); | |
255 | editor.apply(); | |
256 | } | |
257 | ||
258 | private boolean isNickValid(String nick) | |
259 | { | |
260 | //Actually, the user can insert any character as nickname | |
261 | return nick != null && !nick.isEmpty(); | |
262 | } | |
263 | ||
264 | private boolean isEmailValid(String email) | |
265 | { | |
266 | return email.contains("@") && email.contains(".") && !email.contains(" "); | |
267 | } | |
268 | ||
269 | private boolean isPasswordValid(String password) | |
270 | { | |
271 | return !(password.length() < 6 || password.contains(" ")); | |
272 | } | |
273 | ||
274 | /** | |
275 | * Shows the progress UI and hides the login form. | |
276 | */ | |
277 | @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) | |
278 | public void showProgress(final boolean show) | |
279 | { | |
280 | // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow | |
281 | // for very easy animations. If available, use these APIs to fade-in | |
282 | // the progress spinner. | |
283 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) | |
284 | { | |
285 | int shortAnimTime = | |
286 | getResources().getInteger(android.R.integer.config_shortAnimTime); | |
287 | ||
288 | mScrollView.setVisibility(show ? View.GONE : View.VISIBLE); | |
289 | mScrollView.animate().setDuration(shortAnimTime).alpha( | |
290 | show ? 0 : 1).setListener( | |
291 | new AnimatorListenerAdapter() | |
292 | { | |
293 | @Override | |
294 | public void onAnimationEnd(Animator animation) | |
295 | { | |
296 | mScrollView.setVisibility(show ? View.GONE : View.VISIBLE); | |
297 | } | |
298 | }); | |
299 | ||
300 | mProgressView.setVisibility(show ? View.VISIBLE : View.GONE); | |
301 | mProgressView.animate().setDuration(shortAnimTime).alpha( | |
302 | show ? 1 : 0).setListener( | |
303 | new AnimatorListenerAdapter() | |
304 | { | |
305 | @Override | |
306 | public void onAnimationEnd(Animator animation) | |
307 | { | |
308 | mProgressView.setVisibility(show ? View.VISIBLE : View.GONE); | |
309 | } | |
310 | }); | |
311 | } | |
312 | else | |
313 | { | |
314 | // The ViewPropertyAnimator APIs are not available, so simply show | |
315 | // and hide the relevant UI components. | |
316 | mProgressView.setVisibility(show ? View.VISIBLE : View.GONE); | |
317 | mScrollView.setVisibility(show ? View.GONE : View.VISIBLE); | |
318 | } | |
319 | } | |
320 | ||
321 | ||
322 | public enum UserTasks | |
323 | { | |
324 | LOGIN, REGISTER | |
325 | } | |
326 | ||
327 | /** | |
328 | * Represents an asynchronous login/registration task used to authenticate | |
329 | * the user. | |
330 | */ | |
331 | public class UserLoginTask extends AsyncTask<String, Void, Boolean> | |
332 | { | |
333 | private final String mNick; | |
334 | private final String mPassword; | |
335 | private final UserTasks mTaskType; | |
336 | private int mIdResponseCode; | |
337 | ||
338 | UserLoginTask(String nick, String password, UserTasks taskType) | |
339 | { | |
340 | mNick = nick; | |
341 | mPassword = password; | |
342 | mTaskType = taskType; | |
343 | } | |
344 | ||
345 | @Override | |
346 | protected Boolean doInBackground(String... params) | |
347 | { | |
348 | // This is a simplified version that doesn't require further libraries and an | |
349 | // Internet connection. | |
350 | mIdResponseCode = 0; | |
351 | switch (mTaskType) | |
352 | { | |
353 | case LOGIN: | |
354 | { | |
355 | mIdResponseCode = 1; | |
356 | - | Toast.makeText(LoginActivity.this, "Hi", Toast.LENGTH_SHORT).show(); |
356 | + | |
357 | - | finish(); |
357 | + | |
358 | case REGISTER: | |
359 | { | |
360 | mIdResponseCode = 2; | |
361 | return true; | |
362 | } | |
363 | - | mPasswordView.setError( |
363 | + | |
364 | - | getString(R.string.activity_login_error_unauthorized)); |
364 | + | |
365 | - | mPasswordView.requestFocus(); |
365 | + | |
366 | - | SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences |
366 | + | |
367 | } | |
368 | - | editor.remove(LoginActivity.USER_NICKNAME); |
368 | + | |
369 | - | editor.remove(LoginActivity.USER_PASSWORD); |
369 | + | |
370 | - | editor.apply(); |
370 | + | |
371 | { | |
372 | mAuthTask = null; | |
373 | showProgress(false); | |
374 | ||
375 | if (success) | |
376 | { | |
377 | Toast.makeText(LoginActivity.this, "Hi", Toast.LENGTH_SHORT).show(); | |
378 | finish(); | |
379 | } | |
380 | else | |
381 | { | |
382 | if (mIdResponseCode <= 0) | |
383 | { | |
384 | mPasswordView.setError( | |
385 | getString(R.string.activity_login_error_unauthorized)); | |
386 | mPasswordView.requestFocus(); | |
387 | SharedPreferences.Editor editor = | |
388 | getApplicationContext().getSharedPreferences | |
389 | (LoginActivity.SHARED_PREFERENCES, MODE_PRIVATE).edit(); | |
390 | editor.remove(LoginActivity.USER_NICKNAME); | |
391 | editor.remove(LoginActivity.USER_PASSWORD); | |
392 | editor.apply(); | |
393 | } | |
394 | } | |
395 | } | |
396 | ||
397 | @Override | |
398 | protected void onCancelled() | |
399 | { | |
400 | mAuthTask = null; | |
401 | showProgress(false); | |
402 | } | |
403 | } | |
404 | } |