/**
* technoXist
*
* Copyright (c) 2014-2015 Suyash Bhatt
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.technoxist.utils;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.util.LongSparseArray;
import android.util.TypedValue;
import com.technoxist.MainApplication;
public class UiUtils {
static private LongSparseArray<Bitmap> sFaviconCache = new LongSparseArray<Bitmap>();
static public int dpToPixel(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, MainApplication.getContext().getResources().getDisplayMetrics());
}
static public Bitmap getFaviconBitmap(long feedId, Cursor cursor, int iconCursorPos) {
Bitmap bitmap = UiUtils.sFaviconCache.get(feedId);
if (bitmap == null) {
byte[] iconBytes = cursor.getBlob(iconCursorPos);
if (iconBytes != null && iconBytes.length > 0) {
bitmap = UiUtils.getScaledBitmap(iconBytes, 18);
UiUtils.sFaviconCache.put(feedId, bitmap);
}
}
return bitmap;
}
static public Bitmap getScaledBitmap(byte[] iconBytes, int sizeInDp) {
if (iconBytes != null && iconBytes.length > 0) {
Bitmap bitmap = BitmapFactory.decodeByteArray(iconBytes, 0, iconBytes.length);
if (bitmap != null && bitmap.getWidth() != 0 && bitmap.getHeight() != 0) {
int bitmapSizeInDip = UiUtils.dpToPixel(sizeInDp);
if (bitmap.getHeight() != bitmapSizeInDip) {
Bitmap tmp = bitmap;
bitmap = Bitmap.createScaledBitmap(tmp, bitmapSizeInDip, bitmapSizeInDip, false);
tmp.recycle();
}
return bitmap;
}
}
return null;
}
}