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

202 lines
7.0 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-08-14 20:06:18 +05:30
import androidx.preference.PreferenceManager
2022-07-30 14:38:28 +05:30
import androidx.work.ExistingPeriodicWorkPolicy
2022-08-15 13:11:30 +05:30
import coil.ImageLoader
2022-08-14 20:06:18 +05:30
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
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-08-14 13:29:05 +05:30
import com.github.libretube.db.DatabaseHolder
2022-08-14 20:06:18 +05:30
import com.github.libretube.db.obj.WatchHistoryItem
import com.github.libretube.db.obj.WatchPosition
import com.github.libretube.preferences.PreferenceHelper
2022-07-28 18:15:29 +05:30
import com.github.libretube.preferences.PreferenceKeys
2022-08-15 13:11:30 +05:30
import com.github.libretube.util.ConnectionHelper
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-15 13:11:30 +05:30
* Initialize 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
/**
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-08-15 13:11:30 +05:30
initializeRetrofit()
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-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-08-14 20:06:18 +05:30
/**
* Database Migration
*/
databaseMigration()
}
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
*/
2022-08-15 13:11:30 +05:30
private fun initializeRetrofit() {
2022-07-28 18:15:29 +05:30
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-08-15 13:11:30 +05:30
CronetHelper.initCronet(this)
ConnectionHelper.imageLoader = ImageLoader.Builder(this)
.callFactory(CronetHelper.callFactory)
.build()
2022-07-28 18:15:29 +05:30
}
/**
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", "")
}
}
2022-08-14 20:06:18 +05:30
/**
* Migration from the preferences to the database
*/
private fun databaseMigration() {
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
val mapper = ObjectMapper()
Thread {
val legacyWatchHistory = prefs.getString("watch_history", "")
if (legacyWatchHistory != "") {
try {
val type = object : TypeReference<List<WatchHistoryItem>>() {}
val watchHistoryItems = mapper.readValue(legacyWatchHistory, type)
DatabaseHolder.db.watchHistoryDao().insertAll(
*watchHistoryItems.toTypedArray()
)
2022-08-14 21:05:17 +05:30
} catch (e: Exception) {
}
2022-08-14 20:06:18 +05:30
prefs.edit().putString("watch_history", "").commit()
}
val legacyWatchPositions = prefs.getString("watch_positions", "")
if (legacyWatchPositions != "") {
try {
val type = object : TypeReference<List<WatchPosition>>() {}
val watchPositions = mapper.readValue(legacyWatchPositions, type)
DatabaseHolder.db.watchPositionDao().insertAll(
*watchPositions.toTypedArray()
)
2022-08-14 21:05:17 +05:30
} catch (e: Exception) {
}
2022-08-14 20:06:18 +05:30
prefs.edit().remove("watch_positions").commit()
}
prefs.edit()
.remove("custom_instances")
.remove("local_subscriptions")
.commit()
}.start()
}
}