Java tutorial
/** * 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.iteminfo; 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.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import fr.eoit.EOITConst; import fr.eoit.R; import fr.eoit.activity.fragment.iteminfo.PricesDialog.PricesDialogOnLongClickListener; import fr.eoit.activity.listener.PriceRadiogroupOnCheckedChangeListener; import fr.eoit.activity.util.PricesUtils; import fr.eoit.activity.util.ViewUtil; import fr.eoit.db.bean.Groups; import fr.eoit.db.bean.Item; import fr.eoit.db.bean.Prices; import fr.eoit.parameter.Parameters; import fr.eoit.service.updater.PriceUpdaterService; import fr.piconsoft.activity.fragment.LoaderFragment; import fr.piconsoft.db.util.DbUtil; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.Date; /** * @author picon.software * */ public class PricesFragment extends LoaderFragment<Cursor> { private static final NumberFormat nfPercent = new DecimalFormat("##0.#%"); private int itemId; private Bundle pricesInfos = new Bundle(); /** * @return the itemId */ public int getItemId() { return itemId; } /** * @param itemId the itemId to set */ public void setItemId(int itemId) { this.itemId = itemId; initOrRestart(); } /** * @return the pricesInfos */ public Bundle getPricesInfos() { return pricesInfos; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.item_info_prices, container, false); } @Override public void onResume() { super.onResume(); if (itemId > 0) { initOrRestart(); } } @Override public Loader<Cursor> getCursorLoader(int id, Bundle args) { return new CursorLoader(getActivity(), ContentUris.withAppendedId(Item.CONTENT_ID_URI_BASE, itemId), new String[] { Item._ID, Item.COLUMN_NAME_CHOSEN_PRICE_ID, Prices.COLUMN_NAME_BUY_PRICE, Prices.COLUMN_NAME_OWN_PRICE, Prices.COLUMN_NAME_SELL_PRICE, Prices.COLUMN_NAME_PRODUCE_PRICE, Prices.COLUMN_NAME_LAST_UPDATE, Groups.COLUMN_NAME_CATEGORIE_ID }, null, null, null); } @Override public void onLoadFinished(Cursor cursor) { if (DbUtil.hasAtLeastOneRow(cursor)) { int chosenPriceId = cursor.getInt(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_CHOSEN_PRICE_ID)); pricesInfos = new Bundle(); double buyPrice = PricesUtils.getPriceOrNaN(cursor, Prices.COLUMN_NAME_BUY_PRICE); double sellPrice = PricesUtils.getPriceOrNaN(cursor, Prices.COLUMN_NAME_SELL_PRICE); double producePrice = PricesUtils.getPriceOrNaN(cursor, Prices.COLUMN_NAME_PRODUCE_PRICE); double fixedPrice = PricesUtils.getPriceOrNaN(cursor, Prices.COLUMN_NAME_OWN_PRICE); long lastUpdate = cursor.getLong(cursor.getColumnIndexOrThrow(Prices.COLUMN_NAME_LAST_UPDATE)); pricesInfos.putDouble("buyPrice", buyPrice); pricesInfos.putDouble("sellPrice", sellPrice); pricesInfos.putDouble("producePrice", producePrice); pricesInfos.putInt("chosenPriceId", chosenPriceId); RadioButton bTv = ((RadioButton) getView().findViewById(R.id.buy_price)); RadioButton sTv = ((RadioButton) getView().findViewById(R.id.sell_price)); RadioButton pTv = ((RadioButton) getView().findViewById(R.id.produce_price)); RadioButton fTv = ((RadioButton) getView().findViewById(R.id.fixed_price)); TextView prTv = ((TextView) getView().findViewById(R.id.profit)); prTv.setVisibility(View.VISIBLE); PricesUtils.setPrice(bTv, buyPrice, true); PricesUtils.setPrice(sTv, sellPrice, true); PricesUtils.setPrice(pTv, producePrice, true); PricesUtils.setPrice(fTv, fixedPrice, true); if (!Double.isNaN(fixedPrice) && fixedPrice != 0 && chosenPriceId != EOITConst.SELL_PRICE_ID && chosenPriceId != EOITConst.BUY_PRICE_ID) { ViewUtil.hide(bTv, sTv); ViewUtil.show(fTv); } else { ViewUtil.show(bTv, sTv); ViewUtil.hide(fTv); } if (Double.isNaN(buyPrice) && Double.isNaN(sellPrice)) { ViewUtil.show(fTv); } double profit = 0; if (!Double.isNaN(fixedPrice) && fixedPrice != 0 && producePrice > 0 && !Double.isNaN(producePrice)) { profit = (fixedPrice - producePrice) / producePrice; } else if (sellPrice > 0 && !Double.isNaN(sellPrice) && producePrice > 0 && !Double.isNaN(producePrice)) { profit = (sellPrice - producePrice) / producePrice; } else { prTv.setVisibility(View.GONE); } prTv.setText(nfPercent.format(profit)); pricesInfos.putDouble("profit", profit); if (producePrice < 0.01 || Double.isNaN(producePrice)) { pTv.setVisibility(View.GONE); } else { pTv.setVisibility(View.VISIBLE); } switch (chosenPriceId) { case EOITConst.BUY_PRICE_ID: bTv.setBackgroundResource(R.drawable.bg_price_selected); sTv.setBackgroundResource(R.drawable.bg_selector); pTv.setBackgroundResource(R.drawable.bg_selector); fTv.setBackgroundResource(R.drawable.bg_selector); break; case EOITConst.SELL_PRICE_ID: sTv.setBackgroundResource(R.drawable.bg_price_selected); bTv.setBackgroundResource(R.drawable.bg_selector); pTv.setBackgroundResource(R.drawable.bg_selector); fTv.setBackgroundResource(R.drawable.bg_selector); break; case EOITConst.PRODUCE_PRICE_ID: pTv.setBackgroundResource(R.drawable.bg_price_selected); bTv.setBackgroundResource(R.drawable.bg_selector); sTv.setBackgroundResource(R.drawable.bg_selector); fTv.setBackgroundResource(R.drawable.bg_selector); break; case EOITConst.OWN_PRICE_ID: pTv.setBackgroundResource(R.drawable.bg_selector); bTv.setBackgroundResource(R.drawable.bg_selector); sTv.setBackgroundResource(R.drawable.bg_selector); fTv.setBackgroundResource(R.drawable.bg_price_selected); break; default: break; } RadioGroup priceRadioGroup = (RadioGroup) getView().findViewById(R.id.PRICE_RADIO_GROUP); priceRadioGroup.setOnCheckedChangeListener(new PriceRadiogroupOnCheckedChangeListener(itemId)); PricesDialogOnLongClickListener listener = new PricesDialogOnLongClickListener(itemId, getFragmentManager()); bTv.setOnLongClickListener(listener); sTv.setOnLongClickListener(listener); pTv.setOnLongClickListener(listener); fTv.setOnLongClickListener(listener); // if price was updated more than 6h ago launch a full update if ((new Date().getTime() - lastUpdate) > (6 * 60 * 60 * 1000)) { // Update prices Parameters.validateStationParams(getActivity().getApplicationContext()); PriceUpdaterService.launchDeepFullPriceUpdateForItemId(itemId, getActivity().getApplicationContext()); } } } @Override public void onLoaderReset(Loader<Cursor> loader) { } }