Add back toggle for auto install post app download

This commit is contained in:
Rahul Patel
2023-10-14 06:47:20 +05:30
parent 1c7483f27a
commit 24496b1405
5 changed files with 89 additions and 32 deletions

View File

@@ -45,7 +45,7 @@ import java.util.*
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.timerTask
class UpdateService: LifecycleService() {
class UpdateService : LifecycleService() {
lateinit var fetch: Fetch
lateinit var downloadManager: DownloadManager
@@ -53,7 +53,7 @@ class UpdateService: LifecycleService() {
private var fetchActiveDownloadObserver = object : FetchObserver<Boolean> {
override fun onChanged(data: Boolean, reason: Reason) {
if (!data && isEmptyInstalling() && fetchListeners.isEmpty() && appMetadataListeners.isEmpty()) {
Handler(Looper.getMainLooper()).postDelayed ({
Handler(Looper.getMainLooper()).postDelayed({
if (isEmptyInstalling() && fetchListeners.isEmpty() && appMetadataListeners.isEmpty()) {
stopSelf()
}
@@ -74,7 +74,7 @@ class UpdateService: LifecycleService() {
var liveUpdateData: MutableLiveData<MutableMap<Int, UpdateFile>> = MutableLiveData()
inner class UpdateServiceBinder : Binder() {
fun getUpdateService() : UpdateService {
fun getUpdateService(): UpdateService {
return this@UpdateService
}
}
@@ -181,9 +181,11 @@ class UpdateService: LifecycleService() {
private lateinit var authData: AuthData
data class AppDownloadStatus(val download: Download, val fetchGroup: FetchGroup,
val isCancelled: Boolean = false,
val isComplete: Boolean = false)
data class AppDownloadStatus(
val download: Download, val fetchGroup: FetchGroup,
val isCancelled: Boolean = false,
val isComplete: Boolean = false
)
data class AppMetadataStatus(val reason: String, val app: App)
@@ -252,17 +254,32 @@ class UpdateService: LifecycleService() {
fetchGroup: FetchGroup
) {
fetchListeners.forEach {
it.onProgress(groupId, download, etaInMilliSeconds, downloadedBytesPerSecond, fetchGroup)
it.onProgress(
groupId,
download,
etaInMilliSeconds,
downloadedBytesPerSecond,
fetchGroup
)
}
}
override fun onProgress(download: Download, etaInMilliSeconds: Long, downloadedBytesPerSecond: Long) {
override fun onProgress(
download: Download,
etaInMilliSeconds: Long,
downloadedBytesPerSecond: Long
) {
fetchListeners.forEach {
it.onProgress(download, etaInMilliSeconds, downloadedBytesPerSecond)
}
}
override fun onQueued(groupId: Int, download: Download, waitingNetwork: Boolean, fetchGroup: FetchGroup) {
override fun onQueued(
groupId: Int,
download: Download,
waitingNetwork: Boolean,
fetchGroup: FetchGroup
) {
fetchListeners.forEach {
it.onQueued(groupId, download, waitingNetwork, fetchGroup)
}
@@ -311,20 +328,30 @@ class UpdateService: LifecycleService() {
download,
downloadBlocks,
totalBlocks,
fetchGroup)
fetchGroup
)
}
}
override fun onStarted(download: Download, downloadBlocks: List<DownloadBlock>, totalBlocks: Int) {
override fun onStarted(
download: Download,
downloadBlocks: List<DownloadBlock>,
totalBlocks: Int
) {
fetchListeners.forEach {
it.onStarted(
download,
downloadBlocks,
totalBlocks)
totalBlocks
)
}
}
override fun onWaitingNetwork(groupId: Int, download: Download, fetchGroup: FetchGroup) {
override fun onWaitingNetwork(
groupId: Int,
download: Download,
fetchGroup: FetchGroup
) {
fetchListeners.forEach {
it.onWaitingNetwork(groupId, download, fetchGroup)
}
@@ -341,7 +368,8 @@ class UpdateService: LifecycleService() {
it.onCompleted(groupId, download, fetchGroup)
}
if (fetchListeners.isEmpty()) {
fetchPendingEvents[groupId] = AppDownloadStatus(download, fetchGroup, isComplete = true)
fetchPendingEvents[groupId] =
AppDownloadStatus(download, fetchGroup, isComplete = true)
}
var packageDownloadFilesWhichCompleted = downloadsInCompletedGroup[download.tag!!]
if (packageDownloadFilesWhichCompleted == null) {
@@ -349,7 +377,11 @@ class UpdateService: LifecycleService() {
downloadsInCompletedGroup[download.tag!!] = packageDownloadFilesWhichCompleted
}
packageDownloadFilesWhichCompleted.add(download.file)
if (fetchGroup.groupDownloadProgress == 100 && fetchGroup.downloads.all { packageDownloadFilesWhichCompleted.contains(it.file) }) {
if (fetchGroup.groupDownloadProgress == 100 && fetchGroup.downloads.all {
packageDownloadFilesWhichCompleted.contains(
it.file
)
}) {
downloadsInCompletedGroup.remove(download.tag!!)
if (download.tag != null) {
removeFromInstalling(download.tag, runFromCurrentThread = true)
@@ -381,7 +413,8 @@ class UpdateService: LifecycleService() {
it.onCancelled(groupId, download, fetchGroup)
}
if (fetchListeners.isEmpty()) {
fetchPendingEvents[groupId] = AppDownloadStatus(download, fetchGroup, isCancelled = true)
fetchPendingEvents[groupId] =
AppDownloadStatus(download, fetchGroup, isCancelled = true)
}
}
@@ -396,7 +429,8 @@ class UpdateService: LifecycleService() {
it.onDeleted(groupId, download, fetchGroup)
}
if (fetchListeners.isEmpty()) {
fetchPendingEvents[groupId] = AppDownloadStatus(download, fetchGroup, isCancelled = true)
fetchPendingEvents[groupId] =
AppDownloadStatus(download, fetchGroup, isCancelled = true)
}
}
@@ -424,7 +458,11 @@ class UpdateService: LifecycleService() {
}
}
override fun onDownloadBlockUpdated(download: Download, downloadBlock: DownloadBlock, totalBlocks: Int) {
override fun onDownloadBlockUpdated(
download: Download,
downloadBlock: DownloadBlock,
totalBlocks: Int
) {
fetchListeners.forEach {
it.onDownloadBlockUpdated(
download,
@@ -612,11 +650,19 @@ class UpdateService: LifecycleService() {
if (downloads.all { File(it.file).exists() }) {
serviceScope.launch {
try {
AppInstaller.getInstance(this@UpdateService).getPreferredInstaller()
.install(
packageName,
files.filter { it.file.endsWith(".apk") }.map { it.file }.toList()
)
val autoInstall = Preferences.getBoolean(
this@UpdateService,
Preferences.PREFERENCE_AUTO_INSTALL
)
if (autoInstall) {
AppInstaller.getInstance(this@UpdateService).getPreferredInstaller()
.install(
packageName,
files.filter { it.file.endsWith(".apk") }.map { it.file }
.toList()
)
}
} catch (exception: Exception) {
removeFromInstalling(packageName)
Log.e(exception.stackTraceToString())
@@ -645,16 +691,16 @@ class UpdateService: LifecycleService() {
removeFromInstalling(this)
}
synchronized(timerLock) {
if (timer != null) {
timer = if (timer != null) {
timer!!.cancel()
timer = null
}
if (timer == null) {
timer = Timer()
null
} else {
Timer()
}
timer!!.schedule(timerTask { timerTaskRun.run() }, 5 * 1000)
}
}
else -> {
}

View File

@@ -179,7 +179,7 @@ class AppDetailsFragment : BaseFragment(R.layout.fragment_details) {
private var isInstalled: Boolean = false
private var isUpdatable: Boolean = false
private var autoDownload: Boolean = false
private var downloadOnly: Boolean = false
private var autoInstall: Boolean = true
private var uninstallActionEnabled = false
val listOfActionsWhenServiceAttaches = ArrayList<Runnable>()
@@ -441,7 +441,9 @@ class AppDetailsFragment : BaseFragment(R.layout.fragment_details) {
}
private fun verifyAndInstall(files: List<Download>) {
if (downloadOnly)
autoInstall = Preferences.getBoolean(requireContext(), Preferences.PREFERENCE_AUTO_INSTALL)
if (!autoInstall)
return
var filesExist = true

View File

@@ -33,6 +33,7 @@ import com.aurora.store.data.work.UpdateWorker
import com.aurora.store.databinding.FragmentOnboardingBinding
import com.aurora.store.util.Preferences
import com.aurora.store.util.Preferences.PREFERENCE_AUTO_DELETE
import com.aurora.store.util.Preferences.PREFERENCE_AUTO_INSTALL
import com.aurora.store.util.Preferences.PREFERENCE_DEFAULT
import com.aurora.store.util.Preferences.PREFERENCE_DEFAULT_SELECTED_TAB
import com.aurora.store.util.Preferences.PREFERENCE_DOWNLOAD_ACTIVE
@@ -167,6 +168,7 @@ class OnboardingFragment : Fragment(R.layout.fragment_onboarding) {
/*Installer*/
save(PREFERENCE_AUTO_DELETE, true)
save(PREFERENCE_AUTO_INSTALL, true)
save(PREFERENCE_INSTALLER_ID, 0)
/*Updates*/

View File

@@ -14,6 +14,7 @@ import com.aurora.store.data.model.ExodusReport
import com.aurora.store.data.model.Report
import com.aurora.store.data.network.HttpClient
import com.aurora.store.data.providers.AuthProvider
import com.aurora.store.util.Preferences
import com.google.gson.GsonBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
@@ -131,6 +132,12 @@ class AppDetailsViewModel : ViewModel() {
@Synchronized
fun install(context: Context, packageName: String, files: List<Any>) {
val autoInstall = Preferences.getBoolean(context, Preferences.PREFERENCE_AUTO_INSTALL)
if (!autoInstall) {
return
}
try {
viewModelScope.launch(Dispatchers.IO) {
AppInstaller.getInstance(context).getPreferredInstaller()