Android Open Source - Do-not-get-annoyed New Game Activity






From Project

Back to project page Do-not-get-annoyed.

License

The source code is released under:

Apache License

If you think the Android project Do-not-get-annoyed 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 mn100013d.pmu;
//from w  ww  . j a v  a 2  s. c  om
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.Serializable;

import mn100013d.pmu.controllers.GameController;
import mn100013d.pmu.services.FragmentProvider;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class NewGameActivity extends FragmentActivity implements Serializable{
  private static final String GAME_CONTROLLER = "mn100013d.pmu.GAME_CONTROLLER";
  private static final String SAVED = "mn100013d.pmu.SAVED";
  public static final String FILE_NAME = "gamedata.txt";
  public static GameController gameController;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_game);
    LinearLayout l = (LinearLayout) findViewById(R.id.testlayout);
    View v = getLayoutInflater().inflate(R.layout.table_layout, null);
    l.addView(v);
    FragmentProvider.getInstance().setContext(this);
    FragmentProvider.getInstance().setContainerId(
        R.id.fragment_container_new_game);
    
    if (savedInstanceState != null) {
      
      gameController = getGameController();
      if (gameController == null){
        Log.i("Nikola", "Novi");
        gameController = new GameController(
            findViewById(R.id.newgame_layout), this);
      }else
        Log.i("Nikola", "Stari");
    } else{
      gameController = new GameController(
          findViewById(R.id.newgame_layout), this);
      Log.i("Nikola", "Novi");
    }
  }

  private Boolean toggleDebugMode() {
    return gameController.toggleDebugMode();
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.new_game, menu);
    return true;
  }
  public GameController getGameControllerInstance(){
    return gameController;
  }
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
    case R.id.game_over:
      Toast.makeText(this, "Game Over",
          Toast.LENGTH_SHORT).show();
      gameController.gameOver();
      return true;
    case R.id.activate_debug_mode:
      Toast.makeText(this, "Debug mode set to " + toggleDebugMode(),
          Toast.LENGTH_SHORT).show();
      return true;
    case R.id.dice_1:
      gameController.forceDice(1);
      return true;
    case R.id.dice_2:
      gameController.forceDice(2);
      return true;
    case R.id.dice_3:
      gameController.forceDice(3);
      return true;
    case R.id.dice_4:
      gameController.forceDice(4);
      return true;
    case R.id.dice_5:
      gameController.forceDice(5);
      return true;
    case R.id.dice_6:
      gameController.forceDice(6);
      return true;
    default:
      return super.onOptionsItemSelected(item);
    }
  }

  @Override
  protected void onPause() {
    gameController.pause();
    super.onPause();
  }

  @Override
  protected void onResume() {
    gameController.resume();
    super.onResume();
  }

  
  

  @Override
  public void onBackPressed() {
    gameController.pause(); 
    FragmentProvider.getInstance().changeFragment(FragmentProvider.PAUSE);
  }
  
  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
      // Save the user's current game state
      savedInstanceState.putInt(SAVED, 1);
      Log.i("Nikola", "Snimanje");
      try {
        FileOutputStream fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
      ObjectOutputStream os = new ObjectOutputStream(fos);
      os.writeObject(gameController);
      os.close();
    } catch (IOException e) {  
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
      super.onSaveInstanceState(savedInstanceState);
  }
  private GameController getGameController(){
    FileInputStream fis;
    GameController gc = null;
    try {
      fis = openFileInput(FILE_NAME);
      ObjectInputStream is = new ObjectInputStream(fis);
      gc = (GameController) is.readObject();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (OptionalDataException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
    return gc;
  }

  
  

}




Java Source Code List

mn100013d.pmu.BeginingActivity.java
mn100013d.pmu.GameTypeFragment.java
mn100013d.pmu.NewGameActivity.java
mn100013d.pmu.NewGameFragment.java
mn100013d.pmu.PauseFragment.java
mn100013d.pmu.PauseGameFragment.java
mn100013d.pmu.ScoresFragment.java
mn100013d.pmu.SettingsFragment.java
mn100013d.pmu.StartActivity.java
mn100013d.pmu.StartGameFragment.java
mn100013d.pmu.controllers.GameController.java
mn100013d.pmu.data.GameDataDbHelper.java
mn100013d.pmu.data.GameSettingsEditor.java
mn100013d.pmu.data.GameTableContract.java
mn100013d.pmu.exceptions.ContextNotSetException.java
mn100013d.pmu.exceptions.GameExceptions.java
mn100013d.pmu.exceptions.PlayerNotRegisteredException.java
mn100013d.pmu.models.Board.java
mn100013d.pmu.models.CPUGamePlayer.java
mn100013d.pmu.models.Color.java
mn100013d.pmu.models.Dice.java
mn100013d.pmu.models.Field.java
mn100013d.pmu.models.FinishField.java
mn100013d.pmu.models.GamePlayer.java
mn100013d.pmu.models.HomeField.java
mn100013d.pmu.models.HumanGamePlayer.java
mn100013d.pmu.models.PathField.java
mn100013d.pmu.models.Pawn.java
mn100013d.pmu.models.Result.java
mn100013d.pmu.services.FragmentProvider.java
mn100013d.pmu.services.GamePlayerFactory.java
mn100013d.pmu.services.PopupService.java
mn100013d.pmu.services.Randomizer.java
mn100013d.pmu.services.ShakeDetector.java
mn100013d.pmu.services.SoundService.java