<commit_before>/**
 * Copyright 2009 <NAME>
 */

package com.joelapenna.foursquared;

import com.joelapenna.foursquare.error.FoursquareError;
import com.joelapenna.foursquare.error.FoursquareParseException;
import com.joelapenna.foursquare.filters.VenueFilter;
import com.joelapenna.foursquare.types.Checkin;
import com.joelapenna.foursquare.types.Group;
import com.joelapenna.foursquare.types.Venue;
import com.joelapenna.foursquared.util.SeparatedListAdapter;
import com.joelapenna.foursquared.widget.CheckinListAdapter;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;

import java.io.IOException;

/**
 * @author <NAME> (<EMAIL>)
 */
public class VenueCheckinActivity extends ListActivity {
    public static final String TAG = "VenueCheckinActivity";
    public static final boolean DEBUG = Foursquared.DEBUG;

    private static final int DIALOG_CHECKIN = 0;

    private Venue mVenue;
    private Group mGroups;
    public Checkin mCheckin;

    private LookupCheckinsAsyncTask mCheckinTask;

    private Button mCheckinButton;
    private TextView mEmpty;
    private ToggleButton mSilentToggle;
    private ToggleButton mTwitterToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.venue_checkin_activity);

        setListAdapter(new SeparatedListAdapter(this));
        getListView().setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // fireVenueActivityIntent(venue);
            }
        });

        setupUi();
        mVenue = (Venue)getIntent().getExtras().get(Foursquared.EXTRAS_VENUE_KEY);

        if (getLastNonConfigurationInstance() != null) {
            setCheckinGroups((Group)getLastNonConfigurationInstance());
        } else {
            lookupCheckinGroups();
        }
    }

    @Override
    public Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_CHECKIN:
                Checkin checkin = mCheckin;
                if (DEBUG) Log.d(TAG, checkin.getUserid());
                if (DEBUG) Log.d(TAG, checkin.getMessage());
                if (DEBUG) Log.d(TAG, String.valueOf(checkin.status()));
                if (DEBUG) Log.d(TAG, checkin.getUrl());

                WebView webView = new WebView(this);
                webView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.FILL_PARENT));
                webView.loadUrl(checkin.getUrl());
                Spanned title = Html.fromHtml(checkin.getMessage());
                return new AlertDialog.Builder(this) // the builder
                        .setView(webView) // use a web view
                        .setIcon(android.R.drawable.ic_dialog_info) // show an icon
                        .setTitle(title).create(); // return it.
        }
        return null;
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        return mGroups;
    }

    private void setupUi() {
        mEmpty = (TextView)findViewById(android.R.id.empty);

        mCheckinButton = (Button)findViewById(R.id.checkinButton);
        mCheckinButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                sendCheckin();
            }
        });

        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

        mTwitterToggle = (ToggleButton)findViewById(R.id.twitterToggle);
        mSilentToggle = (ToggleButton)findViewById(R.id.silentToggle);
        mSilentToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mTwitterToggle.setChecked(false);
                    mTwitterToggle.setEnabled(false);
                } else {
                    mTwitterToggle.setEnabled(true);
                }
            }
        });

        mSilentToggle.setChecked(settings.getBoolean(Foursquared.PREFERENCE_SILENT_CHECKIN, false));
        mTwitterToggle.setChecked(settings
                .getBoolean(Foursquared.PREFERENCE_TWITTER_CHECKIN, false));
    }

    private void sendCheckin() {
        new AddCheckinAsyncTask().execute(mVenue);
    }

    private void lookupCheckinGroups() {
        if (DEBUG) Log.d(TAG, "lookupCheckin()");

        // If a task is already running, don't start a new one.
        if (mCheckinTask != null && mCheckinTask.getStatus() != AsyncTask.Status.FINISHED) {
            if (DEBUG) Log.d(TAG, "Query already running attempting to cancel: " + mCheckinTask);
            if (!mCheckinTask.cancel(true) && !mCheckinTask.isCancelled()) {
                if (DEBUG) Log.d(TAG, "Unable to cancel checkins? That should not have happened!");
                Toast.makeText(this, "Unable to re-query checkins.", Toast.LENGTH_SHORT);
                return;
            }
        }
        mCheckinTask = (LookupCheckinsAsyncTask)new LookupCheckinsAsyncTask().execute();
    }

    private void setCheckinGroups(Group groups) {
        if (groups == null) {
            Toast.makeText(getApplicationContext(), "Could not complete TODO lookup!",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        if (mVenue != null) {
            mGroups = VenueFilter.filter(groups, mVenue);
        } else {
            mGroups = groups;
        }
        putGroupsInAdapter(mGroups);
    }

    private void putGroupsInAdapter(Group groups) {
        if (DEBUG) Log.d(TAG, "Putting groups in adapter.");
        SeparatedListAdapter mainAdapter = (SeparatedListAdapter)getListAdapter();
        mainAdapter.clear();<commit_msg>stub for checkin click on venuecheckinactivity<commit_after>/**
 * Copyright 2009 <NAME>
 */

package com.joelapenna.foursquared;

import com.joelapenna.foursquare.error.FoursquareError;
import com.joelapenna.foursquare.error.FoursquareParseException;
import com.joelapenna.foursquare.filters.VenueFilter;
import com.joelapenna.foursquare.types.Checkin;
import com.joelapenna.foursquare.types.Group;
import com.joelapenna.foursquare.types.Venue;
import com.joelapenna.foursquared.util.SeparatedListAdapter;
import com.joelapenna.foursquared.widget.CheckinListAdapter;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;

import java.io.IOException;

/**
 * @author <NAME> (<EMAIL>)
 */
public class VenueCheckinActivity extends ListActivity {
    public static final String TAG = "VenueCheckinActivity";
    public static final boolean DEBUG = Foursquared.DEBUG;

    private static final int DIALOG_CHECKIN = 0;

    private Venue mVenue;
    private Group mGroups;
    public Checkin mCheckin;

    private LookupCheckinsAsyncTask mCheckinTask;

    private Button mCheckinButton;
    private TextView mEmpty;
    private ToggleButton mSilentToggle;
    private ToggleButton mTwitterToggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.venue_checkin_activity);

        setListAdapter(new SeparatedListAdapter(this));
        getListView().setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Checkin checkin = (Checkin)parent.getAdapter().getItem(position);
                startCheckinActivity(checkin);
            }
        });

        setupUi();
        mVenue = (Venue)getIntent().getExtras().get(Foursquared.EXTRAS_VENUE_KEY);

        if (getLastNonConfigurationInstance() != null) {
            setCheckinGroups((Group)getLastNonConfigurationInstance());
        } else {
            lookupCheckinGroups();
        }
    }

    @Override
    public Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_CHECKIN:
                Checkin checkin = mCheckin;
                if (DEBUG) Log.d(TAG, checkin.getUserid());
                if (DEBUG) Log.d(TAG, checkin.getMessage());
                if (DEBUG) Log.d(TAG, String.valueOf(checkin.status()));
                if (DEBUG) Log.d(TAG, checkin.getUrl());

                WebView webView = new WebView(this);
                webView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.FILL_PARENT));
                webView.loadUrl(checkin.getUrl());
                Spanned title = Html.fromHtml(checkin.getMessage());
                return new AlertDialog.Builder(this) // the builder
                        .setView(webView) // use a web view
                        .setIcon(android.R.drawable.ic_dialog_info) // show an icon
                        .setTitle(title).create(); // return it.
        }
        return null;
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        return mGroups;
    }

    private void setupUi() {
        mEmpty = (TextView)findViewById(android.R.id.empty);

        mCheckinButton = (Button)findViewById(R.id.checkinButton);
        mCheckinButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                sendCheckin();
            }
        });

        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

        mTwitterToggle = (ToggleButton)findViewById(R.id.twitterToggle);
        mSilentToggle = (ToggleButton)findViewById(R.id.silentToggle);
        mSilentToggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mTwitterToggle.setChecked(false);
                    mTwitterToggle.setEnabled(false);
                } else {
                    mTwitterToggle.setEnabled(true);
                }
            }
        });

        mSilentToggle.setChecked(settings.getBoolean(Foursquared.PREFERENCE_SILENT_CHECKIN, false));
        mTwitterToggle.setChecked(settings
                .getBoolean(Foursquared.PREFERENCE_TWITTER_CHECKIN, false));
    }

    private void sendCheckin() {
        new AddCheckinAsyncTask().execute(mVenue);
    }

    private void lookupCheckinGroups() {
        if (DEBUG) Log.d(TAG, "lookupCheckin()");

        // If a task is already running, don't start a new one.
        if (mCheckinTask != null && mCheckinTask.getStatus() != AsyncTask.Status.FINISHED) {
            if (DEBUG) Log.d(TAG, "Query already running attempting to cancel: " + mCheckinTask);
            if (!mCheckinTask.cancel(true) && !mCheckinTask.isCancelled()) {
                if (DEBUG) Log.d(TAG, "Unable to cancel checkins? That should not have happened!");
                Toast.makeText(this, "Unable to re-query checkins.", Toast.LENGTH_SHORT);
                return;
            }
        }
        mCheckinTask = (LookupCheckinsAsyncTask)new LookupCheckinsAsyncTask().execute();
    }

    private void setCheckinGroups(Group groups) {
        if (groups == null) {
            Toast.makeText(getApplicationContext(), "Could not complete TODO lookup!",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        if (mVenue != null) {
            mGroups = VenueFilter.filter(groups, mVenue);
        } else {
            mGroups = groups;
        }
        putGroupsInAdapter(mGroups);
    }

    void startCheckinActivity(Checkin checkin) {
        if (DEBUG) Log.d(TAG, "(not) firing checkin activity for checkin");
        // Intent intent = new Intent(VenueCheckinActivity.this, CheckinActivity.class);
        // intent.setAction(Intent.ACTION_VIEW);
        // intent.putExtra("venue", checkin);
        // startActivity(intent);
    }

    private void putGroupsInAdapter(Group groups) {
        if (DEBUG) Log.d(TAG, "Putting groups in adapter.");
        SeparatedListAdapter mainAdapter = (SeparatedListAdapter)getListAdapter();
        mainAdapter.clear();
{'fraction_non_alphanumeric': 0.09718051995606006, 'fraction_numerical': 0.0007323324789454412, 'mean_word_length': 2.7088538837588265, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3868588', 'n_tokens_mistral': 3891, 'n_tokens_neox': 3694, 'n_words': 748}