From 43b8aacc6c1d584a308d264957f7acca5903429d Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Tue, 5 Apr 2022 17:43:40 +0600 Subject: [PATCH 1/2] App Lounge: storage and internet checking before install --- .../java/foundation/e/apps/MainActivity.kt | 69 +++++++++++++++++-- .../e/apps/MainActivityViewModel.kt | 10 ++- .../e/apps/api/fused/FusedAPIImpl.kt | 1 + .../e/apps/api/fused/data/FusedApp.kt | 1 + .../database/fusedDownload/FusedDownload.kt | 3 +- .../e/apps/updates/manager/UpdatesWorker.kt | 3 +- app/src/main/res/values/strings.xml | 1 + 7 files changed, 79 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/MainActivity.kt b/app/src/main/java/foundation/e/apps/MainActivity.kt index cc8e20ef7..6b2e2c4c1 100644 --- a/app/src/main/java/foundation/e/apps/MainActivity.kt +++ b/app/src/main/java/foundation/e/apps/MainActivity.kt @@ -18,8 +18,12 @@ package foundation.e.apps +import android.app.usage.StorageStatsManager import android.os.Build import android.os.Bundle +import android.os.Environment +import android.os.StatFs +import android.os.storage.StorageManager import android.util.Log import android.view.View import androidx.appcompat.app.AppCompatActivity @@ -34,6 +38,7 @@ import com.google.android.material.snackbar.Snackbar import dagger.hilt.android.AndroidEntryPoint import foundation.e.apps.application.subFrags.ApplicationDialogFragment import foundation.e.apps.databinding.ActivityMainBinding +import foundation.e.apps.manager.database.fusedDownload.FusedDownload import foundation.e.apps.manager.workmanager.InstallWorkManager import foundation.e.apps.purchase.AppPurchaseFragmentDirections import foundation.e.apps.setup.signin.SignInViewModel @@ -42,6 +47,8 @@ import foundation.e.apps.utils.enums.Status import foundation.e.apps.utils.enums.User import foundation.e.apps.utils.modules.CommonUtilsModule import kotlinx.coroutines.launch +import java.io.File +import java.util.* @AndroidEntryPoint class MainActivity : AppCompatActivity() { @@ -161,11 +168,7 @@ class MainActivity : AppCompatActivity() { viewModel.downloadList.observe(this) { list -> list.forEach { if (it.status == Status.QUEUED) { - lifecycleScope.launch { - viewModel.updateAwaiting(it) - InstallWorkManager.enqueueWork(applicationContext, it) - Log.d(TAG, "===> onCreate: AWAITING ${it.name}") - } + handleFusedDownloadQueued(it, viewModel) } } } @@ -213,6 +216,27 @@ class MainActivity : AppCompatActivity() { } } + private fun handleFusedDownloadQueued( + it: FusedDownload, + viewModel: MainActivityViewModel + ) { + lifecycleScope.launch { + if (!checkStorageAvailability(it)) { + showSnackbarMessage(getString(R.string.not_enough_storage)) + viewModel.updateUnAvailable(it) + return@launch + } + if (viewModel.internetConnection.value == false) { + showNoInternet() + viewModel.updateUnAvailable(it) + return@launch + } + viewModel.updateAwaiting(it) + InstallWorkManager.enqueueWork(applicationContext, it) + Log.d(TAG, "===> onCreate: AWAITING ${it.name}") + } + } + private fun startInstallationOfPurchasedApp( viewModel: MainActivityViewModel, it: String @@ -235,4 +259,39 @@ class MainActivity : AppCompatActivity() { binding.noInternet.visibility = View.VISIBLE binding.fragment.visibility = View.GONE } + + // TODO: move storage availability code to FileManager Class + private fun checkStorageAvailability(fusedDownload: FusedDownload): Boolean { + var availableSpace = 0L + availableSpace = calculateAvailableDiskSpace() + return availableSpace > fusedDownload.appSize + (500 * (1000 * 1000)) + } + + private fun calculateAvailableDiskSpace(): Long { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val storageManager = getSystemService(STORAGE_SERVICE) as StorageManager + val statsManager = getSystemService(STORAGE_STATS_SERVICE) as StorageStatsManager + val uuid = storageManager.primaryStorageVolume.uuid + try { + if (uuid != null) { + statsManager.getFreeBytes(UUID.fromString(uuid)) + } else { + statsManager.getFreeBytes(StorageManager.UUID_DEFAULT) + } + } catch (e: Exception) { + Log.e(TAG, "calculateAvailableDiskSpace: ${e.stackTraceToString()}") + getAvailableInternalMemorySize() + } + } else { + getAvailableInternalMemorySize() + } + } + + private fun getAvailableInternalMemorySize(): Long { + val path: File = Environment.getDataDirectory() + val stat = StatFs(path.path) + val blockSize = stat.blockSizeLong + val availableBlocks = stat.availableBlocksLong + return availableBlocks * blockSize + } } diff --git a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt index cb4561cc6..65fc1b491 100644 --- a/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt +++ b/app/src/main/java/foundation/e/apps/MainActivityViewModel.kt @@ -162,7 +162,8 @@ class MainActivityViewModel @Inject constructor( appIcon, app.latest_version_code, app.offer_type, - app.isFree + app.isFree, + app.originalSize ) } catch (e: Exception) { if (e is ApiException.AppNotPurchased) { @@ -198,7 +199,8 @@ class MainActivityViewModel @Inject constructor( appIcon, app.latest_version_code, app.offer_type, - app.isFree + app.isFree, + app.originalSize ) viewModelScope.launch { fusedManagerRepository.addFusedDownloadPurchaseNeeded(fusedDownload) @@ -210,6 +212,10 @@ class MainActivityViewModel @Inject constructor( fusedManagerRepository.updateAwaiting(fusedDownload) } + suspend fun updateUnAvailable(fusedDownload: FusedDownload) { + fusedManagerRepository.updateUnavailable(fusedDownload) + } + suspend fun updateAwaitingForPurchasedApp(packageName: String): FusedDownload? { val fusedDownload = fusedManagerRepository.getFusedDownload(packageName = packageName) authData.value?.let { diff --git a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt index 409c03fd4..17c77494a 100644 --- a/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt +++ b/app/src/main/java/foundation/e/apps/api/fused/FusedAPIImpl.kt @@ -639,6 +639,7 @@ class FusedAPIImpl @Inject constructor( offer_type = this.offerType, origin = Origin.GPLAY, shareUrl = this.shareUrl, + originalSize = this.size, appSize = Formatter.formatFileSize(context, this.size), isFree = this.isFree, price = this.price diff --git a/app/src/main/java/foundation/e/apps/api/fused/data/FusedApp.kt b/app/src/main/java/foundation/e/apps/api/fused/data/FusedApp.kt index c676eeed2..b654cb645 100644 --- a/app/src/main/java/foundation/e/apps/api/fused/data/FusedApp.kt +++ b/app/src/main/java/foundation/e/apps/api/fused/data/FusedApp.kt @@ -42,6 +42,7 @@ data class FusedApp( var status: Status = Status.UNAVAILABLE, var origin: Origin = Origin.CLEANAPK, val shareUrl: String = String(), + val originalSize: Long = 0, val appSize: String = String(), var source: String = String(), val price: String = String(), diff --git a/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownload.kt b/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownload.kt index 7b9c2be82..39a7ce363 100644 --- a/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownload.kt +++ b/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownload.kt @@ -20,5 +20,6 @@ data class FusedDownload( val iconByteArray: String = String(), val versionCode: Int = 1, val offerType: Int = -1, - val isFree: Boolean = true + val isFree: Boolean = true, + val appSize: Long = 0 ) diff --git a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt index 42cece105..084000ea6 100644 --- a/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt +++ b/app/src/main/java/foundation/e/apps/updates/manager/UpdatesWorker.kt @@ -135,7 +135,8 @@ class UpdatesWorker @AssistedInject constructor( iconBase64, fusedApp.latest_version_code, fusedApp.offer_type, - fusedApp.isFree + fusedApp.isFree, + fusedApp.originalSize ) fusedManagerRepository.addDownload(fusedDownload) fusedManagerRepository.updateAwaiting(fusedDownload) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f1a6ac7c4..2293cc8b9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -102,6 +102,7 @@ Paid apps are not supported in App Lounge yet. Just a little more patience, it will soon be the case. This app will be available later! Unknown Error! + There is not enough space available to download this application! Update All -- GitLab From a0590d44009f516bad7e568272feb76885f328c2 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Tue, 5 Apr 2022 22:23:09 +0600 Subject: [PATCH 2/2] App Lounge: refactored a function name in MainActivity --- app/src/main/java/foundation/e/apps/MainActivity.kt | 4 ++-- .../e/apps/manager/database/fusedDownload/FusedDownload.kt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/MainActivity.kt b/app/src/main/java/foundation/e/apps/MainActivity.kt index 7bf1cc855..9ff89f1e3 100644 --- a/app/src/main/java/foundation/e/apps/MainActivity.kt +++ b/app/src/main/java/foundation/e/apps/MainActivity.kt @@ -221,7 +221,7 @@ class MainActivity : AppCompatActivity() { viewModel: MainActivityViewModel ) { lifecycleScope.launch { - if (!checkStorageAvailability(it)) { + if (!isStorageAvailable(it)) { showSnackbarMessage(getString(R.string.not_enough_storage)) viewModel.updateUnAvailable(it) return@launch @@ -261,7 +261,7 @@ class MainActivity : AppCompatActivity() { } // TODO: move storage availability code to FileManager Class - private fun checkStorageAvailability(fusedDownload: FusedDownload): Boolean { + private fun isStorageAvailable(fusedDownload: FusedDownload): Boolean { var availableSpace = 0L availableSpace = calculateAvailableDiskSpace() return availableSpace > fusedDownload.appSize + (500 * (1000 * 1000)) diff --git a/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownload.kt b/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownload.kt index 88de39ea6..269298dd4 100644 --- a/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownload.kt +++ b/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownload.kt @@ -22,6 +22,6 @@ data class FusedDownload( val versionCode: Int = 1, val offerType: Int = -1, val isFree: Boolean = true, - val appSize: Long = 0 + val appSize: Long = 0, var files: List = mutableListOf() ) -- GitLab