Android Open Source - OD-PlayerStats Main Activity






From Project

Back to project page OD-PlayerStats.

License

The source code is released under:

MIT License

If you think the Android project OD-PlayerStats 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 net.black_pixel.od_playerstats;
// ww w  .j  av a2  s .c  o m
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;

import net.black_pixel.od_playerstats.fileIO.FileIO;
import net.black_pixel.od_playerstats.parser.Parser;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

  private ProgressBar progressBar1;
  private Button loadStatsButton;
  private EditText userID;
  private TextView usernameView;
  private TextView allianceView;
  private TextView empireScoreView;
  private TextView battleScoreView;
  private TextView questScoreView;
  private TextView tradingScoreView;
  private TextView planetsControlledView;
  private ImageView raceImage;
  private Spinner spinner;

  private Map<String, String> savedNames; // to store username,userid mappings
  private String[] namesArray; // to store only the unsernames for the spinner
  private FileIO fileIO;
  private Parser parser;

  public class UserStatsTask extends
      AsyncTask<String, Integer, ArrayList<String>> { // used to get the
                              // user stats

    @Override
    protected void onPreExecute() {
      loadStatsButton.setClickable(false);
      progressBar1.setVisibility(View.VISIBLE);
      raceImage.setVisibility(View.INVISIBLE);
    }

    @Override
    protected ArrayList<String> doInBackground(String... arg0) {
      return parser.getStatsByID(arg0[0]);
    }

    @Override
    protected void onPostExecute(ArrayList<String> parsedValues) {
      if (parsedValues != null) {
        updateViews(parsedValues);
        if (savedNames == null) {
          savedNames = new TreeMap<String, String>();
        }
        if (!savedNames.containsKey(parsedValues.get(0))) {
          savedNames.put(parsedValues.get(0), userID.getText()
              .toString());
          updateSpinner();
        }
      } else {
        Toast.makeText(MainActivity.this,
            "Error: Could not get user data.", Toast.LENGTH_LONG)
            .show();
      }
      loadStatsButton.setClickable(true);
      progressBar1.setVisibility(View.INVISIBLE);
      raceImage.setVisibility(View.VISIBLE);
    }
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    parser = new Parser();
    fileIO = new FileIO(this);
    savedNames = fileIO.getNames();
    
    initViews();
    setListener();
    updateSpinner();
  }

  private void initViews() {
    usernameView = (TextView) findViewById(R.id.usernameView);
    allianceView = (TextView) findViewById(R.id.allianceView);
    empireScoreView = (TextView) findViewById(R.id.empireScoreView);
    battleScoreView = (TextView) findViewById(R.id.battleScoreView);
    questScoreView = (TextView) findViewById(R.id.questScoreView);
    tradingScoreView = (TextView) findViewById(R.id.tradingScoreView);
    planetsControlledView = (TextView) findViewById(R.id.planetsControlledView);
    progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
    loadStatsButton = (Button) findViewById(R.id.loadStatsButton);
    raceImage = (ImageView) findViewById(R.id.raceImage);
    spinner = (Spinner) findViewById(R.id.spinner1);
    userID = (EditText) findViewById(R.id.userID);
  }

  private void setListener() {
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

      @Override
      public void onItemSelected(AdapterView<?> arg0, View arg1,
          int arg2, long arg3) { 
        // get userID for selected name and put it in the editText field
        String selection = spinner.getSelectedItem().toString();
        userID.setText(savedNames.get(selection));
      }

      @Override
      public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

      }

    });

    loadStatsButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View arg0) {
        UserStatsTask userStatsTask = new UserStatsTask();
        userStatsTask
            .execute("http://www.omega-day.com/game/states/live_state.php?userid="
                + userID.getText() + "&world=int11");
      }
    });
  }

  private void updateSpinner() {
    if (savedNames != null) {
      // put all saved names in an array
      namesArray = new String[savedNames.size()];
      int i = 0;
      for (Map.Entry<String, String> entry : savedNames.entrySet()) { 
        namesArray[i] = entry.getKey();
        ++i;
      }
      // populate spinner with array
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
          android.R.layout.simple_spinner_item, namesArray);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(adapter);

      // set the current username as selected
      String currentUsername = usernameView.getText().toString();
      for (int j = 0; j < namesArray.length; ++j) {
        if (namesArray[j] == currentUsername) {
          spinner.setSelection(j);
          break;
        }
      }
    }
  }

  private void updateViews(ArrayList<String> parsedValues) {
    if (parsedValues != null) {
      usernameView.setText(parsedValues.get(0));
      empireScoreView.setText(parsedValues.get(1));
      battleScoreView.setText(parsedValues.get(2));
      allianceView.setText(parsedValues.get(3));
      planetsControlledView.setText(parsedValues.get(4));
      questScoreView.setText(parsedValues.get(5));
      tradingScoreView.setText(parsedValues.get(6));
      String race = parsedValues.get(7);
      if (race.equals("1")) {
        raceImage.setImageResource(R.drawable.menschen);
      } else if (race.equals("11")) {
        raceImage.setImageResource(R.drawable.revisker);
      } else if (race.equals("3")) {
        raceImage.setImageResource(R.drawable.tradoner);
      } else if (race.equals("4")) {
        raceImage.setImageResource(R.drawable.beraluten);
      } else if (race.equals("5")) {
        raceImage.setImageResource(R.drawable.myrianer);
      }
    }
  }

  private void clearSavedNames() {
    // Dialog: Do you really want to delete all saved names?
    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        switch (which) {
        case DialogInterface.BUTTON_POSITIVE:
          // Yes button clicked
          savedNames = new TreeMap<String, String>();
          updateSpinner();
          fileIO.saveNames(savedNames);
          break;

        case DialogInterface.BUTTON_NEGATIVE:
          // No button clicked
          break;
        }
      }
    };

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Are you sure?")
        .setPositiveButton("Yes", dialogClickListener)
        .setNegativeButton("No", dialogClickListener).show();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    menu.add(1, 1, 1, "Clear Saved Names");
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case 1:
      clearSavedNames();
      return true;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  public void onStop() {
    super.onStop();
    fileIO.saveNames(savedNames);
  }

  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putStringArrayList("parsedValues", parser.getCurrentValues());
  }

  @Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);
    parser.setValues(savedInstanceState.getStringArrayList("parsedValues"));
    updateViews(parser.getCurrentValues());
  }
}




Java Source Code List

net.black_pixel.od_playerstats.MainActivity.java
net.black_pixel.od_playerstats.fileIO.FileIO.java
net.black_pixel.od_playerstats.parser.Parser.java