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

113 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
import android.app.Application
2022-09-17 15:57:24 +05:30
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationManagerCompat
2022-07-30 14:38:28 +05:30
import androidx.work.ExistingPeriodicWorkPolicy
import com.github.libretube.helpers.ImageHelper
import com.github.libretube.helpers.NotificationHelper
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.helpers.ProxyHelper
import com.github.libretube.helpers.ShortcutHelper
2023-01-31 22:27:24 +05:30
import com.github.libretube.util.ExceptionHandler
2021-12-14 21:45:53 +05:30
2022-09-17 15:57:24 +05:30
class LibreTubeApp : Application() {
override fun onCreate() {
super.onCreate()
2023-01-31 21:16:36 +05:30
instance = this
/**
2022-09-17 15:57:24 +05:30
* Initialize the needed notification channels 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-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
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-11-06 14:51:37 +05:30
NotificationHelper.enqueueWork(
context = this,
existingPeriodicWorkPolicy = ExistingPeriodicWorkPolicy.KEEP
2022-08-26 02:06:07 +05:30
)
2022-11-21 18:42:46 +05:30
/**
* Fetch the image proxy URL for local playlists and the watch history
*/
ProxyHelper.fetchProxyUrl()
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)
2023-01-17 23:35:22 +05:30
/**
* Dynamically create App Shortcuts
*/
ShortcutHelper.createShortcuts(this)
}
/**
2022-09-17 15:57:24 +05:30
* Initializes the required notification channels for the app.
*/
private fun initializeNotificationChannels() {
2022-09-18 14:14:37 +05:30
val downloadChannel = NotificationChannelCompat.Builder(
PLAYLIST_DOWNLOAD_ENQUEUE_CHANNEL_NAME,
NotificationManagerCompat.IMPORTANCE_LOW
)
.setName(getString(R.string.download_playlist))
.setDescription(getString(R.string.enqueue_playlist_description))
.build()
val playlistDownloadEnqueueChannel = NotificationChannelCompat.Builder(
DOWNLOAD_CHANNEL_NAME,
NotificationManagerCompat.IMPORTANCE_LOW
2022-09-18 14:14:37 +05:30
)
2022-09-17 15:57:24 +05:30
.setName(getString(R.string.download_channel_name))
.setDescription(getString(R.string.download_channel_description))
.build()
val playerChannel = NotificationChannelCompat.Builder(
PLAYER_CHANNEL_NAME,
NotificationManagerCompat.IMPORTANCE_LOW
2022-09-18 14:14:37 +05:30
)
.setName(getString(R.string.player_channel_name))
.setDescription(getString(R.string.player_channel_description))
2022-09-17 15:57:24 +05:30
.build()
2022-09-18 14:14:37 +05:30
val pushChannel = NotificationChannelCompat.Builder(
PUSH_CHANNEL_NAME,
NotificationManagerCompat.IMPORTANCE_DEFAULT
2022-09-18 14:14:37 +05:30
)
2022-09-17 15:57:24 +05:30
.setName(getString(R.string.push_channel_name))
.setDescription(getString(R.string.push_channel_description))
.build()
2022-09-17 15:57:24 +05:30
val notificationManager = NotificationManagerCompat.from(this)
2022-09-18 14:14:37 +05:30
notificationManager.createNotificationChannelsCompat(
listOf(
downloadChannel,
playlistDownloadEnqueueChannel,
pushChannel,
playerChannel
)
2022-09-18 14:14:37 +05:30
)
}
2023-01-31 21:16:36 +05:30
companion object {
lateinit var instance: LibreTubeApp
const val DOWNLOAD_CHANNEL_NAME = "download_service"
const val PLAYLIST_DOWNLOAD_ENQUEUE_CHANNEL_NAME = "playlist_download_enqueue"
const val PLAYER_CHANNEL_NAME = "player_mode"
const val PUSH_CHANNEL_NAME = "notification_worker"
2023-01-31 21:16:36 +05:30
}
}