Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.44 KB | None | 0 0
  1. public class LoginActivity extends ActionBarActivity {
  2.  
  3.     // Pointer to UI elements
  4.     private EditText txtEmail;
  5.     private EditText txtPassword;
  6.     private Button btnLogin;
  7.     private Button btnForgotPassword;
  8.  
  9.     // Holds copy of text from EditText fields
  10.     private String email;
  11.     private String password;
  12.  
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_login);
  17.  
  18.         // Initialize pointer to UI elements
  19.         txtEmail = (EditText) findViewById(R.id.txtemail);
  20.         txtPassword = (EditText) findViewById(R.id.txtPassword);
  21.         btnLogin = (Button) findViewById(R.id.btnLogin);
  22.         btnForgotPassword = (Button) findViewById(R.id.btnForgotPassword);
  23.  
  24.         // Declare UI element event handlers
  25.         btnLogin.setOnClickListener(new View.OnClickListener() {
  26.             @Override
  27.             public void onClick(View v) {
  28.                 btnLogin();
  29.             }
  30.         });
  31.  
  32.         btnForgotPassword.setOnClickListener(new View.OnClickListener() {
  33.             @Override
  34.             public void onClick(View v) {
  35.                 btnForgotPassword();
  36.             }
  37.         });
  38.  
  39.         // Enable local datastore feature
  40.         //Parse.enableLocalDatastore(getApplicationContext());
  41.  
  42.         // Register custom ParseObject subclasses
  43.         ParseObject.registerSubclass(Symptoms.class);
  44.  
  45.         // Initialize Parse
  46.         Parse.initialize(this, "VewvxmlYofAlQkLcRUZjjfJSVf0U9WKeWpWcqfwJ", "K8hbr0l4agNpyJKvRvhmjEBSgkPcvTNrqTVYmxz8");
  47.  
  48.         // Register application to log app opens
  49.         ParseAnalytics.trackAppOpened(getIntent());
  50.  
  51.         // See if we can skip the login view by checking for a cached user session
  52.         ParseUser currentUser = ParseUser.getCurrentUser();
  53.         if (currentUser != null) {
  54.             determineLaunchpage(currentUser);
  55.         }
  56.     }
  57.  
  58.  
  59.     @Override
  60.     public boolean onCreateOptionsMenu(Menu menu) {
  61.         // Inflate the menu; this adds items to the action bar if it is present.
  62.         getMenuInflater().inflate(R.menu.menu_login, menu);
  63.         return true;
  64.     }
  65.  
  66.     @Override
  67.     public boolean onOptionsItemSelected(MenuItem item) {
  68.         // Handle action bar item clicks here. The action bar will
  69.         // automatically handle clicks on the Home/Up button, so long
  70.         // as you specify a parent activity in AndroidManifest.xml.
  71.         int id = item.getItemId();
  72.         return super.onOptionsItemSelected(item);
  73.     }
  74.  
  75.     /**
  76.      * Called when btnLogin is clicked on activity_login.
  77.      * Performs error checking before calling attemptStandardLogin method.
  78.      */
  79.     public void btnLogin() {
  80.         // Retrieve text from EditText fields
  81.         email = (String) txtEmail.getText().toString();
  82.         password = (String) txtPassword.getText().toString();
  83.  
  84.         // Check if both credentials are entered. Error check
  85.         if (email.isEmpty()) {
  86.             txtEmail.setError("Please enter your email address");
  87.             //End the method early before we attempt standard login
  88.             return;
  89.         }
  90.         if (password.isEmpty()) {
  91.             txtPassword.setError("Please enter your password");
  92.             //End the method early before we attempt standard login
  93.             return;
  94.         }
  95.  
  96.         // If email and password are not blank, attempt to login user
  97.         attemptStandardLogin(email, password);
  98.     }
  99.  
  100.     /**
  101.      * Validates credential parameters by communicating with Parse database backend.
  102.      * The term "standard" refers to a typical Email / Password credential combination,
  103.      * as opposed to an optional social media login.
  104.      * @param email The email used for login attempt
  105.      * @param password The password used for login attempt
  106.      */
  107.     private void attemptStandardLogin(String email, String password) {
  108.         ParseUser.logInInBackground(email, password, new LogInCallback() {
  109.             public void done(ParseUser user, ParseException e) {
  110.                 if (user != null) {
  111.                     Toast.makeText(getApplicationContext(), "Login successful", Toast.LENGTH_SHORT).show();
  112.                     determineLaunchpage(user);
  113.                 } else {
  114.                     // Login failed. Details found in <e: ParseException>
  115.                     toastExceptionObject(e);
  116.                 }
  117.             }
  118.         });
  119.     }
  120.  
  121.     /**
  122.      * Determines which screen to present, dependent on user type
  123.      * @param user
  124.      */
  125.     public void determineLaunchpage(ParseUser user) {
  126.         // User is logged in successfully, decide what the present
  127.         if((int) user.get("userType") == 2){ //goes to RegisterPatientActivity if user == admin
  128.             Intent registerIntent = new Intent(getApplicationContext(), RegisterPatientActivity.class);
  129.             startActivity(registerIntent);
  130.         } else if ((int) user.get("userType") == 1) { //goes to PatientListActivity if user == doctor
  131.             Intent patientListIntent = new Intent(getApplicationContext(), PatientListActivity.class);
  132.             startActivity(patientListIntent);
  133.         }
  134.         else{ //goes to PatientMenuActivity if userType != admin/doctor
  135.             Intent patientMenuIntent = new Intent(getApplicationContext(), PatientMenuActivity.class);
  136.             startActivity(patientMenuIntent);
  137.         }
  138.     }
  139.  
  140.     /**
  141.      * Called when btnForgotPassword is clicked on activity_login.
  142.      * Performs error checking before calling sendPasswordRecovery method.
  143.      */
  144.     public void btnForgotPassword() {
  145.         // Retrieve text from EditText fields
  146.         email = (String) txtEmail.getText().toString();
  147.  
  148.         // Check if an email address was entered. Error check
  149.         if (email.isEmpty()) {
  150.             txtEmail.setError("Please enter your email for password recovery");
  151.             //End the method early before we attempt standard login
  152.             return;
  153.         }
  154.         // If the Email field is not blank, send the password recovery email
  155.         sendPasswordRecovery(email);
  156.     }
  157.  
  158.     /**
  159.      * Sends a password recovery email to the specified email address.
  160.      * @param recoveryEmail The email address to send password recovery email
  161.      */
  162.     private void sendPasswordRecovery(final String recoveryEmail) {
  163.         ParseUser.requestPasswordResetInBackground(recoveryEmail,
  164.                 new RequestPasswordResetCallback() {
  165.                     public void done(ParseException e) {
  166.                         if (e == null) {
  167.                             // An email was successfully sent with reset instructions.
  168.                             Toast.makeText(getApplicationContext(), "Password recovery email has been sent to " + recoveryEmail, Toast.LENGTH_LONG).show();
  169.                             // Disable btnForgotPassword so you can't spam email
  170.                             // TODO implement timer so button is re-enabled, just in case they sent to wrong email or something
  171.                             btnForgotPassword.setEnabled(false);
  172.                         } else {
  173.                             // Password recovery failed. Details found in <e: ParseException>
  174.                             toastExceptionObject(e);
  175.                         }
  176.                     }
  177.                 });
  178.     }
  179.  
  180.  
  181.     private void toastExceptionObject(Exception e) {
  182.         // Retrieve error message from e : Exception
  183.         String errorMessage = e.getLocalizedMessage();
  184.  
  185.         // Capitalize the first letter
  186.         errorMessage = errorMessage.substring(0, 1).toUpperCase() + errorMessage.substring(1, errorMessage.length());
  187.  
  188.         // Display toast with error message
  189.         Toast.makeText(getApplicationContext(), errorMessage, Toast.LENGTH_LONG).show();
  190.     }
  191.     /*
  192.     Pre-condition: user has attempted to log in
  193.      */
  194.     private void unitTest4(){
  195.         if(ParseUser.getCurrentUser() == null){
  196.             Log.d("unitTest4", "User not successfully logged in. Test failed.");
  197.         }
  198.         else{
  199.             Log.d("unitTest4", "User successfully logged in. Test successful.");
  200.         }
  201.     }
  202.  
  203.     /*
  204.     Pre-condition: user has attempted to log out
  205.      */
  206.     private void unitTest5(){
  207.         if(ParseUser.getCurrentUser() == null){
  208.             Log.d("unitTest5", "User successfully logged out. Test successful.");
  209.         }
  210.         else{
  211.             Log.d("unitTest5", "User not successfully logged out. Test failed.");
  212.         }
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement