Be more aggressive in starting services

This commit is contained in:
Mygod
2021-10-23 17:24:01 -04:00
parent c1ae7857eb
commit cd6c72f559
2 changed files with 19 additions and 29 deletions

View File

@@ -48,7 +48,6 @@ class App : Application() {
// alternative to PreferenceManager.getDefaultSharedPreferencesName(this)
deviceStorage.moveSharedPreferencesFrom(this, PreferenceManager(this).sharedPreferencesName)
deviceStorage.moveDatabaseFrom(this, AppDatabase.DB_NAME)
BootReceiver.migrateIfNecessary(this, deviceStorage)
} else deviceStorage = this
Services.init { this }
@@ -93,6 +92,7 @@ class App : Application() {
})
EBegFragment.init()
if (DhcpWorkaround.shouldEnable) DhcpWorkaround.enable(true)
BootReceiver.init()
}
override fun onConfigurationChanged(newConfig: Configuration) {

View File

@@ -5,8 +5,8 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Parcelable
import androidx.annotation.RequiresApi
import be.mygod.librootkotlinx.toByteArray
import be.mygod.librootkotlinx.toParcelable
import be.mygod.vpnhotspot.App.Companion.app
@@ -40,8 +40,6 @@ class BootReceiver : BroadcastReceiver() {
enabled = isNotEmpty && app.pref.getBoolean(KEY, false)
}
private var started = false
private const val FILENAME = "bootconfig"
private val configFile by lazy { File(app.deviceStorage.noBackupFilesDir, FILENAME) }
private val config: Config? get() = try {
@@ -75,17 +73,26 @@ class BootReceiver : BroadcastReceiver() {
inline fun <reified T> add(value: Startable) = add(T::class.java.name, value)
inline fun <reified T> delete() = delete(T::class.java.name)
@RequiresApi(24)
fun migrateIfNecessary(old: Context, new: Context) {
val oldFile = File(old.noBackupFilesDir, FILENAME)
fun init() {
if (Build.VERSION.SDK_INT >= 24) {
val oldFile = File(app.noBackupFilesDir, FILENAME)
if (oldFile.canRead()) try {
val newFile = File(new.noBackupFilesDir, FILENAME)
if (!newFile.exists()) oldFile.copyTo(newFile)
if (!configFile.exists()) oldFile.copyTo(configFile)
if (!oldFile.delete()) oldFile.deleteOnExit()
} catch (e: Exception) {
Timber.w(e)
}
}
val config = try {
synchronized(BootReceiver) { config }
} catch (e: Exception) {
Timber.w(e)
null
}
if (config == null || config.startables.isEmpty()) {
enabled = false
} else for (startable in config.startables.values) startable.start(app)
}
}
interface Startable : Parcelable {
@@ -95,22 +102,5 @@ class BootReceiver : BroadcastReceiver() {
@Parcelize
private data class Config(var startables: MutableMap<String, Startable> = mutableMapOf()) : Parcelable
override fun onReceive(context: Context, intent: Intent) {
if (started) return
val isUpdate = when (intent.action) {
Intent.ACTION_BOOT_COMPLETED, Intent.ACTION_LOCKED_BOOT_COMPLETED -> false
Intent.ACTION_MY_PACKAGE_REPLACED -> true
else -> return
}
started = true
val config = try {
synchronized(BootReceiver) { config }
} catch (e: Exception) {
Timber.w(e)
if (isUpdate) null else return
}
if (config == null || config.startables.isEmpty()) {
enabled = false
} else for (startable in config.startables.values) startable.start(context)
}
override fun onReceive(context: Context, intent: Intent) { }
}