Example usage for android.app ActivityManager getMemoryClass

List of usage examples for android.app ActivityManager getMemoryClass

Introduction

In this page you can find the example usage for android.app ActivityManager getMemoryClass.

Prototype

public int getMemoryClass() 

Source Link

Document

Return the approximate per-application memory class of the current device.

Usage

From source file:com.facebook.imagepipeline.animated.impl.AnimatedDrawableCachingBackendImpl.java

private static int getDefaultMaxBytes(ActivityManager activityManager) {
    int memory = activityManager.getMemoryClass();
    if (memory > 32) {
        return 5 * 1024 * 1024;
    } else {//from   ww w  . j a  v  a 2 s .  c o  m
        return 3 * 1024 * 1024;
    }
}

From source file:org.thoughtcrime.securesms.logsubmit.SubmitLogFragment.java

@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";
    }// ww w .  ja va2 s. c  o  m
    return activityManager.getMemoryClass() + lowMem;
}

From source file:me.vijayjaybhay.galleryview.cache.MemoryImageCache.java

/**
 * Computes cache size//from w ww.  ja  v a 2 s  . com
 * @return maximum memory cache size
 */
private int getCacheSize() {
    ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    int memClassBytes = am.getMemoryClass() * 1024 * 1024;
    return memClassBytes / 8;
}

From source file:com.appsimobile.appsii.plugins.IconCache.java

@Inject
public IconCache(ActivityManager activityManager) {
    final int memClass = activityManager.getMemoryClass();
    int sizeInBytes = memClass * 1024 * 1024;
    int desiredSize = sizeInBytes / 20;
    // for 50 mb devices this will be 2.5 mb
    // for 12 mb devices this is 0.6 mb
    mCacheSize = desiredSize;/*  ww w.j av a  2s.co  m*/
    mCache = new LruCache<CacheKey, Bitmap>(desiredSize) {
        @Override
        protected int sizeOf(CacheKey key, Bitmap bitmap) {
            return bitmap.getRowBytes() * bitmap.getHeight();
        }
    };
}

From source file:org.videolan.vlc.util.BitmapCache.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private BitmapCache() {

    // Get memory class of this device, exceeding this amount will throw an
    // OutOfMemory exception.
    final ActivityManager am = ((ActivityManager) VLCApplication.getAppContext()
            .getSystemService(Context.ACTIVITY_SERVICE));
    final int memClass = AndroidUtil.isHoneycombOrLater() ? am.getLargeMemoryClass() : am.getMemoryClass();

    // Use 1/5th of the available memory for this memory cache.
    final int cacheSize = 1024 * 1024 * memClass / 5;

    Log.d(TAG, "LRUCache size sets to " + cacheSize);

    mMemCache = new LruCache<String, Bitmap>(cacheSize) {

        @Override//from   w  w  w  .  ja v  a  2 s . c om
        protected int sizeOf(String key, Bitmap value) {
            return value.getRowBytes() * value.getHeight();
        }

    };
}

From source file:io.github.mkjung.ivi.gui.helpers.BitmapCache.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private BitmapCache() {

    // Get memory class of this device, exceeding this amount will throw an
    // OutOfMemory exception.
    final ActivityManager am = ((ActivityManager) VLCApplication.getAppContext()
            .getSystemService(Context.ACTIVITY_SERVICE));
    final int memClass = AndroidUtil.isHoneycombOrLater() ? am.getLargeMemoryClass() : am.getMemoryClass();

    // Use 1/5th of the available memory for this memory cache.
    final int cacheSize = 1024 * 1024 * memClass / 5;

    Log.i(TAG, "LRUCache size set to " + cacheSize);

    mMemCache = new LruCache<String, CacheableBitmap>(cacheSize) {

        @Override/*  w  ww. j ava 2s .c om*/
        protected int sizeOf(String key, CacheableBitmap value) {
            return value.getSize();
        }
    };
}

From source file:com.andrewreitz.encryptedcamera.di.module.AndroidModule.java

