Convert OSUtils to kotlin
This commit is contained in:
1 parent
4eb1a4141f
commit
8c439b1167
3 files changed
+50
-71
No files matched your search
@@ -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")
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ private const val MIN_CACHE_SIZE = 128 * 1024 * 1024
|
||||
abstract class PageLoader {
|
||||
|
||||
private val mImageCache = lruCache<Int, Image>(
|
||||
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() },
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user