From d59fa1756e8933645b6267ecc3c9727e14d2275e Mon Sep 17 00:00:00 2001 From: seven332 Date: Tue, 10 May 2016 00:46:00 +0800 Subject: [PATCH] Use OSUtils --- .../com/hippo/ehviewer/EhApplication.java | 11 ++--- .../ehviewer/gallery/GalleryProvider.java | 44 +------------------ 2 files changed, 5 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/EhApplication.java b/app/src/main/java/com/hippo/ehviewer/EhApplication.java index 751e1d2a3..a08a7ea0b 100644 --- a/app/src/main/java/com/hippo/ehviewer/EhApplication.java +++ b/app/src/main/java/com/hippo/ehviewer/EhApplication.java @@ -17,7 +17,6 @@ package com.hippo.ehviewer; import android.app.Activity; -import android.app.ActivityManager; import android.content.ComponentCallbacks2; import android.content.Context; import android.content.pm.PackageInfo; @@ -49,6 +48,7 @@ import com.hippo.util.BitmapUtils; import com.hippo.util.ReadableTime; import com.hippo.yorozuya.FileUtils; import com.hippo.yorozuya.IntIdGenerator; +import com.hippo.yorozuya.OSUtils; import com.hippo.yorozuya.SimpleHandler; import java.io.File; @@ -280,11 +280,8 @@ public class EhApplication extends SceneApplication implements Thread.UncaughtEx return application.mImageBitmapHelper; } - private static int getMemoryCacheMaxSize(Context context) { - final ActivityManager activityManager = (ActivityManager) context. - getSystemService(Context.ACTIVITY_SERVICE); - return Math.min(20 * 1024 * 1024, - Math.round(0.2f * activityManager.getMemoryClass() * 1024 * 1024)); + private static int getMemoryCacheMaxSize() { + return Math.min(20 * 1024 * 1024, (int) OSUtils.getAppMaxMemory()); } @NonNull @@ -293,7 +290,7 @@ public class EhApplication extends SceneApplication implements Thread.UncaughtEx if (application.mConaco == null) { Conaco.Builder builder = new Conaco.Builder<>(); builder.hasMemoryCache = true; - builder.memoryCacheMaxSize = getMemoryCacheMaxSize(context); + builder.memoryCacheMaxSize = getMemoryCacheMaxSize(); builder.hasDiskCache = true; builder.diskCacheDir = new File(context.getCacheDir(), "thumb"); builder.diskCacheMaxSize = 80 * 1024 * 1024; // 80MB diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/GalleryProvider.java b/app/src/main/java/com/hippo/ehviewer/gallery/GalleryProvider.java index d2db96ba4..1b4c466d2 100644 --- a/app/src/main/java/com/hippo/ehviewer/gallery/GalleryProvider.java +++ b/app/src/main/java/com/hippo/ehviewer/gallery/GalleryProvider.java @@ -28,18 +28,11 @@ import com.hippo.gl.view.GLRoot; import com.hippo.image.Image; import com.hippo.unifile.UniFile; import com.hippo.yorozuya.ConcurrentPool; -import com.hippo.yorozuya.IOUtils; import com.hippo.yorozuya.MathUtils; -import com.hippo.yorozuya.NumberUtils; import com.hippo.yorozuya.OSUtils; -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import java.util.regex.Matcher; -import java.util.regex.Pattern; public abstract class GalleryProvider { @@ -259,43 +252,8 @@ public abstract class GalleryProvider { private static final long MAX_CACHE_SIZE = 128 * 1024 * 1024; private static final long MIN_CACHE_SIZE = 32 * 1024 * 1024; - 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 sMemTotal = -2L; - - private static long getTotalMemory() { - if (sMemTotal != -2L) { - return sMemTotal; - } - - 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; - } - sMemTotal = mem; - return mem; - } - } - } catch (IOException e) { - e.printStackTrace(); - } finally { - IOUtils.closeQuietly(reader); - } - sMemTotal = -1L; - return -1L; - } - public ImageCache() { - super((int) MathUtils.clamp(getTotalMemory() / 16, MIN_CACHE_SIZE, MAX_CACHE_SIZE)); + super((int) MathUtils.clamp(OSUtils.getTotalMemory() / 16, MIN_CACHE_SIZE, MAX_CACHE_SIZE)); } public void add(Integer key, ImageWrapper value) {