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

146 lines
4.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
2022-08-13 22:52:49 +05:30
import android.content.Context
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-14 13:25:28 +05:30
import com.github.libretube.api.RetrofitInstance
2022-08-14 13:29:05 +05:30
import com.github.libretube.db.DatabaseHolder
import com.github.libretube.preferences.PreferenceHelper
2022-07-28 18:15:29 +05:30
import com.github.libretube.preferences.PreferenceKeys
2022-08-01 14:52:08 +05:30
import com.github.libretube.util.ExceptionHandler
2022-07-28 16:09:56 +05:30
import com.github.libretube.util.NotificationHelper
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-13 23:34:07 +05:30
* Set the applicationContext as context for the [PreferenceHelper]
*/
PreferenceHelper.setContext(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
/**
* 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
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
*/
setRetrofitApiUrls()
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-07-30 14:38:28 +05:30
NotificationHelper.enqueueWork(this, 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-08-13 22:52:49 +05:30
/**
2022-08-13 23:34:07 +05:30
* Legacy preference file migration
2022-08-13 22:52:49 +05:30
*/
prefFileMigration()
}
2022-07-28 18:15:29 +05:30
/**
2022-08-13 23:34:07 +05:30
* Set the api urls needed for the [RetrofitInstance]
2022-07-28 18:15:29 +05:30
*/
private fun setRetrofitApiUrls() {
RetrofitInstance.url =
PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, PIPED_API_URL)
// set auth instance
RetrofitInstance.authUrl =
if (PreferenceHelper.getBoolean(PreferenceKeys.AUTH_INSTANCE_TOGGLE, false)) {
PreferenceHelper.getString(
PreferenceKeys.AUTH_INSTANCE,
PIPED_API_URL
)
} else {
RetrofitInstance.url
}
}
/**
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(
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)
}
}
2022-08-13 22:52:49 +05:30
/**
* Migration from old preference files to new one
*/
private fun prefFileMigration() {
val legacyUserPrefs = getSharedPreferences("username", Context.MODE_PRIVATE)
val username = legacyUserPrefs.getString("username", "")!!
if (username != "") {
PreferenceHelper.setUsername(username)
legacyUserPrefs.edit().putString("username", "")
}
val legacyTokenPrefs = getSharedPreferences("token", Context.MODE_PRIVATE)
val token = legacyUserPrefs.getString("token", "")!!
if (token != "") {
PreferenceHelper.setToken(token)
legacyTokenPrefs.edit().putString("token", "")
}
}
}