fr.eoit.activity.fragment.blueprint.RequiredSkillInventionFragment.java Source code

Java tutorial

Introduction

Here is the source code for fr.eoit.activity.fragment.blueprint.RequiredSkillInventionFragment.java

Source

/**
 * Copyright (C) 2012 Picon software
 *
 * 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 2 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, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package fr.eoit.activity.fragment.blueprint;

import android.content.ContentUris;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import fr.eoit.EOITConst;
import fr.eoit.R;
import fr.piconsoft.activity.fragment.LoaderFragment;
import fr.eoit.db.bean.InventionMapping;
import fr.eoit.db.bean.Item;
import fr.eoit.parameter.skill.Skills;
import fr.piconsoft.db.util.DbUtil;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author picon.software
 *
 */
public class RequiredSkillInventionFragment extends LoaderFragment<Cursor> {

    private static final String[] FROM = { InventionMapping.COLUMN_NAME_LEARNT, Item.COLUMN_NAME_NAME,
            InventionMapping.COLUMN_NAME_SKILL_LEVEL };
    private static final int[] TO = { R.id.SKILL_CHK_OK, R.id.ITEM_NAME, R.id.SKILL_LEVEL_ICON };

    private WeakReference<InventionFragment> fragmentReference;
    private long parentTypeId;

    /**
     * @param parentTypeId the parentTypeId to set
     */
    public void setParentTypeId(long parentTypeId) {
        this.parentTypeId = parentTypeId;
        initOrRestart();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View fragment = inflater.inflate(R.layout.blueprint_required_skills, container, false);

        return fragment;
    }

    @Override
    public Loader<Cursor> getCursorLoader(int id, Bundle args) {
        if (id == LOADER_ID) {
            return new CursorLoader(getActivity(),
                    ContentUris.withAppendedId(InventionMapping.CONTENT_INVENTION_SKILL_ID_URI_BASE, parentTypeId),
                    new String[] { Item.COLUMN_NAME_NAME, InventionMapping.COLUMN_NAME_SKILL_ID }, null, null,
                    "inv." + InventionMapping.COLUMN_NAME_SKILL_ID + " ASC");
        }
        return null;
    }

    @Override
    public void onLoadFinished(Cursor cursor) {
        if (DbUtil.hasAtLeastOneRow(cursor)) {
            short encryptionSkillLevel = -1;
            int encryptionSkillId = -1;
            short datacore1SkillLevel = -1;
            short datacore2SkillLevel = -1;

            List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

            boolean datacore1SkillSet = false;

            while (!cursor.isAfterLast()) {
                Map<String, Object> dataLine = new HashMap<String, Object>();

                int skillId = cursor.getInt(cursor.getColumnIndexOrThrow(InventionMapping.COLUMN_NAME_SKILL_ID));
                String skillName = cursor.getString(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_NAME));
                int skillLevel = 1;

                short userSkillLevel = Skills.getSkill(skillId);
                if (Skills.isEncryptionSkill(skillId)) {
                    encryptionSkillLevel = userSkillLevel;
                    encryptionSkillId = skillId;
                } else {
                    if (datacore1SkillSet) {
                        datacore2SkillLevel = userSkillLevel;
                    } else {
                        datacore1SkillLevel = userSkillLevel;
                        datacore1SkillSet = true;
                    }
                }

                boolean learnt = userSkillLevel >= skillLevel;

                dataLine.put(InventionMapping.COLUMN_NAME_LEARNT, learnt);
                dataLine.put(Item.COLUMN_NAME_NAME, skillName);
                dataLine.put(InventionMapping.COLUMN_NAME_SKILL_LEVEL, userSkillLevel);
                data.add(dataLine);

                cursor.moveToNext();
            }

            SimpleAdapter adapter = new SimpleAdapter(getActivity(), data, R.layout.required_skills_row, FROM, TO);

            adapter.setViewBinder(new RequiredSkillsViewBinder());
            ListView listView = (ListView) getView().findViewById(R.id.REQUIRED_SKILLS_LIST);
            listView.setAdapter(adapter);
            int count = cursor.getCount();
            int width = count * ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 26,
                    getResources().getDisplayMetrics()));
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    width);
            listView.setLayoutParams(lp);

            if (fragmentReference != null && fragmentReference.get() != null) {
                fragmentReference.get().updateInventionChances(encryptionSkillLevel, datacore1SkillLevel,
                        datacore2SkillLevel);
                fragmentReference.get().setEncryptionSkillId(encryptionSkillId);
            }
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
    }

    public void setParentFragment(InventionFragment fragment) {
        fragmentReference = new WeakReference<InventionFragment>(fragment);
    }

    private class RequiredSkillsViewBinder implements SimpleAdapter.ViewBinder {

        @Override
        public boolean setViewValue(View view, Object data, String textRepresentation) {
            int viewId = view.getId();

            switch (viewId) {
            case R.id.SKILL_CHK_OK:
                boolean learnt = (Boolean) data;
                ImageView skillCheckImageView = (ImageView) view;
                if (learnt) {
                    skillCheckImageView.setImageResource(R.drawable.icon_ok);
                } else {
                    skillCheckImageView.setImageResource(R.drawable.icon_ko);
                }
                break;

            case R.id.ITEM_NAME:
                TextView textView = (TextView) view;
                textView.setText(String.valueOf(data));
                break;

            case R.id.SKILL_LEVEL_ICON:
                ImageView skillLevelImageView = (ImageView) view;
                short userSkillLevel = (Short) data;
                skillLevelImageView.setImageResource(EOITConst.skillIconsResourceIds[userSkillLevel]);
                break;

            }

            return true;
        }
    }

}