<commit_before>package de.danoeh.antennapod.util.flattr;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.ListIterator;
import java.util.TimeZone;

import org.shredzone.flattr4j.FlattrService;
import org.shredzone.flattr4j.exception.FlattrException;
import org.shredzone.flattr4j.model.Flattr;
import org.shredzone.flattr4j.model.Thing;
import org.shredzone.flattr4j.oauth.AccessToken;
import org.shredzone.flattr4j.oauth.AndroidAuthenticator;
import org.shredzone.flattr4j.oauth.Scope;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.FlattrAuthActivity;
import de.danoeh.antennapod.asynctask.FlattrTokenFetcher;
import de.danoeh.antennapod.storage.DBWriter;

/** Utility methods for doing something with flattr. */

public class FlattrUtils {
	private static final String TAG = "FlattrUtils";

	private static final String HOST_NAME = "de.danoeh.antennapod";

	private static final String PREF_ACCESS_TOKEN = "de.danoeh.antennapod.preference.flattrAccessToken";

	// Flattr URL for this app.
	public static final String APP_URL = "http://antennapod.com";
	// Human-readable flattr-page.
	public static final String APP_LINK = "https://flattr.com/thing/745609/";
	public static final String APP_THING_ID = "745609";

	private static volatile AccessToken cachedToken;

	private static AndroidAuthenticator createAuthenticator() {
		return new AndroidAuthenticator(HOST_NAME, FlattrConfig.APP_KEY,
				FlattrConfig.APP_SECRET);
	}

	public static void startAuthProcess(Context context) throws FlattrException {
		AndroidAuthenticator auth = createAuthenticator();
		auth.setScope(EnumSet.of(Scope.FLATTR));
		Intent intent = auth.createAuthenticateIntent();
		context.startActivity(intent);
	}

	private static AccessToken retrieveToken() {
		if (cachedToken == null) {
			if (AppConfig.DEBUG)
				Log.d(TAG, "Retrieving access token");
			String token = PreferenceManager.getDefaultSharedPreferences(
					PodcastApp.getInstance())
					.getString(PREF_ACCESS_TOKEN, null);
			if (token != null) {
				if (AppConfig.DEBUG)
					Log.d(TAG, "Found access token. Caching.");
				cachedToken = new AccessToken(token);
			} else {
				if (AppConfig.DEBUG)
					Log.d(TAG, "No access token found");
				return null;
			}
		}
		return cachedToken;

	}

	public static boolean hasToken() {
		return retrieveToken() != null;
	}

	public static void storeToken(AccessToken token) {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Storing token");
		SharedPreferences.Editor editor = PreferenceManager
				.getDefaultSharedPreferences(PodcastApp.getInstance()).edit();
		if (token != null) {
			editor.putString(PREF_ACCESS_TOKEN, token.getToken());
		} else {
			editor.putString(PREF_ACCESS_TOKEN, null);
		}
		editor.commit();
		cachedToken = token;
	}

	public static void deleteToken() {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Deleting flattr token");
		storeToken(null);
	}

	public static Thing getAppThing(Context context) {
		FlattrService fs = FlattrServiceCreator.getService(retrieveToken());
		try {
			Thing thing = fs.getThing(Thing.withId(APP_THING_ID));
			return thing;
		} catch (FlattrException e) {
			e.printStackTrace();
			showErrorDialog(context, e.getMessage());
			return null;
		}
	}

	public static void clickUrl(Context context, String url)
			throws FlattrException {
		if (hasToken()) {
			FlattrService fs = FlattrServiceCreator.getService(retrieveToken());
			fs.click(url);
		} else {
			Log.e(TAG, "clickUrl was called with null access token");
		}
	}
	
