<commit_before>package com.mareksebera.dilbert;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.Calendar;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.CharArrayBuffer;
import org.joda.time.DateMidnight;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

/**
 * This code is roughly optimized and for sure could be improved.
 * 
 * I just needed better version of Dilbert reader than "Quick Dilbert Reader",
 * so I wrote one myself.
 * 
 * Links to images are parsed from dilbert website directly, as I haven't found
 * any better solution.
 * 
 * Supports caching images (Universal Image Loader feature) and caching parsing
 * ({@link #loadCacheUrl(String)}, {@link #saveCurrentUrl(String, String)},
 * {@link #loadLastUrl()})
 * */
public class DilbertActivity extends SherlockActivity implements
		OnDateSetListener, SwipeInterface, ImageLoadingListener {

	private static final int MENU_DATEPICKER = 1, MENU_ABOUT = 2,
			MENU_LATEST = 3, MENU_REFRESH = 4, MENU_LICENSE = 5;
	private static final String PREF_DATE = "dilbert_current_date";
	private static final String PREF_URL = "dilbert_current_url";
	private DateMidnight currentDate;
	private DateTimeFormatter dateFormatter = DateTimeFormat
			.forPattern("yyyy-MM-dd");

	private EnhancedImageView imageView;

	private ProgressBar progressBar;
	private FrameLayout layout;

	@Override
	public void bottom2top(View v) {
	}

	private void configureImageLoader() {
		DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
				.cacheInMemory().cacheOnDisc().build();
		ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
				this).defaultDisplayImageOptions(displayOptions).build();
		ImageLoader.getInstance().init(configuration);
	}

	public void displayImage(String url) {
		if (url != null) {
			Log.d("displayImage", url);
			ImageLoader.getInstance().displayImage(url, imageView,
					DilbertActivity.this);
		}
	}

	private void initLayout() {
		imageView = (EnhancedImageView) findViewById(R.id.imageview);
		progressBar = (ProgressBar) findViewById(R.id.progressbar);
		layout = (FrameLayout) findViewById(R.id.framelayout);
		layout.setOnTouchListener(new ActivitySwipeDetector(this));
	}

	@Override
	public void left2right(View v) {
		if (!currentDate.equals(new LocalDate(1989, 4, 16)))
			setCurrentDate(currentDate.minusDays(1));
		else
			Toast.makeText(this, R.string.no_older_strip, Toast.LENGTH_SHORT)
					.show();
	}

	private boolean loadCacheUrl(String dateKey) {
		String cached = PreferenceManager.getDefaultSharedPreferences(this)
				.getString(dateKey, null);
		if (cached == null) {
			return false;
		} else {
			displayImage(cached);
			return true;
		}
	}

	private void loadCurrentDate() {
		String savedDate = PreferenceManager.getDefaultSharedPreferences(this)
				.getString(PREF_DATE, null);
		if (savedDate == null)
			currentDate = DateMidnight.now();<commit_msg>enhancement due to findbugs report<commit_after>package com.mareksebera.dilbert;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.Calendar;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.CharArrayBuffer;
import org.joda.time.DateMidnight;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Html;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

/**
 * This code is roughly optimized and for sure could be improved.
 * 
 * I just needed better version of Dilbert reader than "Quick Dilbert Reader",
 * so I wrote one myself.
 * 
 * Links to images are parsed from dilbert website directly, as I haven't found
 * any better solution.
 * 
 * Supports caching images (Universal Image Loader feature) and caching parsing
 * ({@link #loadCacheUrl(String)}, {@link #saveCurrentUrl(String, String)},
 * {@link #loadLastUrl()})
 * */
public class DilbertActivity extends SherlockActivity implements
		OnDateSetListener, SwipeInterface, ImageLoadingListener {

	private static final int MENU_DATEPICKER = 1, MENU_ABOUT = 2,
			MENU_LATEST = 3, MENU_REFRESH = 4, MENU_LICENSE = 5;
	private static final String PREF_DATE = "dilbert_current_date";
	private static final String PREF_URL = "dilbert_current_url";
	private DateMidnight currentDate;
	private DateTimeFormatter dateFormatter = DateTimeFormat
			.forPattern("yyyy-MM-dd");

	private EnhancedImageView imageView;

	private ProgressBar progressBar;
	private FrameLayout layout;

	@Override
	public void bottom2top(View v) {
	}

	private void configureImageLoader() {
		DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
				.cacheInMemory().cacheOnDisc().build();
		ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
				this).defaultDisplayImageOptions(displayOptions).build();
		ImageLoader.getInstance().init(configuration);
	}

	public void displayImage(String url) {
		if (url != null) {
			Log.d("displayImage", url);
			ImageLoader.getInstance().displayImage(url, imageView,
					DilbertActivity.this);
		}
	}

	private void initLayout() {
		imageView = (EnhancedImageView) findViewById(R.id.imageview);
		progressBar = (ProgressBar) findViewById(R.id.progressbar);
		layout = (FrameLayout) findViewById(R.id.framelayout);
		layout.setOnTouchListener(new ActivitySwipeDetector(this));
	}

	@Override
	public void left2right(View v) {
		if (!currentDate.equals(DateMidnight.parse("1989-04-16", dateFormatter)))
			setCurrentDate(currentDate.minusDays(1));
		else
			Toast.makeText(this, R.string.no_older_strip, Toast.LENGTH_SHORT)
					.show();
	}

	private boolean loadCacheUrl(String dateKey) {
		String cached = PreferenceManager.getDefaultSharedPreferences(this)
				.getString(dateKey, null);
		if (cached == null) {
			return false;
		} else {
			displayImage(cached);
			return true;
		}
	}

	private void loadCurrentDate() {
		String savedDate = PreferenceManager.getDefaultSharedPreferences(this)
				.getString(PREF_DATE, null);
		if (savedDate == null)
			currentDate = DateMidnight.now();
{'fraction_non_alphanumeric': 0.09894398530762168, 'fraction_numerical': 0.005853994490358127, 'mean_word_length': 5.738592420726992, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3826458', 'n_tokens_mistral': 2794, 'n_tokens_neox': 2545, 'n_words': 577}