diff options
| author | 2023-06-28 16:51:27 -0400 | |
|---|---|---|
| committer | 2023-06-30 01:00:19 -0400 | |
| commit | 11991fbd7f8b97a13bb315e7c035b900052df204 (patch) | |
| tree | 39afd7cfdaab24b54f0e19e5f8dc13fd58687ca7 /src | |
| parent | android: Make MemoryUtil an object (diff) | |
| download | yuzu-11991fbd7f8b97a13bb315e7c035b900052df204.tar.gz yuzu-11991fbd7f8b97a13bb315e7c035b900052df204.tar.xz yuzu-11991fbd7f8b97a13bb315e7c035b900052df204.zip | |
android: Rework MemoryUtil
Uses string templates and rounds up memory amount for potentially inaccurate checks now
Diffstat (limited to 'src')
3 files changed, 85 insertions, 25 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt index 4052eead5..2c671fea3 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt | |||
| @@ -47,6 +47,7 @@ import org.yuzu.yuzu_emu.utils.InputHandler | |||
| 47 | import org.yuzu.yuzu_emu.utils.MemoryUtil | 47 | import org.yuzu.yuzu_emu.utils.MemoryUtil |
| 48 | import org.yuzu.yuzu_emu.utils.NfcReader | 48 | import org.yuzu.yuzu_emu.utils.NfcReader |
| 49 | import org.yuzu.yuzu_emu.utils.ThemeHelper | 49 | import org.yuzu.yuzu_emu.utils.ThemeHelper |
| 50 | import java.text.NumberFormat | ||
| 50 | import kotlin.math.roundToInt | 51 | import kotlin.math.roundToInt |
| 51 | 52 | ||
| 52 | class EmulationActivity : AppCompatActivity(), SensorEventListener { | 53 | class EmulationActivity : AppCompatActivity(), SensorEventListener { |
| @@ -106,13 +107,17 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener { | |||
| 106 | inputHandler = InputHandler() | 107 | inputHandler = InputHandler() |
| 107 | inputHandler.initialize() | 108 | inputHandler.initialize() |
| 108 | 109 | ||
| 109 | if (MemoryUtil.isLessThan(8, MemoryUtil.Gb)) { | 110 | if (MemoryUtil.isLessThan(MemoryUtil.REQUIRED_MEMORY, MemoryUtil.Gb)) { |
| 110 | Toast.makeText( | 111 | Toast.makeText( |
| 111 | this, | 112 | this, |
| 112 | getString( | 113 | getString( |
| 113 | R.string.device_memory_inadequate, | 114 | R.string.device_memory_inadequate, |
| 114 | MemoryUtil.getDeviceRAM(), | 115 | MemoryUtil.getDeviceRAM(), |
| 115 | "8 ${getString(R.string.memory_gigabyte)}" | 116 | getString( |
| 117 | R.string.memory_formatted, | ||
| 118 | NumberFormat.getInstance().format(MemoryUtil.REQUIRED_MEMORY), | ||
| 119 | getString(R.string.memory_gigabyte) | ||
| 120 | ) | ||
| 116 | ), | 121 | ), |
| 117 | Toast.LENGTH_LONG | 122 | Toast.LENGTH_LONG |
| 118 | ).show() | 123 | ).show() |
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/MemoryUtil.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/MemoryUtil.kt index 59e9f8f87..aa4a5539a 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/MemoryUtil.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/MemoryUtil.kt | |||
| @@ -5,44 +5,101 @@ package org.yuzu.yuzu_emu.utils | |||
| 5 | 5 | ||
| 6 | import android.app.ActivityManager | 6 | import android.app.ActivityManager |
| 7 | import android.content.Context | 7 | import android.content.Context |
| 8 | import android.os.Build | ||
| 8 | import org.yuzu.yuzu_emu.R | 9 | import org.yuzu.yuzu_emu.R |
| 9 | import org.yuzu.yuzu_emu.YuzuApplication | 10 | import org.yuzu.yuzu_emu.YuzuApplication |
| 10 | import java.util.Locale | 11 | import java.util.Locale |
| 12 | import kotlin.math.ceil | ||
| 11 | 13 | ||
| 12 | object MemoryUtil { | 14 | object MemoryUtil { |
| 13 | private val context get() = YuzuApplication.appContext | 15 | private val context get() = YuzuApplication.appContext |
| 14 | 16 | ||
| 15 | private val Long.floatForm: String | 17 | private val Float.hundredths: String |
| 16 | get() = String.format(Locale.ROOT, "%.2f", this.toDouble()) | 18 | get() = String.format(Locale.ROOT, "%.2f", this) |
| 17 | 19 | ||
| 18 | const val Kb: Long = 1024 | 20 | // Required total system memory |
| 21 | const val REQUIRED_MEMORY = 8 | ||
| 22 | |||
| 23 | const val Kb: Float = 1024F | ||
| 19 | const val Mb = Kb * 1024 | 24 | const val Mb = Kb * 1024 |
| 20 | const val Gb = Mb * 1024 | 25 | const val Gb = Mb * 1024 |
| 21 | const val Tb = Gb * 1024 | 26 | const val Tb = Gb * 1024 |
| 22 | const val Pb = Tb * 1024 | 27 | const val Pb = Tb * 1024 |
| 23 | const val Eb = Pb * 1024 | 28 | const val Eb = Pb * 1024 |
| 24 | 29 | ||
| 25 | private fun bytesToSizeUnit(size: Long): String { | 30 | private fun bytesToSizeUnit(size: Float): String = |
| 26 | return when { | 31 | when { |
| 27 | size < Kb -> "${size.floatForm} ${context.getString(R.string.memory_byte)}" | 32 | size < Kb -> { |
| 28 | size < Mb -> "${(size / Kb).floatForm} ${context.getString(R.string.memory_kilobyte)}" | 33 | context.getString( |
| 29 | size < Gb -> "${(size / Mb).floatForm} ${context.getString(R.string.memory_megabyte)}" | 34 | R.string.memory_formatted, |
| 30 | size < Tb -> "${(size / Gb).floatForm} ${context.getString(R.string.memory_gigabyte)}" | 35 | size.hundredths, |
| 31 | size < Pb -> "${(size / Tb).floatForm} ${context.getString(R.string.memory_terabyte)}" | 36 | context.getString(R.string.memory_byte) |
| 32 | size < Eb -> "${(size / Pb).floatForm} ${context.getString(R.string.memory_petabyte)}" | 37 | ) |
| 33 | else -> "${(size / Eb).floatForm} ${context.getString(R.string.memory_exabyte)}" | 38 | } |
| 39 | size < Mb -> { | ||
| 40 | context.getString( | ||
| 41 | R.string.memory_formatted, | ||
| 42 | (size / Kb).hundredths, | ||
| 43 | context.getString(R.string.memory_kilobyte) | ||
| 44 | ) | ||
| 45 | } | ||
| 46 | size < Gb -> { | ||
| 47 | context.getString( | ||
| 48 | R.string.memory_formatted, | ||
| 49 | (size / Mb).hundredths, | ||
| 50 | context.getString(R.string.memory_megabyte) | ||
| 51 | ) | ||
| 52 | } | ||
| 53 | size < Tb -> { | ||
| 54 | context.getString( | ||
| 55 | R.string.memory_formatted, | ||
| 56 | (size / Gb).hundredths, | ||
| 57 | context.getString(R.string.memory_gigabyte) | ||
| 58 | ) | ||
| 59 | } | ||
| 60 | size < Pb -> { | ||
| 61 | context.getString( | ||
| 62 | R.string.memory_formatted, | ||
| 63 | (size / Tb).hundredths, | ||
| 64 | context.getString(R.string.memory_terabyte) | ||
| 65 | ) | ||
| 66 | } | ||
| 67 | size < Eb -> { | ||
| 68 | context.getString( | ||
| 69 | R.string.memory_formatted, | ||
| 70 | (size / Pb).hundredths, | ||
| 71 | context.getString(R.string.memory_petabyte) | ||
| 72 | ) | ||
| 73 | } | ||
| 74 | else -> { | ||
| 75 | context.getString( | ||
| 76 | R.string.memory_formatted, | ||
| 77 | (size / Eb).hundredths, | ||
| 78 | context.getString(R.string.memory_exabyte) | ||
| 79 | ) | ||
| 80 | } | ||
| 34 | } | 81 | } |
| 35 | } | ||
| 36 | 82 | ||
| 37 | private val totalMemory = | 83 | // Devices are unlikely to have 0.5GB increments of memory so we'll just round up to account for |
| 38 | with(context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager) { | 84 | // the potential error created by memInfo.totalMem |
| 85 | private val totalMemory: Float | ||
| 86 | get() { | ||
| 39 | val memInfo = ActivityManager.MemoryInfo() | 87 | val memInfo = ActivityManager.MemoryInfo() |
| 40 | getMemoryInfo(memInfo) | 88 | with(context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager) { |
| 41 | memInfo.totalMem | 89 | getMemoryInfo(memInfo) |
| 90 | } | ||
| 91 | |||
| 92 | return ceil( | ||
| 93 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { | ||
| 94 | memInfo.advertisedMem.toFloat() | ||
| 95 | } else { | ||
| 96 | memInfo.totalMem.toFloat() | ||
| 97 | } | ||
| 98 | ) | ||
| 42 | } | 99 | } |
| 43 | 100 | ||
| 44 | fun isLessThan(minimum: Int, size: Long): Boolean { | 101 | fun isLessThan(minimum: Int, size: Float): Boolean = |
| 45 | return when (size) { | 102 | when (size) { |
| 46 | Kb -> totalMemory < Mb && totalMemory < minimum | 103 | Kb -> totalMemory < Mb && totalMemory < minimum |
| 47 | Mb -> totalMemory < Gb && (totalMemory / Mb) < minimum | 104 | Mb -> totalMemory < Gb && (totalMemory / Mb) < minimum |
| 48 | Gb -> totalMemory < Tb && (totalMemory / Gb) < minimum | 105 | Gb -> totalMemory < Tb && (totalMemory / Gb) < minimum |
| @@ -51,9 +108,6 @@ object MemoryUtil { | |||
| 51 | Eb -> totalMemory / Eb < minimum | 108 | Eb -> totalMemory / Eb < minimum |
| 52 | else -> totalMemory < Kb && totalMemory < minimum | 109 | else -> totalMemory < Kb && totalMemory < minimum |
| 53 | } | 110 | } |
| 54 | } | ||
| 55 | 111 | ||
| 56 | fun getDeviceRAM(): String { | 112 | fun getDeviceRAM(): String = bytesToSizeUnit(totalMemory) |
| 57 | return bytesToSizeUnit(totalMemory) | ||
| 58 | } | ||
| 59 | } | 113 | } |
diff --git a/src/android/app/src/main/res/values/strings.xml b/src/android/app/src/main/res/values/strings.xml index af7450619..b3c737979 100644 --- a/src/android/app/src/main/res/values/strings.xml +++ b/src/android/app/src/main/res/values/strings.xml | |||
| @@ -273,6 +273,7 @@ | |||
| 273 | <string name="fatal_error_message">A fatal error occurred. Check the log for details.\nContinuing emulation may result in crashes and bugs.</string> | 273 | <string name="fatal_error_message">A fatal error occurred. Check the log for details.\nContinuing emulation may result in crashes and bugs.</string> |
| 274 | <string name="performance_warning">Turning off this setting will significantly reduce emulation performance! For the best experience, it is recommended that you leave this setting enabled.</string> | 274 | <string name="performance_warning">Turning off this setting will significantly reduce emulation performance! For the best experience, it is recommended that you leave this setting enabled.</string> |
| 275 | <string name="device_memory_inadequate">Device RAM: %1$s\nRecommended: %2$s</string> | 275 | <string name="device_memory_inadequate">Device RAM: %1$s\nRecommended: %2$s</string> |
| 276 | <string name="memory_formatted">%1$s %2$s</string> | ||
| 276 | 277 | ||
| 277 | <!-- Region Names --> | 278 | <!-- Region Names --> |
| 278 | <string name="region_japan">Japan</string> | 279 | <string name="region_japan">Japan</string> |