Java Code Examples for android.app.activitymanager#getMemoryClass()

The following examples show how to use android.app.activitymanager#getMemoryClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageManager.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
/**
 * Enables or disables the disk cache for the life of the application. This method cannot be
 * called more than once per lifetime of the app and should therefore be called only during
 * application startup; subsequent calls have no effect on the disk cache state.
 *
 * Note that when enabling Piccaso cache indicators, you may still find that some images appear
 * as though they've been loaded from disk. This will be true for any app-packaged drawable or
 * bitmap resource that's placed by Picasso. (These are always "from disk" with or without the
 * presence of a disk cache.) Similarly, it's possible to get green-tagged images when using
 * Picasso to "load" manually pre-loaded BitmapDrawables. 
 *
 * @param diskCacheEnabled
 */
public static void setConfiguration(Context context, boolean diskCacheEnabled, Integer cacheHeapPercent) {
    try {

        Picasso.Builder builder = new Picasso.Builder((ArcusApplication.getContext()));

        if (diskCacheEnabled) {
            builder.downloader(new OkHttp3Downloader(new OkHttpClient()));
        }

        if (cacheHeapPercent != null) {
            ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
            int memoryClass = am.getMemoryClass();

            int heapSize = (int) ((float)(1024 * 1024 * memoryClass) * ((float) cacheHeapPercent / 100.0));
            builder.memoryCache(new LruCache(heapSize));
            logger.debug("Setting Picasso in-memory LRU cache max size to {} bytes; {}% of heap sized {}", heapSize, cacheHeapPercent, 1024 * 1024 * memoryClass);
        }

        Picasso.setSingletonInstance(builder.build());

    } catch (IllegalStateException e) {
        logger.warn("Picasso setConfiguration() has already been called; ignoring request.");
    }
}
 
Example 2
Source File: CacheType.java    From Aurora with Apache License 2.0 5 votes vote down vote up
@Override
public int calculateCacheSize(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
    if (targetMemoryCacheSize >= MAX_SIZE) {
        return MAX_SIZE;
    }
    return targetMemoryCacheSize;
}
 
Example 3
Source File: DefaultConfigurationFactory.java    From Android-Application-ZJB with Apache License 2.0 5 votes vote down vote up
/**
 * Creates default implementation of {@link MemoryCache} - {@link LruMemoryCache}<br />
 * Default cache size = 1/8 of available app memory.
 */
public static MemoryCache createMemoryCache(Context context, int memoryCacheSize) {
    if (memoryCacheSize == 0) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        int memoryClass = am.getMemoryClass();
        if (hasHoneycomb() && isLargeHeap(context)) {
            memoryClass = getLargeMemoryClass(am);
        }
        memoryCacheSize = 1024 * 1024 * memoryClass / 8;
    }
    return new LruMemoryCache(memoryCacheSize);
}
 
Example 4
Source File: CacheType.java    From MVPArms with Apache License 2.0 5 votes vote down vote up
@Override
public int calculateCacheSize(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
    if (targetMemoryCacheSize >= MAX_SIZE) {
        return MAX_SIZE;
    }
    return targetMemoryCacheSize;
}
 
Example 5
Source File: CacheType.java    From MVPArms with Apache License 2.0 5 votes vote down vote up
@Override
public int calculateCacheSize(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
    if (targetMemoryCacheSize >= MAX_SIZE) {
        return MAX_SIZE;
    }
    return targetMemoryCacheSize;
}
 
Example 6
Source File: DocumentsApplication.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    if(!BuildConfig.DEBUG) {
        AnalyticsManager.intialize(getApplicationContext());
    }
    sInstance = this;
    final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final int memoryClassBytes = am.getMemoryClass() * 1024 * 1024;

    mRoots = new RootsCache(this);
    mRoots.updateAsync();

    mSAFManager = new SAFManager(this);

    mThumbnails = new ThumbnailCache(memoryClassBytes / 4);

    final IntentFilter packageFilter = new IntentFilter();
    packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
    packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
    packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    packageFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED);
    packageFilter.addDataScheme("package");
    registerReceiver(mCacheReceiver, packageFilter);

    final IntentFilter localeFilter = new IntentFilter();
    localeFilter.addAction(Intent.ACTION_LOCALE_CHANGED);
    registerReceiver(mCacheReceiver, localeFilter);

    isTelevision = Utils.isTelevision(this);
}
 
