<commit_before>package com.ternaryop.photoshelf;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.ternaryop.utils.IOUtils;

public class ImageUrlRetriever {
	private final Context context;
	private String title;
	private Exception error = null;
	private Map<String, String> urlSelectorMap = new HashMap<String, String>();
	private ActionMode actionMode;
	private final OnImagesRetrieved callback;
	private boolean useActionMode;
	private boolean useFile;
	private ArrayList<String> imageUrls;
	private ArrayList<File> imageFiles;

	public ImageUrlRetriever(Context context, OnImagesRetrieved callback) {
		this.context = context;
		this.callback = callback;
		useActionMode = true;
	}

	public void addOrRemoveUrl(String domSelector, String url) {
		if (urlSelectorMap.get(url) == null) {
			urlSelectorMap.put(url, domSelector);
		} else {
			urlSelectorMap.remove(url);
		}
		if (useActionMode) {
			if (urlSelectorMap.size() == 0) {
				getActionMode((Activity) context).finish();
			} else {
				getActionMode((Activity) context).invalidate();
			}
		}
	}

	protected ActionMode getActionMode(Activity activity) {
		if (actionMode == null) {
			actionMode = activity.startActionMode(mActionModeCallback);
		}
		return actionMode;
	}

	public String getTitle() {
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}

	private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			MenuInflater inflater = mode.getMenuInflater();
			inflater.inflate(R.menu.action_context, menu);
			return true;
		}

		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			mode.setTitle(context.getString(R.string.select_images));
	        mode.setSubtitle(context.getResources().getQuantityString(
	                R.plurals.selected_items,
	                urlSelectorMap.size(),
	                urlSelectorMap.size()));
			return true;
		}

		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
		    useFile = item.getItemId() == R.id.create_from_file;

		    switch (item.getItemId()) {
			case R.id.showDialog:
				retrieve();
				mode.finish();
				return true;
            case R.id.create_from_file:
                retrieve();
                mode.finish();
                return true;
			default:
				return false;
			}
		}

		// Called when the user exits the action mode
		public void onDestroyActionMode(ActionMode mode) {
			actionMode = null;
		}
	};

	public void retrieve() {
		new UrlRetrieverAsyncTask().execute(urlSelectorMap);
	}

	public boolean isUseActionMode() {
		return useActionMode;
	}

	public void setUseActionMode(boolean useActionMode) {
		this.useActionMode = useActionMode;
	}

	public interface OnImagesRetrieved {
		public void onImagesRetrieved(ImageUrlRetriever imageUrlRetriever);
	}

	class UrlRetrieverAsyncTask extends AsyncTask<Object, Integer, Void> {
		private ProgressDialog progressDialog;

		@Override
		protected void onPreExecute() {
			progressDialog = new ProgressDialog(context);
			progressDialog.setMessage(context.getString(R.string.image_retriever_title));
			progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			progressDialog.setMax(urlSelectorMap.size());
			progressDialog.show();
		}
<commit_msg>Clear selection when action mode is closed<commit_after>package com.ternaryop.photoshelf;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.ternaryop.utils.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class ImageUrlRetriever {
	private final Context context;
	private String title;
	private Exception error = null;
	private Map<String, String> urlSelectorMap = new HashMap<String, String>();
	private ActionMode actionMode;
	private final OnImagesRetrieved callback;
	private boolean useActionMode;
	private boolean useFile;
	private ArrayList<String> imageUrls;
	private ArrayList<File> imageFiles;

	public ImageUrlRetriever(Context context, OnImagesRetrieved callback) {
		this.context = context;
		this.callback = callback;
		useActionMode = true;
	}

	public void addOrRemoveUrl(String domSelector, String url) {
		if (urlSelectorMap.get(url) == null) {
			urlSelectorMap.put(url, domSelector);
		} else {
			urlSelectorMap.remove(url);
		}
		if (useActionMode) {
			if (urlSelectorMap.size() == 0) {
				getActionMode((Activity) context).finish();
			} else {
				getActionMode((Activity) context).invalidate();
			}
		}
	}

	protected ActionMode getActionMode(Activity activity) {
		if (actionMode == null) {
			actionMode = activity.startActionMode(mActionModeCallback);
		}
		return actionMode;
	}

	public String getTitle() {
		return title;
	}


	public void setTitle(String title) {
		this.title = title;
	}

	private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
		public boolean onCreateActionMode(ActionMode mode, Menu menu) {
			MenuInflater inflater = mode.getMenuInflater();
			inflater.inflate(R.menu.action_context, menu);
			return true;
		}

		public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
			mode.setTitle(context.getString(R.string.select_images));
	        mode.setSubtitle(context.getResources().getQuantityString(
	                R.plurals.selected_items,
	                urlSelectorMap.size(),
	                urlSelectorMap.size()));
			return true;
		}

		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
		    useFile = item.getItemId() == R.id.create_from_file;

		    switch (item.getItemId()) {
			case R.id.showDialog:
				retrieve();
				mode.finish();
				return true;
            case R.id.create_from_file:
                retrieve();
                mode.finish();
                return true;
			default:
				return false;
			}
		}

		// Called when the user exits the action mode
		public void onDestroyActionMode(ActionMode mode) {
			actionMode = null;
            // remove selection to ensures it's empty if the user clicks on 'done' actionbar's button
            urlSelectorMap.clear();
		}
	};

	public void retrieve() {
		new UrlRetrieverAsyncTask().execute(urlSelectorMap);
	}

	public boolean isUseActionMode() {
		return useActionMode;
	}

	public void setUseActionMode(boolean useActionMode) {
		this.useActionMode = useActionMode;
	}

	public interface OnImagesRetrieved {
		public void onImagesRetrieved(ImageUrlRetriever imageUrlRetriever);
	}

	class UrlRetrieverAsyncTask extends AsyncTask<Object, Integer, Void> {
		private ProgressDialog progressDialog;

		@Override
		protected void onPreExecute() {
			progressDialog = new ProgressDialog(context);
			progressDialog.setMessage(context.getString(R.string.image_retriever_title));
			progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			progressDialog.setMax(urlSelectorMap.size());
			progressDialog.show();
		}

{'fraction_non_alphanumeric': 0.09628681177976953, 'fraction_numerical': 0.00025608194622279127, 'mean_word_length': 4.115258677144729, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '2764542', 'n_tokens_mistral': 2557, 'n_tokens_neox': 2289, 'n_words': 525}