fr.eoit.activity.ItemInfo2Activity.java Source code

Java tutorial

Introduction

Here is the source code for fr.eoit.activity.ItemInfo2Activity.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;

import android.content.BroadcastReceiver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
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.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import fr.eoit.EOITConst;
import fr.eoit.R;
import fr.eoit.activity.fragment.actionview.FavoriteActionView;
import fr.eoit.activity.fragment.iteminfo.BlueprintInfoFragment;
import fr.eoit.activity.fragment.iteminfo.PricesFragment;
import fr.eoit.activity.fragment.iteminfo.ProducesFragment;
import fr.eoit.activity.fragment.iteminfo.RefineMaterialListFragment;
import fr.eoit.activity.fragment.util.DefaultStationTips;
import fr.eoit.db.bean.Groups;
import fr.eoit.db.bean.Item;
import fr.eoit.db.bean.Station;
import fr.eoit.parameter.Parameters;
import fr.eoit.parameter.station.Stations;
import fr.eoit.service.updater.PriceUpdaterService;
import fr.eoit.util.IconUtil;
import fr.piconsoft.activity.fragment.util.SimpleOkDialog;
import fr.piconsoft.db.util.DbUtil;

/**
 * @author picon.software
 *
 */
public class ItemInfo2Activity extends LoaderActivity<Cursor> {

    private PricesFragment pricesFragment;
    private BlueprintInfoFragment blueprintInfoFragment;
    private ProducesFragment producesFragment;
    private RefineMaterialListFragment refineMaterialListFragment;

    private PriceChangedBroadCastReceiver receiver;

    private MenuItem favoriteMenuItem;

