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

Java tutorial

Introduction

Here is the source code for fr.eoit.activity.fragment.blueprint.BlueprintInfoFragment.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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import fr.eoit.R;
import fr.eoit.activity.util.BlueprintUtil;
import fr.eoit.activity.util.PricesUtils;
import fr.eoit.db.bean.Blueprint;
import fr.eoit.formula.FormulaCalculator;
import fr.eoit.util.Formatter;
import fr.piconsoft.activity.fragment.LoaderFragment;
import fr.piconsoft.db.util.DbUtil;

import java.text.DecimalFormat;
import java.text.NumberFormat;

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

    private final static NumberFormat nfPercent = new DecimalFormat("##0.##%");

    private long blueprintId = -1;
    //private ItemInvestFragment investFragment;

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

        blueprintId = getArguments().getInt("blueprintId");

        initOrRestart();

        return fragment;
    }

    @Override
    protected Loader<Cursor> getCursorLoader(int id, Bundle args) {
        return new CursorLoader(getActivity(),
                ContentUris.withAppendedId(Blueprint.CONTENT_ID_URI_BASE, blueprintId),
                new String[] { Blueprint._ID, Blueprint.COLUMN_NAME_NAME, Blueprint.COLUMN_NAME_ML,
                        Blueprint.COLUMN_NAME_PL, Blueprint.COLUMN_NAME_PRODUCE_ITEM_ID,
                        Blueprint.COLUMN_NAME_UNIT_PER_BATCH, Blueprint.COLUMN_NAME_PRODUCTION_TIME,
                        Blueprint.COLUMN_NAME_PRODUCTIVITY_MODIFIER, Blueprint.COLUMN_NAME_TECH_LEVEL,
                        Blueprint.COLUMN_NAME_MAX_PRODUCTION_LIMIT, Blueprint.COLUMN_NAME_RESEARCH_PRICE,
                        Blueprint.COLUMN_NAME_PARENT_TYPE_ID, Blueprint.COLUMN_NAME_DECRYPTOR_ID,
                        Blueprint.COLUMN_NAME_RESEARCH_COPY_TIME },
                null, null, null);
    }

    @Override
    public void onLoadFinished(Cursor data) {
        if (DbUtil.hasAtLeastOneRow(data)) {

            int metaGroupId = getArguments().getInt("metaGroupId", -1);
            Bundle blueprintBundle = BlueprintUtil.getBlueprintBundle(data, metaGroupId, true, false);

            int ml = blueprintBundle.getInt("ml");
            int pl = blueprintBundle.getInt("pl");
            int numberOfRuns = blueprintBundle.getInt("numberOfRuns", 0);
            int unitPerBatch = blueprintBundle.getInt("unitPerBatch", 0);

            float baseProductionTime = data
                    .getFloat(data.getColumnIndexOrThrow(Blueprint.COLUMN_NAME_PRODUCTION_TIME));
            float productivityModifier = data
                    .getFloat(data.getColumnIndexOrThrow(Blueprint.COLUMN_NAME_PRODUCTIVITY_MODIFIER));
            int baseCopyTime = data.getInt(data.getColumnIndexOrThrow(Blueprint.COLUMN_NAME_RESEARCH_COPY_TIME));

            float wasteFactor = FormulaCalculator.calculateWasteFactor(ml);
            double productionTime = FormulaCalculator.calculateProductionTime(baseProductionTime,
                    productivityModifier, pl);

            double copyPrice = FormulaCalculator.calculateMaxRunsCopyCost(baseCopyTime, 1000, 4343, 1.5F, 0.5F);
            double copyTime = FormulaCalculator.calculateMaxRunsCopyTime(baseCopyTime);

            ((TextView) getView().findViewById(R.id.ML)).setText(Integer.toString(ml));
            ((TextView) getView().findViewById(R.id.PL)).setText(Integer.toString(pl));
            ((TextView) getView().findViewById(R.id.MAX_RUNS)).setText(Integer.toString(numberOfRuns));
            ((TextView) getView().findViewById(R.id.UNIT_PER_BATCH)).setText(Integer.toString(unitPerBatch));

            ((TextView) getView().findViewById(R.id.WASTE_FACTOR)).setText(nfPercent.format(wasteFactor));
            ((TextView) getView().findViewById(R.id.PRODUCTION_TIME)).setText(Formatter.formatTime(productionTime));
            ((TextView) getView().findViewById(R.id.RESEARCH_COPY_COST))
                    .setText(PricesUtils.formatPrice(copyPrice, getActivity(), true));
            ((TextView) getView().findViewById(R.id.RESEARCH_COPY_TIME)).setText(Formatter.formatTime(copyTime));

            /*
            investFragment =
                  (ItemInvestFragment) getFragmentManager().findFragmentById(R.id.INVEST_ITEMS_CONTAINER);
            investFragment.setItemId(getArguments().getInt("produceItemId"));
            */
        }
    }

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

}