@Provides
@Singleton/*from ww w  .  j a va2 s.  com*/
LruCache<String, Bitmap> provideCache(ActivityManager am) {
    // Keep the cache as a singleton so as along as the application is running we are
    // using the cache hopefully keeping the speed of the application up
    // Also allows us to hook into the application class easily to handle onLowMemory events

    int memoryClassBytes = am.getMemoryClass() * 1024 * 1024;
    // TODO Play with this number to find the best size
    return new ThumbnailCache(memoryClassBytes / 4);
}

From source file:ch.carteggio.ui.ConversationIconLoader.java

/**
 * Constructor.//w  w w. j a v a 2 s.co m
 *
 * @param context
 *         A {@link Context} instance.
 * @param defaultBackgroundColor
 *         The ARGB value to be used as background color for the fallback picture. {@code 0} to
 *         use a dynamically calculated background color.
 */
public ConversationIconLoader(Context context, int defaultBackgroundColor) {
    Context appContext = context.getApplicationContext();
    mContentResolver = appContext.getContentResolver();
    mResources = appContext.getResources();
    mHelper = new CarteggioProviderHelper(appContext);

    float scale = mResources.getDisplayMetrics().density;
    mPictureSizeInPx = (int) (PICTURE_SIZE * scale);

    mDefaultBackgroundColor = defaultBackgroundColor;

    ActivityManager activityManager = (ActivityManager) appContext.getSystemService(Context.ACTIVITY_SERVICE);
    int memClass = activityManager.getMemoryClass();

    // Use 1/16th of the available memory for this memory cache.
    final int cacheSize = 1024 * 1024 * memClass / 16;

    mBitmapCache = new LruCache<Long, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(Long key, Bitmap bitmap) {
            // The cache size will be measured in bytes rather than number of items.
            return bitmap.getByteCount();
        }
    };
}

From source file:org.chromium.chrome.browser.preferences.privacy.ConfirmImportantSitesDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // We check the domains and urls as well due to crbug.com/622879.
    if (savedInstanceState != null) {
        // The important domains and favicon URLs aren't currently saved, so if this dialog
        // is recreated from a saved instance they will be null. This method must return a
        // valid dialog, so these two array's are initialized, then the dialog is dismissed.
        // TODO(dmurph): save mImportantDomains and mFaviconURLs so that they can be restored
        // from a savedInstanceState and the dialog can be properly recreated rather than
        // dismissed.
        mImportantDomains = new String[0];
        mFaviconURLs = new String[0];
        dismiss();//from   w ww.  j av a 2 s  .c  o m
    }
    mProfile = Profile.getLastUsedProfile().getOriginalProfile();
    mLargeIconBridge = new LargeIconBridge(mProfile);
    ActivityManager activityManager = ((ActivityManager) ContextUtils.getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE));
    int maxSize = Math.min(activityManager.getMemoryClass() / 16 * 25 * 1024, FAVICON_MAX_CACHE_SIZE_BYTES);
    mLargeIconBridge.createCache(maxSize);

    mAdapter = new ClearBrowsingDataAdapter(mImportantDomains, mFaviconURLs, getResources());
    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            if (which == AlertDialog.BUTTON_POSITIVE) {
                Intent data = new Intent();
                List<String> deselectedDomains = new ArrayList<>();
                List<Integer> deselectedDomainReasons = new ArrayList<>();
                List<String> ignoredDomains = new ArrayList<>();
                List<Integer> ignoredDomainReasons = new ArrayList<>();
                for (Entry<String, Boolean> entry : mCheckedState.entrySet()) {
                    Integer reason = mImportantDomainsReasons.get(entry.getKey());
                    if (entry.getValue()) {
                        ignoredDomains.add(entry.getKey());
                        ignoredDomainReasons.add(reason);
                    } else {
                        deselectedDomains.add(entry.getKey());
                        deselectedDomainReasons.add(reason);
                    }
                }
                data.putExtra(DESELECTED_DOMAINS_TAG, deselectedDomains.toArray(new String[0]));
                data.putExtra(DESELECTED_DOMAIN_REASONS_TAG, toIntArray(deselectedDomainReasons));
                data.putExtra(IGNORED_DOMAINS_TAG, ignoredDomains.toArray(new String[0]));
                data.putExtra(IGNORED_DOMAIN_REASONS_TAG, toIntArray(ignoredDomainReasons));
                getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, data);
            } else {
                getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED,
                        getActivity().getIntent());
            }
        }
    };
    // We create our own ListView, as AlertDialog doesn't let us set a message and a list
    // adapter at the same time.
    View messageAndListView = getActivity().getLayoutInflater()
            .inflate(R.layout.clear_browsing_important_dialog_listview, null);
    mSitesListView = (ListView) messageAndListView.findViewById(R.id.select_dialog_listview);
    mSitesListView.setAdapter(mAdapter);
    mSitesListView.setOnItemClickListener(mAdapter);
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertDialogTheme)
            .setTitle(R.string.storage_clear_site_storage_title)
            .setPositiveButton(R.string.clear_browsing_data_important_dialog_button, listener)
            .setNegativeButton(R.string.cancel, listener).setView(messageAndListView);
    mDialog = builder.create();

    return mDialog;
}

