UpdateWorker: Only auto-update apps when possible
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
@@ -63,6 +63,19 @@ class AppInstaller @Inject constructor(
|
||||
const val EXTRA_VERSION_CODE = "com.aurora.store.data.installer.AppInstaller.EXTRA_VERSION_CODE"
|
||||
const val EXTRA_DISPLAY_NAME = "com.aurora.store.data.installer.AppInstaller.EXTRA_DISPLAY_NAME"
|
||||
|
||||
enum class Installer {
|
||||
SESSION,
|
||||
NATIVE,
|
||||
ROOT,
|
||||
SERVICE,
|
||||
AM,
|
||||
SHIZUKU
|
||||
}
|
||||
|
||||
fun getCurrentInstaller(context: Context): Installer {
|
||||
return Installer.entries[Preferences.getInteger(context, PREFERENCE_INSTALLER_ID)]
|
||||
}
|
||||
|
||||
fun getAvailableInstallersInfo(context: Context): List<InstallerInfo> {
|
||||
val installers = mutableListOf(
|
||||
SessionInstaller.getInstallerInfo(context),
|
||||
@@ -162,20 +175,19 @@ class AppInstaller @Inject constructor(
|
||||
get() = sessionInstaller
|
||||
|
||||
fun getPreferredInstaller(): IInstaller {
|
||||
return when (Preferences.getInteger(context, PREFERENCE_INSTALLER_ID)) {
|
||||
0 -> sessionInstaller
|
||||
1 -> nativeInstaller
|
||||
2 -> if (hasRootAccess()) rootInstaller else defaultInstaller
|
||||
3 -> if (hasAuroraService(context)) serviceInstaller else defaultInstaller
|
||||
4 -> if (hasAppManager(context)) amInstaller else defaultInstaller
|
||||
5 -> {
|
||||
return when (getCurrentInstaller(context)) {
|
||||
Installer.SESSION -> sessionInstaller
|
||||
Installer.NATIVE -> nativeInstaller
|
||||
Installer.ROOT -> if (hasRootAccess()) rootInstaller else defaultInstaller
|
||||
Installer.SERVICE -> if (hasAuroraService(context)) serviceInstaller else defaultInstaller
|
||||
Installer.AM -> if (hasAppManager(context)) amInstaller else defaultInstaller
|
||||
Installer.SHIZUKU -> {
|
||||
if (isOAndAbove() && hasShizukuOrSui(context) && hasShizukuPerm()) {
|
||||
shizukuInstaller
|
||||
} else {
|
||||
defaultInstaller
|
||||
}
|
||||
}
|
||||
else -> defaultInstaller
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,8 +14,13 @@ import androidx.work.WorkManager
|
||||
import androidx.work.WorkerParameters
|
||||
import com.aurora.extensions.isIgnoringBatteryOptimizations
|
||||
import com.aurora.extensions.isMAndAbove
|
||||
import com.aurora.extensions.isOAndAbove
|
||||
import com.aurora.extensions.isSAndAbove
|
||||
import com.aurora.store.data.installer.AppInstaller
|
||||
import com.aurora.store.data.installer.AppInstaller.Companion.Installer
|
||||
import com.aurora.store.data.providers.AuthProvider
|
||||
import com.aurora.store.util.AppUtil
|
||||
import com.aurora.store.util.CertUtil
|
||||
import com.aurora.store.util.DownloadWorkerUtil
|
||||
import com.aurora.store.util.NotificationUtil
|
||||
import com.aurora.store.util.Preferences
|
||||
@@ -120,8 +125,24 @@ class UpdateWorker @AssistedInject constructor(
|
||||
)
|
||||
} else {
|
||||
if (appContext.isIgnoringBatteryOptimizations()) {
|
||||
Log.i(TAG, "Found updates, updating!")
|
||||
updatesList.forEach { downloadWorkerUtil.enqueueApp(it) }
|
||||
// Trigger download for apps if they can be auto-updated (if any)
|
||||
updatesList.filter { canAutoUpdate(it.packageName) }.let { list ->
|
||||
if (list.isEmpty()) return@let
|
||||
|
||||
Log.i(TAG, "Found auto-update enabled apps, updating!")
|
||||
list.forEach { downloadWorkerUtil.enqueueApp(it) }
|
||||
}
|
||||
|
||||
// Notify about remaining apps (if any)
|
||||
updatesList.filterNot { canAutoUpdate(it.packageName) }.let { list ->
|
||||
if (list.isEmpty()) return@let
|
||||
|
||||
Log.i(TAG, "Found apps that cannot be auto-updated, notifying!")
|
||||
notifyManager.notify(
|
||||
notificationID,
|
||||
NotificationUtil.getUpdateNotification(appContext, list)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// Fallback to notification if battery optimizations are enabled
|
||||
Log.i(TAG, "Found updates, but battery optimizations are enabled!")
|
||||
@@ -143,4 +164,16 @@ class UpdateWorker @AssistedInject constructor(
|
||||
}
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
private fun canAutoUpdate(packageName: String): Boolean {
|
||||
return when (AppInstaller.getCurrentInstaller(appContext)) {
|
||||
Installer.SESSION -> isSAndAbove() && CertUtil.isAuroraStoreApp(appContext, packageName)
|
||||
Installer.NATIVE -> false
|
||||
Installer.ROOT -> AppInstaller.hasRootAccess()
|
||||
Installer.SERVICE -> AppInstaller.hasAuroraService(appContext)
|
||||
Installer.AM -> false // We cannot check if AppManager has ability to auto-update
|
||||
Installer.SHIZUKU -> isOAndAbove() && AppInstaller.hasShizukuOrSui(appContext) &&
|
||||
AppInstaller.hasShizukuPerm()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import android.content.pm.PackageInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.util.Base64
|
||||
import android.util.Log
|
||||
import com.aurora.Constants
|
||||
import com.aurora.extensions.generateX509Certificate
|
||||
import com.aurora.extensions.getInstallerPackageNameCompat
|
||||
import com.aurora.extensions.isPAndAbove
|
||||
@@ -35,6 +36,10 @@ object CertUtil {
|
||||
|
||||
private val TAG = "CertUtil"
|
||||
|
||||
fun isAuroraStoreApp(context: Context, packageName: String): Boolean {
|
||||
return context.packageManager.getInstallerPackageNameCompat(packageName) == context.packageName
|
||||
}
|
||||
|
||||
fun isFDroidApp(context: Context, packageName: String): Boolean {
|
||||
return isInstalledByFDroid(context, packageName) || isSignedByFDroid(context, packageName)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user