package com.androidbegin.buttonclicktutorial;


import android.content.Intent;
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;
	
	public static final String PREFERENCES_NAME = "MyPrefsFile";

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

	/* 
	 public String old()
	 {
		 Preference pref = findPreference(DATE_RANGE);
 		 CharSequence etat = pref.getSummary();
 		 return(etat.toString());
	 }
	 
	 public static String retourne()
	 {
		 String aqw = old();
		 return(aqw);
	 }
	 */ 
		@Override
		protected void onPause() 
		{
			super.onPause();
			getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
			
			 
		         	SharedPreferences settings = getSharedPreferences(PREFERENCES_NAME, 0);
		    		SharedPreferences.Editor editor = settings.edit();
		    		// sauvegarde la selection 	
		    		Preference pref = findPreference(DATE_RANGE);
		    		CharSequence etat = pref.getSummary();
		    		editor.putString("DATE_RANGE_CHANGE", etat.toString());
		    		editor.commit();
		    		//editor.putInt("deux", 123);
		    		//editor.putBoolean("trois", true); 
		         
		    		// String freq = pref.getString("frequency_key", "8k Hz");
		    		
		    		 //pref.getSummary()
		    		 //pref.setSummary();
		    		 
		    		 /*Intent intent=new Intent();
					  String message= "lkjkllkhkl";
				         intent.putExtra("MESSAGE", message);  
				         setResult(2,intent);  
				         finish();//finishing activity  
				      */   		
		       
		   /*      
		         Intent data = new Intent();
	             data.putExtra("valeur_retour", "alpha");
	             // Activity finished ok, return the data
	             setResult(RESULT_OK, data);
	             finish();
		   */      
		/*         
		         Intent intent = getIntent();
		         intent.putExtra("Date","dateSelected");
		         setResult(RESULT_OK, intent);
		         finish();
			*/
		}    

		@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());
		}*/
		
		/*
		 protected void onPause()
		 {
		     
		   
		        Intent intent=new Intent();  
		         intent.putExtra("MESSAGE","coucou");  
		         setResult(2,intent);  
		         finish();//finishing activity  
		         super.onDestroy();
		    }
		
		/*
		 Intent intent=new Intent();  
         intent.putExtra("MESSAGE",message);  
         setResult(2,intent);  
         finish();//finishing activity  
		*/
}