Example 7
Source File: Util.java    From SoBitmap with Apache License 2.0 5 votes vote down vote up
static int getAvailableMemorySize(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    int ret = -1;
    boolean largeHeap = false;
    if (Build.VERSION.SDK_INT >= 11) {
        largeHeap = (context.getApplicationInfo().flags & ApplicationInfo.FLAG_LARGE_HEAP) != 0;
        if (largeHeap)
            ret = am.getLargeMemoryClass() * 1024;
    }
    if (!largeHeap || Build.VERSION.SDK_INT < 11) {
        ret = am.getMemoryClass() * 1024;
    }
    return ret / 5;
}
 
Example 8
Source File: LogViewFragment.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
@TargetApi(VERSION_CODES.KITKAT)
public static String getMemoryClass(Context context) {
  ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  String          lowMem          = "";

  if (VERSION.SDK_INT >= VERSION_CODES.KITKAT && activityManager.isLowRamDevice()) {
    lowMem = ", low-mem device";
  }
  return activityManager.getMemoryClass() + lowMem;
}
 
Example 9
Source File: CacheType.java    From MVPArms with Apache License 2.0 5 votes vote down vote up
@Override
public int calculateCacheSize(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    int targetMemoryCacheSize = (int) (activityManager.getMemoryClass() * MAX_SIZE_MULTIPLIER * 1024);
    if (targetMemoryCacheSize >= MAX_SIZE) {
        return MAX_SIZE;
    }
    return targetMemoryCacheSize;
}
 
Example 10
Source File: CachingAsyncImageLoader.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
public CachingAsyncImageLoader(Context context) {
    ActivityManager am = (ActivityManager)context.getSystemService(
            Context.ACTIVITY_SERVICE);
    int memoryClass = (am.getMemoryClass() * 1024 * 1024) / CACHE_DIVISOR;        //basically, set the heap to be everything we can get
    this.context = context;
    this.cache = new LruCache<>(memoryClass);
}
 
Example 11
Source File: Utils.java    From bubble with MIT License 5 votes vote down vote up
public static int getHeapSize(Context context) {
    ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
    boolean isLargeHeap = (context.getApplicationInfo().flags & ApplicationInfo.FLAG_LARGE_HEAP) != 0;
    int memoryClass = am.getMemoryClass();
    if (isLargeHeap && Utils.isHoneycombOrLater()) {
        memoryClass = am.getLargeMemoryClass();
    }
    return 1024 * memoryClass;
}
 
Example 12
Source File: DefaultConfigurationFactory.java    From BigApp_WordPress_Android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates default implementation of {@link MemoryCache} - {@link LruMemoryCache}<br />
 * Default cache size = 1/8 of available app memory.
 */
public static MemoryCache createMemoryCache(Context context, int memoryCacheSize) {
	if (memoryCacheSize == 0) {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		int memoryClass = am.getMemoryClass();
		if (hasHoneycomb() && isLargeHeap(context)) {
			memoryClass = getLargeMemoryClass(am);
		}
		memoryCacheSize = 1024 * 1024 * memoryClass / 8;
	}
	return new LruMemoryCache(memoryCacheSize);
}
 
Example 13
Source File: Utils.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
static int calculateMemoryCacheSize(Context context) {
  ActivityManager am = getService(context, ACTIVITY_SERVICE);
  boolean largeHeap = (context.getApplicationInfo().flags & FLAG_LARGE_HEAP) != 0;
  int memoryClass = am.getMemoryClass();
  if (largeHeap && SDK_INT >= HONEYCOMB) {
    memoryClass = ActivityManagerHoneycomb.getLargeMemoryClass(am);
  }
  // Target ~15% of the available heap.
  return 1024 * 1024 * memoryClass / 7;
}
 
Example 14
Source File: Utils.java    From picasso with Apache License 2.0 5 votes vote down vote up
static int calculateMemoryCacheSize(Context context) {
  ActivityManager am = ContextCompat.getSystemService(context, ActivityManager.class);
  boolean largeHeap = (context.getApplicationInfo().flags & FLAG_LARGE_HEAP) != 0;
  int memoryClass = largeHeap ? am.getLargeMemoryClass() : am.getMemoryClass();
  // Target ~15% of the available heap.
  return (int) (1024L * 1024L * memoryClass / 7);
}
 
