<commit_before>package info.spielproject.spiel

import android.content.{Context, SharedPreferences}
import android.media.AudioManager
import android.preference.PreferenceManager
import android.util.Log

import triggers._

/**
 * Singleton for convenient access to preference values. Also tracks 
 * preference changes and updates relevant subsystems.
*/

object Preferences extends SharedPreferences.OnSharedPreferenceChangeListener {

  private var prefs:SharedPreferences = null

  def sharedPreferences = prefs

  private var audioManager:AudioManager = null

  /**
   * Initialized based on the provided <code>SpielService</code>.
  */

  def apply(service:SpielService) {
    prefs = PreferenceManager.getDefaultSharedPreferences(service)
    prefs.registerOnSharedPreferenceChangeListener(this)
    audioManager = service.getSystemService(Context.AUDIO_SERVICE).asInstanceOf[AudioManager]
  }

  /**
   * Indicates desired speech engine.
  */

  def speechEngine = prefs.getString("speechEngine", TTS.defaultEngine)

  /**
   * Sets the desired speech engine.
  */

  def speechEngine_=(e:String) {
    val editor = prefs.edit()
    editor.putString("speechEngine", e)
    editor.commit()
  }

  /**
   * Indicates the desired rate scale.
  */

  def rateScale = prefs.getString("rateScale", "1").toFloat

  /**
   * Indicates the desired pitch scale.
  */

  def pitchScale = prefs.getString("pitchScale", "1").toFloat

  /**
   * Indicates whether character echo is enabled.
  */

  def echoByChar = prefs.getBoolean("echoByChar", true)

  /**
   * Indicates whether word echo is enabled.<commit_msg>If rateScale and pitchScale aren't set to floats, reset them to sane values.<commit_after>package info.spielproject.spiel

import android.content.{Context, SharedPreferences}
import android.media.AudioManager
import android.preference.PreferenceManager
import android.util.Log

import triggers._

/**
 * Singleton for convenient access to preference values. Also tracks 
 * preference changes and updates relevant subsystems.
*/

object Preferences extends SharedPreferences.OnSharedPreferenceChangeListener {

  private var prefs:SharedPreferences = null

  def sharedPreferences = prefs

  private var audioManager:AudioManager = null

  /**
   * Initialized based on the provided <code>SpielService</code>.
  */

  def apply(service:SpielService) {
    prefs = PreferenceManager.getDefaultSharedPreferences(service)
    prefs.registerOnSharedPreferenceChangeListener(this)
    audioManager = service.getSystemService(Context.AUDIO_SERVICE).asInstanceOf[AudioManager]
  }

  /**
   * Indicates desired speech engine.
  */

  def speechEngine = prefs.getString("speechEngine", TTS.defaultEngine)

  /**
   * Sets the desired speech engine.
  */

  def speechEngine_=(e:String) {
    val editor = prefs.edit()
    editor.putString("speechEngine", e)
    editor.commit()
  }

  private def resetFloatPreference(name:String) = {
    val editor = prefs.edit()
    editor.putFloat("rateScale", 1)
    editor.commit()
    1f
  }

  /**
   * Indicates the desired rate scale.
  */

  def rateScale = {
    try {
      prefs.getFloat("rateScale", 1)
    } catch {
      case _ =>
        resetFloatPreference("rateScale")
    }
  }

  /**
   * Indicates the desired pitch scale.
  */

  def pitchScale = {
    try {
      prefs.getFloat("pitchScale", 1)
    } catch {
      case _ =>
        resetFloatPreference("pitchScale")
    }
  }

  /**
   * Indicates whether character echo is enabled.
  */

  def echoByChar = prefs.getBoolean("echoByChar", true)

  /**
   * Indicates whether word echo is enabled.
{'fraction_non_alphanumeric': 0.0992239467849224, 'fraction_numerical': 0.0016629711751662971, 'mean_word_length': 3.6507731958762886, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3607114', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1013, 'n_words': 251}