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

114 lines
3.8 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
2022-09-08 22:25:42 +05:30
import android.annotation.SuppressLint
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-30 14:38:28 +05:30
import androidx.work.ExistingPeriodicWorkPolicy
2022-08-15 13:11:30 +05:30
import com.github.libretube.api.CronetHelper
2022-08-14 13:25:28 +05:30
import com.github.libretube.api.RetrofitInstance
2022-09-08 22:25:42 +05:30
import com.github.libretube.constants.BACKGROUND_CHANNEL_ID
import com.github.libretube.constants.DOWNLOAD_CHANNEL_ID
import com.github.libretube.constants.PUSH_CHANNEL_ID
2022-08-14 13:29:05 +05:30
import com.github.libretube.db.DatabaseHolder
2022-08-01 14:52:08 +05:30
import com.github.libretube.util.ExceptionHandler
2022-08-15 14:17:31 +05:30
import com.github.libretube.util.ImageHelper
2022-07-28 16:09:56 +05:30
import com.github.libretube.util.NotificationHelper
2022-09-08 21:59:00 +05:30
import com.github.libretube.util.PreferenceHelper
2021-12-14 21:45:53 +05:30
2022-06-05 15:12:33 +05:30
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
/**
2022-08-13 23:34:07 +05:30
* Initialize the needed [NotificationChannel]s for DownloadService and BackgroundMode
*/
initializeNotificationChannels()
/**
2022-08-15 13:11:30 +05:30
* Initialize the [PreferenceHelper]
*/
2022-08-26 12:12:13 +05:30
PreferenceHelper.initialize(applicationContext)
2022-07-18 22:45:35 +05:30
2022-08-13 23:34:07 +05:30
/**
* Initialize the [DatabaseHolder]
*/
DatabaseHolder.initializeDatabase(this)
2022-07-18 22:45:35 +05:30
/**
2022-08-15 13:11:30 +05:30
* Bypassing fileUriExposedException, see https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed
2022-07-18 22:45:35 +05:30
*/
val builder = VmPolicy.Builder()
StrictMode.setVmPolicy(builder.build())
2022-07-28 16:09:56 +05:30
2022-07-28 18:15:29 +05:30
/**
2022-08-13 23:34:07 +05:30
* Set the api and the auth api url
2022-07-28 18:15:29 +05:30
*/
2022-09-08 22:59:35 +05:30
RetrofitInstance.initialize()
CronetHelper.initCronet(this)
ImageHelper.initializeImageLoader(this)
2022-07-28 18:15:29 +05:30
2022-07-28 16:09:56 +05:30
/**
2022-08-13 23:34:07 +05:30
* Initialize the notification listener in the background
2022-07-28 16:09:56 +05:30
*/
2022-08-26 02:06:07 +05:30
NotificationHelper(this).enqueueWork(
existingPeriodicWorkPolicy = ExistingPeriodicWorkPolicy.KEEP
)
2022-08-01 14:52:08 +05:30
/**
* Handler for uncaught exceptions
*/
2022-08-02 16:35:27 +05:30
val defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
val exceptionHandler = ExceptionHandler(defaultExceptionHandler)
2022-08-01 14:52:08 +05:30
Thread.setDefaultUncaughtExceptionHandler(exceptionHandler)
}
/**
2022-06-06 20:12:46 +05:30
* Initializes the required [NotificationChannel]s for the app.
*/
2022-09-08 22:25:42 +05:30
@SuppressLint("InlinedApi")
private fun initializeNotificationChannels() {
2022-06-06 20:12:46 +05:30
createNotificationChannel(
2022-07-30 14:51:18 +05:30
DOWNLOAD_CHANNEL_ID,
2022-06-06 20:12:46 +05:30
"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(
2022-07-30 14:51:18 +05:30
BACKGROUND_CHANNEL_ID,
2022-06-06 20:12:46 +05:30
"Background Mode",
"Shows a notification with buttons to control the audio player",
NotificationManager.IMPORTANCE_LOW
)
2022-07-28 16:09:56 +05:30
createNotificationChannel(
2022-07-30 14:51:18 +05:30
PUSH_CHANNEL_ID,
2022-07-28 16:09:56 +05:30
"Notification Worker",
"Shows a notification when new streams are available.",
NotificationManager.IMPORTANCE_DEFAULT
)
2022-06-06 20:12:46 +05:30
}
2022-08-13 22:52:49 +05:30
/**
* Creates a [NotificationChannel]
*/
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)
}
}
}