From 8c439b1167db3fb0229c9f27207b18b96b3169e4 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Wed, 21 Jun 2023 18:38:53 +0800 Subject: [PATCH] Convert OSUtils to kotlin --- app/src/main/java/com/hippo/ehviewer/Crash.kt | 6 +- .../com/hippo/ehviewer/yorozuya/OSUtils.kt | 113 +++++++----------- .../tachiyomi/ui/reader/loader/PageLoader.kt | 2 +- 3 files changed, 50 insertions(+), 71 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/Crash.kt b/app/src/main/java/com/hippo/ehviewer/Crash.kt index 83eadf630..1a2f03f92 100644 --- a/app/src/main/java/com/hippo/ehviewer/Crash.kt +++ b/app/src/main/java/com/hippo/ehviewer/Crash.kt @@ -49,16 +49,16 @@ object Crash { fw.write("${collectClassStaticInfo(Build::class.java)}\n") fw.write("${collectClassStaticInfo(Build.VERSION::class.java)}\n") fw.write("MEMORY=") - fw.write(FileUtils.humanReadableByteCount(OSUtils.getAppAllocatedMemory(), false)) + fw.write(FileUtils.humanReadableByteCount(OSUtils.appAllocatedMemory, false)) fw.write("\n") fw.write("MEMORY_NATIVE=") fw.write(FileUtils.humanReadableByteCount(Debug.getNativeHeapAllocatedSize(), false)) fw.write("\n") fw.write("MEMORY_MAX=") - fw.write(FileUtils.humanReadableByteCount(OSUtils.getAppMaxMemory(), false)) + fw.write(FileUtils.humanReadableByteCount(OSUtils.appMaxMemory, false)) fw.write("\n") fw.write("MEMORY_TOTAL=") - fw.write(FileUtils.humanReadableByteCount(OSUtils.getTotalMemory(), false)) + fw.write(FileUtils.humanReadableByteCount(OSUtils.totalMemory, false)) fw.write("\n") fw.write("\n") } diff --git a/app/src/main/java/com/hippo/ehviewer/yorozuya/OSUtils.kt b/app/src/main/java/com/hippo/ehviewer/yorozuya/OSUtils.kt index 38bfd4a8c..3dd8ee5d8 100644 --- a/app/src/main/java/com/hippo/ehviewer/yorozuya/OSUtils.kt +++ b/app/src/main/java/com/hippo/ehviewer/yorozuya/OSUtils.kt @@ -13,76 +13,55 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.hippo.ehviewer.yorozuya -package com.hippo.ehviewer.yorozuya; +import java.io.BufferedReader +import java.io.FileReader +import java.util.regex.Pattern -import android.os.Looper; - -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public final class OSUtils { - private static final String PROCFS_MEMFILE = "/proc/meminfo"; - private static final Pattern PROCFS_MEMFILE_FORMAT = - Pattern.compile("^([a-zA-Z]*):[ \t]*([0-9]*)[ \t]kB"); - private static final String MEMTOTAL_STRING = "MemTotal"; - private static long sTotalMem = Long.MIN_VALUE; - - private OSUtils() { - } - - public static void checkMainLoop() { - if (Looper.myLooper() != Looper.getMainLooper()) { - throw new IllegalStateException("It is in not main loop!"); - } - } - - /** - * Get application allocated memory size - */ - public static long getAppAllocatedMemory() { - return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); - } - - /** - * Get application max memory size - */ - public static long getAppMaxMemory() { - return Runtime.getRuntime().maxMemory(); - } - - /** - * Get device RAM size - */ - public static long getTotalMemory() { - if (sTotalMem == Long.MIN_VALUE) { - BufferedReader reader = null; - try { - reader = new BufferedReader(new FileReader(PROCFS_MEMFILE), 64); - String line; - while ((line = reader.readLine()) != null) { - Matcher matcher = PROCFS_MEMFILE_FORMAT.matcher(line); - if (matcher.find() && MEMTOTAL_STRING.equals(matcher.group(1))) { - long mem = NumberUtils.parseLongSafely(matcher.group(2), -1L); - if (mem != -1L) { - mem *= 1024; - } - sTotalMem = mem; - break; - } - } - } catch (IOException e) { - e.printStackTrace(); - } finally { - IOUtils.closeQuietly(reader); - } +object OSUtils { + private const val PROCFS_MEMFILE = "/proc/meminfo" + private val PROCFS_MEMFILE_FORMAT = Pattern.compile("^([a-zA-Z]*):[ \t]*([0-9]*)[ \t]kB") + private const val MEMTOTAL_STRING = "MemTotal" + private var sTotalMem = Long.MIN_VALUE + val appAllocatedMemory: Long + /** + * Get application allocated memory size + */ + get() = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() + val appMaxMemory: Long + /** + * Get application max memory size + */ + get() = Runtime.getRuntime().maxMemory() + val totalMemory: Long + /** + * Get device RAM size + */ + get() { if (sTotalMem == Long.MIN_VALUE) { - sTotalMem = -1L; + runCatching { + BufferedReader(FileReader(PROCFS_MEMFILE), 64).use { reader -> + var line: String? + while (reader.readLine().also { line = it } != null) { + val matcher = PROCFS_MEMFILE_FORMAT.matcher(line!!) + if (matcher.find() && MEMTOTAL_STRING == matcher.group(1)) { + var mem = NumberUtils.parseLongSafely(matcher.group(2)!!, -1L) + if (mem != -1L) { + mem *= 1024 + } + sTotalMem = mem + break + } + } + } + }.onFailure { + it.printStackTrace() + } + if (sTotalMem == Long.MIN_VALUE) { + sTotalMem = -1L + } } + return sTotalMem } - return sTotalMem; - } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/PageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/PageLoader.kt index 547204ab4..7e8f8fd30 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/PageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/PageLoader.kt @@ -14,7 +14,7 @@ private const val MIN_CACHE_SIZE = 128 * 1024 * 1024 abstract class PageLoader { private val mImageCache = lruCache( - maxSize = (OSUtils.getTotalMemory() / 16).toInt().coerceIn(MIN_CACHE_SIZE, MAX_CACHE_SIZE), + maxSize = (OSUtils.totalMemory / 16).toInt().coerceIn(MIN_CACHE_SIZE, MAX_CACHE_SIZE), sizeOf = { _, v -> v.size }, onEntryRemoved = { _, _, o, _ -> o.recycle() }, )