	public static List<Flattr> retrieveFlattredThings() 
		throws FlattrException {
		ArrayList<Flattr> myFlattrs = new ArrayList<Flattr>();

		if (hasToken()) {
			FlattrService fs = FlattrServiceCreator.getService(retrieveToken());

			Calendar firstOfMonth = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
			firstOfMonth.set(Calendar.MILLISECOND, 0);
			firstOfMonth.set(Calendar.SECOND, 0);
			firstOfMonth.set(Calendar.MINUTE, 0);
			firstOfMonth.set(Calendar.HOUR_OF_DAY, 0);
			firstOfMonth.set(Calendar.DAY_OF_MONTH, Calendar.getInstance().getActualMinimum(Calendar.DAY_OF_MONTH));
			
			Date firstOfMonthDate = firstOfMonth.getTime();

			// subscriptions some times get flattrd slightly before midnight - give it an hour leeway
			firstOfMonthDate = new Date(firstOfMonthDate.getTime() - 60*60*1000);
			
			final int FLATTR_COUNT = 30;
			final int FLATTR_MAXPAGE = 5;
			
			int page = 0;
			do {
				myFlattrs.ensureCapacity(FLATTR_COUNT*(page+1));
				
				for (Flattr fl: fs.getMyFlattrs(FLATTR_COUNT, page)) {
					if (fl.getCreated().after(firstOfMonthDate))
						myFlattrs.add(fl);
					else
						break;
				}
				page++;
			}
			while (myFlattrs.get(myFlattrs.size()-1).getCreated().after( firstOfMonthDate ) && page < FLATTR_MAXPAGE);
			
			if (AppConfig.DEBUG) {
				Log.d(TAG, "Got my flattrs list of length " + Integer.toString(myFlattrs.size()) + " comparison date" + firstOfMonthDate);

				for (Flattr fl: myFlattrs) {
					Thing thing = fl.getThing();
					Log.d(TAG, "Flattr thing: " + fl.getThingId() + " name: " + thing.getTitle() +  " url: " + thing.getUrl() + " on: " + fl.getCreated());
				}
			}
			
		} else {
			Log.e(TAG, "retrieveFlattrdThings was called with null access token");
		}
		
		return myFlattrs;
	}

	public static void handleCallback(Context context, Uri uri) {
		AndroidAuthenticator auth = createAuthenticator();
		new FlattrTokenFetcher(context, auth, uri).executeAsync();
	}<commit_msg>Possible fix of issue #365<commit_after>package de.danoeh.antennapod.util.flattr;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.EnumSet;
import java.util.List;
import java.util.ListIterator;
import java.util.TimeZone;

import org.shredzone.flattr4j.FlattrService;
import org.shredzone.flattr4j.exception.FlattrException;
import org.shredzone.flattr4j.model.Flattr;
import org.shredzone.flattr4j.model.Thing;
import org.shredzone.flattr4j.oauth.AccessToken;
import org.shredzone.flattr4j.oauth.AndroidAuthenticator;
import org.shredzone.flattr4j.oauth.Scope;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.PodcastApp;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.FlattrAuthActivity;
import de.danoeh.antennapod.asynctask.FlattrTokenFetcher;
import de.danoeh.antennapod.storage.DBWriter;

/** Utility methods for doing something with flattr. */

public class FlattrUtils {
	private static final String TAG = "FlattrUtils";

	private static final String HOST_NAME = "de.danoeh.antennapod";

	private static final String PREF_ACCESS_TOKEN = "de.danoeh.antennapod.preference.flattrAccessToken";

	// Flattr URL for this app.
	public static final String APP_URL = "http://antennapod.com";
	// Human-readable flattr-page.
	public static final String APP_LINK = "https://flattr.com/thing/745609/";
	public static final String APP_THING_ID = "745609";

	private static volatile AccessToken cachedToken;

	private static AndroidAuthenticator createAuthenticator() {
		return new AndroidAuthenticator(HOST_NAME, FlattrConfig.APP_KEY,
				FlattrConfig.APP_SECRET);
	}

	public static void startAuthProcess(Context context) throws FlattrException {
		AndroidAuthenticator auth = createAuthenticator();
		auth.setScope(EnumSet.of(Scope.FLATTR));
		Intent intent = auth.createAuthenticateIntent();
		context.startActivity(intent);
	}

