// Path: library/src/main/java/com/onegravity/contactpicker/Helper.java
// public class Helper {
// 
//     public static boolean isNullOrEmpty(CharSequence string){
//         return string == null || string.length() == 0;
//     }
// 
//     public static DisplayMetrics getDisplayMetrics(Context context) {
//         Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
//         DisplayMetrics metrics = new DisplayMetrics();
//         display.getMetrics( metrics );
//         return metrics;
//     }
// 
//     public static void closeQuietly(Cursor cursor) {
//         try {
//             cursor.close();
//         }
//         catch (Exception ignore) {}
//     }
// 
//     public static void closeQuietly(Closeable closeable) {
//         try {
//             if (closeable != null) {
//                 closeable.close();
//             }
//         } catch (IOException ignore) {}
//     }
// 
// }
// 
// Path: library/src/main/java/com/onegravity/contactpicker/picture/cache/ContactPictureCache.java
// public class ContactPictureCache extends InMemoryCache<Uri, Bitmap> {
// 
//     private static ContactPictureCache sInstance;
//     private static int sMemClass;
// 
//     // we need to synchronize this to make sure there's no race condition instantiating the cache
//     public synchronized static ContactPictureCache getInstance(Context context) {
//         if (sInstance == null) {
//             ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//             sMemClass = activityManager.getMemoryClass();
//             sInstance = new ContactPictureCache();
//         }
//         return sInstance;
//     }
// 
//     private ContactPictureCache() {
//         // purge after 5 minutes of being idle, the cacheCapacity parameter is ignored
//         super(1000 * 60 * 5, 50);
//     }
// 
//     @Override
//     protected HardLruCache createHardLruCache(int cacheCapacity) {
//         // Use 1/16th of the available memory for this memory cache.
//         final int cacheSize = 1024 * 1024 * sMemClass / 16;
//         return new PhotoHardLruCache(cacheSize);
//     }
// 
//     private class PhotoHardLruCache extends HardLruCache {
//         public PhotoHardLruCache(int initialCapacity) {
//             super(initialCapacity);
//         }
// 
//         @Override
//         protected int sizeOf(Uri key, Bitmap bitmap) {
//             // The cache size will be measured in bytes rather than number of items.
//             return bitmap.getByteCount();
//         }
//     }
// }


import java.io.InputStream;
import java.lang.ref.SoftReference;
import static android.graphics.Bitmap.createBitmap;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.net.Uri;
import com.onegravity.contactpicker.Helper;
import com.onegravity.contactpicker.picture.cache.ContactPictureCache;
import java.io.FileNotFoundException;
/*
 * Copyright (C) 2015-2017 Emanuel Moecklin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.onegravity.contactpicker.picture;





/**
 * Runnable to load a contact picture for a specific ContactBadge.
 */
public class ContactPictureLoader implements Runnable {

    private final String mKey;
    private final SoftReference<ContactBadge> mBadge;
    private final Uri mPhotoUri;
    private final boolean mRoundContactPictures;

    ContactPictureLoader(String key, ContactBadge badge, Uri photoUri, boolean roundContactPictures) {
        mKey = key;
        mBadge = new SoftReference<>(badge);
        mPhotoUri = photoUri;
        mRoundContactPictures = roundContactPictures;
    }

    @Override
    public void run() {
        ContactBadge badge = mBadge.get();
        if (badge == null) return; // fail fast

        Bitmap bitmap = retrievePicture(badge.getContext(), mPhotoUri, mRoundContactPictures);
        if (bitmap != null) {
            ContactPictureLoaded.post(mKey, badge, bitmap);
        }
    }

    public static Bitmap retrievePicture(Context context, Uri photoUri, boolean roundContactPictures) {
// Path: library/src/main/java/com/onegravity/contactpicker/Helper.java
// public class Helper {
// 
//     public static boolean isNullOrEmpty(CharSequence string){
//         return string == null || string.length() == 0;
//     }
// 
//     public static DisplayMetrics getDisplayMetrics(Context context) {
//         Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
//         DisplayMetrics metrics = new DisplayMetrics();
//         display.getMetrics( metrics );
//         return metrics;
//     }
// 
//     public static void closeQuietly(Cursor cursor) {
//         try {
//             cursor.close();
//         }
//         catch (Exception ignore) {}
//     }
// 
//     public static void closeQuietly(Closeable closeable) {
//         try {
//             if (closeable != null) {
//                 closeable.close();
//             }
//         } catch (IOException ignore) {}
//     }
// 
// }
// 
// Path: library/src/main/java/com/onegravity/contactpicker/picture/cache/ContactPictureCache.java
// public class ContactPictureCache extends InMemoryCache<Uri, Bitmap> {
// 
//     private static ContactPictureCache sInstance;
//     private static int sMemClass;
// 
//     // we need to synchronize this to make sure there's no race condition instantiating the cache
//     public synchronized static ContactPictureCache getInstance(Context context) {
//         if (sInstance == null) {
//             ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//             sMemClass = activityManager.getMemoryClass();
//             sInstance = new ContactPictureCache();
//         }
//         return sInstance;
//     }
// 
//     private ContactPictureCache() {
//         // purge after 5 minutes of being idle, the cacheCapacity parameter is ignored
//         super(1000 * 60 * 5, 50);
//     }
// 
//     @Override
//     protected HardLruCache createHardLruCache(int cacheCapacity) {
//         // Use 1/16th of the available memory for this memory cache.
//         final int cacheSize = 1024 * 1024 * sMemClass / 16;
//         return new PhotoHardLruCache(cacheSize);
//     }
// 
//     private class PhotoHardLruCache extends HardLruCache {
//         public PhotoHardLruCache(int initialCapacity) {
//             super(initialCapacity);
//         }
// 
//         @Override
//         protected int sizeOf(Uri key, Bitmap bitmap) {
//             // The cache size will be measured in bytes rather than number of items.
//             return bitmap.getByteCount();
//         }
//     }
// }


// Path: library/src/main/java/com/onegravity/contactpicker/picture/ContactPictureLoader.java
import java.io.InputStream;
import java.lang.ref.SoftReference;
import static android.graphics.Bitmap.createBitmap;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.net.Uri;
import com.onegravity.contactpicker.Helper;
import com.onegravity.contactpicker.picture.cache.ContactPictureCache;
import java.io.FileNotFoundException;

/*
 * Copyright (C) 2015-2017 Emanuel Moecklin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.onegravity.contactpicker.picture;





/**
 * Runnable to load a contact picture for a specific ContactBadge.
 */
public class ContactPictureLoader implements Runnable {

    private final String mKey;
    private final SoftReference<ContactBadge> mBadge;
    private final Uri mPhotoUri;
    private final boolean mRoundContactPictures;

    ContactPictureLoader(String key, ContactBadge badge, Uri photoUri, boolean roundContactPictures) {
        mKey = key;
        mBadge = new SoftReference<>(badge);
        mPhotoUri = photoUri;
        mRoundContactPictures = roundContactPictures;
    }

    @Override
    public void run() {
        ContactBadge badge = mBadge.get();
        if (badge == null) return; // fail fast

        Bitmap bitmap = retrievePicture(badge.getContext(), mPhotoUri, mRoundContactPictures);
        if (bitmap != null) {
            ContactPictureLoaded.post(mKey, badge, bitmap);
        }
    }

    public static Bitmap retrievePicture(Context context, Uri photoUri, boolean roundContactPictures) {
        if (context == null || photoUri == null || Helper.isNullOrEmpty(photoUri.toString())) {