Example #1
0
 public static Set<String> getAvailableSettingSets(Context context) {
   String available = getMeta(context).getString(AVAILABLE_SETTINGS_SET, "");
   return new HashSet<String>(StringUtil.split(available, ','));
 }
Example #2
0
  public List<GameSummary> findAllGameSummaries() {
    synchronized (GameDBHelper.class) {
      String[] columns = {
        "g." + COLUMN_ID,
        "g." + COLUMN_NAME,
        "g." + COLUMN_DATE_SAVED,
        // player names; the "separator" is a trick to ensure that we can cleanly separate the
        // response,
        // and put it into the proper order, since group_concat is always unordered in sqlite
        "group_concat((ps.name || '"
            + GROUP_CONCAT_INNER_SEPARATOR
            + "' || ps.playerNumber), '"
            + GROUP_CONCAT_SEPARATOR
            + "')",
        "max(length(ps.history) - length(replace(ps.history, ',', '')) + 1)" // num rounds
      };

      String table =
          TABLE_GAMES
              + " g join "
              + TABLE_PLAYER_SCORES
              + " ps "
              + " on g."
              + COLUMN_ID
              + " = ps."
              + COLUMN_GAME_ID;
      String groupBy = "g." + COLUMN_ID;

      Cursor cursor = null;

      try {

        cursor = db.query(table, columns, null, null, groupBy, null, null);

        List<GameSummary> result = new ArrayList<GameSummary>();

        // re-use sparse array for performance
        SparseArray<String> playerNumbersToNames = new SparseArray<String>();

        while (cursor.moveToNext()) {
          GameSummary gameSummary = new GameSummary();

          gameSummary.setId(cursor.getInt(0));
          gameSummary.setName(cursor.getString(1));
          gameSummary.setDateSaved(cursor.getLong(2));

          String playerNumbersAndNames = cursor.getString(3);
          // sort by player number, get player names in order (no way to do this in sqlite,
          // unfortunately)

          playerNumbersToNames.clear();
          for (String playerNumberAndName :
              StringUtil.split(playerNumbersAndNames, GROUP_CONCAT_SEPARATOR)) {
            int idx = playerNumberAndName.indexOf(GROUP_CONCAT_INNER_SEPARATOR);
            String playerName = playerNumberAndName.substring(0, idx);
            int playerNumber =
                Integer.parseInt(
                    playerNumberAndName.substring(idx + GROUP_CONCAT_INNER_SEPARATOR.length()));
            playerNumbersToNames.put(playerNumber, playerName);
          }
          List<String> playerNames = new ArrayList<String>(playerNumbersToNames.size());
          for (int i = 0, len = playerNumbersToNames.size(); i < len; i++) {
            int playerNumber = playerNumbersToNames.keyAt(i);
            playerNames.add(playerNumbersToNames.get(playerNumber));
          }
          gameSummary.setPlayerNames(playerNames);

          gameSummary.setNumRounds(cursor.getInt(4));

          result.add(gameSummary);
        }

        return result;

      } finally {
        if (cursor != null) {
          cursor.close();
        }
      }
    }
  }