Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.roduntaf;
- import com.roduntaf.objects.AlertDialogManager;
- import com.roduntaf.objects.User;
- import com.roduntaf.bdd.MySQLiteUser;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import java.util.List;
- /**
- *
- * @author GEN11
- */
- public class Connexion extends Activity {
- EditText txtUsername, txtPassword;
- // login button
- Button btnLogin;
- AlertDialogManager alert = new AlertDialogManager();
- // Session Manager Class
- SharedPreferences sharedpreferences;
- public static final String MyPREFERENCES = "MyPrefs" ;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState); //To change body of generated methods, choose Tools | Templates.
- setContentView(R.layout.connexion);
- // Email, Password input text
- txtUsername = (EditText) findViewById(R.id.editText2);
- txtPassword = (EditText) findViewById(R.id.editText);
- sharedpreferences = getApplicationContext().getSharedPreferences(MyPREFERENCES, 0);
- SharedPreferences.Editor editor = sharedpreferences.edit();
- // Login button
- btnLogin = (Button) findViewById(R.id.button);
- // Login button click event
- btnLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // Get username, password from EditText
- String username = txtUsername.getText().toString();
- String password = txtPassword.getText().toString();
- List<User> user = null;
- MySQLiteUser users = new MySQLiteUser(getApplicationContext());
- user=users.getAllUser();
- User userOsk= new User();
- // Check if username, password is filled
- if(username.trim().length() > 0 && password.trim().length() > 0){
- // For testing puspose username, password is checked with sample data
- // username = test
- // password = test
- boolean loginok = false;
- for (int i = 0; i < user.size(); i++) {
- if (username.equals(user.get(i).getLogin()) && password.equals(user.get(i).getMdp()))
- {
- userOsk= user.get(i);
- loginok = true;
- break;
- }
- }
- if(loginok){
- // Creating user login session
- // For testing i am stroing name, email as follow
- // Use user real data
- SharedPreferences.Editor editor = sharedpreferences.edit();
- editor.putBoolean("islog", true);
- editor.putInt("id", userOsk.getId());
- editor.putString("name", username);
- editor.putString("mdp", password);
- editor.commit();
- // Staring MainActivity
- Intent i = new Intent(getApplicationContext(), Menu.class);
- startActivity(i);
- finish();
- }else{
- // username / password doesn't match
- alert.showAlertDialog(Connexion.this, "Login failed..", "Username/Password is incorrect", false);
- }
- }else{
- // user didn't entered username or password
- // Show alert asking him to enter the details
- alert.showAlertDialog(Connexion.this, "Login failed..", "Please enter username and password", false);
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement