Commit 8e658a571a34babdba3b083ad50e9ffa0ac8fe8e
Committed by
geraldo morais
1 parent
3971c770
Exists in
master
refactoring do login
Showing
1 changed file
with
0 additions
and
457 deletions
Show diff stats
app/src/main/java/com/monitorabrasil/participacidadao/view/LoginActivity.java
| ... | ... | @@ -1,457 +0,0 @@ |
| 1 | -package com.monitorabrasil.participacidadao.view; | |
| 2 | - | |
| 3 | -import android.app.Activity; | |
| 4 | -import android.content.Intent; | |
| 5 | -import android.graphics.Bitmap; | |
| 6 | -import android.graphics.BitmapFactory; | |
| 7 | -import android.net.Uri; | |
| 8 | -import android.os.Bundle; | |
| 9 | -import android.support.design.widget.Snackbar; | |
| 10 | -import android.support.v7.app.AppCompatActivity; | |
| 11 | -import android.support.v7.widget.Toolbar; | |
| 12 | -import android.text.TextUtils; | |
| 13 | -import android.view.KeyEvent; | |
| 14 | -import android.view.View; | |
| 15 | -import android.view.View.OnClickListener; | |
| 16 | -import android.view.inputmethod.EditorInfo; | |
| 17 | -import android.widget.ArrayAdapter; | |
| 18 | -import android.widget.AutoCompleteTextView; | |
| 19 | -import android.widget.Button; | |
| 20 | -import android.widget.EditText; | |
| 21 | -import android.widget.ImageButton; | |
| 22 | -import android.widget.ImageView; | |
| 23 | -import android.widget.TextView; | |
| 24 | - | |
| 25 | -import com.monitorabrasil.participacidadao.R; | |
| 26 | -import com.monitorabrasil.participacidadao.application.MyApp; | |
| 27 | -import com.nostra13.universalimageloader.core.assist.FailReason; | |
| 28 | -import com.nostra13.universalimageloader.core.listener.ImageLoadingListener; | |
| 29 | -import com.parse.LogInCallback; | |
| 30 | -import com.parse.LogOutCallback; | |
| 31 | -import com.parse.ParseTwitterUtils; | |
| 32 | -import com.parse.ParseUser; | |
| 33 | -import com.parse.SignUpCallback; | |
| 34 | - | |
| 35 | -import java.io.FileNotFoundException; | |
| 36 | -import java.io.InputStream; | |
| 37 | -import java.util.List; | |
| 38 | - | |
| 39 | -/** | |
| 40 | - * A login screen that offers login via email/password. | |
| 41 | - */ | |
| 42 | -public class LoginActivity extends AppCompatActivity { | |
| 43 | - | |
| 44 | - | |
| 45 | - /** | |
| 46 | - * Keep track of the login task to ensure we can cancel it if requested. | |
| 47 | - */ | |
| 48 | - | |
| 49 | - | |
| 50 | - // UI references. | |
| 51 | - private AutoCompleteTextView mEmailView; | |
| 52 | - private EditText mPasswordView; | |
| 53 | - private EditText mNome; | |
| 54 | - private View mProgressView; | |
| 55 | - private View mLoginFormView; | |
| 56 | - private View mLogoutFormView; | |
| 57 | - private Button btnFazerCadastro; | |
| 58 | - private Button btnLogar; | |
| 59 | - private Button btnLogout; | |
| 60 | - private Button btnLoginTwitter; | |
| 61 | - private TextView lblNome; | |
| 62 | - private TextView lblEmail; | |
| 63 | - private ImageView foto; | |
| 64 | - private ParseUser currentUser; | |
| 65 | - private ImageButton btnFoto; | |
| 66 | - | |
| 67 | - private final int PICK_IMAGE=2; | |
| 68 | - | |
| 69 | - @Override | |
| 70 | - protected void onCreate(Bundle savedInstanceState) { | |
| 71 | - super.onCreate(savedInstanceState); | |
| 72 | - setContentView(R.layout.activity_login); | |
| 73 | - setupActionBar(); | |
| 74 | - mLoginFormView = findViewById(R.id.login_form); | |
| 75 | - mLogoutFormView = findViewById(R.id.logout_form); | |
| 76 | - mProgressView = findViewById(R.id.login_progress); | |
| 77 | - btnFoto = (ImageButton) findViewById(R.id.btnFoto); | |
| 78 | - currentUser = ParseUser.getCurrentUser(); | |
| 79 | - | |
| 80 | - | |
| 81 | - | |
| 82 | - | |
| 83 | - if (currentUser != null) { | |
| 84 | - montaFormLogout(); | |
| 85 | - | |
| 86 | - }else{ | |
| 87 | - mLogoutFormView.setVisibility(View.GONE); | |
| 88 | - mLoginFormView.setVisibility(View.VISIBLE); | |
| 89 | - | |
| 90 | - } | |
| 91 | - | |
| 92 | - // Set up the login form. | |
| 93 | - btnFoto.setOnClickListener(new OnClickListener() { | |
| 94 | - @Override | |
| 95 | - public void onClick(View v) { | |
| 96 | - Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT); | |
| 97 | - getIntent.setType("image/*"); | |
| 98 | - | |
| 99 | - Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); | |
| 100 | - pickIntent.setType("image/*"); | |
| 101 | - | |
| 102 | - Intent chooserIntent = Intent.createChooser(getIntent, "Selecione uma foto"); | |
| 103 | - chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent}); | |
| 104 | - | |
| 105 | - startActivityForResult(chooserIntent, PICK_IMAGE); | |
| 106 | - } | |
| 107 | - }); | |
| 108 | - | |
| 109 | - mNome = (EditText)findViewById(R.id.txtNome); | |
| 110 | - mNome.setVisibility(View.GONE); | |
| 111 | - | |
| 112 | - btnLogar = (Button)findViewById(R.id.email_sign_in_button); | |
| 113 | - btnLogout = (Button)findViewById(R.id.btnLogout); | |
| 114 | - | |
| 115 | - btnLogout.setOnClickListener(new OnClickListener() { | |
| 116 | - | |
| 117 | - @Override | |
| 118 | - public void onClick(View v) { | |
| 119 | - showProgress(true,null); | |
| 120 | - ParseUser.logOutInBackground(new LogOutCallback() { | |
| 121 | - @Override | |
| 122 | - public void done(com.parse.ParseException e) { | |
| 123 | - showProgress(false, mLoginFormView); | |
| 124 | - } | |
| 125 | - }); | |
| 126 | - } | |
| 127 | - }); | |
| 128 | - | |
| 129 | - btnFazerCadastro = (Button)findViewById(R.id.fazer_cadastro); | |
| 130 | - btnFazerCadastro.setOnClickListener(new OnClickListener() { | |
| 131 | - @Override | |
| 132 | - public void onClick(View v) { | |
| 133 | - if(btnFazerCadastro.getText().toString().equals("Fazer Cadastro")){ | |
| 134 | - mNome.setVisibility(View.VISIBLE); | |
| 135 | - btnLogar.setVisibility(View.GONE); | |
| 136 | - btnLoginTwitter.setVisibility(View.GONE); | |
| 137 | - btnFazerCadastro.setText("Cadastrar"); | |
| 138 | - View view = mNome; | |
| 139 | - view.requestFocus(); | |
| 140 | - }else{ | |
| 141 | - realizarCadastro(); | |
| 142 | - } | |
| 143 | - | |
| 144 | - } | |
| 145 | - }); | |
| 146 | - | |
| 147 | - btnLoginTwitter = (Button)findViewById(R.id.login_twitter); | |
| 148 | - btnLoginTwitter.setOnClickListener(new OnClickListener() { | |
| 149 | - @Override | |
| 150 | - public void onClick(View v) { | |
| 151 | - ParseTwitterUtils.logIn(v.getContext(), new LogInCallback() { | |
| 152 | - @Override | |
| 153 | - public void done(ParseUser parseUser, com.parse.ParseException e) { | |
| 154 | - if (parseUser == null) { | |
| 155 | - Snackbar.make(mLoginFormView, "Login cancelado ", Snackbar.LENGTH_LONG) | |
| 156 | - .setAction("Action", null).show(); | |
| 157 | - } else if (parseUser.isNew()) { | |
| 158 | - parseUser.put("nome",ParseTwitterUtils.getTwitter().getScreenName()); | |
| 159 | - parseUser.saveInBackground(); | |
| 160 | - montaFormLogout(); | |
| 161 | - } else { | |
| 162 | - montaFormLogout(); | |
| 163 | - } | |
| 164 | - } | |
| 165 | - }); | |
| 166 | - } | |
| 167 | - }); | |
| 168 | - | |
| 169 | - mEmailView = (AutoCompleteTextView) findViewById(R.id.email); | |
| 170 | - | |
| 171 | - | |
| 172 | - mPasswordView = (EditText) findViewById(R.id.password); | |
| 173 | - mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() { | |
| 174 | - @Override | |
| 175 | - public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) { | |
| 176 | - if (id == R.id.login || id == EditorInfo.IME_NULL) { | |
| 177 | - attemptLogin(); | |
| 178 | - return true; | |
| 179 | - } | |
| 180 | - return false; | |
| 181 | - } | |
| 182 | - }); | |
| 183 | - | |
| 184 | - Button mEmailSignInButton = (Button) findViewById(R.id.email_sign_in_button); | |
| 185 | - mEmailSignInButton.setOnClickListener(new OnClickListener() { | |
| 186 | - @Override | |
| 187 | - public void onClick(View view) { | |
| 188 | - attemptLogin(); | |
| 189 | - } | |
| 190 | - }); | |
| 191 | - | |
| 192 | - | |
| 193 | - | |
| 194 | - } | |
| 195 | - | |
| 196 | - @Override | |
| 197 | - public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
| 198 | - super.onActivityResult(requestCode, resultCode, data); | |
| 199 | - if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) { | |
| 200 | - if (data == null) { | |
| 201 | - //Display an error | |
| 202 | - return; | |
| 203 | - } | |
| 204 | - try { | |
| 205 | - Uri selectedImage = data.getData(); | |
| 206 | - InputStream imageStream = getContentResolver().openInputStream(selectedImage); | |
| 207 | - Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream); | |
| 208 | - btnFoto.setImageBitmap(yourSelectedImage); | |
| 209 | - } catch (FileNotFoundException e) { | |
| 210 | - e.printStackTrace(); | |
| 211 | - } | |
| 212 | - //Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap... | |
| 213 | - } | |
| 214 | - } | |
| 215 | - | |
| 216 | - private void showProgress(boolean b,View v) { | |
| 217 | - if(b){ | |
| 218 | - mProgressView.setVisibility(View.VISIBLE); | |
| 219 | - mLogoutFormView.setVisibility(View.GONE); | |
| 220 | - mLoginFormView.setVisibility(View.GONE); | |
| 221 | - }else{ | |
| 222 | - mProgressView.setVisibility(View.GONE); | |
| 223 | - v.setVisibility(View.VISIBLE); | |
| 224 | - } | |
| 225 | - } | |
| 226 | - | |
| 227 | - private void montaFormLogout() { | |
| 228 | - lblNome = (TextView)findViewById(R.id.lblNome); | |
| 229 | - lblEmail = (TextView)findViewById(R.id.lblEmail); | |
| 230 | - foto = (ImageView)findViewById(R.id.foto); | |
| 231 | - | |
| 232 | - mLoginFormView.setVisibility(View.GONE); | |
| 233 | - showProgress(false,mLogoutFormView); | |
| 234 | - | |
| 235 | - if(ParseTwitterUtils.getTwitter().getUserId() !=null){ | |
| 236 | - String screenName = ParseTwitterUtils.getTwitter().getScreenName(); | |
| 237 | - lblNome.setText(screenName); | |
| 238 | - lblEmail.setText(""); | |
| 239 | - MyApp.getInstance().getmImagemLoader().loadImage("https://twitter.com/" + screenName + "/profile_image?size=bigger", new ImageLoadingListener() { | |
| 240 | - | |
| 241 | - | |
| 242 | - @Override | |
| 243 | - public void onLoadingStarted(String imageUri, View view) { | |
| 244 | - | |
| 245 | - } | |
| 246 | - | |
| 247 | - @Override | |
| 248 | - public void onLoadingFailed(String imageUri, View view, FailReason failReason) { | |
| 249 | - | |
| 250 | - } | |
| 251 | - | |
| 252 | - @Override | |
| 253 | - public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { | |
| 254 | - foto.setImageBitmap(loadedImage); | |
| 255 | - } | |
| 256 | - | |
| 257 | - @Override | |
| 258 | - public void onLoadingCancelled(String imageUri, View view) { | |
| 259 | - | |
| 260 | - } | |
| 261 | - | |
| 262 | - | |
| 263 | - }); | |
| 264 | - }else{ | |
| 265 | - lblNome.setText(currentUser.get("nome").toString()); | |
| 266 | - lblEmail.setText(currentUser.getEmail()); | |
| 267 | - } | |
| 268 | - } | |
| 269 | - | |
| 270 | - | |
| 271 | - private void realizarCadastro() { | |
| 272 | - | |
| 273 | - // Reset errors. | |
| 274 | - mEmailView.setError(null); | |
| 275 | - mPasswordView.setError(null); | |
| 276 | - mNome.setError(null); | |
| 277 | - | |
| 278 | - // Store values at the time of the login attempt. | |
| 279 | - String email = mEmailView.getText().toString(); | |
| 280 | - String password = mPasswordView.getText().toString(); | |
| 281 | - String nome =mNome.getText().toString(); | |
| 282 | - | |
| 283 | - boolean cancel = false; | |
| 284 | - View focusView = null; | |
| 285 | - | |
| 286 | - if (TextUtils.isEmpty(nome)) { | |
| 287 | - mNome.setError(getString(R.string.error_field_required)); | |
| 288 | - focusView = mNome; | |
| 289 | - cancel = true; | |
| 290 | - } | |
| 291 | - | |
| 292 | - // Check for a valid password, if the user entered one. | |
| 293 | - if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) { | |
| 294 | - mPasswordView.setError(getString(R.string.error_invalid_password)); | |
| 295 | - focusView = mPasswordView; | |
| 296 | - cancel = true; | |
| 297 | - } | |
| 298 | - | |
| 299 | - // Check for a valid email address. | |
| 300 | - if (TextUtils.isEmpty(email)) { | |
| 301 | - mEmailView.setError(getString(R.string.error_field_required)); | |
| 302 | - focusView = mEmailView; | |
| 303 | - cancel = true; | |
| 304 | - } else if (!isEmailValid(email)) { | |
| 305 | - mEmailView.setError(getString(R.string.error_invalid_email)); | |
| 306 | - focusView = mEmailView; | |
| 307 | - cancel = true; | |
| 308 | - } | |
| 309 | - | |
| 310 | - if (cancel) { | |
| 311 | - // There was an error; don't attempt login and focus the first | |
| 312 | - // form field with an error. | |
| 313 | - focusView.requestFocus(); | |
| 314 | - } else { | |
| 315 | - // Show a progress spinner, and kick off a background task to | |
| 316 | - // perform the user login attempt. | |
| 317 | - showProgress(true,null); | |
| 318 | - | |
| 319 | - ParseUser user = new ParseUser(); | |
| 320 | - user.setUsername(email); | |
| 321 | - user.setPassword(password); | |
| 322 | - user.setEmail(email); | |
| 323 | - user.put("nome",mNome.getText().toString()); | |
| 324 | - | |
| 325 | - | |
| 326 | - user.signUpInBackground(new SignUpCallback() { | |
| 327 | - @Override | |
| 328 | - public void done(com.parse.ParseException e) { | |
| 329 | - if (e == null) { | |
| 330 | - montaFormLogout(); | |
| 331 | - } else { | |
| 332 | - showProgress(false,mLoginFormView); | |
| 333 | - Snackbar.make(mLoginFormView, "Houve um erro ao fazer seu cadastro ", Snackbar.LENGTH_LONG) | |
| 334 | - .setAction("Action", null).show(); | |
| 335 | - } | |
| 336 | - } | |
| 337 | - | |
| 338 | - | |
| 339 | - }); | |
| 340 | - | |
| 341 | - } | |
| 342 | - } | |
| 343 | - | |
| 344 | - | |
| 345 | - | |
| 346 | - /** | |
| 347 | - * Set up the {@link android.app.ActionBar}, if the API is available. | |
| 348 | - */ | |
| 349 | -// @TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
| 350 | - private void setupActionBar() { | |
| 351 | - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
| 352 | - | |
| 353 | - // toolbar.setNavigationIcon(R.drawable.ic_good); | |
| 354 | - toolbar.setTitle("Login"); | |
| 355 | - | |
| 356 | - toolbar.setTitleTextColor(getResources().getColor(R.color.md_white_1000)); | |
| 357 | - toolbar.setSubtitleTextColor(getResources().getColor(R.color.md_white_1000)); | |
| 358 | - setSupportActionBar(toolbar); | |
| 359 | - getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
| 360 | - } | |
| 361 | - | |
| 362 | - /** | |
| 363 | - * Attempts to sign in or register the account specified by the login form. | |
| 364 | - * If there are form errors (invalid email, missing fields, etc.), the | |
| 365 | - * errors are presented and no actual login attempt is made. | |
| 366 | - */ | |
| 367 | - public void attemptLogin() { | |
| 368 | - | |
| 369 | - | |
| 370 | - // Reset errors. | |
| 371 | - mEmailView.setError(null); | |
| 372 | - mPasswordView.setError(null); | |
| 373 | - | |
| 374 | - // Store values at the time of the login attempt. | |
| 375 | - String email = mEmailView.getText().toString(); | |
| 376 | - String password = mPasswordView.getText().toString(); | |
| 377 | - | |
| 378 | - boolean cancel = false; | |
| 379 | - View focusView = null; | |
| 380 | - | |
| 381 | - // Check for a valid password, if the user entered one. | |
| 382 | - if (TextUtils.isEmpty(password) && !isPasswordValid(password)) { | |
| 383 | - mPasswordView.setError(getString(R.string.error_invalid_password)); | |
| 384 | - focusView = mPasswordView; | |
| 385 | - cancel = true; | |
| 386 | - } | |
| 387 | - | |
| 388 | - // Check for a valid email address. | |
| 389 | - if (TextUtils.isEmpty(email)) { | |
| 390 | - mEmailView.setError(getString(R.string.error_field_required)); | |
| 391 | - focusView = mEmailView; | |
| 392 | - cancel = true; | |
| 393 | - } else if (!isEmailValid(email)) { | |
| 394 | - mEmailView.setError(getString(R.string.error_invalid_email)); | |
| 395 | - focusView = mEmailView; | |
| 396 | - cancel = true; | |
| 397 | - } | |
| 398 | - | |
| 399 | - if (cancel) { | |
| 400 | - // There was an error; don't attempt login and focus the first | |
| 401 | - // form field with an error. | |
| 402 | - focusView.requestFocus(); | |
| 403 | - } else { | |
| 404 | - // Show a progress spinner, and kick off a background task to | |
| 405 | - // perform the user login attempt. | |
| 406 | - showProgress(true,null); | |
| 407 | - | |
| 408 | - //fazer o login aqui | |
| 409 | - fazerLogin(email, password); | |
| 410 | - | |
| 411 | - } | |
| 412 | - } | |
| 413 | - | |
| 414 | - private void fazerLogin(String email, String password) { | |
| 415 | - ParseUser.logInInBackground(email, password, new LogInCallback() { | |
| 416 | - | |
| 417 | - @Override | |
| 418 | - public void done(ParseUser parseUser, com.parse.ParseException e) { | |
| 419 | - if (parseUser != null) { | |
| 420 | - finish(); | |
| 421 | - } else { | |
| 422 | - mPasswordView.setError(getString(R.string.error_incorrect_password)); | |
| 423 | - mPasswordView.requestFocus(); | |
| 424 | - showProgress(false,mLoginFormView); | |
| 425 | - } | |
| 426 | - } | |
| 427 | - | |
| 428 | - | |
| 429 | - }); | |
| 430 | - } | |
| 431 | - | |
| 432 | - private boolean isEmailValid(String email) { | |
| 433 | - //TODO: Replace this with your own logic | |
| 434 | - return email.contains("@"); | |
| 435 | - } | |
| 436 | - | |
| 437 | - private boolean isPasswordValid(String password) { | |
| 438 | - //TODO: Replace this with your own logic | |
| 439 | - return password.length() > 4; | |
| 440 | - } | |
| 441 | - | |
| 442 | - | |
| 443 | - | |
| 444 | - | |
| 445 | - | |
| 446 | - private void addEmailsToAutoComplete(List<String> emailAddressCollection) { | |
| 447 | - //Create adapter to tell the AutoCompleteTextView what to show in its dropdown list. | |
| 448 | - ArrayAdapter<String> adapter = | |
| 449 | - new ArrayAdapter<String>(LoginActivity.this, | |
| 450 | - android.R.layout.simple_dropdown_item_1line, emailAddressCollection); | |
| 451 | - | |
| 452 | - mEmailView.setAdapter(adapter); | |
| 453 | - } | |
| 454 | - | |
| 455 | - | |
| 456 | -} | |
| 457 | - |