package com.tzapps.tzpalette.ui;

import com.tzapps.tzpalette.R;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.util.Log;

public class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener
{
    private static final String TAG = "SettingsFragment";
    
    public static final String KEY_PREF_COLOR_TYPE = "pref_analysisColorType";
    public static final String KEY_PREF_COLOR_NUMBER = "pref_setColorNumber";
    
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
    {
        if (key.equals(KEY_PREF_COLOR_TYPE))
        {
            Preference colorTypePref = findPreference(key);
            String defaultColorType = getResources().getString(R.string.pref_analysisColorType_default);
            String colorType = sharedPreferences.getString(key, defaultColorType);
            colorTypePref.setSummary(colorType);
        }
        else if (key.equals(KEY_PREF_COLOR_NUMBER))
        {
            Preference colorNumberPref = findPreference(key);
            int defaultColorNumber = getResources().getInteger(R.integer.pref_setColorNumber_default);
            int colorNumber = sharedPreferences.getInt(key, defaultColorNumber);
            colorNumberPref.setSummary(colorNumber + "");
        }
    }
    
    @Override
    public void onResume()
    {
        super.onResume();
        getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }
    
    @Override
    public void onPause()
    {
        super.onPause();
        getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }
}

@@ -31,40 +31,8 @@
 i;%0A%0A
-import com.tzapps.tzpalette.R;%0A%0A
 impo
@@ -69,16 +69,16 @@
 rences;%0A
+
 import a
@@ -258,31 +258,38 @@
 nt;%0A
+%0A
 import 
-android.util.Log
+com.tzapps.tzpalette.R
 ;%0A%0Ap
@@ -844,24 +844,725 @@
 eferences);%0A
+        %0A        // Initialize the pref summary label%0A        loadPrefs();%0A    %7D%0A    %0A    private void loadPrefs()%0A    %7B%0A        SharedPreferences sp = getPreferenceScreen().getSharedPreferences();%0A        %0A        String colorType = sp.getString(KEY_PREF_COLOR_TYPE, getString(R.string.pref_analysisColorType_default));%0A        Preference colorTypePref = findPreference(KEY_PREF_COLOR_TYPE);%0A        colorTypePref.setSummary(colorType);%0A        %0A        int colurNumber = sp.getInt(KEY_PREF_COLOR_NUMBER, getResources().getInteger(R.integer.pref_setColorNumber_default));%0A        Preference colorNumberPref = findPreference(KEY_PREF_COLOR_NUMBER);%0A        colorNumberPref.setSummary(colurNumber+%22%22);%0A
     %7D%0A%0A    @
@@ -1801,24 +1801,17 @@
  String 
-defaultC
+c
 olorType
@@ -1813,31 +1813,49 @@
 rType = 
-getResources().
+sharedPreferences.getString(key, 
 getStrin
@@ -1900,90 +1900,8 @@
 ult)
-;%0A            String colorType = sharedPreferences.getString(key, defaultColorType
 );%0A 

load the prefs when create the fragment