@ -5,44 +5,101 @@ package org.yuzu.yuzu_emu.utils
import android.app.ActivityManager
import android.content.Context
import android.os.Build
import org.yuzu.yuzu_emu.R
import org.yuzu.yuzu_emu.YuzuApplication
import java.util.Locale
import kotlin.math.ceil
object MemoryUtil {
private val context get ( ) = YuzuApplication . appContext
private val Long. floatForm : String
get ( ) = String . format ( Locale . ROOT , " %.2f " , this .toDouble ( ) )
private val Float. hundredths : String
get ( ) = String . format ( Locale . ROOT , " %.2f " , this )
const val Kb : Long = 1024
// Required total system memory
const val REQUIRED _MEMORY = 8
const val Kb : Float = 1024F
const val Mb = Kb * 1024
const val Gb = Mb * 1024
const val Tb = Gb * 1024
const val Pb = Tb * 1024
const val Eb = Pb * 1024
private fun bytesToSizeUnit ( size : Long ) : String {
return when {
size < Kb -> " ${size.floatForm} ${context.getString(R.string.memory_byte)} "
size < Mb -> " ${(size / Kb).floatForm} ${context.getString(R.string.memory_kilobyte)} "
size < Gb -> " ${(size / Mb).floatForm} ${context.getString(R.string.memory_megabyte)} "
size < Tb -> " ${(size / Gb).floatForm} ${context.getString(R.string.memory_gigabyte)} "
size < Pb -> " ${(size / Tb).floatForm} ${context.getString(R.string.memory_terabyte)} "
size < Eb -> " ${(size / Pb).floatForm} ${context.getString(R.string.memory_petabyte)} "
else -> " ${(size / Eb).floatForm} ${context.getString(R.string.memory_exabyte)} "
private fun bytesToSizeUnit ( size : Float ) : String =
when {
size < Kb -> {
context . getString (
R . string . memory _formatted ,
size . hundredths ,
context . getString ( R . string . memory _byte )
)
}
size < Mb -> {
context . getString (
R . string . memory _formatted ,
( size / Kb ) . hundredths ,
context . getString ( R . string . memory _kilobyte )
)
}
size < Gb -> {
context . getString (
R . string . memory _formatted ,
( size / Mb ) . hundredths ,
context . getString ( R . string . memory _megabyte )
)
}
size < Tb -> {
context . getString (
R . string . memory _formatted ,
( size / Gb ) . hundredths ,
context . getString ( R . string . memory _gigabyte )
)
}
size < Pb -> {
context . getString (
R . string . memory _formatted ,
( size / Tb ) . hundredths ,
context . getString ( R . string . memory _terabyte )
)
}
size < Eb -> {
context . getString (
R . string . memory _formatted ,
( size / Pb ) . hundredths ,
context . getString ( R . string . memory _petabyte )
)
}
else -> {
context . getString (
R . string . memory _formatted ,
( size / Eb ) . hundredths ,
context . getString ( R . string . memory _exabyte )
)
}
}
private val totalMemory =
with ( context . getSystemService ( Context . ACTIVITY _SERVICE ) as ActivityManager ) {
// Devices are unlikely to have 0.5GB increments of memory so we'll just round up to account for
// the potential error created by memInfo.totalMem
private val totalMemory : Float
get ( ) {
val memInfo = ActivityManager . MemoryInfo ( )
with ( context . getSystemService ( Context . ACTIVITY _SERVICE ) as ActivityManager ) {
getMemoryInfo ( memInfo )
memInfo . totalMem
}
fun isLessThan ( minimum : Int , size : Long ) : Boolean {
return when ( size ) {
return ceil (
if ( Build . VERSION . SDK _INT >= Build . VERSION_CODES . UPSIDE _DOWN _CAKE ) {
memInfo . advertisedMem . toFloat ( )
} else {
memInfo . totalMem . toFloat ( )
}
)
}
fun isLessThan ( minimum : Int , size : Float ) : Boolean =
when ( size ) {
Kb -> totalMemory < Mb && totalMemory < minimum
Mb -> totalMemory < Gb && ( totalMemory / Mb ) < minimum
Gb -> totalMemory < Tb && ( totalMemory / Gb ) < minimum
@ -51,9 +108,6 @@ object MemoryUtil {
Eb -> totalMemory / Eb < minimum
else -> totalMemory < Kb && totalMemory < minimum
}
}
fun getDeviceRAM ( ) : String {
return bytesToSizeUnit ( totalMemory )
}
fun getDeviceRAM ( ) : String = bytesToSizeUnit ( totalMemory )
}