Example #1
0
  @TargetApi(Build.VERSION_CODES.KITKAT)
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    myDB = new DBHelper(this);

    Bundle extras = getIntent().getExtras();
    if (savedInstanceState != null) {
      // Restore game state if there is any
      game = (Game) savedInstanceState.getSerializable("game");
    }

    // here a game is recreated (from recent games)
    else if (extras.getSerializable("game") != null) {
      game = (Game) extras.getSerializable("game");
      if (extras.containsKey("flag_EN")) {
        game.flagEN = extras.getBoolean("flag_EN");
      }
      if (game != null) {
        // update players  in this game from DB because score may have changed from other games
        player1 = myDB.getPlayer(game.p1.getId());
        player2 = myDB.getPlayer(game.p2.getId());
        game.setPlayers(player1, player2);
        if (game.flagEN) { // determine which language and rebuild lexicon
          lexicon = new Lexicon(this, ("english.txt"));
          lexicon.filter(game.guessedLetters);
          game.lexicon = lexicon;
        } else {
          alt_lexicon = new Lexicon(this, ("dutch.txt"));
          alt_lexicon.filter(game.guessedLetters);
          game.lexicon = alt_lexicon;
        }
        dictionaryEnglish = game.flagEN;
      }
    }
    // this is only true if a user switches locale (system) languages
    else if (extras.containsKey("gameID")) {
      dictionaryEnglish = extras.getBoolean("flag_EN");
      // sync the game with game from DB
      game = myDB.getGame(extras.getString("gameID"));
      player1 = (Player) extras.getSerializable("p1");
      player2 = (Player) extras.getSerializable("p2");
    }

    // if all these conditions are false we are creating a new game from scratch
    else {
      // load up the two players from intent
      if (player1 == null) player1 = (Player) extras.getSerializable("p1");
      if (player2 == null) player2 = (Player) extras.getSerializable("p2");
      // get language from intent
      dictionaryEnglish = extras.getBoolean("flag_EN");

      // create new lexicon only once here for selected language
      if (dictionaryEnglish) {
        lexicon = new Lexicon(this, "english.txt");
        game = new Game(lexicon);
      } else {
        alt_lexicon = new Lexicon(this, "dutch.txt");
        game = new Game(alt_lexicon);
      }
      game.setPlayers(player1, player2);
      game.flagEN = dictionaryEnglish;
      myDB.insertGame(game);
    }
    // set the two avatars
    if (player1 != null && player2 != null) {
      ImageView p1image = (ImageView) findViewById(R.id.p1avatar);
      ImageView p2image = (ImageView) findViewById(R.id.p2avatar);
      p1image.setImageResource(player1.avatarId);
      p2image.setImageResource(player2.avatarId);
    }
    createSettingsFragment();
    updateView();
  }