Back to project page MythTrack.
The source code is released under:
MIT License
If you think the Android project MythTrack listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** * HeroTracker allow the user to view and modify their hero's stats and equipment. */*from www. ja v a 2s . c o m*/ * @author Nolan Jurgens */ package nolanjurgens.mythtrack.app; // IMPORTS ///////////////////////////////////////////////////////////////////////////////////////// import android.app.Activity; import android.app.DialogFragment; import android.app.FragmentTransaction; import android.content.ContentValues; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import nolanjurgens.mythtrack.R; import nolanjurgens.mythtrack.provider.MythTrackContract; //////////////////////////////////////////////////////////////////////////////////////////////////// // CLASS - HeroTrackerActivity // //////////////////////////////////////////////////////////////////////////////////////////////////// public class HeroTrackerActivity extends Activity implements BuyItemDialog.OnItemBoughtListener, EquipmentInfoDialog.OnHeroRequestedListener, HeroDisplayFragment.OnHeroLoadedListener, InventoryList.OnItemEquippedListener, InventoryList.OnPotionUsedListener, ItemList.OnItemAddedListener, RemoveItemDialog.OnItemRemovedListener, StatPickerDialog.OnStatSetListener { // ASYNCTASKS //////////////////////////////////////////////////////////////////////////////////// // Add an item to a hero's inventory private class addInventoryItem extends AsyncTask<Long, Void, Long> { /** * Add an item to a hero's inventory * @param ids A hero ID, followed by an item ID. * @return The ID of the new inventory item. */ @Override protected Long doInBackground(Long... ids) { // Check that we have two IDs (hero & inventory item) if(ids.length != 2) { return 0l; } // Build the new entry. ContentValues inventoryItem = new ContentValues(); inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, ids[0]); inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, ids[1]); // Insert it into the inventory table. Uri inventoryURI = getContentResolver().insert(MythTrackContract.Inventories.CONTENT_URI, inventoryItem); return Long.parseLong(inventoryURI.getLastPathSegment()); } } // Remove an item from a hero's inventory private class deleteInventoryItem extends AsyncTask<Uri, Void, Boolean> { /** * Delete the item from the hero's inventory * @param uri URI of item to delete. * @return True if the item was deleted. */ @Override protected Boolean doInBackground(Uri... uri) { int itemsDeleted = getContentResolver().delete(uri[0], null, null); return (itemsDeleted == 1); } } // METHODS /////////////////////////////////////////////////////////////////////////////////////// /** * Determines if the activity is running in dual pane mode. * @return True if the activity is in dual pane mode. */ public boolean checkForDualPaneMode() { // See if the dedicated HeroDisplay container is present and visible. View heroDisplayContainer = findViewById(R.id.herotracker_herodisplay_container); return (heroDisplayContainer != null && heroDisplayContainer.getVisibility() == View.VISIBLE); } /** * Called when the activity is launched. * @param savedInstanceState Saved instance information. */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set the user interface. setContentView(R.layout.activity_hero_tracker); // Don't add fragments if resuming from a previous state. if(savedInstanceState != null) { return; } // Create the HeroDisplay fragment. HeroDisplayFragment heroDisplayFragment = new HeroDisplayFragment(); // Pass along the intent that contains the hero ID. heroDisplayFragment.setArguments(getIntent().getExtras()); // Add the HeroDisplay fragment to the user interface. if(checkForDualPaneMode()) { FragmentTransaction heroDisplayTransaction = getFragmentManager().beginTransaction(); heroDisplayTransaction.add(R.id.herotracker_herodisplay_container, heroDisplayFragment, "HeroDisplayFragment"); heroDisplayTransaction.commit(); // Also add the EquipmentInfo dialog to the right pane. DialogFragment equipmentInfoDialog = new EquipmentInfoDialog(); FragmentTransaction equipmentInfoTransaction = getFragmentManager().beginTransaction(); equipmentInfoTransaction.add(R.id.herotracker_fragment_container, equipmentInfoDialog, "EquipmentInfoDialog"); equipmentInfoTransaction.commit(); } else { FragmentTransaction heroDisplayTransaction = getFragmentManager().beginTransaction(); heroDisplayTransaction.add(R.id.herotracker_fragment_container, heroDisplayFragment, "HeroDisplayFragment"); heroDisplayTransaction.commit(); } } /** * Add items to the action bar. * @param menu The menu that will be populated. * @return Boolean value for if the menu should be shown. */ @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the resource to create the menu. MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.hero_tracker, menu); return super.onCreateOptionsMenu(menu); } /** * Refreshes the EquipmentInfoDialog once the hero has finished loading when in dual pane mode. */ @Override public void onHeroLoaded() { // Get a handle to the equipment info fragment. EquipmentInfoDialog equipmentInfoFragment = (EquipmentInfoDialog) getFragmentManager().findFragmentByTag("EquipmentInfoDialog"); if(equipmentInfoFragment != null) { equipmentInfoFragment.refreshView(); } } /** * Gets the hero from the HeroDisplay fragment. * @return The hero from HeroDisplay */ @Override public Hero onHeroRequested() { // Get a handle to the hero display. HeroDisplayFragment heroDisplayFragment = (HeroDisplayFragment) getFragmentManager().findFragmentByTag("HeroDisplayFragment"); return heroDisplayFragment.getActiveHero(); } /** * Adds the item with the given ID to the inventory of the hero with the given ID. * @param heroID The ID of the hero who is having the item added to their inventory. * @param itemID The ID of the item being added to the inventory. */ @Override public void onItemAdded(long heroID, long itemID) { new addInventoryItem().execute(heroID, itemID); getFragmentManager().popBackStack(); } /** * Adds the item with the given ID to the inventory of the hero with the given ID and subtracts * the cost of the item from the hero's gold. * @param heroID The ID of the hero purchasing the item. * @param itemID The ID of the item being purchased. * @param itemCost The cost of the item being purchased. */ @Override public void onItemBought(long heroID, long itemID, int itemCost) { // Get a handle to the hero display. HeroDisplayFragment heroDisplayFragment = (HeroDisplayFragment) getFragmentManager().findFragmentByTag("HeroDisplayFragment"); // Check that the hero has enough gold. boolean enoughGold = heroDisplayFragment.checkForAvailableGold(itemCost); // Purchase the item if the hero has enough gold. if(enoughGold) { // Remove the purchase amount from the hero's gold. heroDisplayFragment.onItemPurchased(itemCost); // Refresh the hero display now if in dual pane mode. if(checkForDualPaneMode()) { heroDisplayFragment.refreshView(); } // Add the item to the hero's inventory. ContentValues inventoryItem = new ContentValues(); inventoryItem.put(MythTrackContract.Inventories.COLUMN_HERO_ID, heroID); inventoryItem.put(MythTrackContract.Inventories.COLUMN_ITEM_ID, itemID); getContentResolver().insert(MythTrackContract.Inventories.CONTENT_URI, inventoryItem); } getFragmentManager().popBackStack(); } /** * Equips the specified inventory item in the hero's specified slot. * @param inventoryID The inventory ID of the item being equipped. * @param type The type of item being equipped. */ @Override public void onItemEquipped(long inventoryID, int type) { // Get a handle to the hero display. HeroDisplayFragment heroDisplayFragment = (HeroDisplayFragment) getFragmentManager().findFragmentByTag("HeroDisplayFragment"); // Update the hero equipment via the hero display fragment. heroDisplayFragment.onItemEquipped(inventoryID, type); // Refresh the hero display now if in dual pane mode. if(checkForDualPaneMode()) { heroDisplayFragment.refreshView(); } // Go back to hero display. getFragmentManager().popBackStack(); } /** * Deletes the inventory entry with the given ID. * @param inventoryID The ID the inventory entry being deleted. * @param goldValue The amount of gold received if sale goes through. */ @Override public void onItemRemoved(long inventoryID, int goldValue) { // Get a handle to the hero display. HeroDisplayFragment heroDisplayFragment = (HeroDisplayFragment) getFragmentManager().findFragmentByTag("HeroDisplayFragment"); // Remove the item from the hero's equipped items if necessary. boolean deleteOkay = heroDisplayFragment.checkForDeletedEquipment(inventoryID); // Delete the item, as long as it won't kill the hero. if(deleteOkay) { // Add sale value to hero's gold. heroDisplayFragment.onItemSold(goldValue); // Refresh the hero display now if in dual pane mode. if(checkForDualPaneMode()) { heroDisplayFragment.refreshView(); } // Get a URI to the inventory entry. Uri inventoryURI = Uri.withAppendedPath(MythTrackContract.Inventories.CONTENT_URI, Long.toString(inventoryID)); // Delete the entry. new deleteInventoryItem().execute(inventoryURI); } } /** * Handle a selected action button. * @param item The item that was selected. * @return Returns true to signal item was processed. */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()) { default: { return super.onOptionsItemSelected(item); } } } /** * Use the given potion on the hero. * @param inventoryID The inventory ID of the potion used. * @param potionType The potion ID of the potion used. */ @Override public void onPotionUsed(long inventoryID, int potionType, String potionName) { // Get a handle to the hero display. HeroDisplayFragment heroDisplayFragment = (HeroDisplayFragment) getFragmentManager().findFragmentByTag("HeroDisplayFragment"); // Have the hero try to use the potion. if(heroDisplayFragment.onPotionUsed(potionType, potionName)) { // If used, delete the potion from the inventory. // Get a URI to the inventory entry. Uri inventoryURI = Uri.withAppendedPath(MythTrackContract.Inventories.CONTENT_URI, Long.toString(inventoryID)); // Delete the entry. new deleteInventoryItem().execute(inventoryURI); // Refresh the hero display now if in dual pane mode. if(checkForDualPaneMode()) { heroDisplayFragment.refreshView(); } } // Go back to hero display. getFragmentManager().popBackStack(); } /** * Update the hero and interface after a stat is set by a dialog. * @param stat The stat that needs to be updated. * @param value The new value to set. */ @Override public void onStatSet(int stat, int value) { // Get a handle to the hero display. HeroDisplayFragment heroDisplayFragment = (HeroDisplayFragment) getFragmentManager().findFragmentByTag("HeroDisplayFragment"); // Update the stat value in the hero display. heroDisplayFragment.onStatSet(stat, value); } }