Example #1
0
  private void saveGameWithinTransaction(Game game, boolean updateDateSaved) {

    long dateSaved = updateDateSaved ? System.currentTimeMillis() : game.getDateSaved();
    game.setDateSaved(dateSaved);

    if (game.getId() != -1) {
      // game was already saved, so try to overwrite

      updateGame(game.getId(), game.getDateStarted(), game.getDateSaved(), game.getName());
    } else {
      // else create a new row in the table

      int newGameId = getMaxGameId() + 1;

      ContentValues contentValues = new ContentValues();

      contentValues.put(COLUMN_DATE_STARTED, game.getDateStarted());
      contentValues.put(COLUMN_DATE_SAVED, dateSaved);
      contentValues.put(COLUMN_NAME, game.getName());
      contentValues.put(COLUMN_ID, newGameId);
      contentValues.put(COLUMN_AUTOSAVED, 1); // legacy "autosaved" column
      // that must be specified

      db.insert(TABLE_GAMES, null, contentValues);

      game.setId(newGameId);
      log.d("new game id is %s", newGameId);
    }

    savePlayerScores(game.getId(), game.getPlayerScores());
  }
Example #2
0
  private void savePlayerScores(int gameId, List<PlayerScore> playerScores) {
    synchronized (GameDBHelper.class) {
      int newId = -1;

      for (PlayerScore playerScore : playerScores) {

        Pair<String, String> historyAsStrings = Delta.toJoinedStrings(playerScore.getHistory());

        if (playerScore.getId() != -1) {
          // already exists; update

          updatePlayerScore(
              playerScore.getId(),
              playerScore.getName(),
              playerScore.getScore(),
              playerScore.getPlayerNumber(),
              historyAsStrings.getFirst(),
              historyAsStrings.getSecond(),
              playerScore.getLastUpdate(),
              PlayerColor.serialize(playerScore.getPlayerColor()));

        } else {
          // else insert new rows in the table

          if (newId == -1) {
            newId = getMaxPlayerScoreId() + 1;
          } else {
            newId++;
          }

          ContentValues values = new ContentValues();
          values.put(COLUMN_ID, newId);
          values.put(COLUMN_GAME_ID, gameId);
          values.put(COLUMN_HISTORY, historyAsStrings.getFirst());
          values.put(COLUMN_HISTORY_TIMESTAMPS, historyAsStrings.getSecond());
          values.put(COLUMN_NAME, playerScore.getName());
          values.put(COLUMN_PLAYER_NUMBER, playerScore.getPlayerNumber());
          values.put(COLUMN_SCORE, playerScore.getScore());
          values.put(COLUMN_COLOR, PlayerColor.serialize(playerScore.getPlayerColor()));
          values.put(COLUMN_LAST_UPDATE, playerScore.getLastUpdate());
          db.insert(TABLE_PLAYER_SCORES, null, values);

          // set the new id on the PlayerScore
          playerScore.setId(newId);

          log.d("new playerScore id is %s", newId);
        }
      }
    }
  }
Example #3
0
  private void init() {

    // enable or disable onscreen delta buttons based on whether we have
    // enough room onscreen or not
    deltaButtonsViewStub = view.findViewById(R.id.onscreen_delta_buttons_stub);
    int versionInt = VersionHelper.getVersionSdkIntCompat();
    if (versionInt > VersionHelper.VERSION_DONUT && versionInt < VersionHelper.VERSION_FROYO) {
      // in eclair, there's a bug where ViewStubs within ViewSubs do not
      // render correctly, so inflate the ViewStubs no matter what
      if (deltaButtonsViewStub instanceof ViewStub) {
        deltaButtonsViewStub = ((ViewStub) deltaButtonsViewStub).inflate();
      }
    }
    deltaButtonsViewStub.setVisibility(showOnscreenDeltaButtons ? View.VISIBLE : View.GONE);
    onscreenDeltaButtonsLayout =
        (LinearLayout) view.findViewById(R.id.onscreen_delta_buttons_table_layout);

    divider1 = view.findViewById(R.id.player_score_divider_1);
    divider2 = view.findViewById(R.id.player_score_divider_2);
    nameTextView = (TextView) view.findViewById(R.id.text_name);
    scoreTextView = (TextView) view.findViewById(R.id.text_score);
    historyTextView = (TextView) view.findViewById(R.id.text_history);
    badgeTextView = (TextView) view.findViewById(R.id.text_badge);
    badgeLinearLayout = (LinearLayout) view.findViewById(R.id.linear_layout_badge);

    minusButton = (Button) view.findViewById(R.id.button_minus);
    plusButton = (Button) view.findViewById(R.id.button_plus);
    deltaButton1 = (Button) view.findViewById(android.R.id.button1);
    deltaButton2 = (Button) view.findViewById(android.R.id.button2);
    deltaButton3 = (Button) view.findViewById(android.R.id.button3);
    deltaButton4 = (Button) view.findViewById(R.id.button4);

    minusButton.setOnClickListener(this);
    minusButton.setOnLongClickListener(this);
    plusButton.setOnClickListener(this);
    plusButton.setOnLongClickListener(this);
    nameTextView.setOnClickListener(this);
    nameTextView.setOnLongClickListener(this);
    historyTextView.setOnClickListener(this);
    historyTextView.setOnLongClickListener(this);

    ColorScheme colorScheme = PreferenceHelper.getColorScheme(context);
    setNewColorScheme(colorScheme);

    updateViews();

    log.d("history is: %s", playerScore.getHistory());
  }