commit 6b42da2b921f75ba3be5473ff77e0212347e0f06 Author: LJ5O <75009579+LJ5O@users.noreply.github.com> Date: Mon Jan 19 22:51:07 2026 +0100 TP7 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/.idea/AndroidProjectSystem.xml b/.idea/AndroidProjectSystem.xml new file mode 100644 index 0000000..4a53bee --- /dev/null +++ b/.idea/AndroidProjectSystem.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..b86273d --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/deploymentTargetSelector.xml b/.idea/deploymentTargetSelector.xml new file mode 100644 index 0000000..b268ef3 --- /dev/null +++ b/.idea/deploymentTargetSelector.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/deviceManager.xml b/.idea/deviceManager.xml new file mode 100644 index 0000000..91f9558 --- /dev/null +++ b/.idea/deviceManager.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..639c779 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/markdown.xml b/.idea/markdown.xml new file mode 100644 index 0000000..c61ea33 --- /dev/null +++ b/.idea/markdown.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/.idea/migrations.xml b/.idea/migrations.xml new file mode 100644 index 0000000..f8051a6 --- /dev/null +++ b/.idea/migrations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b2c751a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..16660f1 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file 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..c18a9de --- /dev/null +++ b/app/build.gradle.kts @@ -0,0 +1,44 @@ +plugins { + alias(libs.plugins.android.application) +} + +android { + namespace = "com.example.contentprovidertaccoen" + compileSdk { + version = release(36) + } + + defaultConfig { + applicationId = "com.example.contentprovidertaccoen" + 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/contentprovidertaccoen/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/example/contentprovidertaccoen/ExampleInstrumentedTest.java new file mode 100644 index 0000000..2b0a35e --- /dev/null +++ b/app/src/androidTest/java/com/example/contentprovidertaccoen/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.example.contentprovidertaccoen; + +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.contentprovidertaccoen", 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..9179bad --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/contentprovidertaccoen/EventAdapter.java b/app/src/main/java/com/example/contentprovidertaccoen/EventAdapter.java new file mode 100644 index 0000000..0988304 --- /dev/null +++ b/app/src/main/java/com/example/contentprovidertaccoen/EventAdapter.java @@ -0,0 +1,53 @@ +package com.example.contentprovidertaccoen; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; +import java.util.Locale; + +// Adapteur pour afficher le layout de la liste des évènements +public class EventAdapter extends ArrayAdapter { + + public EventAdapter(@NonNull Context context, @NonNull List events) { + super(context, 0, events); + } + + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + // Récupérer l'objet EventItem pour cette position + EventItem event = getItem(position); + + if (convertView == null) { // Vérifier si une vue existante est réutilisée, sinon la créer + convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_event, parent, false); + } + + TextView tvTitle = convertView.findViewById(R.id.eventTitle); // Récupérer les TextViews du layout + TextView tvStartDate = convertView.findViewById(R.id.eventStartDate); + TextView tvEndDate = convertView.findViewById(R.id.eventEndDate); + + if (event != null) { + tvTitle.setText(event.getTitle()); // Remplir les textes avec les données de l'événement + tvStartDate.setText(getContext().getString(R.string.debut) + formatDate(event.getStartDate())); + tvEndDate.setText(getContext().getString(R.string.fin) + formatDate(event.getEndDate())); + } + + return convertView; // Retourner vers l'affichage sur l'écran + } + + // Méthode pour formater le timestamp en une chaîne de caractères lisible + private String formatDate(long millis) { + SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.getDefault()); // https://stackoverflow.com/a/37183301 + return sdf.format(new Date(millis)); + } +} diff --git a/app/src/main/java/com/example/contentprovidertaccoen/EventItem.java b/app/src/main/java/com/example/contentprovidertaccoen/EventItem.java new file mode 100644 index 0000000..c7be898 --- /dev/null +++ b/app/src/main/java/com/example/contentprovidertaccoen/EventItem.java @@ -0,0 +1,26 @@ +package com.example.contentprovidertaccoen; + +// Classe qui me permet de représenter et stocker les évènements +public class EventItem { + private String title; + private long startDate; + private long endDate; + + public EventItem(String title, long startDate, long endDate) { + this.title = title; + this.startDate = startDate; + this.endDate = endDate; + } + + public String getTitle() { + return title; + } + + public long getStartDate() { + return startDate; + } + + public long getEndDate() { + return endDate; + } +} diff --git a/app/src/main/java/com/example/contentprovidertaccoen/MainActivity.java b/app/src/main/java/com/example/contentprovidertaccoen/MainActivity.java new file mode 100644 index 0000000..41d17fb --- /dev/null +++ b/app/src/main/java/com/example/contentprovidertaccoen/MainActivity.java @@ -0,0 +1,43 @@ +package com.example.contentprovidertaccoen; + +import android.content.Intent; +import android.os.Bundle; +import android.widget.Button; + +import androidx.activity.EdgeToEdge; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; + +public class MainActivity extends AppCompatActivity { + + private Button gererAgendaButton; + private Button visualiserButton; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EdgeToEdge.enable(this); + setContentView(R.layout.activity_main); + 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; + }); + + gererAgendaButton = findViewById(R.id.gererAgendaButton); + visualiserButton = findViewById(R.id.visualiserBitton); + + gererAgendaButton.setOnClickListener(v -> { + Intent intent = new Intent(Intent.ACTION_MAIN); // Intention utilisée pour lancer une nouvelle app + intent.addCategory(Intent.CATEGORY_APP_CALENDAR); // Et en données, j'ajoute que je veux une app qui gère l'agenda + startActivity(intent); // Et je lance. Je n'attends aucun retour de cet intent + }); + + visualiserButton.setOnClickListener(v -> { + Intent intent = new Intent(MainActivity.this, VisualiserActivity.class); + startActivity(intent); + }); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/contentprovidertaccoen/VisualiserActivity.java b/app/src/main/java/com/example/contentprovidertaccoen/VisualiserActivity.java new file mode 100644 index 0000000..87f64c4 --- /dev/null +++ b/app/src/main/java/com/example/contentprovidertaccoen/VisualiserActivity.java @@ -0,0 +1,141 @@ +package com.example.contentprovidertaccoen; + +import android.Manifest; +import android.content.ContentResolver; +import android.content.pm.PackageManager; +import android.database.Cursor; +import android.net.Uri; +import android.os.Bundle; +import android.provider.CalendarContract; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.Toast; + +import androidx.activity.EdgeToEdge; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.SwitchCompat; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; + +public class VisualiserActivity extends AppCompatActivity { + + private static final int PERMISSIONS_REQUEST_READ_CALENDAR = 100; + private ListView eventsListView; + private SwitchCompat sortSwitch; + private ArrayList eventItems; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EdgeToEdge.enable(this); + setContentView(R.layout.activity_visualiser); + 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; + }); + + eventsListView = findViewById(R.id.eventsListView); + sortSwitch = findViewById(R.id.sortSwitch); // Switch pour changer le tri + eventItems = new ArrayList<>(); + + sortSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> { + sortAndRefreshList(); // Fonction qui trie la liste + }); + + checkPermissionAndLoadEvents(); // Méthode qui me permet de vérifier la permission et de continuer + } + + private void checkPermissionAndLoadEvents() { + if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) { + // L'appli n'a pas la permission, je tente de la demander + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALENDAR}, PERMISSIONS_REQUEST_READ_CALENDAR);// Lancer la demande + } else { + // OK + loadCalendarEvents(); + } + } + + @Override // Méthode appelée lors de la réponse à la demande d'accès agenda + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + if (requestCode == PERMISSIONS_REQUEST_READ_CALENDAR) { // Utilisé pour s'assurer de vérifier le bon event/la bonne demande + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // Si la perm a été acceptée + // Permission accordée + loadCalendarEvents(); + } else { + // Pas d'accès + Toast.makeText(this, "Permission de lire l'agenda refusée", Toast.LENGTH_SHORT).show(); + finish(); // Retour à l'écran d'avant, il n'y a rien que je puisse faire sans l'accès + } + } + } + + private void loadCalendarEvents() { + eventItems.clear(); + ContentResolver contentResolver = getContentResolver(); // Préparation du récupérateur + Uri uri = CalendarContract.Events.CONTENT_URI; + String[] projection = new String[]{ // Projection ne lisant que les champs utiles des évents + CalendarContract.Events.TITLE, + CalendarContract.Events.DTSTART, + CalendarContract.Events.DTEND + }; + + Cursor cursor = null; + try { + cursor = contentResolver.query(uri, projection, null, null, null); + } catch (SecurityException e) { + // Sécurité, s'il n'y a pas/plus l'accès au calendrier, Android devrait lever une SecurityException que je gère ici + Toast.makeText(this, "Erreur de permission: " + e.getMessage(), Toast.LENGTH_LONG).show(); + finish(); // On arrête là + return; + } + + + if (cursor != null) { // Sécurité + try { + int titleColumn = cursor.getColumnIndexOrThrow(CalendarContract.Events.TITLE); // LEcture des données des events + int startDateColumn = cursor.getColumnIndexOrThrow(CalendarContract.Events.DTSTART); + int endDateColumn = cursor.getColumnIndexOrThrow(CalendarContract.Events.DTEND); + + while (cursor.moveToNext()) { // Vérifier tous les évents pour en extraire le titre + String title = cursor.getString(titleColumn); + long startDate = cursor.getLong(startDateColumn); + long endDate = cursor.getLong(endDateColumn); + + if (title != null) { // éviter les titres vides (vu en test) + eventItems.add(new EventItem(title, startDate, endDate)); + } + } + } finally { + cursor.close(); + } + } + + sortAndRefreshList(); + } + + private void sortAndRefreshList() { + // Tri alphabétique, utilisant le titre de l'évent + eventItems.sort(Comparator.comparing(EventItem::getTitle)); + + // Et si le switch est activé, j'inverse la liste + if (sortSwitch.isChecked()) { + Collections.reverse(eventItems); + } + + //ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, eventTitles); // Adapteur tout fait pous rapide à utiliser + //eventsListView.setAdapter(adapter); // https://stackoverflow.com/a/3663829 + + EventAdapter adapter = new EventAdapter(this, eventItems); + eventsListView.setAdapter(adapter); + } +} \ 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..b473449 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,45 @@ + + + + + +