    private int itemId;
    private int categorieId;
    private int metaGroupId;
    private boolean favorite, isItemIconExpended = false;
    private double volume;
    private String itemName;

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.item_info);

        receiver = new PriceChangedBroadCastReceiver();

        setActionBarInderterminate(true);

        itemId = (int) ContentUris.parseId(getIntent().getData());

        initOrRestart();

        pricesFragment = (PricesFragment) getSupportFragmentManager().findFragmentById(R.id.PRICES_FRAGMENT);
        blueprintInfoFragment = (BlueprintInfoFragment) getSupportFragmentManager()
                .findFragmentById(R.id.BLUEPRINT_FRAGMENT);
        producesFragment = (ProducesFragment) getSupportFragmentManager().findFragmentById(R.id.PRODUCE_FRAGMENT);
        refineMaterialListFragment = (RefineMaterialListFragment) getSupportFragmentManager()
                .findFragmentById(R.id.REFINE_FRAGMENT);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(false);

        if (findViewById(R.id.ITEM_ICON_LAYOUT) != null) {
            findViewById(R.id.ITEM_ICON_LAYOUT).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (isItemIconExpended) {
                        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
                                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150,
                                        getResources().getDisplayMetrics()));

                        v.setLayoutParams(layoutParams);
                        isItemIconExpended = false;
                    } else {
                        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
                                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250,
                                        getResources().getDisplayMetrics()));
                        v.setLayoutParams(layoutParams);
                        isItemIconExpended = true;
                    }
                }
            });

            DefaultStationTips dialog = new DefaultStationTips(this);
            if (Stations.getProductionStation().stationId == EOITConst.Stations.JITA_STATION_ID
                    && Stations.getTradeStation().stationId == EOITConst.Stations.JITA_STATION_ID
                    && dialog.isActive(this)) {
                dialog.show(getSupportFragmentManager(), "default_station");
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.iteminfomenu, menu);
        favoriteMenuItem = menu.findItem(R.id.FAVORITE_OPTION);
        if (favoriteMenuItem != null && favoriteMenuItem.getActionView() == null) {
            favoriteMenuItem.setActionView(new FavoriteActionView(getApplicationContext(), itemId, favorite));
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent;
        switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; go home
            intent = new Intent(this, ItemListActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        case R.id.MANUFACTURE_OPTION:
            intent = new Intent(null, ContentUris.withAppendedId(Item.CONTENT_ID_URI_BASE, itemId), this,
                    ManufactureActivity.class);
            if (pricesFragment != null)
                intent.putExtras(pricesFragment.getPricesInfos());
            if (blueprintInfoFragment != null)
                intent.putExtras(blueprintInfoFragment.getBlueprintInfos());
            intent.putExtra("itemId", itemId);
            intent.putExtra("metaGroupId", metaGroupId);
            intent.putExtra("categorieId", categorieId);
            intent.putExtra("itemName", itemName);
            intent.putExtra("volume", volume);
            startActivity(intent);
            break;
        case R.id.PARAMETERS_OPTION:
            final Intent paramIntent = new Intent(this, ParameterActivity.class);
            startActivity(paramIntent);
            break;
        case R.id.REFRESH_OPTION:
            setActionBarInderterminate(true);
            PriceUpdaterService.launchDeepFullPriceUpdateForItemId(itemId, getBaseContext());
            break;
        case R.id.HELP_OPTION:
            SimpleOkDialog<?> dialog = new SimpleOkDialog<Object>(R.string.item_info_activity_name,
                    R.layout.help_item_info);
            dialog.show(getSupportFragmentManager(), "helpDialog");
            break;
        case R.id.STATIONS_PRICES_OPTION:
            intent = new Intent(null,
                    ContentUris.withAppendedId(Station.CONTENT_ID_FAVORITE_STATIONS_PRICES_URI_BASE, itemId), this,
                    LocationPricesActivity.class);
            startActivity(intent);
        default:
            break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();

        registerReceiver(receiver, new IntentFilter(PriceUpdaterService.PRICE_CHANGED_ACTION));

        if (favoriteMenuItem != null && favoriteMenuItem.getActionView() == null) {
            favoriteMenuItem.setActionView(new FavoriteActionView(getApplicationContext(), itemId, favorite));
        }

        setActionBarInderterminate(false);
    }

    @Override
    protected void onPause() {
        super.onPause();

        unregisterReceiver(receiver);
    }

    @Override
    public Loader<Cursor> getCursorLoader(int id, Bundle args) {
        return new CursorLoader(this, getIntent().getData(),
                new String[] { Item._ID, Item.COLUMN_NAME_NAME, Item.COLUMN_NAME_VOLUME,
                        Item.COLUMN_NAME_CHOSEN_PRICE_ID, Item.COLUMN_NAME_FAVORITE, Item.COLUMN_NAME_META_GROUP_ID,
                        Item.COLUMN_NAME_GROUP_ID, Groups.COLUMN_NAME_CATEGORIE_ID },
                null, null, null);
    }

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

            itemId = cursor.getInt(cursor.getColumnIndexOrThrow(Item._ID));
            categorieId = cursor.getInt(cursor.getColumnIndexOrThrow(Groups.COLUMN_NAME_CATEGORIE_ID));
            favorite = cursor.getInt(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_FAVORITE)) > 0;
            metaGroupId = cursor.getInt(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_META_GROUP_ID));
            int groupId = cursor.getInt(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_GROUP_ID));
            volume = cursor.getDouble(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_VOLUME));

            itemName = cursor.getString(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_NAME));

            setActionBarTitle(itemName);

            trackEvent("Event", "Item view", itemName, (int) Parameters.characterId);

            int choosen_price_id = cursor.getInt(cursor.getColumnIndexOrThrow(Item.COLUMN_NAME_CHOSEN_PRICE_ID));
            switch (choosen_price_id) {
            case EOITConst.BUY_PRICE_ID:
                setActionBarSubtitle(R.string.item_buy_desc);
                break;
            case EOITConst.SELL_PRICE_ID:
                setActionBarSubtitle(R.string.item_sell_desc);
                break;
            case EOITConst.PRODUCE_PRICE_ID:
                setActionBarSubtitle(R.string.item_prod_desc);
                break;

            default:
                break;
            }

            IconUtil.initIconOrRender(itemId, categorieId, groupId, (ImageView) findViewById(R.id.ITEM_ICON));

            pricesFragment.setItemId(itemId);
            if (categorieId == EOITConst.Categories.PLANETARY_COMMODITIES_CATEGORIE_ID
                    || categorieId == EOITConst.Categories.COMMODITY_CATEGORIE_ID
                            && groupId == EOITConst.Groups.GENERAL_COMMODITY_GROUP_ID) {
                producesFragment.getView().setVisibility(View.GONE);
            } else {
                producesFragment.setItemId(itemId);
            }
            if (categorieId == EOITConst.Categories.ASTEROID_CATEGORIE_ID) {
                refineMaterialListFragment.setCategoryId(categorieId);
                refineMaterialListFragment.setItemId(itemId);
            } else {
                refineMaterialListFragment.getView().setVisibility(View.GONE);
            }
            blueprintInfoFragment.setGroupId(groupId);
            blueprintInfoFragment.setMetaGroupId(metaGroupId);
            blueprintInfoFragment.setCategorieId(categorieId);
            blueprintInfoFragment.setItemName(itemName);
            blueprintInfoFragment.setItemId(itemId);

            if (favoriteMenuItem != null) {
                favoriteMenuItem.setActionView(new FavoriteActionView(getApplicationContext(), itemId, favorite));
            }

            setActionBarInderterminate(false);
        }
    }

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

    private class PriceChangedBroadCastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            boolean priceFound = intent.getBooleanExtra(PriceUpdaterService.PRICE_FOUND, false);
            boolean error = intent.getBooleanExtra(PriceUpdaterService.ERROR, false);

            if (!priceFound && !error) {
                Toast.makeText(context, R.string.no_price_found, Toast.LENGTH_SHORT).show();
            }

            if (error) {
                Toast.makeText(context, R.string.price_error, Toast.LENGTH_LONG).show();
            }
        }
    }

    /**
     * @return the pricesInfos
     */
    public Bundle getPricesInfos() {
        return pricesFragment.getPricesInfos();
    }
}