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

110 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-07-18 22:45:35 +05:30
import android.os.StrictMode
import android.os.StrictMode.VmPolicy
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
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
2022-11-21 18:42:46 +05:30
import com.github.libretube.util.ProxyHelper
2021-12-14 21:45:53 +05:30
2022-09-17 15:57:24 +05:30
class LibreTubeApp : Application() {
override fun onCreate() {
super.onCreate()
/**
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-08-13 23:34:07 +05:30
/**
* Initialize the [DatabaseHolder]
*/
2022-09-18 22:54:31 +05:30
DatabaseHolder().initializeDatabase(this)
2022-08-13 23:34:07 +05:30
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-11-06 14:51:37 +05:30
NotificationHelper.enqueueWork(
context = this,
2022-08-26 02:06:07 +05:30
existingPeriodicWorkPolicy = ExistingPeriodicWorkPolicy.KEEP
)
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)
}
/**
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(
DOWNLOAD_CHANNEL_ID,
NotificationManagerCompat.IMPORTANCE_NONE
)
2022-09-17 15:57:24 +05:30
.setName(getString(R.string.download_channel_name))
.setDescription(getString(R.string.download_channel_description))
.build()
2022-09-18 14:14:37 +05:30
val backgroundChannel = NotificationChannelCompat.Builder(
BACKGROUND_CHANNEL_ID,
NotificationManagerCompat.IMPORTANCE_LOW
)
2022-09-17 15:57:24 +05:30
.setName(getString(R.string.background_channel_name))
.setDescription(getString(R.string.background_channel_description))
.build()
2022-09-18 14:14:37 +05:30
val pushChannel = NotificationChannelCompat.Builder(
PUSH_CHANNEL_ID,
NotificationManagerCompat.IMPORTANCE_DEFAULT
)
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,
backgroundChannel,
pushChannel
)
)
}
}