diff --git a/app/src/main/java/com/example/vdcscore/LoginActivity.java b/app/src/main/java/com/example/vdcscore/LoginActivity.java index 402e4e4..79bc92d 100644 --- a/app/src/main/java/com/example/vdcscore/LoginActivity.java +++ b/app/src/main/java/com/example/vdcscore/LoginActivity.java @@ -1,8 +1,10 @@ package com.example.vdcscore; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; import android.widget.Button; +import android.widget.CheckBox; import android.widget.TextView; import android.widget.Toast; @@ -15,10 +17,16 @@ public class LoginActivity extends AppCompatActivity { TextInputEditText editEmail, editPassword; Button btnLogin; + CheckBox checkRememberMe; FirebaseAuth mAuth; private TextView criarContaTextView; private TextView txtForgotPassword; + + private static final String PREFS_NAME = "LoginPrefs"; + private static final String KEY_EMAIL = "email"; + private static final String KEY_PASSWORD = "password"; + private static final String KEY_REMEMBER = "remember"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -28,6 +36,7 @@ public class LoginActivity extends AppCompatActivity { editEmail = findViewById(R.id.editEmail); editPassword = findViewById(R.id.editPassword2); btnLogin = findViewById(R.id.btnLogin); + checkRememberMe = findViewById(R.id.checkRememberMe); criarContaTextView = findViewById(R.id.txtRegister); txtForgotPassword = findViewById(R.id.txtForgotPassword); @@ -36,6 +45,45 @@ public class LoginActivity extends AppCompatActivity { btnLogin.setOnClickListener(v -> loginUser()); criarContaTextView.setOnClickListener(view -> criarConta()); txtForgotPassword.setOnClickListener(view -> recuperarPassword()); + + // Carregar credenciais guardadas + loadSavedCredentials(); + } + + private void loadSavedCredentials() { + SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); + boolean remember = prefs.getBoolean(KEY_REMEMBER, false); + + if (remember) { + String savedEmail = prefs.getString(KEY_EMAIL, ""); + String savedPassword = prefs.getString(KEY_PASSWORD, ""); + + if (!savedEmail.isEmpty() && !savedPassword.isEmpty()) { + editEmail.setText(savedEmail); + editPassword.setText(savedPassword); + checkRememberMe.setChecked(true); + + // Tentar login automático + autoLogin(savedEmail, savedPassword); + } + } + } + + private void autoLogin(String email, String password) { + mAuth.signInWithEmailAndPassword(email, password) + .addOnCompleteListener(task -> { + if (task.isSuccessful()) { + // Login automático bem-sucedido + Intent intent = new Intent(LoginActivity.this, MainActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + startActivity(intent); + finish(); + } else { + // Se o login automático falhar, apenas limpar a password do campo + // O utilizador pode tentar fazer login manualmente + editPassword.setText(""); + } + }); } private void criarConta() { @@ -60,11 +108,19 @@ public class LoginActivity extends AppCompatActivity { mAuth.signInWithEmailAndPassword(email, password) .addOnCompleteListener(task -> { if (task.isSuccessful()) { + // Guardar credenciais se "Lembrar-me" estiver marcado + if (checkRememberMe.isChecked()) { + saveCredentials(email, password); + } else { + clearSavedCredentials(); + } Toast.makeText(this, "Login efetuado!", Toast.LENGTH_SHORT).show(); // Abre a tua página principal - startActivity(new Intent(LoginActivity.this, MainActivity.class)); + Intent intent = new Intent(LoginActivity.this, MainActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + startActivity(intent); finish(); } else { @@ -76,13 +132,32 @@ public class LoginActivity extends AppCompatActivity { } }); } + + private void saveCredentials(String email, String password) { + SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); + SharedPreferences.Editor editor = prefs.edit(); + editor.putString(KEY_EMAIL, email); + editor.putString(KEY_PASSWORD, password); + editor.putBoolean(KEY_REMEMBER, true); + editor.apply(); + } + + private void clearSavedCredentials() { + SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); + SharedPreferences.Editor editor = prefs.edit(); + editor.clear(); + editor.apply(); + } @Override protected void onStart() { super.onStart(); + // Verificar se já está autenticado (caso o login automático não tenha funcionado) if (FirebaseAuth.getInstance().getCurrentUser() != null){ Intent intent = new Intent(LoginActivity.this, MainActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); + finish(); } } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/example/vdcscore/ui/definicoes/ContaActivity.java b/app/src/main/java/com/example/vdcscore/ui/definicoes/ContaActivity.java index 2a4fce1..0fc45bb 100644 --- a/app/src/main/java/com/example/vdcscore/ui/definicoes/ContaActivity.java +++ b/app/src/main/java/com/example/vdcscore/ui/definicoes/ContaActivity.java @@ -3,6 +3,7 @@ package com.example.vdcscore.ui.definicoes; import android.Manifest; import android.app.ProgressDialog; import android.content.Intent; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -89,6 +90,7 @@ public class ContaActivity extends AppCompatActivity { editName = findViewById(R.id.editName); textEmail = findViewById(R.id.textEmail); btnSaveName = findViewById(R.id.btnSaveName); + btnLogout = findViewById(R.id.btnLogout); btnChangePhoto = findViewById(R.id.btnChangePhoto); cardImageContainer = findViewById(R.id.cardImageContainer); @@ -224,6 +226,28 @@ public class ContaActivity extends AppCompatActivity { if (btnSaveName != null) { btnSaveName.setOnClickListener(v -> saveUserName()); } + if (btnLogout != null) { + btnLogout.setOnClickListener(v -> logoutUser()); + } + } + + private void logoutUser() { + // Limpar credenciais guardadas + SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); + SharedPreferences.Editor editor = prefs.edit(); + editor.clear(); + editor.apply(); + + // Fazer logout do Firebase + mAuth.signOut(); + + Toast.makeText(this, "Sessão terminada", Toast.LENGTH_SHORT).show(); + + // Voltar para o login + Intent intent = new Intent(ContaActivity.this, LoginActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + startActivity(intent); + finish(); } private void openImagePicker() { diff --git a/app/src/main/res/layout/activity_conta.xml b/app/src/main/res/layout/activity_conta.xml index bb85261..8af0565 100644 --- a/app/src/main/res/layout/activity_conta.xml +++ b/app/src/main/res/layout/activity_conta.xml @@ -233,6 +233,39 @@ + + + + + +