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