package co.epitre.aelf_lectures;

import com.actionbarsherlock.app.SherlockPreferenceActivity;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;

public class SyncPrefActivity extends SherlockPreferenceActivity implements OnSharedPreferenceChangeListener {
    public static final String KEY_PREF_SYNC_LECTURES = "pref_sync_lectures";
    public static final String KEY_PREF_SYNC_DUREE = "pref_sync_duree";
    public static final String KEY_PREF_SYNC_CONSERV = "pref_sync_conserv";

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.sync_preferences);

        // hacky hack, but does the job --> init summaries
        onSharedPreferenceChanged(null, KEY_PREF_SYNC_LECTURES);
        onSharedPreferenceChanged(null, KEY_PREF_SYNC_DUREE);
        onSharedPreferenceChanged(null, KEY_PREF_SYNC_CONSERV);
    }

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

    @SuppressLint("NewApi") // check
    @SuppressWarnings("deprecation")
    @Override
    protected void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
        if(android.os.Build.VERSION.SDK_INT >= 9) {
            // need this to make sure Sync sees the new preferences...
        	SharedPreferences.Editor editor = getPreferenceScreen().getSharedPreferences().edit();
        	editor.apply();
        }
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        // set summary
        if (key.equals(KEY_PREF_SYNC_LECTURES) ||
            key.equals(KEY_PREF_SYNC_DUREE) ||
            key.equals(KEY_PREF_SYNC_CONSERV)) {
            ListPreference pref = (ListPreference)findPreference(key);
            pref.setSummary(pref.getEntry());
        }
    }

}

@@ -93,48 +93,8 @@
 y;%0A%0A
-import android.annotation.SuppressLint;%0A
 impo
@@ -1301,45 +1301,8 @@
  %7D%0A%0A
-    @SuppressLint(%22NewApi%22) // check%0A
     
@@ -1326,32 +1326,32 @@
 (%22deprecation%22)%0A
+
     @Override%0A  
@@ -1511,262 +1511,8 @@
 s);%0A
-        if(android.os.Build.VERSION.SDK_INT %3E= 9) %7B%0A            // need this to make sure Sync sees the new preferences...%0A        %09SharedPreferences.Editor editor = getPreferenceScreen().getSharedPreferences().edit();%0A        %09editor.apply();%0A        %7D%0A
     
@@ -1898,16 +1898,16 @@
 e(key);%0A
-
         
@@ -1954,16 +1954,355 @@
       %7D%0A
+        %0A        // called with null from the constructor%0A        if(sharedPreferences != null) %7B%0A%09        // Apply changes so that sync engines takes them into account%0A%09    %09SharedPreferences.Editor editor = sharedPreferences.edit();%0A%09        editor.commit(); // commit to file so that sync service is able to load it from disk%0A        %7D%0A
     %7D%0A%0A%7D

improve preference saving. still not working well