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

View File

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