PathUtil: Switch to File instead of Path class

Path is only available since Android 8.0+ and it makes no sense to
add a dependency upon desugaring lib for it.

Switch to File class instead which is available on all Android versions

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2023-12-18 22:20:34 +05:30
parent cd8c4c1364
commit 691a3c61a4
8 changed files with 19 additions and 40 deletions

View File

@@ -7,7 +7,6 @@ import com.aurora.Constants
import com.aurora.store.data.installer.AppInstaller import com.aurora.store.data.installer.AppInstaller
import com.aurora.store.util.PathUtil import com.aurora.store.util.PathUtil
import java.io.File import java.io.File
import kotlin.io.path.pathString
class InstallActivity : AppCompatActivity() { class InstallActivity : AppCompatActivity() {
@@ -22,8 +21,7 @@ class InstallActivity : AppCompatActivity() {
val version = intent.extras?.getInt(Constants.STRING_VERSION) val version = intent.extras?.getInt(Constants.STRING_VERSION)
if (packageName.isNotBlank() && version != null) { if (packageName.isNotBlank() && version != null) {
try { try {
val downloadDir = val downloadDir = File(PathUtil.getAppDownloadDir(this, packageName, version).path)
File(PathUtil.getAppDownloadDir(this, packageName, version).pathString)
AppInstaller.getInstance(this).getPreferredInstaller() AppInstaller.getInstance(this).getPreferredInstaller()
.install( .install(
packageName, packageName,

View File

@@ -37,7 +37,6 @@ import com.aurora.store.util.PathUtil
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import java.io.File import java.io.File
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
import kotlin.io.path.pathString
@AndroidEntryPoint @AndroidEntryPoint
class InstallReceiver : BroadcastReceiver() { class InstallReceiver : BroadcastReceiver() {
@@ -58,13 +57,7 @@ class InstallReceiver : BroadcastReceiver() {
if (packageName.isNotBlank() && version != null) { if (packageName.isNotBlank() && version != null) {
try { try {
val downloadDir = val downloadDir =
File( File(PathUtil.getAppDownloadDir(context, packageName, version).path)
PathUtil.getAppDownloadDir(
context,
packageName,
version
).pathString
)
AppInstaller.getInstance(context).getPreferredInstaller() AppInstaller.getInstance(context).getPreferredInstaller()
.install( .install(
packageName, packageName,

View File

@@ -36,8 +36,6 @@ import java.io.File
import java.net.URL import java.net.URL
import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.NonCancellable
import kotlin.io.path.ExperimentalPathApi import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.createDirectories
import kotlin.io.path.deleteRecursively
import kotlin.properties.Delegates import kotlin.properties.Delegates
import com.aurora.gplayapi.data.models.File as GPlayFile import com.aurora.gplayapi.data.models.File as GPlayFile
@@ -94,10 +92,9 @@ class DownloadWorker @AssistedInject constructor(
// Download and verify all files exists // Download and verify all files exists
totalBytes = files.sumOf { it.size } totalBytes = files.sumOf { it.size }
PathUtil.getAppDownloadDir(appContext, download.packageName, download.versionCode) PathUtil.getAppDownloadDir(appContext, download.packageName, download.versionCode).mkdirs()
.createDirectories()
if (files.any { it.type == GPlayFile.FileType.OBB || it.type == GPlayFile.FileType.PATCH }) { if (files.any { it.type == GPlayFile.FileType.OBB || it.type == GPlayFile.FileType.PATCH }) {
PathUtil.getObbDownloadDir(download.packageName).createDirectories() PathUtil.getObbDownloadDir(download.packageName).mkdirs()
} }
val requestList = getDownloadRequest(files) val requestList = getDownloadRequest(files)

View File

@@ -23,8 +23,6 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.deleteRecursively
@OptIn(DelicateCoroutinesApi::class) @OptIn(DelicateCoroutinesApi::class)
class DownloadWorkerUtil @Inject constructor( class DownloadWorkerUtil @Inject constructor(
@@ -87,7 +85,6 @@ class DownloadWorkerUtil @Inject constructor(
?.let { downloadDao.update(it.copy(status = DownloadStatus.CANCELLED)) } ?.let { downloadDao.update(it.copy(status = DownloadStatus.CANCELLED)) }
} }
@OptIn(ExperimentalPathApi::class)
suspend fun clearDownload(packageName: String, versionCode: Int) { suspend fun clearDownload(packageName: String, versionCode: Int) {
Log.i(TAG, "Clearing downloads for $packageName ($versionCode)") Log.i(TAG, "Clearing downloads for $packageName ($versionCode)")
downloadDao.delete(packageName) downloadDao.delete(packageName)

View File

@@ -22,11 +22,9 @@ package com.aurora.store.util
import android.content.Context import android.content.Context
import android.os.Environment import android.os.Environment
import com.aurora.extensions.isRAndAbove import com.aurora.extensions.isRAndAbove
import com.aurora.gplayapi.data.models.App import java.io.File
import com.aurora.gplayapi.data.models.File
import java.nio.file.Path
import java.util.UUID import java.util.UUID
import kotlin.io.path.Path import com.aurora.gplayapi.data.models.File as GPlayFile
fun Context.getInternalBaseDirectory(): String { fun Context.getInternalBaseDirectory(): String {
return (getExternalFilesDir(null) ?: filesDir).path return (getExternalFilesDir(null) ?: filesDir).path
@@ -54,15 +52,15 @@ object PathUtil {
return getPackageDirectory(context, packageName) + "/$versionCode" return getPackageDirectory(context, packageName) + "/$versionCode"
} }
fun getAppDownloadDir(context: Context, packageName: String, versionCode: Int): Path { fun getAppDownloadDir(context: Context, packageName: String, versionCode: Int): File {
return Path(getPackageDirectory(context, packageName), versionCode.toString()) return File(getPackageDirectory(context, packageName), versionCode.toString())
} }
fun getApkDownloadFile( fun getApkDownloadFile(
context: Context, context: Context,
packageName: String, packageName: String,
versionCode: Int, versionCode: Int,
file: File file: GPlayFile
): String { ): String {
return getVersionDirectory(context, packageName, versionCode) + "/${file.name}" return getVersionDirectory(context, packageName, versionCode) + "/${file.name}"
} }
@@ -94,21 +92,20 @@ object PathUtil {
.toString() + "/Android/obb/" + packageName .toString() + "/Android/obb/" + packageName
} }
fun getObbDownloadDir(packageName: String): Path { fun getObbDownloadDir(packageName: String): File {
return Path( return File(
Environment.getExternalStorageDirectory().absolutePath, Environment.getExternalStorageDirectory().absolutePath,
"/Android/obb/", "/Android/obb/$packageName"
packageName
) )
} }
fun getObbDownloadFile(packageName: String, file: File): String { fun getObbDownloadFile(packageName: String, file: GPlayFile): String {
val obbDir = getObbDownloadPath(packageName) val obbDir = getObbDownloadPath(packageName)
return "$obbDir/${file.name}" return "$obbDir/${file.name}"
} }
fun needsStorageManagerPerm(fileList: List<File>): Boolean { fun needsStorageManagerPerm(fileList: List<GPlayFile>): Boolean {
return fileList.any { it.type == File.FileType.OBB || it.type == File.FileType.PATCH } return fileList.any { it.type == GPlayFile.FileType.OBB || it.type == GPlayFile.FileType.PATCH }
} }
fun getSpoofDirectory(context: Context): String { fun getSpoofDirectory(context: Context): String {

View File

@@ -47,7 +47,6 @@ import com.aurora.store.util.CommonUtil
import com.aurora.store.util.PathUtil import com.aurora.store.util.PathUtil
import com.aurora.store.view.epoxy.views.BaseView import com.aurora.store.view.epoxy.views.BaseView
import java.io.File import java.io.File
import kotlin.io.path.pathString
@ModelView( @ModelView(
autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT, autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT,
@@ -146,7 +145,7 @@ class AppUpdateView : RelativeLayout {
context, context,
download.packageName, download.packageName,
download.versionCode download.versionCode
).pathString ).path
).listFiles() ).listFiles()
if (files.isNullOrEmpty()) B.btnAction.updateState(DownloadStatus.UNAVAILABLE) if (files.isNullOrEmpty()) B.btnAction.updateState(DownloadStatus.UNAVAILABLE)
} }

View File

@@ -100,7 +100,6 @@ import java.io.File
import java.util.Locale import java.util.Locale
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.filter
import kotlin.io.path.pathString
@AndroidEntryPoint @AndroidEntryPoint
class AppDetailsFragment : BaseFragment(R.layout.fragment_details) { class AppDetailsFragment : BaseFragment(R.layout.fragment_details) {
@@ -445,7 +444,7 @@ class AppDetailsFragment : BaseFragment(R.layout.fragment_details) {
requireContext(), requireContext(),
app.packageName, app.packageName,
app.versionCode app.versionCode
).pathString ).path
).listFiles() ?: return ).listFiles() ?: return
val apkFiles = files.filter { it.path.endsWith(".apk") } val apkFiles = files.filter { it.path.endsWith(".apk") }
@@ -574,7 +573,7 @@ class AppDetailsFragment : BaseFragment(R.layout.fragment_details) {
requireContext(), requireContext(),
app.packageName, app.packageName,
app.versionCode app.versionCode
).pathString ).path
).listFiles() ).listFiles()
if (files?.isNotEmpty() == true) install() else purchase() if (files?.isNotEmpty() == true) install() else purchase()

View File

@@ -49,7 +49,6 @@ import java.io.File
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlin.io.path.pathString
@AndroidEntryPoint @AndroidEntryPoint
class UpdatesFragment : BaseFragment(R.layout.fragment_updates) { class UpdatesFragment : BaseFragment(R.layout.fragment_updates) {
@@ -166,7 +165,7 @@ class UpdatesFragment : BaseFragment(R.layout.fragment_updates) {
requireContext(), requireContext(),
app.packageName, app.packageName,
app.versionCode app.versionCode
).pathString ).path
).listFiles() ).listFiles()
// Downloaded files are missing, trigger re-download // Downloaded files are missing, trigger re-download