Example usage for android.graphics Bitmap getGenerationId

List of usage examples for android.graphics Bitmap getGenerationId

Introduction

In this page you can find the example usage for android.graphics Bitmap getGenerationId.

Prototype

public int getGenerationId() 

Source Link

Document

Returns the generation ID of this bitmap.

Usage

From source file:tk.wasdennnoch.androidn_ify.utils.NotificationColorUtil.java

/**
 * Checks whether a Bitmap is a small grayscale icon.
 * Grayscale here means "very close to a perfect gray"; icon means "no larger than 64dp".
 *
 * @param bitmap The bitmap to test./*from  w  w w  .  j a v  a2  s . c o m*/
 * @return True if the bitmap is grayscale; false if it is color or too large to examine.
 */
public boolean isGrayscaleIcon(Bitmap bitmap) {
    // quick test: reject large bitmaps
    if (bitmap.getWidth() > mGrayscaleIconMaxSize || bitmap.getHeight() > mGrayscaleIconMaxSize) {
        return false;
    }

    synchronized (sLock) {
        Pair<Boolean, Integer> cached = mGrayscaleBitmapCache.get(bitmap);
        if (cached != null) {
            if (cached.second == bitmap.getGenerationId()) {
                return cached.first;
            }
        }
    }
    boolean result;
    int generationId;
    synchronized (mImageUtils) {
        result = mImageUtils.isGrayscale(bitmap);

        // generationId and the check whether the Bitmap is grayscale can't be read atomically
        // here. However, since the thread is in the process of posting the notification, we can
        // assume that it doesn't modify the bitmap while we are checking the pixels.
        generationId = bitmap.getGenerationId();
    }
    synchronized (sLock) {
        mGrayscaleBitmapCache.put(bitmap, Pair.create(result, generationId));
    }
    return result;
}