<commit_before>/**
 * Copyright 2008 <NAME>
 */

package com.joelapenna.foursquared;

import com.joelapenna.foursquare.Foursquare;
import com.joelapenna.foursquared.preferences.Preferences;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceChangeListener;
import android.util.Log;

import java.lang.reflect.Constructor;

/**
 * @author <NAME> (<EMAIL>)
 * @author <NAME> (<EMAIL>)
 *    -added notifications settings (May 21, 2010).
 *    -removed user update, moved to NotificationSettingsActivity (June 2, 2010)
 */
public class PreferenceActivity extends android.preference.PreferenceActivity {
    private static final String TAG = "PreferenceActivity";

    private static final boolean DEBUG = FoursquaredSettings.DEBUG;


    private SharedPreferences mPrefs;
    
    private OnPreferenceChangeListener syncChangeListener;

    private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (DEBUG) Log.d(TAG, "onReceive: " + intent);
            finish();
        }
    };



    @Override
    protected void onResume() {
        super.onResume();
        CheckBoxPreference syncPref = (CheckBoxPreference)getPreferenceScreen().findPreference(Preferences.PREFERENCE_SYNC_CONTACTS);

        syncPref.setChecked(Foursquared.get(this).getSync().isEnabled());
        if ( syncChangeListener != null ) {
            syncPref.setOnPreferenceChangeListener(syncChangeListener);
        }
    }
    
    @Override
    protected void onPause() {
        Preference syncContactsPreference = getPreferenceScreen().findPreference(Preferences.PREFERENCE_SYNC_CONTACTS);
        syncContactsPreference.setOnPreferenceChangeListener(null);
        super.onPause();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerReceiver(mLoggedOutReceiver, new IntentFilter(Foursquared.INTENT_ACTION_LOGGED_OUT));

        syncChangeListener = new OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                boolean enableSync = (Boolean)newValue;
                Sync sync = Foursquared.get(PreferenceActivity.this).getSync();
                return sync.setEnabled(enableSync);<commit_msg>change sync pref off of the UI thread<commit_after>/**
 * Copyright 2008 <NAME>
 */

package com.joelapenna.foursquared;

import android.os.AsyncTask;
import com.joelapenna.foursquare.Foursquare;
import com.joelapenna.foursquared.preferences.Preferences;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.preference.Preference.OnPreferenceChangeListener;
import android.util.Log;

import java.lang.reflect.Constructor;

/**
 * @author <NAME> (<EMAIL>)
 * @author <NAME> (<EMAIL>)
 *    -added notifications settings (May 21, 2010).
 *    -removed user update, moved to NotificationSettingsActivity (June 2, 2010)
 */
public class PreferenceActivity extends android.preference.PreferenceActivity {

    final private class ChangeSyncTask extends AsyncTask<Boolean,Object,Boolean[]> {
        @Override
        protected Boolean[] doInBackground(Boolean... enableSync) {
            Sync sync = Foursquared.get(PreferenceActivity.this).getSync();
            return new Boolean[] { sync.setEnabled(enableSync[0]), enableSync[0] };
        }

        @Override
        protected void onPostExecute(Boolean[] updateFlagAndNewState) {
            if (updateFlagAndNewState[0]) {
                CheckBoxPreference syncPref = (CheckBoxPreference)getPreferenceScreen().findPreference(Preferences.PREFERENCE_SYNC_CONTACTS);
                syncPref.setChecked(updateFlagAndNewState[1]);
            }
            Preference pref = getSyncPref();
            pref.setEnabled(true);
            pref.setOnPreferenceChangeListener(syncChangeListener);
        }
    }



    private static final String TAG = "PreferenceActivity";

    private static final boolean DEBUG = FoursquaredSettings.DEBUG;


    private SharedPreferences mPrefs;
    
    private OnPreferenceChangeListener syncChangeListener;

    private BroadcastReceiver mLoggedOutReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (DEBUG) Log.d(TAG, "onReceive: " + intent);
            finish();
        }
    };

    private CheckBoxPreference getSyncPref() {
        return (CheckBoxPreference)getPreferenceScreen().findPreference(Preferences.PREFERENCE_SYNC_CONTACTS);
    }


    @Override
    protected void onResume() {
        super.onResume();

        CheckBoxPreference syncPref = getSyncPref();

        syncPref.setChecked(Foursquared.get(this).getSync().isEnabled());
        if ( syncChangeListener != null ) {
            syncPref.setOnPreferenceChangeListener(syncChangeListener);
        }
    }
    
    @Override
    protected void onPause() {
        Preference syncContactsPreference = getPreferenceScreen().findPreference(Preferences.PREFERENCE_SYNC_CONTACTS);
        syncContactsPreference.setOnPreferenceChangeListener(null);
        super.onPause();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerReceiver(mLoggedOutReceiver, new IntentFilter(Foursquared.INTENT_ACTION_LOGGED_OUT));

        syncChangeListener = new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                preference.setOnPreferenceChangeListener(null);
                preference.setEnabled(false);
                new ChangeSyncTask().execute((Boolean)newValue);
                return false;
{'fraction_non_alphanumeric': 0.08759902498476539, 'fraction_numerical': 0.005179768433881779, 'mean_word_length': 3.639575971731449, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '254860', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1713, 'n_words': 344}