Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.roduntaf;
  7.  
  8. import com.roduntaf.objects.AlertDialogManager;
  9. import com.roduntaf.objects.User;
  10. import com.roduntaf.bdd.MySQLiteUser;
  11. import android.app.Activity;
  12. import android.content.Intent;
  13. import android.content.SharedPreferences;
  14. import android.os.Bundle;
  15. import android.view.View;
  16. import android.widget.Button;
  17. import android.widget.EditText;
  18. import android.widget.Toast;
  19. import java.util.List;
  20.  
  21. /**
  22.  *
  23.  * @author GEN11
  24.  */
  25. public class Connexion extends Activity {
  26.  
  27.      EditText txtUsername, txtPassword;
  28.      
  29.     // login button
  30.     Button btnLogin;
  31.      
  32.  AlertDialogManager alert = new AlertDialogManager();
  33.      
  34.     // Session Manager Class
  35.    
  36.     SharedPreferences sharedpreferences;
  37.     public static final String MyPREFERENCES = "MyPrefs" ;
  38.     @Override
  39.     protected void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState); //To change body of generated methods, choose Tools | Templates.
  41.         setContentView(R.layout.connexion);
  42.                    
  43.          
  44.         // Email, Password input text
  45.         txtUsername = (EditText) findViewById(R.id.editText2);
  46.         txtPassword = (EditText) findViewById(R.id.editText);
  47.          sharedpreferences = getApplicationContext().getSharedPreferences(MyPREFERENCES, 0);
  48.       SharedPreferences.Editor editor = sharedpreferences.edit();
  49.          
  50.         // Login button
  51.         btnLogin = (Button) findViewById(R.id.button);
  52.          
  53.          
  54.         // Login button click event
  55.         btnLogin.setOnClickListener(new View.OnClickListener() {
  56.              
  57.             @Override
  58.             public void onClick(View arg0) {
  59.                 // Get username, password from EditText
  60.                 String username = txtUsername.getText().toString();
  61.                 String password = txtPassword.getText().toString();
  62.                  List<User> user = null;
  63.                  MySQLiteUser users = new MySQLiteUser(getApplicationContext());
  64.                  user=users.getAllUser();
  65.                  User userOsk= new User();
  66.                 // Check if username, password is filled                
  67.                 if(username.trim().length() > 0 && password.trim().length() > 0){
  68.                     // For testing puspose username, password is checked with sample data
  69.                     // username = test
  70.                     // password = test
  71.                     boolean loginok = false;
  72.                     for (int i = 0; i < user.size(); i++) {
  73.                        if (username.equals(user.get(i).getLogin()) && password.equals(user.get(i).getMdp()))
  74.                        {
  75.                            userOsk= user.get(i);
  76.                            loginok = true;
  77.                            break;
  78.                        }
  79.                     }
  80.                     if(loginok){
  81.                          
  82.                         // Creating user login session
  83.                         // For testing i am stroing name, email as follow
  84.                         // Use user real data
  85.                          SharedPreferences.Editor editor = sharedpreferences.edit();
  86.                          editor.putBoolean("islog", true);
  87.                          editor.putInt("id", userOsk.getId());
  88.                         editor.putString("name", username);
  89.                         editor.putString("mdp", password);
  90.                         editor.commit();
  91.                          
  92.                         // Staring MainActivity
  93.                         Intent i = new Intent(getApplicationContext(), Menu.class);
  94.                         startActivity(i);
  95.                         finish();
  96.                          
  97.                     }else{
  98.                         // username / password doesn't match
  99.                         alert.showAlertDialog(Connexion.this, "Login failed..", "Username/Password is incorrect", false);
  100.                     }              
  101.                 }else{
  102.                     // user didn't entered username or password
  103.                     // Show alert asking him to enter the details
  104.                     alert.showAlertDialog(Connexion.this, "Login failed..", "Please enter username and password", false);
  105.                 }
  106.                  
  107.             }
  108.         });
  109.     }        
  110.    
  111.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement