package com.antew.redditinpictures.library.preferences;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceManager;

import com.actionbarsherlock.app.SherlockPreferenceActivity;
import com.antew.redditinpictures.library.R;
import com.antew.redditinpictures.library.logging.Log;
import com.antew.redditinpictures.library.ui.About;
import com.antew.redditinpictures.library.utils.Consts;

public class RedditInPicturesPreferences extends SherlockPreferenceActivity implements OnSharedPreferenceChangeListener {
    CheckBoxPreference useMobileInterface;
    CheckBoxPreference showNsfwImages;
    SharedPreferences  sharedPreferences;
    private boolean showNsfwImagesOldValue;
    private boolean showNsfwImagesNewValue;
    private Preference useHoloBackground;

    /**
     * This uses the deprecated addPreferencesFromResource because fragment
     * preferences aren't part of the support library
     */
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        if (getIntent().hasExtra(Consts.EXTRA_SHOW_NSFW_IMAGES)) {
            showNsfwImagesOldValue = getIntent().getBooleanExtra(Consts.EXTRA_SHOW_NSFW_IMAGES, false);
            showNsfwImagesNewValue = showNsfwImagesOldValue;
        }
        
        addPreferencesFromResource(R.xml.preferences);
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(RedditInPicturesPreferences.this);

        useMobileInterface = (CheckBoxPreference) getPreferenceScreen().findPreference(SharedPreferencesHelper.USE_MOBILE_INTERFACE);
        showNsfwImages = (CheckBoxPreference) getPreferenceScreen().findPreference(SharedPreferencesHelper.SHOW_NSFW_IMAGES);
        useHoloBackground = getPreferenceScreen().findPreference(SharedPreferencesHelper.USE_HOLO_BACKGROUND);
        
        getPreferenceScreen().findPreference(SharedPreferencesHelper.ABOUT).setOnPreferenceClickListener(new OnPreferenceClickListener() {
            
            @Override
            public boolean onPreferenceClick(Preference preference) {
                startActivity(new Intent(RedditInPicturesPreferences.this, About.class));
                return true;
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals(SharedPreferencesHelper.USE_MOBILE_INTERFACE))
        {
            Log.d("Mobile Interface Changed to", "" + sharedPreferences.getBoolean(SharedPreferencesHelper.USE_MOBILE_INTERFACE, true));
        }
        else if (key.equals(SharedPreferencesHelper.SHOW_NSFW_IMAGES))
        {
            showNsfwImagesNewValue = sharedPreferences.getBoolean(SharedPreferencesHelper.SHOW_NSFW_IMAGES, true);
            Log.d("Show Nsfw Images Changed To", "" + sharedPreferences.getBoolean(SharedPreferencesHelper.SHOW_NSFW_IMAGES, true));
        }
        
    }
    
    @Override
    public void onBackPressed() {
        Intent i = new Intent();
        i.putExtra(Consts.EXTRA_SHOW_NSFW_IMAGES_CHANGED, showNsfwImagesNewValue != showNsfwImagesOldValue);
        setResult(RESULT_OK, i);
        super.onBackPressed();
    }

}
@@ -375,53 +375,8 @@
 ner;
-%0Aimport android.preference.PreferenceManager;
 %0A%0Aim
@@ -765,16 +765,104 @@
 tener %7B%0A
+    public static final String TAG = RedditInPicturesPreferences.class.getSimpleName();%0A
     Chec
@@ -939,50 +939,8 @@
 es;%0A
-    SharedPreferences  sharedPreferences;%0A
     
@@ -1436,24 +1436,168 @@
 _IMAGES)) %7B%0A
+            Log.i(TAG, %22Had EXTRA_SHOW_NSFW_IMAGES in intent, value was %22 + getIntent().getBooleanExtra(Consts.EXTRA_SHOW_NSFW_IMAGES, false));%0A
             
@@ -1827,118 +1827,8 @@
 s);%0A
-        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(RedditInPicturesPreferences.this);%0A%0A
     
@@ -2650,34 +2650,8 @@
 ) %7B%0A
-        super.onResume();%0A
     
@@ -2743,24 +2743,234 @@
 ener(this);%0A
+        super.onResume();%0A    %7D%0A    %0A    @Override%0A    protected void onPause() %7B%0A        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);%0A        super.onPause();%0A
     %7D%0A%0A    @
@@ -3067,24 +3067,87 @@
 ring key) %7B%0A
+        Log.i(TAG, %22onSharedPreferencesChanged key = %22 + key);%0A
         if (
@@ -3230,17 +3230,17 @@
     Log.
-d
+i
 (%22Mobile
@@ -3550,19 +3550,20 @@
 IMAGES, 
-tru
+fals
 e);%0A    
@@ -3574,17 +3574,17 @@
     Log.
-d
+i
 (%22Show N
@@ -3683,19 +3683,20 @@
 IMAGES, 
-tru
+fals
 e));%0A   
@@ -3799,24 +3799,194 @@
 w Intent();%0A
+        boolean result = showNsfwImagesNewValue != showNsfwImagesOldValue;%0A        Log.i(TAG, %22onBackPressed Passing back %22 + Boolean.toString(result) + %22 to activity%22);%0A
         i.pu

Add more logging, fix for showNsfwImages not updating.