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-05-20 17:50:26 +05:30
|
|
|
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-07-17 20:49:55 +05:30
|
|
|
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
|
2022-07-28 18:15:29 +05:30
|
|
|
import com.github.libretube.util.RetrofitInstance
|
2021-12-14 21:45:53 +05:30
|
|
|
|
2022-06-05 15:12:33 +05:30
|
|
|
class MyApp : Application() {
|
2022-05-20 17:50:26 +05:30
|
|
|
override fun onCreate() {
|
|
|
|
super.onCreate()
|
|
|
|
|
2022-07-17 20:49:55 +05:30
|
|
|
/**
|
|
|
|
* initialize the needed [NotificationChannel]s for DownloadService and BackgroundMode
|
|
|
|
*/
|
2022-05-20 17:50:26 +05:30
|
|
|
initializeNotificationChannels()
|
2022-07-17 20:49:55 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2022-07-28 18:15:29 +05:30
|
|
|
/**
|
|
|
|
* set the api and the auth api url
|
|
|
|
*/
|
|
|
|
setRetrofitApiUrls()
|
|
|
|
|
2022-07-28 16:09:56 +05:30
|
|
|
/**
|
2022-07-28 16:57:52 +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
|
|
|
|
*/
|
|
|
|
val exceptionHandler = ExceptionHandler()
|
|
|
|
Thread.setDefaultUncaughtExceptionHandler(exceptionHandler)
|
2022-05-20 17:50:26 +05:30
|
|
|
}
|
|
|
|
|
2022-07-28 18:15:29 +05:30
|
|
|
/**
|
|
|
|
* set the api urls needed for the [RetrofitInstance]
|
|
|
|
*/
|
|
|
|
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-05-20 17:50:26 +05:30
|
|
|
/**
|
2022-06-06 20:12:46 +05:30
|
|
|
* Initializes the required [NotificationChannel]s for the app.
|
2022-05-20 17:50:26 +05:30
|
|
|
*/
|
|
|
|
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-05-20 17:50:26 +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
|
2022-05-20 17:50:26 +05:30
|
|
|
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
2022-06-06 20:12:46 +05:30
|
|
|
notificationManager.createNotificationChannel(channel)
|
2022-05-20 17:50:26 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|