Example 15
Source File: FragmentOptionsMisc.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private void setOptions() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());

    PackageManager pm = getContext().getPackageManager();
    int state = pm.getComponentEnabledSetting(new ComponentName(getContext(), ActivitySearch.class));

    swExternalSearch.setChecked(state != PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
    swShortcuts.setChecked(prefs.getBoolean("shortcuts", true));
    swFts.setChecked(prefs.getBoolean("fts", false));
    swEnglish.setChecked(prefs.getBoolean("english", false));
    swWatchdog.setChecked(prefs.getBoolean("watchdog", true));
    swUpdates.setChecked(prefs.getBoolean("updates", true));
    swUpdates.setVisibility(
            Helper.isPlayStoreInstall() || !Helper.hasValidFingerprint(getContext())
                    ? View.GONE : View.VISIBLE);
    swExperiments.setChecked(prefs.getBoolean("experiments", false));
    swQueries.setChecked(prefs.getInt("query_threads", 4) < 4);
    swCrashReports.setChecked(prefs.getBoolean("crash_reports", false));
    tvUuid.setText(prefs.getString("uuid", null));
    swDebug.setChecked(prefs.getBoolean("debug", false));
    swAuthSasl.setChecked(prefs.getBoolean("auth_sasl", true));
    swCleanupAttachments.setChecked(prefs.getBoolean("cleanup_attachments", false));

    tvProcessors.setText(getString(R.string.title_advanced_processors, Runtime.getRuntime().availableProcessors()));

    ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
    int class_mb = am.getMemoryClass();
    tvMemoryClass.setText(getString(R.string.title_advanced_memory_class, class_mb + " MB"));

    tvStorageSpace.setText(getString(R.string.title_advanced_storage_space,
            Helper.humanReadableByteCount(Helper.getAvailableStorageSpace(), true),
            Helper.humanReadableByteCount(Helper.getTotalStorageSpace(), true)));
    tvFingerprint.setText(Helper.getFingerprint(getContext()));

    grpDebug.setVisibility(swDebug.isChecked() || BuildConfig.DEBUG ? View.VISIBLE : View.GONE);
}
 
Example 16
Source File: DefaultConfigurationFactory.java    From WliveTV with Apache License 2.0 5 votes vote down vote up
/**
 * Creates default implementation of {@link MemoryCache} - {@link LruMemoryCache}<br />
 * Default cache size = 1/8 of available app memory.
 */
public static MemoryCache createMemoryCache(Context context, int memoryCacheSize) {
	if (memoryCacheSize == 0) {
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		int memoryClass = am.getMemoryClass();
		if (hasHoneycomb() && isLargeHeap(context)) {
			memoryClass = getLargeMemoryClass(am);
		}
		memoryCacheSize = 1024 * 1024 * memoryClass / 8;
	}
	return new LruMemoryCache(memoryCacheSize);
}
 
Example 17
Source File: BitmapUtils.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static long availableMemory() {
    final Runtime runtime = Runtime.getRuntime();
    final long used = runtime.totalMemory() - runtime.freeMemory();

    final ActivityManager activityManager = (ActivityManager) sContext.
            getSystemService(Context.ACTIVITY_SERVICE);
    final long total = activityManager.getMemoryClass() * 1024 * 1024;

    return total - used;
}
 
Example 18
Source File: MemorySizeCalculator.java    From sketch with Apache License 2.0 4 votes vote down vote up
private static int getMaxSize(@Nullable ActivityManager activityManager) {
    final int memoryClassBytes = activityManager != null ? activityManager.getMemoryClass() * 1024 * 1024 : 100;
    final boolean isLowMemoryDevice = isLowMemoryDevice(activityManager);
    return Math.round(memoryClassBytes
            * (isLowMemoryDevice ? LOW_MEMORY_MAX_SIZE_MULTIPLIER : MAX_SIZE_MULTIPLIER));
}
 
Example 19
Source File: DefaultCrossbowComponents.java    From CrossBow with Apache License 2.0 4 votes vote down vote up
public CrossbowImageCache onCreateImageCache() {
    ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
    int memCacheSize = ((am.getMemoryClass() * 1024 * 1024) / 10);
    return new DefaultImageCache(memCacheSize);
}
 
Example 20
Source File: MemoryUtil.java    From MemoryMonitor with MIT License 2 votes vote down vote up
/**
 * 获取应用能够获取的max dalvik堆内存大小
 * 和Runtime.getRuntime().maxMemory()一样
 *
 * @param context
 * @return 单位M
 */
public static long getAppTotalDalvikHeapSize(Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    return manager.getMemoryClass();
}