corrigir os novos erros amnhã e adicionar qual tipo de sexo a pessoa é no register
This commit is contained in:
@@ -1,18 +1,26 @@
|
||||
package com.example.pap;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Color;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.Base64;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.contract.ActivityResultContracts;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
@@ -34,6 +42,9 @@ public class DesafiosActivity extends AppCompatActivity {
|
||||
private int desafioAtualSendoGravado = -1; // 0=Agua, 1=D1, 2=D2, 3=D3, 4=D4
|
||||
private float litrosAgua = 0.0f;
|
||||
|
||||
private ActivityResultLauncher<Intent> videoLauncher;
|
||||
private AlertDialog popupCarregamento; // A janela de loading
|
||||
|
||||
private final String MINHA_API_KEY = "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";
|
||||
|
||||
@Override
|
||||
@@ -42,6 +53,9 @@ public class DesafiosActivity extends AppCompatActivity {
|
||||
setContentView(R.layout.activity_desafios);
|
||||
|
||||
tvStatusGeralIA = findViewById(R.id.tvStatusGeralIA);
|
||||
// Esconde o texto antigo do topo, já não precisamos dele!
|
||||
tvStatusGeralIA.setVisibility(View.GONE);
|
||||
|
||||
tvStatusAgua = findViewById(R.id.tvStatusAgua);
|
||||
tvStatusD1 = findViewById(R.id.tvStatusD1);
|
||||
tvStatusD2 = findViewById(R.id.tvStatusD2);
|
||||
@@ -58,7 +72,7 @@ public class DesafiosActivity extends AppCompatActivity {
|
||||
|
||||
verificarResetMeiaNoite();
|
||||
|
||||
ActivityResultLauncher<Intent> videoLauncher = registerForActivityResult(
|
||||
videoLauncher = registerForActivityResult(
|
||||
new ActivityResultContracts.StartActivityForResult(),
|
||||
result -> {
|
||||
if (result.getResultCode() == RESULT_OK && result.getData() != null) {
|
||||
@@ -69,18 +83,41 @@ public class DesafiosActivity extends AppCompatActivity {
|
||||
}
|
||||
});
|
||||
|
||||
btnVideoAgua.setOnClickListener(v -> { desafioAtualSendoGravado = 0; abrirCamera(videoLauncher); });
|
||||
btnVideoD1.setOnClickListener(v -> { desafioAtualSendoGravado = 1; abrirCamera(videoLauncher); });
|
||||
btnVideoD2.setOnClickListener(v -> { desafioAtualSendoGravado = 2; abrirCamera(videoLauncher); });
|
||||
btnVideoD3.setOnClickListener(v -> { desafioAtualSendoGravado = 3; abrirCamera(videoLauncher); });
|
||||
btnVideoD4.setOnClickListener(v -> { desafioAtualSendoGravado = 4; abrirCamera(videoLauncher); });
|
||||
btnVideoAgua.setOnClickListener(v -> { desafioAtualSendoGravado = 0; verificarPermissaoEAbrir(); });
|
||||
btnVideoD1.setOnClickListener(v -> { desafioAtualSendoGravado = 1; verificarPermissaoEAbrir(); });
|
||||
btnVideoD2.setOnClickListener(v -> { desafioAtualSendoGravado = 2; verificarPermissaoEAbrir(); });
|
||||
btnVideoD3.setOnClickListener(v -> { desafioAtualSendoGravado = 3; verificarPermissaoEAbrir(); });
|
||||
btnVideoD4.setOnClickListener(v -> { desafioAtualSendoGravado = 4; verificarPermissaoEAbrir(); });
|
||||
}
|
||||
|
||||
private void abrirCamera(ActivityResultLauncher<Intent> launcher) {
|
||||
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
|
||||
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
|
||||
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
|
||||
launcher.launch(intent);
|
||||
private void verificarPermissaoEAbrir() {
|
||||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 100);
|
||||
} else {
|
||||
abrirCameraReal();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if (requestCode == 100) {
|
||||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
abrirCameraReal();
|
||||
} else {
|
||||
Toast.makeText(this, "Precisas de dar permissão da câmara para fazer o desafio!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void abrirCameraReal() {
|
||||
try {
|
||||
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
|
||||
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
|
||||
videoLauncher.launch(intent);
|
||||
} catch (Exception e) {
|
||||
Toast.makeText(this, "Erro: Não foi possível iniciar a câmara.", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
private void verificarResetMeiaNoite() {
|
||||
@@ -97,7 +134,6 @@ public class DesafiosActivity extends AppCompatActivity {
|
||||
editor.putBoolean("d3_concluido", false);
|
||||
editor.putBoolean("d4_concluido", false);
|
||||
|
||||
// Zera a água e as calorias dos desafios diários
|
||||
editor.putInt("agua_hoje", 0);
|
||||
editor.putInt("calorias_desafios", 0);
|
||||
editor.apply();
|
||||
@@ -128,14 +164,46 @@ public class DesafiosActivity extends AppCompatActivity {
|
||||
}
|
||||
}
|
||||
|
||||
// ==========================================
|
||||
// NOVOS POP-UPS DE LOADING E RESULTADOS
|
||||
// ==========================================
|
||||
private void mostrarLoading() {
|
||||
runOnUiThread(() -> {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setTitle("A analisar o vídeo... ⏳");
|
||||
builder.setMessage("A Inteligência Artificial está a avaliar o teu desempenho. Por favor, aguarda um momento.");
|
||||
builder.setCancelable(false); // Impede que o utilizador feche sem querer
|
||||
|
||||
popupCarregamento = builder.create();
|
||||
popupCarregamento.show();
|
||||
});
|
||||
}
|
||||
|
||||
private void mostrarResultadoFinal(String titulo, String mensagem) {
|
||||
runOnUiThread(() -> {
|
||||
// Fecha a janela de "A aguardar..."
|
||||
if (popupCarregamento != null && popupCarregamento.isShowing()) {
|
||||
popupCarregamento.dismiss();
|
||||
}
|
||||
|
||||
// Abre a janela com o resultado final
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setTitle(titulo);
|
||||
builder.setMessage(mensagem);
|
||||
builder.setPositiveButton("OK", null);
|
||||
builder.show();
|
||||
});
|
||||
}
|
||||
|
||||
private void enviarVideoParaIA(Uri uri) {
|
||||
tvStatusGeralIA.setText("A processar vídeo e enviar para a IA... ⏳");
|
||||
bloquearBotoes(false);
|
||||
mostrarLoading(); // Mostra a janela de loading logo aqui!
|
||||
|
||||
new Thread(() -> {
|
||||
String base64Video = converterVideo(uri);
|
||||
if (base64Video == null) {
|
||||
runOnUiThread(() -> { tvStatusGeralIA.setText("Erro ao ler vídeo."); bloquearBotoes(true); });
|
||||
mostrarResultadoFinal("Erro no Vídeo ⚠️", "Não foi possível ler o vídeo gravado.");
|
||||
runOnUiThread(() -> bloquearBotoes(true));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -162,12 +230,14 @@ public class DesafiosActivity extends AppCompatActivity {
|
||||
String respostaIA = response.body().choices.get(0).message.content;
|
||||
processarResposta(respostaIA);
|
||||
} else {
|
||||
runOnUiThread(() -> { tvStatusGeralIA.setText("Erro na IA: " + response.code()); bloquearBotoes(true); });
|
||||
mostrarResultadoFinal("Erro de Servidor ⚠️", "Erro ao contactar a IA. Código: " + response.code());
|
||||
runOnUiThread(() -> bloquearBotoes(true));
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onFailure(Call<AiResponse> call, Throwable t) {
|
||||
runOnUiThread(() -> { tvStatusGeralIA.setText("Erro de rede."); bloquearBotoes(true); });
|
||||
mostrarResultadoFinal("Sem Internet 🌐", "Não conseguimos ligar à IA. Verifica a tua rede.");
|
||||
runOnUiThread(() -> bloquearBotoes(true));
|
||||
}
|
||||
});
|
||||
}).start();
|
||||
@@ -191,10 +261,13 @@ public class DesafiosActivity extends AppCompatActivity {
|
||||
editor.putFloat("agua_litros", litrosAgua);
|
||||
editor.putInt("agua_hoje", (int) (litrosAgua / 0.25f));
|
||||
|
||||
tvStatusGeralIA.setText("IA leu: +" + lido + " Litros!");
|
||||
// Mostra o resultado da Água!
|
||||
mostrarResultadoFinal("Bom trabalho! 💧", "A IA detetou que bebeste +" + lido + " Litros!");
|
||||
} catch (Exception e) {
|
||||
tvStatusGeralIA.setText("Erro a ler os litros. Tenta de novo.");
|
||||
mostrarResultadoFinal("Oops! 🤔", "A IA teve dificuldade em ler a quantidade exata. Tenta gravar de novo.");
|
||||
}
|
||||
} else {
|
||||
mostrarResultadoFinal("Oops! 🤔", "A IA não percebeu o vídeo. Garante que o copo ou garrafa se vê bem!");
|
||||
}
|
||||
} else {
|
||||
if (texto.contains("Status: Concluido")) {
|
||||
@@ -205,17 +278,17 @@ public class DesafiosActivity extends AppCompatActivity {
|
||||
if (desafioAtualSendoGravado == 3) { editor.putBoolean("d3_concluido", true); caloriasAQueimar = 2; }
|
||||
if (desafioAtualSendoGravado == 4) { editor.putBoolean("d4_concluido", true); caloriasAQueimar = 3; }
|
||||
|
||||
// Soma as calorias queimadas e atualiza a pontuação
|
||||
int caloriasTotaisQueimadas = prefs.getInt("calorias_desafios", 0) + caloriasAQueimar;
|
||||
editor.putInt("calorias_desafios", caloriasTotaisQueimadas);
|
||||
|
||||
tvStatusGeralIA.setText("IA: Desafio Validado! ✅ +50 Pontos | 🔥 +" + caloriasAQueimar + " kcal");
|
||||
|
||||
perfilEditor.putInt("pontos", perfilPrefs.getInt("pontos", 0) + 50);
|
||||
perfilEditor.putInt("desafios_concluidos", perfilPrefs.getInt("desafios_concluidos", 0) + 1);
|
||||
perfilEditor.apply();
|
||||
|
||||
// Mostra o resultado do Exercício!
|
||||
mostrarResultadoFinal("Desafio Validado! ✅", "Ganhaste +50 Pontos e queimaste " + caloriasAQueimar + " kcal. Continua assim!");
|
||||
} else {
|
||||
tvStatusGeralIA.setText("IA: Desafio Falhou ou vídeo pouco claro. ❌");
|
||||
mostrarResultadoFinal("Desafio Falhou ❌", "A IA acha que o movimento não foi claro ou bem feito. Tenta outra vez!");
|
||||
}
|
||||
}
|
||||
editor.apply();
|
||||
|
||||
Reference in New Issue
Block a user