	private static AccessToken retrieveToken() {
		if (cachedToken == null) {
			if (AppConfig.DEBUG)
				Log.d(TAG, "Retrieving access token");
			String token = PreferenceManager.getDefaultSharedPreferences(
					PodcastApp.getInstance())
					.getString(PREF_ACCESS_TOKEN, null);
			if (token != null) {
				if (AppConfig.DEBUG)
					Log.d(TAG, "Found access token. Caching.");
				cachedToken = new AccessToken(token);
			} else {
				if (AppConfig.DEBUG)
					Log.d(TAG, "No access token found");
				return null;
			}
		}
		return cachedToken;

	}

	public static boolean hasToken() {
		return retrieveToken() != null;
	}

	public static void storeToken(AccessToken token) {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Storing token");
		SharedPreferences.Editor editor = PreferenceManager
				.getDefaultSharedPreferences(PodcastApp.getInstance()).edit();
		if (token != null) {
			editor.putString(PREF_ACCESS_TOKEN, token.getToken());
		} else {
			editor.putString(PREF_ACCESS_TOKEN, null);
		}
		editor.commit();
		cachedToken = token;
	}

	public static void deleteToken() {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Deleting flattr token");
		storeToken(null);
	}

	public static Thing getAppThing(Context context) {
		FlattrService fs = FlattrServiceCreator.getService(retrieveToken());
		try {
			Thing thing = fs.getThing(Thing.withId(APP_THING_ID));
			return thing;
		} catch (FlattrException e) {
			e.printStackTrace();
			showErrorDialog(context, e.getMessage());
			return null;
		}
	}

	public static void clickUrl(Context context, String url)
			throws FlattrException {
		if (hasToken()) {
			FlattrService fs = FlattrServiceCreator.getService(retrieveToken());
			fs.click(url);
		} else {
			Log.e(TAG, "clickUrl was called with null access token");
		}
	}
	
	public static List<Flattr> retrieveFlattredThings() 
		throws FlattrException {
		ArrayList<Flattr> myFlattrs = new ArrayList<Flattr>();

		if (hasToken()) {
			FlattrService fs = FlattrServiceCreator.getService(retrieveToken());

			Calendar firstOfMonth = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
			firstOfMonth.set(Calendar.MILLISECOND, 0);
			firstOfMonth.set(Calendar.SECOND, 0);
			firstOfMonth.set(Calendar.MINUTE, 0);
			firstOfMonth.set(Calendar.HOUR_OF_DAY, 0);
			firstOfMonth.set(Calendar.DAY_OF_MONTH, Calendar.getInstance().getActualMinimum(Calendar.DAY_OF_MONTH));
			
			Date firstOfMonthDate = firstOfMonth.getTime();

			// subscriptions some times get flattrd slightly before midnight - give it an hour leeway
			firstOfMonthDate = new Date(firstOfMonthDate.getTime() - 60*60*1000);
			
			final int FLATTR_COUNT = 30;
			final int FLATTR_MAXPAGE = 5;
			
			for (int page = 0; page < FLATTR_MAXPAGE; page++) {
				for (Flattr fl: fs.getMyFlattrs(FLATTR_COUNT, page)) {
					if (fl.getCreated().after(firstOfMonthDate))
						myFlattrs.add(fl);
					else
						break;
				}
			}
			
			if (AppConfig.DEBUG) {
				Log.d(TAG, "Got my flattrs list of length " + Integer.toString(myFlattrs.size()) + " comparison date" + firstOfMonthDate);

				for (Flattr fl: myFlattrs) {
					Thing thing = fl.getThing();
					Log.d(TAG, "Flattr thing: " + fl.getThingId() + " name: " + thing.getTitle() +  " url: " + thing.getUrl() + " on: " + fl.getCreated());
				}
			}
			
		} else {
			Log.e(TAG, "retrieveFlattrdThings was called with null access token");
		}
		
		return myFlattrs;
	}

	public static void handleCallback(Context context, Uri uri) {
		AndroidAuthenticator auth = createAuthenticator();
		new FlattrTokenFetcher(context, auth, uri).executeAsync();
	}
{'fraction_non_alphanumeric': 0.11306125926557042, 'fraction_numerical': 0.006390048564369089, 'mean_word_length': 4.515977443609023, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '526291', 'n_tokens_mistral': 4306, 'n_tokens_neox': 3795, 'n_words': 814}