Android Open Source - wherewithal Async Load Game File






From Project

Back to project page wherewithal.

License

The source code is released under:

GNU General Public License

If you think the Android project wherewithal listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.curchod.domartin;
//ww w  .jav a 2  s  .  c o m
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Enumeration;
import java.util.Hashtable;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TableLayout;
import android.widget.TableLayout.*;
import android.widget.TableRow;
import android.widget.TextView;

import com.curchod.dto.Game;
import com.curchod.wherewithal.GameReadingStonesActivity;
import com.curchod.wherewithal.R;

/**
 *  AsyncTask<Params, Progress, Result>{....}
 * @author Administrator
 *
 */
public class AsyncLoadGameFile extends AsyncTask<Context, Void, Game> 
{

  private String DEBUG_TAG = "AsyncLoadGameFile";
  private TableLayout table;
  private GameReadingStonesActivity grsa;
  private Hashtable <String, String> id_player_names;
  
  public void setIdPlayerNamse(Hashtable <String, String> _id_player_names)
  {
    this.id_player_names = _id_player_names;
  }
  
  public void setGameReadingStonesActivity(GameReadingStonesActivity _grsa)
  {
    this.grsa = _grsa;
  }
  
  public void setTable(TableLayout _table)
  {
    this.table = _table;
  }
  
  /**
   * Open the game.xml file and read the data into a Game object.
   * This file has the following format:
   * <game>
     *   <test_name>
     *   <test_id>
     *  <class_id>
     *   <test_type>
     *   <test_status>
     *   <test_format>
     *  <player_id>
     *  <player_status id="7655807335881695697">setup</player_status>
     *  ...
     * </game>
   */
    protected Game doInBackground(Context ... context) 
    {
      final String method = "loadGameFile"; 
      Log.i(DEBUG_TAG, method+": Open the local game.xml file and parse it for game info.");
      Game game = new Game();
      FileInputStream fis = null;
    try 
    {
      fis = context[0].openFileInput("game.xml");
    } catch (FileNotFoundException e1) 
    {
      Log.i(DEBUG_TAG, method+": fnfe");
      e1.printStackTrace();
    }
        try 
        {
          String parse_player_id = "";
          String tag = "";
            XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
            XmlPullParser parser = parserCreator.newPullParser();
            parser.setInput(fis, null);
            int parser_event = parser.getEventType();
            while (parser_event != XmlPullParser.END_DOCUMENT) 
            {
                switch (parser_event) 
                {
                case XmlPullParser.TEXT:
                  if (true)
                  {
                    if (tag!=null)
                    {
                      String value = parser.getText();
                      if (tag.equals("test_id"))
                      {
                        game.setTestId(value);
                        //Log.i(DEBUG_TAG, "test_id loaded "+value);
                      } else if (tag.equals("class_id"))
                      {
                        game.setClassId(value);
                        Log.i(DEBUG_TAG, "class_id loaded "+value);
                      } else if (tag.equals("test_status"))
                      {
                        game.setTestStatus(value);
                        //game.setTestStatus(value);
                        //game_status = value;
                        //Log.i(DEBUG_TAG, "loaded "+test_status);
                      } else if (tag.equals("test_name"))
                      {
                        game.setTestName(value);
                        //Log.i(DEBUG_TAG, "loaded "+test_name);
                      } else if (tag.equals("player_id"))
                      {
                        //student_id = value;
                        //Log.i(DEBUG_TAG, "added player_id "+value);
                      } else if (tag.equals("player_status"))
                      {
                        Log.i(DEBUG_TAG, "game.setPlayerStatus("+parse_player_id+","+value+") for test_name "+game.getTestName());
                        game.setPlayerStatus(parse_player_id, value);
                        parse_player_id = "";
                      }
                    }
                    tag = null;
                  }
                    
                case XmlPullParser.START_TAG:
                  tag = parser.getName();
                  //Log.i(DEBUG_TAG, tag+" attribute "+parser.getAttributeValue(null, "id"));
                  parse_player_id = parser.getAttributeValue(null, "id");
                }
                parser_event = parser.next();
            }
        } catch (Exception e) 
        {
          Log.i(DEBUG_TAG, method+": (not) exception(al)");
          e.printStackTrace();
        }
    return game;
    }

    protected void onPostExecute(Game result_game) 
    {
      table.removeAllViews();
      Hashtable id_status = result_game.getPlayerStatus();
      Enumeration<String> e = id_status.keys();
    while (e.hasMoreElements())
    {
      table = (TableLayout) grsa.findViewById(R.id.TableLayout01);
          TableRow row = new TableRow(grsa);
      String key = (String)e.nextElement();
      String name = id_player_names.get(key);
      int score = Integer.parseInt((String) id_status.get(key));
      TextView t = new TextView(grsa);
          TextView s = new TextView(grsa);   
          t.setText(name);
          s.setText(score+"");
          row.addView(t);
          row.addView(s);
          table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    }
    }
}




Java Source Code List

com.curchod.domartin.AsyncLoadGameFile.java
com.curchod.domartin.Constants.java
com.curchod.domartin.Filer.java
com.curchod.domartin.HouseDeck.java
com.curchod.domartin.IWantTo.java
com.curchod.domartin.MockNdefMessages.java
com.curchod.domartin.NfcUtils.java
com.curchod.domartin.RemoteCall.java
com.curchod.domartin.Sarray.java
com.curchod.domartin.Scoring.java
com.curchod.domartin.TagDescription.java
com.curchod.domartin.UtilityTo.java
com.curchod.dto.Card.java
com.curchod.dto.DeckCard.java
com.curchod.dto.GameWord.java
com.curchod.dto.Game.java
com.curchod.dto.PlayerInfo.java
com.curchod.dto.SavedTest.java
com.curchod.dto.SingleWordTestResult.java
com.curchod.dto.SingleWord.java
com.curchod.json.VocabularyDefinition.java
com.curchod.json.VocabularyLearningObject.java
com.curchod.wherewithal.AddPlayerActivity.java
com.curchod.wherewithal.CardDeckActivity.java
com.curchod.wherewithal.CardDecksActivity.java
com.curchod.wherewithal.CardPlayerHouseDeckActivity.java
com.curchod.wherewithal.CardPlayerWordsActivity.java
com.curchod.wherewithal.CardPlayersListActivity.java
com.curchod.wherewithal.CardsActivity.java
com.curchod.wherewithal.GameConcentrationActivity.java
com.curchod.wherewithal.GameReadingStonesActivity.java
com.curchod.wherewithal.GameReadingStonesInstructionsActivity.java
com.curchod.wherewithal.GameSnazzyThumbworkActivity.java
com.curchod.wherewithal.GameWritingStonesActivity.java
com.curchod.wherewithal.GamesActivity.java
com.curchod.wherewithal.InstructionsActivity.java
com.curchod.wherewithal.MainActivity.java
com.curchod.wherewithal.PlayerActivity.java
com.curchod.wherewithal.PlayersActivity.java