package com.example.db01;

import android.os.Bundle;
import android.preference.PreferenceActivity;

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.PreferenceCategory;

// https://androidresearch.wordpress.com/2012/03/09/creating-a-preference-activity-in-android/

// http://www.mysamplecode.com/2011/11/android-shared-preferences-example_12.html

public class PrefsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
	 
	public static final String TIME_OPTION = "time_option";
	public static final String DATE_RANGE = "date_range";
	public static final String DATE_FORMAT = "date_format";
	public static final String TIME_FORMAT = "time_format";
	public static final String VIBRATE_PREF = "vibrate_pref";
	public static final String RINGTONE_PREF = "ringtone_pref";
	
	public static final String TEST = "checkBox";
	
	
	public static final String DEFAULT_DATE_FORMAT = "yyyy-M-d";
	
	public static SharedPreferences sp;

	
		@Override
		protected void onCreate(Bundle savedInstanceState) 
		{
		   super.onCreate(savedInstanceState);
		   addPreferencesFromResource(R.xml.prefs);
		}
		
		
		@Override
		  protected void onResume() {
		    super.onResume();
		    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
		  }

		  @Override
		  protected void onPause() {
		    super.onPause();
		    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
		   }

		  @Override
		  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,  String key)
		  {
		    //updatePreferences(findPreference(key));
		    
		    updatePreference(TEST);
			//updatePreference(DATE_RANGE);
			//updatePreference(DATE_FORMAT);
			//updatePreference(RINGTONE_PREF);
		  }
		
		 
		  /*private void updatePreferences(Preference p) 
		  {
			    if (p instanceof EditTextPreference) 
			    {
			      EditTextPreference editTextPref = (EditTextPreference) p;
			      p.setSummary(editTextPref.getText());
			    }
		  }*/
		  
	
		  // 	  SharedPreferences.Editor prefEditor = sharedPreferences.edit();
		  //      prefEditor.putString(MY_OPTION_KEY, "Default value");
          //	  prefEditor.commit();
		  
		  // commit ????
		  
		  private void updatePreference(String key)
			{
				Preference pref = findPreference(key);

			    if (pref instanceof ListPreference) {
			        ListPreference listPref = (ListPreference) pref;
			        pref.setSummary(listPref.getEntry());
			        return;
			    }		
			}		
		
}