DownloadMenuSheet: Add install action

This would be useful for self-updates when they get downloaded but user
isn't automatically installed.

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2024-01-16 18:41:19 +05:30
parent a3591406b2
commit 580717931d
2 changed files with 30 additions and 0 deletions

View File

@@ -20,6 +20,7 @@
package com.aurora.store.view.ui.sheets
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@@ -29,8 +30,11 @@ import androidx.navigation.fragment.navArgs
import com.aurora.extensions.copyToClipBoard
import com.aurora.extensions.toast
import com.aurora.store.R
import com.aurora.store.data.installer.AppInstaller
import com.aurora.store.data.model.DownloadStatus
import com.aurora.store.databinding.SheetDownloadMenuBinding
import com.aurora.store.util.DownloadWorkerUtil
import com.aurora.store.util.PathUtil
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.NonCancellable
@@ -39,6 +43,8 @@ import kotlinx.coroutines.launch
@AndroidEntryPoint
class DownloadMenuSheet : BaseBottomSheet() {
private val TAG = DownloadMenuSheet::class.java.simpleName
private var _binding: SheetDownloadMenuBinding? = null
private val binding get() = _binding!!
@@ -63,9 +69,12 @@ class DownloadMenuSheet : BaseBottomSheet() {
with(binding.navigationView) {
menu.findItem(R.id.action_cancel).isVisible = !args.download.isFinished
menu.findItem(R.id.action_clear).isVisible = args.download.isFinished
menu.findItem(R.id.action_install).isVisible =
args.download.downloadStatus == DownloadStatus.COMPLETED
setNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.action_install -> install()
R.id.action_copy -> {
requireContext().copyToClipBoard(
"${playStoreURL}${args.download.packageName}"
@@ -96,4 +105,22 @@ class DownloadMenuSheet : BaseBottomSheet() {
super.onDestroyView()
_binding = null
}
private fun install() {
try {
val downloadDir = PathUtil.getAppDownloadDir(
requireContext(),
args.download.packageName,
args.download.versionCode
)
AppInstaller.getInstance(requireContext())
.getPreferredInstaller()
.install(
args.download.packageName,
downloadDir.listFiles()!!.filter { it.path.endsWith(".apk") }
)
} catch (exception: Exception) {
Log.e(TAG, "Failed to install ${args.download.packageName}", exception)
}
}
}