diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 05f3716..a7cdee8 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -60,6 +60,7 @@ dependencies { implementation("androidx.compose.ui:ui-tooling-preview") implementation("androidx.compose.material3:material3") implementation("androidx.core:core-ktx:1.12.0") + implementation("androidx.work:work-runtime-ktx:2.8.1") val roomVersion = "2.6.0" implementation("androidx.room:room-runtime:$roomVersion") diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e660465..741afb9 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ xmlns:tools="http://schemas.android.com/tools"> + diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt index 2b00f50..a4b928d 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MainActivity.kt @@ -61,7 +61,7 @@ fun MainScreenMainActivity() { @Preview(showBackground = true) @Composable -fun MainScreenActivityPreview() { +fun MainScreenPreview() { MainScreen() } diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MemoApplication.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MemoApplication.kt index 5880500..c8764d2 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MemoApplication.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/MemoApplication.kt @@ -1,8 +1,70 @@ package fr.uparis.diamantkennel.memorisationapplication import android.app.Application +import android.app.NotificationChannel +import android.app.NotificationManager +import android.content.Context +import android.content.pm.PackageManager +import android.os.Build +import android.util.Log +import androidx.work.PeriodicWorkRequest +import androidx.work.WorkManager import fr.uparis.diamantkennel.memorisationapplication.data.QuestionsDB +import java.util.Calendar +import java.util.concurrent.TimeUnit + +const val CHANNEL_ID = "MY_CHANNEL_ID" class MemoApplication : Application() { val database: QuestionsDB by lazy { QuestionsDB.getDataBase(this) } + + override fun onCreate() { + super.onCreate() + createChannel(this) + + if (this.checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) + schedule() + else + Log.d("MemoApplication", "onCreate: no permission") + } + + private fun createChannel(c: Context) + { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + { + val name = "MY_CHANNEL" + val descriptionText = "notification channel for Memorisation project" + val importance = NotificationManager.IMPORTANCE_DEFAULT + val channel = NotificationChannel(CHANNEL_ID, name, importance).apply { + description = descriptionText + } + val notificationManager = c.getSystemService(Context.NOTIFICATION_SERVICE) + as NotificationManager + notificationManager.createNotificationChannel(channel) + } + } + + private fun schedule() + { + val wm = WorkManager.getInstance(this) + wm.cancelAllWork() + wm.enqueue(request(10, 45)) + } + + private fun request(h: Int, m: Int): PeriodicWorkRequest + { + val now = Calendar.getInstance() + val target = Calendar.getInstance().apply { + set(Calendar.HOUR_OF_DAY, h) + set(Calendar.MINUTE, m) + } + if (target.before(now)) + target.add(Calendar.DAY_OF_YEAR, 1) + val delta=target.timeInMillis - now.timeInMillis + val request = PeriodicWorkRequest.Builder(RappelWorker::class.java, 1, TimeUnit.DAYS) + .setInitialDelay(delta, TimeUnit.MILLISECONDS) + .build() + Log.d("Periodic", "request: $request") + return request + } } diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/RappelWorker.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/RappelWorker.kt new file mode 100644 index 0000000..3bdf58a --- /dev/null +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/RappelWorker.kt @@ -0,0 +1,37 @@ +package fr.uparis.diamantkennel.memorisationapplication + +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import androidx.core.app.NotificationCompat +import androidx.work.Worker +import androidx.work.WorkerParameters + + +class RappelWorker(private val context: Context, workerParams: WorkerParameters) : + Worker(context, workerParams) { + override fun doWork(): Result { + createNotification(context) + return Result.success() + } + + private fun createNotification(context: Context) + { + val intent = Intent(context, MainActivity::class.java) + val pendingIntent = PendingIntent.getActivity( + context, 0, intent, PendingIntent.FLAG_IMMUTABLE + ) + + val builder = NotificationCompat.Builder(context, CHANNEL_ID) + .setSmallIcon(R.drawable.ic_launcher_foreground) + .setContentTitle("Rappel") + .setContentText("Il est temps de réviser !") + .setContentIntent(pendingIntent) + .setPriority(NotificationCompat.PRIORITY_DEFAULT) + val notification = builder.build() + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) + as NotificationManager + notificationManager.notify(0, notification) + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/SettingsScreen.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/SettingsScreen.kt index df7d4ff..b67982c 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/SettingsScreen.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/SettingsScreen.kt @@ -1,5 +1,8 @@ package fr.uparis.diamantkennel.memorisationapplication +import android.util.Log +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues @@ -33,6 +36,12 @@ fun SettingsScreen(padding: PaddingValues, model: SettingsViewModel = viewModel( var deletionDBRequest by model.deletionDB var cleanStatRequest by model.deletionStat + val permissionLauncher = rememberLauncherForActivityResult( + contract = ActivityResultContracts.RequestPermission(), + ) { + Log.d("permissions", if(it) "granted" else "denied") + } + if (deletionDBRequest) { DeletionDBDialog(model::deleteDb) { deletionDBRequest = false } } @@ -70,6 +79,15 @@ fun SettingsScreen(padding: PaddingValues, model: SettingsViewModel = viewModel( Text(text = context.getString(R.string.clean_stat_button)) } } + + Button( + enabled = model.isNotGranted(context), + onClick = { + model.requestNotificationPermission(permissionLauncher) + } + ) { + Text(text = context.getString(R.string.permission_button)) + } } } diff --git a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/SettingsViewModel.kt b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/SettingsViewModel.kt index 4e95763..30df806 100644 --- a/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/SettingsViewModel.kt +++ b/app/src/main/java/fr/uparis/diamantkennel/memorisationapplication/ui/SettingsViewModel.kt @@ -1,6 +1,9 @@ package fr.uparis.diamantkennel.memorisationapplication.ui import android.app.Application +import android.content.Context +import android.content.pm.PackageManager +import androidx.activity.result.ActivityResultLauncher import androidx.compose.runtime.mutableStateOf import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.intPreferencesKey @@ -59,4 +62,14 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application } return ((good.toFloat() / total.toFloat()) * 100).toInt() } + + fun requestNotificationPermission(launcher: ActivityResultLauncher) + { + launcher.launch(android.Manifest.permission.POST_NOTIFICATIONS) + } + + fun isNotGranted(context: Context): Boolean + { + return context.checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b98e409..83c0b79 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -50,4 +50,5 @@ Réinitialiser stats Réinitialiser les statistiques Voulez-vous réinitialiser les statistiques ? + Autoriser notification