Android Open Source - ArcTimer Prefs






From Project

Back to project page ArcTimer.

License

The source code is released under:

GNU General Public License

If you think the Android project ArcTimer 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.taavo.arctimer;
/*from ww  w.j  a  v a  2 s . co  m*/
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;

public class Prefs extends PreferenceActivity implements OnSharedPreferenceChangeListener {

  
  // TODO: Prefs are not reloaded on exit of activity
  

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.addPreferencesFromResource(R.xml.prefs);
    this.initSummaries(this.getPreferenceScreen());
    this.getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
  }
  
  /**
   * Set the summaries of all preferences
   */
  private void initSummaries(PreferenceGroup pg) {
    for (int i = 0; i < pg.getPreferenceCount(); ++i) {
      Preference p = pg.getPreference(i);
      if (p instanceof PreferenceGroup) {
        this.initSummaries((PreferenceGroup) p); // recursion
      } else {
        this.setSummary(p);
      }
    }
  }

  /**
   * Set the summaries of the given preference
   */
  private void setSummary(Preference pref) {
    // react on type or key
    if (pref instanceof ListPreference) {
      ListPreference listPref = (ListPreference) pref;
      pref.setSummary(listPref.getEntry());
    } else if (pref instanceof EditTextPreference) {      
      EditTextPreference textPref = (EditTextPreference) pref;
      String sum = textPref.getSummary().toString();      
      pref.setSummary(sum.replaceFirst(": .*", ": " + textPref.getText()));
    }
  }
  

  /**
   * used to change the summary of a preference
   */
  public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
    Preference pref = findPreference(key);
    this.setSummary(pref);
  }

  /**
   * Select all text in editor so that user doesn't have to delete old entry first
   */
  @Override
  public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    if (preference instanceof EditTextPreference) {
      EditTextPreference textPref = (EditTextPreference) preference;
      textPref.getEditText().selectAll();
    }
    return super.onPreferenceTreeClick(preferenceScreen, preference);
  }

  
}




Java Source Code List

com.taavo.arctimer.ArcTimerActivity.java
com.taavo.arctimer.ArcTimerView.java
com.taavo.arctimer.IntervalSession.java
com.taavo.arctimer.Prefs.java