[Android] Increase default cache size

-Increase the default cache size depending on the device detected RAM (higher end tablets and phones will have greater benefit for having large RAM).
This commit is contained in:
Anthony Calosa
2020-10-06 10:16:50 +08:00
parent fb16232c9f
commit 9e00fcd8d8
5 changed files with 29 additions and 12 deletions

View File

@@ -1,5 +1,6 @@
package forge.app;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
@@ -56,9 +57,15 @@ public class Main extends AndroidApplication {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get total device RAM in mb
ActivityManager actManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
actManager.getMemoryInfo(memInfo);
int totalMemory = Math.round(memInfo.totalMem / 1024f / 1024f);
boolean permissiongranted = checkPermission();
Gadapter = new AndroidAdapter(this.getContext());
initForge(Gadapter, permissiongranted);
initForge(Gadapter, permissiongranted, totalMemory);
//permission
if(!permissiongranted){
@@ -185,7 +192,7 @@ public class Main extends AndroidApplication {
builder.show();
}
private void initForge(AndroidAdapter adapter, boolean permissiongranted){
private void initForge(AndroidAdapter adapter, boolean permissiongranted, int totalRAM){
boolean isPortrait;
if (permissiongranted){
//establish assets directory
@@ -225,12 +232,12 @@ public class Main extends AndroidApplication {
Main.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
initialize(Forge.getApp(new AndroidClipboard(), adapter, assetsDir, propertyConfig, isPortrait));
initialize(Forge.getApp(new AndroidClipboard(), adapter, assetsDir, propertyConfig, isPortrait, totalRAM));
} else {
isPortrait = true;
//set current orientation
Main.this.setRequestedOrientation(Main.this.getResources().getConfiguration().orientation);
initialize(Forge.getApp(new AndroidClipboard(), adapter, "", false, isPortrait));
initialize(Forge.getApp(new AndroidClipboard(), adapter, "", false, isPortrait, totalRAM));
}
}

View File

@@ -32,8 +32,8 @@ public class Main extends IOSApplication.Delegate {
config.useAccelerometer = false;
config.useCompass = false;
ForgePreferences prefs = FModel.getPreferences();
boolean propertyConfig = prefs != null && prefs.getPrefBoolean(ForgePreferences.FPref.UI_NETPLAY_COMPAT);
final ApplicationListener app = Forge.getApp(new IOSClipboard(), new IOSAdapter(), assetsDir, propertyConfig, false);
boolean propertyConfig = prefs != null && prefs.getPrefBoolean(ForgePreferences.FPref.UI_NETPLAY_COMPAT);//todo get totalRAM
final ApplicationListener app = Forge.getApp(new IOSClipboard(), new IOSAdapter(), assetsDir, propertyConfig, false, 0);
final IOSApplication iosApp = new IOSApplication(app, config);
return iosApp;
}

View File

@@ -96,8 +96,8 @@ public class Main {
ForgePreferences prefs = FModel.getPreferences();
boolean propertyConfig = prefs != null && prefs.getPrefBoolean(ForgePreferences.FPref.UI_NETPLAY_COMPAT);
new LwjglApplication(Forge.getApp(new LwjglClipboard(), new DesktopAdapter(switchOrientationFile),
desktopMode ? desktopModeAssetsDir : assetsDir, propertyConfig, false), config);
new LwjglApplication(Forge.getApp(new LwjglClipboard(), new DesktopAdapter(switchOrientationFile),//todo get totalRAM
desktopMode ? desktopModeAssetsDir : assetsDir, propertyConfig, false, 0), config);
}
private static class DesktopAdapter implements IDeviceAdapter {

View File

@@ -71,14 +71,20 @@ public class Forge implements ApplicationListener {
public static boolean hdstart = false;
public static boolean isPortraitMode = false;
public static boolean gameInProgress = false;
public static int cacheSize = 400;
public static int totalDeviceRAM = 0;
public static ApplicationListener getApp(Clipboard clipboard0, IDeviceAdapter deviceAdapter0, String assetDir0, boolean value, boolean androidOrientation) {
public static ApplicationListener getApp(Clipboard clipboard0, IDeviceAdapter deviceAdapter0, String assetDir0, boolean value, boolean androidOrientation, int totalRAM) {
if (GuiBase.getInterface() == null) {
clipboard = clipboard0;
deviceAdapter = deviceAdapter0;
GuiBase.setInterface(new GuiMobile(assetDir0));
GuiBase.enablePropertyConfig(value);
isPortraitMode = androidOrientation;
totalDeviceRAM = totalRAM;
//increase cacheSize for devices with RAM more than 5GB, default is 400. Some phones have more than 10GB RAM (Mi 10, OnePlus 8, S20, etc..)
if (totalDeviceRAM>5000) //devices with more than 10GB RAM will have 1000 Cache size, 700 Cache size for morethan 5GB RAM
cacheSize = totalDeviceRAM>10000 ? 1000: 700;
}
return app;
}
@@ -142,8 +148,12 @@ public class Forge implements ApplicationListener {
splashScreen.getProgressBar().setDescription(localizer.getMessage("lblFinishingStartup"));
//add reminder to preload
if (enablePreloadExtendedArt)
splashScreen.getProgressBar().setDescription(localizer.getMessage("lblPreloadExtendedArt"));
if (enablePreloadExtendedArt) {
splashScreen.getProgressBar().setDescription(localizer.getMessage("lblPreloadExtendedArt")+"\nDetected RAM: " +totalDeviceRAM+"MB. Cache size: "+cacheSize);
} else {
splashScreen.getProgressBar().setDescription(localizer.getMessage("lblFinishingStartup")+"\nDetected RAM: " +totalDeviceRAM+"MB. Cache size: "+cacheSize);
}
Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {

View File

@@ -69,7 +69,7 @@ public class ImageCache {
private static final Set<String> missingIconKeys = new HashSet<>();
private static final LoadingCache<String, Texture> cache = CacheBuilder.newBuilder()
.maximumSize(400)
.maximumSize(Forge.cacheSize)
.expireAfterAccess(15, TimeUnit.MINUTES)
.removalListener(new RemovalListener<String, Texture>() {
@Override