<commit_before>package org.svij.taskwarriorapp;

import java.util.ArrayList;
import java.util.List;

import net.simonvt.widget.MenuDrawer;
import net.simonvt.widget.MenuDrawerManager;

import org.svij.taskwarriorapp.data.Task;
import org.svij.taskwarriorapp.db.TaskArrayAdapter;
import org.svij.taskwarriorapp.db.TaskDataSource;
import org.svij.taskwarriorapp.ui.MenuListView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.ActionMode.Callback;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class TasksActivity extends SherlockFragmentActivity {
	TaskDataSource datasource;

	private MenuDrawerManager mMenuDrawer;

	private MenuAdapter mAdapter;
	private MenuListView mList;

	private int mActivePosition = -1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
		super.onCreate(savedInstanceState);

		mMenuDrawer = new MenuDrawerManager(this, MenuDrawer.MENU_DRAG_WINDOW);
		mMenuDrawer.setContentView(R.layout.activity_main);

		if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
			ArrayListFragment list = new ArrayListFragment();
			getSupportFragmentManager().beginTransaction()
					.add(android.R.id.content, list).commit();
		}

		// TODO: Dummy Menu for now. Implement 'real' Menu later with icons.
		List<Object> items = new ArrayList<Object>();
		items.add(new Category("task commands"));
		items.add(new Item("task next"));
		items.add(new Item("task long"));
		items.add(new Category("Projects"));
		items.add(new Item("project1"));
		items.add(new Item("project2"));

		// A custom ListView is needed so the drawer can be notified when it's
		// scrolled. This is to update the position
		// of the arrow indicator.
		mList = new MenuListView(this);
		mAdapter = new MenuAdapter(items);
		mList.setAdapter(mAdapter);
		mList.setOnItemClickListener(mItemClickListener);
		mList.setOnScrollChangedListener(new MenuListView.OnScrollChangedListener() {
			@Override
			public void onScrollChanged() {
				mMenuDrawer.getMenuDrawer().invalidate();
			}
		});

		mMenuDrawer.setMenuView(mList);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}

	private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			mActivePosition = position;
			mMenuDrawer.setActiveView(view, position);
			mMenuDrawer.closeMenu();
		}
	};

	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onBackPressed() {
		final int drawerState = mMenuDrawer.getDrawerState();
		if (drawerState == MenuDrawer.STATE_OPEN
				|| drawerState == MenuDrawer.STATE_OPENING) {
			mMenuDrawer.closeMenu();
			return;
		}

		super.onBackPressed();
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.task_add:
			Intent intent = new Intent(this, TaskAddActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		case android.R.id.home:
			mMenuDrawer.toggleMenu();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onResume() {
		try {
			datasource.open();
		} catch (Exception e) {
		} finally {
			super.onResume();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	public static class ArrayListFragment extends SherlockListFragment {

		/**
		 *
		 * This class is based on GnuCash Mobiles "AccountsActivity.java" by
		 * <NAME> <<EMAIL>>
		 *
		 * Licensed under the Apache License, Version 2.0 (the "License"); you
		 * may not use this file except in compliance with the License. You may
		 * obtain a copy of the License at
		 *
		 * http://www.apache.org/licenses/LICENSE-2.0
		 *
		 * Unless required by applicable law or agreed to in writing, software
		 * distributed under the License is distributed on an "AS IS" BASIS,
		 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
		 * implied. See the License for the specific language governing
		 * permissions and limitations under the License.
		 */

		TaskDataSource datasource;
		ArrayAdapter<Task> adapter = null;
		@SuppressWarnings("unused")
		private boolean inEditMode = false;
		@SuppressWarnings("unused")
		private ActionMode actionMode = null;
		private int selectedViewPosition = -1;
		private long selectedItemId = -1;

		private ActionMode.Callback actionModeCallbacks = new Callback() {

			@Override
			public boolean onCreateActionMode(ActionMode mode, Menu menu) {
				MenuInflater inflater = mode.getMenuInflater();
				inflater.inflate(R.menu.context_menu, menu);
				mode.setTitle(getString(R.string.task_selected, 1));
				return true;
			}

			@Override
			public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
				// nothing to see here, move along
				return false;
			}

			@Override
			public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
				switch (item.getItemId()) {
				case R.id.context_menu_edit_task:
					showAddTaskActivity(getTaskWithId(selectedItemId));
					return true;

				case R.id.context_menu_delete_task:
					deleteTask(getTaskWithId(selectedItemId));
					mode.finish();
					return true;

				case R.id.context_menu_done_task:
					doneTask(getTaskWithId(selectedItemId));
					mode.finish();
					return true;

				default:
					return false;
				}
			}

			@Override
			public void onDestroyActionMode(ActionMode mode) {
				finishEditMode();
			}

			private void showAddTaskActivity(long selectedItemId) {
				Intent intent = new Intent(getActivity(), TaskAddActivity.class);
				intent.putExtra("taskID", selectedItemId);
				startActivity(intent);
			}

			private void deleteTask(long selectedItemId) {
				datasource.deleteTask(selectedItemId);
				refreshListView();
			}

			private void doneTask(long selectedItemId) {
				datasource.doneTask(selectedItemId);
				refreshListView();
			}

			private long getTaskWithId(long selectedItemId) {
				return ((Task) getListAdapter().getItem(
						(int) selectedItemId - 1)).getId();
			}
		};

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);

			datasource = new TaskDataSource(getActivity());
			datasource.open();

			refreshListView();
		}

		public void refreshListView() {
			ArrayList<Task> values = datasource.getAllTasks();
			adapter = new TaskArrayAdapter(getActivity(), R.layout.task_row,
					values);
			setListAdapter(adapter);
		}

		@Override
		public void onListItemClick(ListView l, View v, int position, long id) {
			Log.i("FragmentList", "Item clicked: " + id);

			inEditMode = true;
			selectedItemId = id + 1;
			// Start the CAB using the ActionMode.Callback defined above
			Log.i("ListItem clicked: ", Integer.toString(position));
			actionMode = getSherlockActivity().startActionMode(
					actionModeCallbacks);

			selectItem(position);
		}

		@Override
		public void onResume() {
			try {<commit_msg>Changed ItemClick to LongItemClick on a task.<commit_after>package org.svij.taskwarriorapp;

import java.util.ArrayList;
import java.util.List;

import net.simonvt.widget.MenuDrawer;
import net.simonvt.widget.MenuDrawerManager;

import org.svij.taskwarriorapp.data.Task;
import org.svij.taskwarriorapp.db.TaskArrayAdapter;
import org.svij.taskwarriorapp.db.TaskDataSource;
import org.svij.taskwarriorapp.ui.MenuListView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.view.ActionMode;
import com.actionbarsherlock.view.ActionMode.Callback;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class TasksActivity extends SherlockFragmentActivity {
	TaskDataSource datasource;

	private MenuDrawerManager mMenuDrawer;

	private MenuAdapter mAdapter;
	private MenuListView mList;

	private int mActivePosition = -1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setTheme(R.style.Theme_Sherlock_Light_DarkActionBar);
		super.onCreate(savedInstanceState);

		mMenuDrawer = new MenuDrawerManager(this, MenuDrawer.MENU_DRAG_WINDOW);
		mMenuDrawer.setContentView(R.layout.activity_main);

		if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
			ArrayListFragment list = new ArrayListFragment();
			getSupportFragmentManager().beginTransaction()
					.add(android.R.id.content, list).commit();
		}

		// TODO: Dummy Menu for now. Implement 'real' Menu later with icons.
		List<Object> items = new ArrayList<Object>();
		items.add(new Category("task commands"));
		items.add(new Item("task next"));
		items.add(new Item("task long"));
		items.add(new Category("Projects"));
		items.add(new Item("project1"));
		items.add(new Item("project2"));

		// A custom ListView is needed so the drawer can be notified when it's
		// scrolled. This is to update the position
		// of the arrow indicator.
		mList = new MenuListView(this);
		mAdapter = new MenuAdapter(items);
		mList.setAdapter(mAdapter);
		mList.setOnItemClickListener(mItemClickListener);
		mList.setOnScrollChangedListener(new MenuListView.OnScrollChangedListener() {
			@Override
			public void onScrollChanged() {
				mMenuDrawer.getMenuDrawer().invalidate();
			}
		});

		mMenuDrawer.setMenuView(mList);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}

	private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			mActivePosition = position;
			mMenuDrawer.setActiveView(view, position);
			mMenuDrawer.closeMenu();
		}
	};

	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	public void onBackPressed() {
		final int drawerState = mMenuDrawer.getDrawerState();
		if (drawerState == MenuDrawer.STATE_OPEN
				|| drawerState == MenuDrawer.STATE_OPENING) {
			mMenuDrawer.closeMenu();
			return;
		}

		super.onBackPressed();
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.task_add:
			Intent intent = new Intent(this, TaskAddActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		case android.R.id.home:
			mMenuDrawer.toggleMenu();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onResume() {
		try {
			datasource.open();
		} catch (Exception e) {
		} finally {
			super.onResume();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	public static class ArrayListFragment extends SherlockListFragment {

		/**
		 *
		 * This class is based on GnuCash Mobiles "AccountsActivity.java" by
		 * <NAME> <<EMAIL>>
		 *
		 * Licensed under the Apache License, Version 2.0 (the "License"); you
		 * may not use this file except in compliance with the License. You may
		 * obtain a copy of the License at
		 *
		 * http://www.apache.org/licenses/LICENSE-2.0
		 *
		 * Unless required by applicable law or agreed to in writing, software
		 * distributed under the License is distributed on an "AS IS" BASIS,
		 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
		 * implied. See the License for the specific language governing
		 * permissions and limitations under the License.
		 */

		TaskDataSource datasource;
		ArrayAdapter<Task> adapter = null;
		private boolean inEditMode = false;
		private ActionMode actionMode = null;
		private int selectedViewPosition = -1;
		private long selectedItemId = -1;

		private ActionMode.Callback actionModeCallbacks = new Callback() {

			@Override
			public boolean onCreateActionMode(ActionMode mode, Menu menu) {
				MenuInflater inflater = mode.getMenuInflater();
				inflater.inflate(R.menu.context_menu, menu);
				mode.setTitle(getString(R.string.task_selected, 1));
				return true;
			}

			@Override
			public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
				// nothing to see here, move along
				return false;
			}

			@Override
			public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
				switch (item.getItemId()) {
				case R.id.context_menu_edit_task:
					showAddTaskActivity(getTaskWithId(selectedItemId));
					return true;

				case R.id.context_menu_delete_task:
					deleteTask(getTaskWithId(selectedItemId));
					mode.finish();
					return true;

				case R.id.context_menu_done_task:
					doneTask(getTaskWithId(selectedItemId));
					mode.finish();
					return true;

				default:
					return false;
				}
			}

			@Override
			public void onDestroyActionMode(ActionMode mode) {
				finishEditMode();
			}

			private void showAddTaskActivity(long selectedItemId) {
				Intent intent = new Intent(getActivity(), TaskAddActivity.class);
				intent.putExtra("taskID", selectedItemId);
				startActivity(intent);
			}

			private void deleteTask(long selectedItemId) {
				datasource.deleteTask(selectedItemId);
				refreshListView();
			}

			private void doneTask(long selectedItemId) {
				datasource.doneTask(selectedItemId);
				refreshListView();
			}

			private long getTaskWithId(long selectedItemId) {
				return ((Task) getListAdapter().getItem(
						(int) selectedItemId - 1)).getId();
			}
		};

		@Override
		public void onActivityCreated(Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);

			datasource = new TaskDataSource(getActivity());
			datasource.open();

			ListView listview = getListView();
			listview.setOnItemLongClickListener(new OnItemLongClickListener() {

				@Override
				public boolean onItemLongClick(AdapterView<?> parent,
						View view, int position, long id) {
					if (actionMode != null) {
						return false;
					}
					inEditMode = true;
					selectedItemId = id + 1;
					// Start the CAB using the ActionMode.Callback defined
					// above
					actionMode = getSherlockActivity().startActionMode(
							actionModeCallbacks);

					selectItem(position);
					return true;
				}
			});

			refreshListView();
		}

		public void refreshListView() {
			ArrayList<Task> values = datasource.getAllTasks();
			adapter = new TaskArrayAdapter(getActivity(), R.layout.task_row,
					values);
			setListAdapter(adapter);
		}

		@Override
		public void onResume() {
			try {
{'fraction_non_alphanumeric': 0.10028412759912178, 'fraction_numerical': 0.0015497869043006587, 'mean_word_length': 4.331153184165232, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '679888', 'n_tokens_mistral': 5401, 'n_tokens_neox': 4542, 'n_words': 1083}