<commit_before>package nerd.tuxmobil.fahrplan.congress;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.provider.CalendarContract;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.widget.Toast;

public class FahrplanMisc {

	private static final String LOG_TAG = "FahrplanMisc";

	static void loadDays(Context context) {
		MyApp.dateList = new ArrayList<DateList>();
		LecturesDBOpenHelper lecturesDB = new LecturesDBOpenHelper(context);

		SQLiteDatabase lecturedb = lecturesDB.getReadableDatabase();
		Cursor cursor;

		try {
			cursor = lecturedb.query("lectures", LecturesDBOpenHelper.allcolumns,
					null, null, null,
					null, null);
		} catch (SQLiteException e) {
			e.printStackTrace();
			lecturedb.close();
			lecturesDB.close();
			return;
		}

		if (cursor.getCount() == 0) {
			// evtl. Datenbankreset wg. DB Formatänderung -> neu laden
			cursor.close();
			lecturesDB.close();
			lecturedb.close();
			return;
		}

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			int day = cursor.getInt(3);
			String date = cursor.getString(14);

			if (DateList.dateInList(MyApp.dateList, day) == false) {
				MyApp.dateList.add(new DateList(day, date));
			}
			cursor.moveToNext();
		}
		cursor.close();

		for (DateList dayL : MyApp.dateList) {
			MyApp.LogDebug(LOG_TAG, "date day " + dayL.dayIdx + " = " + dayL.date);
		}
		lecturesDB.close();
		lecturedb.close();
	}

	static void loadMeta(Context context) {
		MetaDBOpenHelper metaDB = new MetaDBOpenHelper(context);
		SQLiteDatabase metadb = metaDB.getReadableDatabase();

		Cursor cursor;
		try {
			cursor = metadb.query("meta", MetaDBOpenHelper.allcolumns, null, null,
					null, null, null);
		} catch (SQLiteException e) {
			e.printStackTrace();
			metaDB.close();
			metadb.close();
			metadb = null;
			return;
		}

		MyApp.numdays = 0;
		MyApp.version = "";
		MyApp.title = "";
		MyApp.subtitle = "";
		MyApp.dayChangeHour = 4;
		MyApp.dayChangeMinute = 0;
		MyApp.eTag = null;

		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			if (cursor.getColumnCount() > 0)
				MyApp.numdays = cursor.getInt(0);
			if (cursor.getColumnCount() > 1)
				MyApp.version = cursor.getString(1);
			if (cursor.getColumnCount() > 2)
				MyApp.title = cursor.getString(2);
			if (cursor.getColumnCount() > 3)
				MyApp.subtitle = cursor.getString(3);
			if (cursor.getColumnCount() > 4)
				MyApp.dayChangeHour = cursor.getInt(4);
			if (cursor.getColumnCount() > 5)
				MyApp.dayChangeMinute = cursor.getInt(5);
			if (cursor.getColumnCount() > 6)
				MyApp.eTag = cursor.getString(6);
		}

		MyApp.LogDebug(LOG_TAG, "loadMeta: numdays=" + MyApp.numdays + " version:"
				+ MyApp.version + " " + MyApp.title + " " + MyApp.eTag);
		cursor.close();

		metadb.close();
		metaDB.close();
	}

	public static void share(Context context, Lecture l) {
		Intent sendIntent = new Intent();
		sendIntent.setAction(Intent.ACTION_SEND);
		StringBuilder sb = new StringBuilder();
		Time time = l.getTime();
		sb.append(l.title).append("\n").append(DateFormat.format("E, MMMM dd, yyyy hh:mm", time.toMillis(true)));
		sb.append(", ").append(l.room).append("\n\n").append("http://events.ccc.de/congress/2013/Fahrplan/events/").append(l.lecture_id).append(".html");
		sendIntent.putExtra(Intent.EXTRA_TEXT, sb.toString());
		sendIntent.setType("text/plain");
		context.startActivity(sendIntent);
	}

	public static String getEventUrl(final Context context, final String eventId) {
		StringBuilder sb = new StringBuilder();
		sb.append(context.getString(R.string.schedule_domain_part));
		sb.append(context.getString(R.string.schedule_part));
		// TODO The event url can be localized by providing individual values
		// for `schedule_event_part` in `values` and `values-de`.
		sb.append(context.getString(R.string.schedule_event_part, eventId));
		return sb.toString();
	}

	@SuppressLint("NewApi")
	public static void addToCalender(Context context, Lecture l) {
		Intent intent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);

		intent.putExtra(CalendarContract.Events.TITLE, l.title);
		intent.putExtra(CalendarContract.Events.EVENT_LOCATION, l.room);

		long when;
		if (l.dateUTC > 0) {
			when = l.dateUTC;
		} else {
			Time time = l.getTime();
			when = time.normalize(true);<commit_msg>Re-use existing helper method to compile share url.<commit_after>package nerd.tuxmobil.fahrplan.congress;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.provider.CalendarContract;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.widget.Toast;

public class FahrplanMisc {

	private static final String LOG_TAG = "FahrplanMisc";

	static void loadDays(Context context) {
		MyApp.dateList = new ArrayList<DateList>();
		LecturesDBOpenHelper lecturesDB = new LecturesDBOpenHelper(context);

		SQLiteDatabase lecturedb = lecturesDB.getReadableDatabase();
		Cursor cursor;

		try {
			cursor = lecturedb.query("lectures", LecturesDBOpenHelper.allcolumns,
					null, null, null,
					null, null);
		} catch (SQLiteException e) {
			e.printStackTrace();
			lecturedb.close();
			lecturesDB.close();
			return;
		}

		if (cursor.getCount() == 0) {
			// evtl. Datenbankreset wg. DB Formatänderung -> neu laden
			cursor.close();
			lecturesDB.close();
			lecturedb.close();
			return;
		}

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			int day = cursor.getInt(3);
			String date = cursor.getString(14);

			if (DateList.dateInList(MyApp.dateList, day) == false) {
				MyApp.dateList.add(new DateList(day, date));
			}
			cursor.moveToNext();
		}
		cursor.close();

		for (DateList dayL : MyApp.dateList) {
			MyApp.LogDebug(LOG_TAG, "date day " + dayL.dayIdx + " = " + dayL.date);
		}
		lecturesDB.close();
		lecturedb.close();
	}

	static void loadMeta(Context context) {
		MetaDBOpenHelper metaDB = new MetaDBOpenHelper(context);
		SQLiteDatabase metadb = metaDB.getReadableDatabase();

		Cursor cursor;
		try {
			cursor = metadb.query("meta", MetaDBOpenHelper.allcolumns, null, null,
					null, null, null);
		} catch (SQLiteException e) {
			e.printStackTrace();
			metaDB.close();
			metadb.close();
			metadb = null;
			return;
		}

		MyApp.numdays = 0;
		MyApp.version = "";
		MyApp.title = "";
		MyApp.subtitle = "";
		MyApp.dayChangeHour = 4;
		MyApp.dayChangeMinute = 0;
		MyApp.eTag = null;

		if (cursor.getCount() > 0) {
			cursor.moveToFirst();
			if (cursor.getColumnCount() > 0)
				MyApp.numdays = cursor.getInt(0);
			if (cursor.getColumnCount() > 1)
				MyApp.version = cursor.getString(1);
			if (cursor.getColumnCount() > 2)
				MyApp.title = cursor.getString(2);
			if (cursor.getColumnCount() > 3)
				MyApp.subtitle = cursor.getString(3);
			if (cursor.getColumnCount() > 4)
				MyApp.dayChangeHour = cursor.getInt(4);
			if (cursor.getColumnCount() > 5)
				MyApp.dayChangeMinute = cursor.getInt(5);
			if (cursor.getColumnCount() > 6)
				MyApp.eTag = cursor.getString(6);
		}

		MyApp.LogDebug(LOG_TAG, "loadMeta: numdays=" + MyApp.numdays + " version:"
				+ MyApp.version + " " + MyApp.title + " " + MyApp.eTag);
		cursor.close();

		metadb.close();
		metaDB.close();
	}

	public static void share(Context context, Lecture l) {
		Intent sendIntent = new Intent();
		sendIntent.setAction(Intent.ACTION_SEND);
		StringBuilder sb = new StringBuilder();
		Time time = l.getTime();
		sb.append(l.title).append("\n").append(DateFormat.format("E, MMMM dd, yyyy hh:mm", time.toMillis(true)));
		sb.append(", ").append(l.room).append("\n\n");
		final String eventUrl = getEventUrl(context, l.lecture_id);
		sb.append(eventUrl);
		sendIntent.putExtra(Intent.EXTRA_TEXT, sb.toString());
		sendIntent.setType("text/plain");
		context.startActivity(sendIntent);
	}

	public static String getEventUrl(final Context context, final String eventId) {
		StringBuilder sb = new StringBuilder();
		sb.append(context.getString(R.string.schedule_domain_part));
		sb.append(context.getString(R.string.schedule_part));
		// TODO The event url can be localized by providing individual values
		// for `schedule_event_part` in `values` and `values-de`.
		sb.append(context.getString(R.string.schedule_event_part, eventId));
		return sb.toString();
	}

	@SuppressLint("NewApi")
	public static void addToCalender(Context context, Lecture l) {
		Intent intent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);

		intent.putExtra(CalendarContract.Events.TITLE, l.title);
		intent.putExtra(CalendarContract.Events.EVENT_LOCATION, l.room);

		long when;
		if (l.dateUTC > 0) {
			when = l.dateUTC;
		} else {
			Time time = l.getTime();
			when = time.normalize(true);
{'fraction_non_alphanumeric': 0.13645898888189636, 'fraction_numerical': 0.005244388504300398, 'mean_word_length': 4.294280955024986, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '2491259', 'n_tokens_mistral': 3566, 'n_tokens_neox': 3157, 'n_words': 630}