Android Open Source - Game-of-thrones-trivia High Score Prefs






From Project

Back to project page Game-of-thrones-trivia.

License

The source code is released under:

MIT License

If you think the Android project Game-of-thrones-trivia 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 com.GameOfThrones.Trivia.data;
/*  ww  w . j  ava2 s  .com*/
import com.GameOfThrones.Trivia.R;
import com.GameOfThrones.Trivia.core.HighScore;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Store multiple HighScores in SharedPreferences
 * 
 * @author andre
 * 
 */
public class HighScorePrefs {

  /** This application's preferences label */

  private static String PREFS_NAME;

  private String appName;

  /** This application's preferences */

  private static SharedPreferences settings;

  /** This application's settings editor */

  private static SharedPreferences.Editor editor;

  public String KEY_PREFIX = appName + ".KEY";
  public String KEY_SCORE_COUNT = appName + ".ScoreCount";

  /** generic field keys */
  private String KEY_DATE = appName + ".KEY_DATE";
  private String KEY_SCORE = appName + ".KEY_SCORE";

  /**
   * Constructor
   * 
   * @param ctx
   *            - Context in which the sharedPreferences are stored
   */
  public HighScorePrefs(Context ctx) {
    this.appName = ctx.getString(R.string.app_name);
    if (settings == null) {
      PREFS_NAME = appName + ".HighScore";
      settings = ctx.getSharedPreferences(PREFS_NAME,
          Context.MODE_PRIVATE);
    }
    editor = settings.edit();

    KEY_PREFIX = appName + ".KEY";
    KEY_SCORE_COUNT = appName + ".ScoreCount";
    
    /** generic field keys */
    KEY_DATE = appName + ".KEY_DATE";
    KEY_SCORE = appName + ".KEY_SCORE";
  }

  /** The prefix for flattened user keys */

  /**
   * Method to return a unique key for any field belonging to a given object
   * 
   * @param id
   *            of the object
   * @param fieldKey
   *            of a particular field belonging to that object
   * @return key String uniquely identifying the object's field
   */
  private String getFieldKey(int id, String fieldKey) {
    return KEY_PREFIX + id + "_" + fieldKey;
  }

  /**
   * Saves a new highscore in the shared preferences
   * @param date
   * @param scoreView
   * @return
   */
  public HighScore addNewHighScore(String date, int score) {
    if (date == null)
      return null; // don't bother

    int id = getHighScoreCount();
    editor.putString(getFieldKey(id, KEY_DATE), date);
    editor.putInt(getFieldKey(id, KEY_SCORE), score);

    addScoreCount(1);
    editor.commit();

    return new HighScore(id, date, score);
  }

  /**
   * Retrieves the HighScore that is mapped to this id
   * 
   * @param id
   * @return
   */
  public HighScore getHighScore(int id) {
    String date = settings.getString(getFieldKey(id, KEY_DATE), ""); // default
                                      // value
    int score = settings.getInt(getFieldKey(id, KEY_SCORE), 0); // default
                                  // value
    return new HighScore(id, date, score);
  }
  
  /**
   * Returns the number of high scores currently saved
   * @return
   */
  public int getHighScoreCount() {
    return settings.getInt(KEY_SCORE_COUNT, 0);
  }
  
  /**
   * Increment High scoreView count.
   * @param number
   * @return
   */
  public int addScoreCount(int number) {
    int scoreCount = getHighScoreCount();
    editor.putInt(KEY_SCORE_COUNT, scoreCount + number);

    editor.commit();
    return getHighScoreCount();
  }
}




Java Source Code List

com.GameOfThrones.Trivia.core.CharacterToQuestionsMap.java
com.GameOfThrones.Trivia.core.GameCharacter.java
com.GameOfThrones.Trivia.core.HighScore.java
com.GameOfThrones.Trivia.core.OutOfQuestionsException.java
com.GameOfThrones.Trivia.core.QuestionCollection.java
com.GameOfThrones.Trivia.core.Question.java
com.GameOfThrones.Trivia.core.Session.java
com.GameOfThrones.Trivia.core.SimpleIterator.java
com.GameOfThrones.Trivia.core.TriviaGame.java
com.GameOfThrones.Trivia.data.HighScorePrefs.java
com.GameOfThrones.Trivia.data.RequestWebService.java
com.GameOfThrones.Trivia.data.StringsXMLFileData.java
com.GameOfThrones.Trivia.ui.AboutActivity.java
com.GameOfThrones.Trivia.ui.DynamicBackgroundActivity.java
com.GameOfThrones.Trivia.ui.GameActivity.java
com.GameOfThrones.Trivia.ui.HighScoreActivity.java
com.GameOfThrones.Trivia.ui.MainMenuActivity.java
com.GameOfThrones.Trivia.ui.ResultsActivity.java
com.GameOfThrones.Trivia.ui.TriviaSelectionActivity.java
com.GameOfThrones.Trivia.ui.music.MusicService.java
com.GameOfThrones.Trivia.util.GeneralAlgorithms.java
com.Spartacus.TriviaTest.TriviaMenuActivityTest.java
com.Spartacus.Trivia.utilTest.GeneralAlgorithmsTest.java