commit 0c11ae21493022333f5d851f9203883ce7849afa Author: 230419 <230419@epvc.pt> Date: Thu Apr 23 10:22:11 2026 +0100 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts new file mode 100644 index 0000000..fe2487c --- /dev/null +++ b/app/build.gradle.kts @@ -0,0 +1,43 @@ +plugins { + alias(libs.plugins.android.application) +} + +android { + namespace = "com.example.trabalhoapi_vitormoreira" + compileSdk = 36 + + defaultConfig { + applicationId = "com.example.trabalhoapi_vitormoreira" + minSdk = 24 + targetSdk = 36 + versionCode = 1 + versionName = "1.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } +} + +dependencies { + + implementation(libs.appcompat) + implementation(libs.material) + implementation(libs.activity) + implementation(libs.constraintlayout) + testImplementation(libs.junit) + androidTestImplementation(libs.ext.junit) + androidTestImplementation(libs.espresso.core) +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/trabalhoapi_vitormoreira/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/example/trabalhoapi_vitormoreira/ExampleInstrumentedTest.java new file mode 100644 index 0000000..bf6bde7 --- /dev/null +++ b/app/src/androidTest/java/com/example/trabalhoapi_vitormoreira/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.example.trabalhoapi_vitormoreira; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("com.example.trabalhoapi_vitormoreira", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..a90a412 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/trabalhoapi_vitormoreira/MainActivity.java b/app/src/main/java/com/example/trabalhoapi_vitormoreira/MainActivity.java new file mode 100644 index 0000000..5ec7bba --- /dev/null +++ b/app/src/main/java/com/example/trabalhoapi_vitormoreira/MainActivity.java @@ -0,0 +1,119 @@ +package com.example.trabalhoapi_vitormoreira; + +import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; +import android.widget.Button; +import android.widget.TextView; +import android.widget.Toast; +import android.app.AlertDialog; + +import androidx.activity.EdgeToEdge; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; + +import org.json.JSONObject; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +public class MainActivity extends AppCompatActivity { + + private TextView textViewFact; + private Button buttonGetFact; + private Button buttonShowHistory; + private final ExecutorService executorService = Executors.newSingleThreadExecutor(); + private final Handler handler = new Handler(Looper.getMainLooper()); + private final List factHistory = new ArrayList<>(); + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EdgeToEdge.enable(this); + setContentView(R.layout.activity_main); + + textViewFact = findViewById(R.id.textViewFact); + buttonGetFact = findViewById(R.id.buttonGetFact); + buttonShowHistory = findViewById(R.id.buttonShowHistory); + + ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { + Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); + v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); + return insets; + }); + + buttonGetFact.setOnClickListener(v -> fetchCatFact()); + + buttonShowHistory.setOnClickListener(v -> showHistoryDialog()); + } + + private void showHistoryDialog() { + if (factHistory.isEmpty()) { + Toast.makeText(this, "Ainda não existem factos no histórico.", Toast.LENGTH_SHORT).show(); + return; + } + + String[] factsArray = factHistory.toArray(new String[0]); + + new AlertDialog.Builder(this) + .setTitle("Histórico de Factos") + .setItems(factsArray, null) + .setPositiveButton("Fechar", null) + .show(); + } + + private void fetchCatFact() { + buttonGetFact.setEnabled(false); + textViewFact.setText("A carregar..."); + + executorService.execute(() -> { + try { + URL url = new URL("https://catfact.ninja/fact"); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.connect(); + + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String inputLine; + StringBuilder response = new StringBuilder(); + + while ((inputLine = in.readLine()) != null) { + response.append(inputLine); + } + in.close(); + + JSONObject jsonObject = new JSONObject(response.toString()); + String fact = jsonObject.getString("fact"); + + handler.post(() -> { + factHistory.add(fact); + textViewFact.setText(fact); + buttonGetFact.setEnabled(true); + }); + } else { + handler.post(() -> { + textViewFact.setText("Erro ao obter facto."); + buttonGetFact.setEnabled(true); + }); + } + } catch (Exception e) { + e.printStackTrace(); + handler.post(() -> { + Toast.makeText(MainActivity.this, "Erro: " + e.getMessage(), Toast.LENGTH_SHORT).show(); + textViewFact.setText("Falha ao carregar facto."); + buttonGetFact.setEnabled(true); + }); + } + }); + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_foreground.xml b/app/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..13fdad6 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,46 @@ + + + + + +