From b10fa425b5f1c43f72df76e621d2816ce0ece0da Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Mon, 26 Mar 2018 11:28:29 +0200 Subject: [PATCH] scale UIL parallelization based on amount of RAM the device comes with This uses the total RAM that the device comes with as a rough measure of the devices capabilities. That is then used to set how many parallel threads UIL can use. --- .../java/org/fdroid/fdroid/FDroidApp.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/fdroid/fdroid/FDroidApp.java b/app/src/main/java/org/fdroid/fdroid/FDroidApp.java index 460237f88..58b16de3f 100644 --- a/app/src/main/java/org/fdroid/fdroid/FDroidApp.java +++ b/app/src/main/java/org/fdroid/fdroid/FDroidApp.java @@ -397,7 +397,7 @@ public class FDroidApp extends Application { ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()) .imageDownloader(new ImageLoaderForUIL(getApplicationContext())) .defaultDisplayImageOptions(Utils.getDefaultDisplayImageOptionsBuilder().build()) - .threadPoolSize(4) + .threadPoolSize(getThreadPoolSize()) .build(); ImageLoader.getInstance().init(config); @@ -470,6 +470,24 @@ public class FDroidApp extends Application { return false; } + /** + * Return the number of threads Universal Image Loader should use, based on + * the total RAM in the device. Devices with lots of RAM can do lots of + * parallel operations for fast icon loading. + */ + @TargetApi(16) + private int getThreadPoolSize() { + if (Build.VERSION.SDK_INT >= 16) { + ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); + ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo(); + if (activityManager != null) { + activityManager.getMemoryInfo(memInfo); + return (int) Math.max(1, Math.min(16, memInfo.totalMem / 256 / 1024 / 1024)); + } + } + return 2; + } + @TargetApi(18) private BluetoothAdapter getBluetoothAdapter() { // to use the new, recommended way of getting the adapter