LibreTube/app/src/main/java/com/github/libretube/MyApp.kt

87 lines
2.9 KiB
Kotlin
Raw Normal View History

2022-02-01 21:22:06 +05:30
package com.github.libretube
2021-12-14 21:45:53 +05:30
import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
2022-07-18 22:45:35 +05:30
import android.os.StrictMode
import android.os.StrictMode.VmPolicy
2022-07-28 16:09:56 +05:30
import androidx.work.Constraints
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.NetworkType
import androidx.work.PeriodicWorkRequest
import androidx.work.WorkManager
import com.github.libretube.preferences.PreferenceHelper
2022-07-28 16:09:56 +05:30
import com.github.libretube.util.NotificationHelper
import com.github.libretube.util.NotificationWorker
import java.util.concurrent.TimeUnit
2021-12-14 21:45:53 +05:30
2022-06-05 15:12:33 +05:30
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
/**
* initialize the needed [NotificationChannel]s for DownloadService and BackgroundMode
*/
initializeNotificationChannels()
/**
* set the applicationContext as context for the [PreferenceHelper]
*/
PreferenceHelper.setContext(applicationContext)
2022-07-18 22:45:35 +05:30
/**
* bypassing fileUriExposedException, see https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed
*/
val builder = VmPolicy.Builder()
StrictMode.setVmPolicy(builder.build())
2022-07-28 16:09:56 +05:30
/**
* initialize the notification
*/
NotificationHelper.enqueueWork(this)
}
/**
2022-06-06 20:12:46 +05:30
* Initializes the required [NotificationChannel]s for the app.
*/
private fun initializeNotificationChannels() {
2022-06-06 20:12:46 +05:30
createNotificationChannel(
"download_service",
"Download Service",
2022-07-28 16:09:56 +05:30
"Shows a notification when downloading media.",
2022-06-06 20:12:46 +05:30
NotificationManager.IMPORTANCE_NONE
)
createNotificationChannel(
"background_mode",
"Background Mode",
"Shows a notification with buttons to control the audio player",
NotificationManager.IMPORTANCE_LOW
)
2022-07-28 16:09:56 +05:30
createNotificationChannel(
"notification_worker",
"Notification Worker",
"Shows a notification when new streams are available.",
NotificationManager.IMPORTANCE_DEFAULT
)
2022-06-06 20:12:46 +05:30
}
2022-06-06 20:12:46 +05:30
private fun createNotificationChannel(
id: String,
name: String,
descriptionText: String,
importance: Int
) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(id, name, importance)
channel.description = descriptionText
// Register the channel in the system
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
2022-06-06 20:12:46 +05:30
notificationManager.createNotificationChannel(channel)
}
}
}