Skip to content

Commit

Permalink
Show a dialog if the storage is low
Browse files Browse the repository at this point in the history
Closes #667
This denies installation, so no crashes
  • Loading branch information
Iamlooker committed Dec 25, 2024
1 parent ef13dc9 commit bbdd644
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
9 changes: 9 additions & 0 deletions app/src/main/kotlin/com/looker/droidify/ui/MessageDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ class MessageDialog() : DialogFragment() {
dialog.setMessage(stringRes.incompatible_signature_DESC)
dialog.setPositiveButton(stringRes.ok, null)
}

is Message.InsufficientStorage -> {
dialog.setTitle(stringRes.insufficient_storage)
dialog.setMessage(stringRes.insufficient_storage_DESC)
dialog.setPositiveButton(stringRes.ok, null)
}
}::class
return dialog.create()
}
Expand Down Expand Up @@ -220,6 +226,9 @@ sealed interface Message : Parcelable {

@Parcelize
data object ReleaseSignatureMismatch : Message

@Parcelize
data object InsufficientStorage : Message
}

class ReleaseIncompatibilityParceler : Parceler<Release.Incompatibility> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@ import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import coil.load
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.looker.core.common.cache.Cache
import com.looker.core.common.extension.getLauncherActivities
import com.looker.core.common.extension.getMutatedIcon
import com.looker.core.common.extension.isFirstItemVisible
import com.looker.core.common.extension.isSystemApplication
import com.looker.core.common.extension.systemBarsPadding
import com.looker.core.common.extension.updateAsMutable
import com.looker.droidify.content.ProductPreferences
import com.looker.droidify.model.InstalledItem
import com.looker.droidify.model.Product
import com.looker.droidify.model.ProductPreference
import com.looker.droidify.model.Release
import com.looker.droidify.model.Repository
import com.looker.droidify.model.findSuggested
import com.looker.droidify.content.ProductPreferences
import com.looker.droidify.service.Connection
import com.looker.droidify.service.DownloadService
import com.looker.droidify.ui.Message
Expand Down Expand Up @@ -341,12 +342,17 @@ class AppDetailFragment() : ScreenFragment(), AppDetailAdapter.Callbacks {
override fun onActionClick(action: AppDetailAdapter.Action) {
when (action) {
AppDetailAdapter.Action.INSTALL,
AppDetailAdapter.Action.UPDATE
-> downloadConnection.startUpdate(
viewModel.packageName,
installed?.installedItem,
products
)
AppDetailAdapter.Action.UPDATE -> {
if (Cache.getEmptySpace(requireContext()) < products.first().first.releases.first().size) {
MessageDialog(Message.InsufficientStorage).show(childFragmentManager)
return
}
downloadConnection.startUpdate(
viewModel.packageName,
installed?.installedItem,
products
)
}

AppDetailAdapter.Action.LAUNCH -> {
val launcherActivities = installed?.launcherActivities.orEmpty()
Expand Down Expand Up @@ -461,6 +467,10 @@ class AppDetailFragment() : ScreenFragment(), AppDetailAdapter.Callbacks {
).show(childFragmentManager)
}

Cache.getEmptySpace(requireContext()) < release.size -> {
MessageDialog(Message.InsufficientStorage).show(childFragmentManager)
}

installedItem != null && installedItem.versionCode > release.versionCode -> {
MessageDialog(Message.ReleaseOlder).show(childFragmentManager)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import android.database.MatrixCursor
import android.net.Uri
import android.os.Build
import android.os.ParcelFileDescriptor
import android.os.storage.StorageManager
import android.provider.OpenableColumns
import android.system.Os
import com.looker.core.common.SdkCheck
import com.looker.core.common.sdkAbove
import java.io.File
import java.util.UUID
import kotlin.concurrent.thread
import kotlin.math.min
import kotlin.time.Duration
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours
Expand Down Expand Up @@ -50,6 +52,11 @@ object Cache {
return filePath.substring(dirPath.length)
}

fun getEmptySpace(context: Context): Long {
val dir = context.cacheDir
return min(dir.usableSpace, dir.freeSpace)
}

fun getImagesDir(context: Context): File {
return ensureCacheDir(context, IMAGES_DIR)
}
Expand Down
2 changes: 2 additions & 0 deletions core/common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
<string name="install">Install</string>
<string name="install_types">Installation Types</string>
<string name="installer">Installer</string>
<string name="insufficient_storage">Insufficient Space</string>
<string name="insufficient_storage_DESC">There is not enough free space on the device to install this application. Try clearing some space</string>
<string name="legacy_installer">Legacy Installer</string>
<string name="session_installer">Session Installer</string>
<string name="root_installer">Root Installer</string>
Expand Down

0 comments on commit bbdd644

Please sign in to comment.