From source file:org.chromium.chrome.browser.bookmarks.BookmarkManager.java

/**
 * Creates an instance of {@link BookmarkManager}. It also initializes resources,
 * bookmark models and jni bridges./*ww w . j av  a  2  s.c  o  m*/
 * @param activity The activity context to use.
 * @param isDialogUi Whether the main bookmarks UI will be shown in a dialog, not a NativePage.
 */
public BookmarkManager(Activity activity, boolean isDialogUi) {
    mActivity = activity;
    mIsDialogUi = isDialogUi;

    mSelectionDelegate = new SelectionDelegate<BookmarkId>() {
        @Override
        public boolean toggleSelectionForItem(BookmarkId bookmark) {
            if (!mBookmarkModel.getBookmarkById(bookmark).isEditable())
                return false;
            return super.toggleSelectionForItem(bookmark);
        }
    };

    mBookmarkModel = new BookmarkModel();
    mMainView = (ViewGroup) mActivity.getLayoutInflater().inflate(R.layout.bookmark_main, null);
    mDrawer = (DrawerLayout) mMainView.findViewById(R.id.bookmark_drawer_layout);
    mDrawerListView = (BookmarkDrawerListView) mMainView.findViewById(R.id.bookmark_drawer_list);
    mContentView = (BookmarkContentView) mMainView.findViewById(R.id.bookmark_content_view);
    mViewSwitcher = (ViewSwitcher) mMainView.findViewById(R.id.bookmark_view_switcher);
    mUndoController = new BookmarkUndoController(activity, mBookmarkModel,
            ((SnackbarManageable) activity).getSnackbarManager());
    mSearchView = (BookmarkSearchView) getView().findViewById(R.id.bookmark_search_view);
    mBookmarkModel.addObserver(mBookmarkModelObserver);
    initializeToLoadingState();
    mBookmarkModel.runAfterBookmarkModelLoaded(mModelLoadedRunnable);

    // Load partner bookmarks explicitly. We load partner bookmarks in the deferred startup
    // code, but that might be executed much later. Especially on L, showing loading
    // progress bar blocks that so it won't be loaded. http://crbug.com/429383
    PartnerBookmarksShim.kickOffReading(activity);

    mLargeIconBridge = new LargeIconBridge(Profile.getLastUsedProfile().getOriginalProfile());
    ActivityManager activityManager = ((ActivityManager) ContextUtils.getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE));
    int maxSize = Math.min(activityManager.getMemoryClass() / 4 * 1024 * 1024, FAVICON_MAX_CACHE_SIZE_BYTES);
    mLargeIconBridge.createCache(maxSize);

    RecordUserAction.record("MobileBookmarkManagerOpen");
    if (!isDialogUi) {
        RecordUserAction.record("MobileBookmarkManagerPageOpen");
    }
}