com.carlrice.reader.utils.UiUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.carlrice.reader.utils.UiUtils.java

Source

/**
 * Flym
 *
 * Copyright (c) 2012-2013 Frederic Julian
 *
 * 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.carlrice.reader.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.util.LongSparseArray;
import android.support.v7.app.ActionBar;
import android.support.v7.graphics.Palette;
import android.util.TypedValue;
import android.view.View;
import android.widget.ListView;

import com.carlrice.reader.Application;

import com.carlrice.reader.R;

import static android.support.v7.graphics.Palette.*;

public class UiUtils {

    static private final LongSparseArray<Bitmap> FAVICON_CACHE = new LongSparseArray<>();
    static private final LongSparseArray<Palette> PALETTE_CACHE = new LongSparseArray<>();

    static public void setPreferenceTheme(Activity a) {
        if (!PrefUtils.getBoolean(PrefUtils.LIGHT_THEME, true)) {
            a.setTheme(R.style.Theme_Dark);
        }
    }

    static public int dpToPixel(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                Application.context().getResources().getDisplayMetrics());
    }

    static public Bitmap getFaviconBitmap(long feedId, Cursor cursor, int iconCursorPos) {
        Bitmap bitmap = UiUtils.FAVICON_CACHE.get(feedId);
        if (bitmap == null) {
            byte[] iconBytes = cursor.getBlob(iconCursorPos);
            if (iconBytes != null && iconBytes.length > 0) {
                bitmap = UiUtils.getScaledBitmap(iconBytes, 18);
                UiUtils.FAVICON_CACHE.put(feedId, bitmap);
            }
        }
        return bitmap;
    }

    static public void getFaviconPalette(final Bitmap bitmap, final PaletteAsyncListener listener) {
        if (bitmap != null) {
            Palette palette = UiUtils.PALETTE_CACHE.get(bitmap.hashCode());
            if (palette != null) {
                if (listener != null) {
                    listener.onGenerated(palette);
                }
            } else {
                Palette.generateAsync(bitmap, new PaletteAsyncListener() {
                    @Override
                    public void onGenerated(Palette palette) {
                        UiUtils.PALETTE_CACHE.put(bitmap.hashCode(), palette);
                        if (listener != null) {
                            listener.onGenerated(palette);
                        }
                    }
                });
            }
        }
    }

    static public void getFaviconPalette(final long feedId, Cursor cursor, int iconCursorPos,
            PaletteAsyncListener listener) {
        Bitmap bitmap = getFaviconBitmap(feedId, cursor, iconCursorPos);
        UiUtils.getFaviconPalette(bitmap, listener);
    }

    static public void setActionBarColors(ActionBar supportActionBar, Palette palette) {
        int color = Application.color(R.color.light_theme_color_primary);
        int darkColor = Application.color(R.color.light_theme_color_primary_dark);
        if (palette != null) {
            color = palette.getVibrantColor(color);
            darkColor = palette.getDarkVibrantColor(darkColor);
        }
        supportActionBar.setBackgroundDrawable(new ColorDrawable(color));
    }

    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;
    }

    static public void addEmptyFooterView(ListView listView, int dp) {
        View view = new View(listView.getContext());
        view.setMinimumHeight(dpToPixel(dp));
        view.setClickable(true);
        listView.addFooterView(view);
    }

    static public int getAttrResource(Context context, int attrId, int defValue) {
        TypedArray a = context.getTheme().obtainStyledAttributes(new int[] { attrId });
        int result = a.getResourceId(0, defValue);
        a.recycle();
        return result;
    }
}