com.pdftron.pdf.utils.IconPickerGridViewAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.pdftron.pdf.utils.IconPickerGridViewAdapter.java

Source

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2014 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------
package com.pdftron.pdf.utils;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.pdftron.pdf.tools.R;
import com.pdftron.pdf.tools.Tool;

import java.util.List;

public class IconPickerGridViewAdapter extends ArrayAdapter<String> {
    private Context mContext;
    private String mSelected;
    private List<String> mSource;
    private int mIconColor;

    public IconPickerGridViewAdapter(Context context, List<String> list) {
        super(context, 0, list);

        mContext = context;
        mSelected = "";
        mSource = list;
        mIconColor = 0;
    }

    public int getCount() {
        return mSource.size();
    }

    public String getItem(int position) {
        return mSource.get(position);
    }

    public int getItemIndex(String icon) {
        for (int i = 0; i < mSource.size(); i++) {
            if (icon.equals(mSource.get(i))) {
                return i;
            }
        }
        return -1;
    }

    public String getSelected() {
        return mSelected;
    }

    public void setSelected(int position) {
        if (position > -1) {
            mSelected = getItem(position);
        } else {
            mSelected = "";
        }
        notifyDataSetChanged();
    }

    // create a new ImageView for each item referenced by the Adapter
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            convertView = LayoutInflater.from(mContext).inflate(R.layout.tools_gridview_icon_picker, null);

            RelativeLayout layout = (RelativeLayout) convertView.findViewById(R.id.cell_layout);
            ImageView imageView = (ImageView) convertView.findViewById(R.id.icon_image_view);

            holder = new ViewHolder();
            holder.mIconLayout = layout;
            holder.mIconImage = imageView;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final String iconOutline = Tool.ANNOTATION_NOTE_ICON_FILE_PREFIX + mSource.get(position).toLowerCase()
                + Tool.ANNOTATION_NOTE_ICON_FILE_POSTFIX_OUTLINE;
        final String iconFill = Tool.ANNOTATION_NOTE_ICON_FILE_PREFIX + mSource.get(position).toLowerCase()
                + Tool.ANNOTATION_NOTE_ICON_FILE_POSTFIX_FILL;

        int iconOutlineID = mContext.getResources().getIdentifier(iconOutline, "drawable",
                mContext.getPackageName());
        int iconFillID = mContext.getResources().getIdentifier(iconFill, "drawable", mContext.getPackageName());

        // set selected icon
        if (!mSelected.equals("")) {
            if (mSelected.equals(getItem(position))) {
                Drawable[] layers = new Drawable[2];
                layers[0] = ContextCompat.getDrawable(mContext, iconFillID);
                layers[0].setColorFilter(mIconColor, PorterDuff.Mode.SRC_ATOP);
                layers[1] = ContextCompat.getDrawable(mContext, iconOutlineID);
                LayerDrawable layerDrawable = new LayerDrawable(layers);
                holder.mIconImage.setImageDrawable(layerDrawable);
            } else {
                holder.mIconImage.setImageDrawable(mContext.getResources().getDrawable(iconOutlineID));
            }
        }
        return convertView;
    }

    public void updateIconColor(int color) {
        mIconColor = color;
        notifyDataSetChanged();
    }

    public static class ViewHolder {
        protected RelativeLayout mIconLayout;
        protected ImageView mIconImage;
    }

    /*
    * Overriding unregisterDataSetObserver resolves a bug on ICS where
    * this function is called twice the number of times it should be. This causes the
    * observer to be unregistered twice and throws a "Observer is null" exception when
    * unregistered the second time.
    */
    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        if (observer != null) {
            super.unregisterDataSetObserver(observer);
        }
    }
}