package com.example.db01;


import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class SettingsActivity 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 DEFAULT_DATE_FORMAT = "yyyy-M-d";
	
	public static SharedPreferences sp;

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

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

		@Override
		public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
			updatePreference(key);
		}
		
		private void updatePreferences() 
		{
			updatePreference(TIME_OPTION);
			updatePreference(DATE_RANGE);
			updatePreference(DATE_FORMAT);
			updatePreference(RINGTONE_PREF);
		}

		private void updatePreference(String key)
		{
			Preference pref = findPreference(key);

		    if (pref instanceof ListPreference) {
		        ListPreference listPref = (ListPreference) pref;
		        pref.setSummary(listPref.getEntry());
		        return;
		    }		
			
	
			/*
		    if (RINGTONE_PREF.equals(key)) 
		    {
		    	Uri ringtoneUri = Uri.parse(getRingtone());
		    	Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
		        if (ringtone != null) pref.setSummary(ringtone.getTitle(this));
		    }	*/	
		}

		/*
		public static String getRingtone() 
		{
			return sp.getString(RINGTONE_PREF, android.provider.Settings.System.DEFAULT_NOTIFICATION_URI.toString());
		}*/
		
}
