<commit_before>package panaimin.pdfmarker;

/***
 Copyright (C) <2015>  <<NAME>>

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class FileSelector extends ListActivity implements View.OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_file_selector);
		_path = (TextView)findViewById(R.id.cd);
		_cd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
		_adapter = new FileSysAdapter();
		setListAdapter(_adapter);
		_handler = new Handler();
		_updater = new FileSysUpdater();
		_handler.post(_updater);
		findViewById(R.id.ib_search).setOnClickListener(this);
	}
	
	@Override
	public void onStop() {
		super.onStop();
		// return to the Main activity
		Intent intent = new Intent(this, RecentlyUsedActivity.class);
		setResult(RESULT_CANCELED, intent);
		finish();
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		FileSysObject obj = (FileSysObject)_adapter.getItem(position);
		if(obj._isDirectory) {
			_cd = obj._file;
			_handler.post(_updater);
			return;
		}
		// return to the Main activity
		Intent intent = new Intent(this, RecentlyUsedActivity.class);
		intent.putExtra("NAME", obj._file.getName());
		intent.putExtra("PATH", obj._file.getPath());
		setResult(RESULT_OK, intent);
		finish();
	}

	private void searchDirectory(File directory) {
		File[] files = directory.listFiles(new FileFilter() {
			public boolean accept(File file) {
				if (file.isDirectory())
					return false;
				String fname = file.getName().toLowerCase();
				return fname.endsWith(".pdf");
			}
		});
		if (files != null) {
			for (File f : files)
				_adapter.add(new FileSysObject(f, false, false));
		}
		File[] directories = directory.listFiles(new FileFilter() {
			public boolean accept(File file) {
				return file.isDirectory();
			}
		});
		if (directories != null) {
			for (File d : directories)
				searchDirectory(d);
		}
	}

	@Override
	public void onClick(View v) {
		_adapter.clear();
		File parent = _cd.getParentFile();
		if (parent != null)
			_adapter.add(new FileSysObject(parent, true, true));
		searchDirectory(_cd);
		_adapter.notifyDataSetChanged();
	}
	
	private TextView		_path;
	private File			_cd = null;
	private Handler			_handler;
	private FileSysUpdater	_updater = null;
	private FileSysAdapter	_adapter = null;
	
	// Runnable class that updates the file/directory list
	private class FileSysUpdater implements Runnable {
		@Override
		public void run() {
			_path.setText(_cd.getAbsolutePath());
			File parent = _cd.getParentFile();
			File[] directories = _cd.listFiles(new FileFilter() {
				public boolean accept(File file) {
					return file.isDirectory();
				}
			});
			if (directories == null)
				directories = new File[0];
			File[] files = _cd.listFiles(new FileFilter() {
				public boolean accept(File file) {
					if (file.isDirectory())
						return false;
					String fname = file.getName().toLowerCase();
					return fname.endsWith(".pdf");
				}
			});
			if (files == null)
				files = new File[0];
			Arrays.sort(files, new Comparator<File>() {
				public int compare(File arg0, File arg1) {
					return arg0.getName().compareToIgnoreCase(arg1.getName());
				}
			});
			Arrays.sort(directories, new Comparator<File>() {
				public int compare(File arg0, File arg1) {
					return arg0.getName().compareToIgnoreCase(arg1.getName());
				}
			});
			_adapter.clear();
			if (parent != null)
				_adapter.add(new FileSysObject(parent, true, true));
			for (File f : directories)
				_adapter.add(new FileSysObject(f, true, false));
			for (File f : files)
				_adapter.add(new FileSysObject(f, false, false));
			_adapter.notifyDataSetChanged();
		}
	}
	
	// FileSysObject
	private class FileSysObject {
		public File		_file;
		public boolean	_isDirectory;
		public boolean	_isParent;
		public FileSysObject(File file, boolean isDirectory, boolean isParent) {
			_file = file;
			_isDirectory = isDirectory;<commit_msg>notify can only be done at UI thread<commit_after>package panaimin.pdfmarker;

/***
 Copyright (C) <2015>  <<NAME>>

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class FileSelector extends ListActivity implements View.OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_file_selector);
		_path = (TextView)findViewById(R.id.cd);
		_cd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
		_adapter = new FileSysAdapter();
		setListAdapter(_adapter);
		_handler.post(_updater);
		findViewById(R.id.ib_search).setOnClickListener(this);
	}
	
	@Override
	public void onStop() {
		super.onStop();
		// return to the Main activity
		Intent intent = new Intent(this, RecentlyUsedActivity.class);
		setResult(RESULT_CANCELED, intent);
		finish();
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		FileSysObject obj = (FileSysObject)_adapter.getItem(position);
		if(obj._isDirectory) {
			_cd = obj._file;
			_handler.post(_updater);
			return;
		}
		// return to the Main activity
		Intent intent = new Intent(this, RecentlyUsedActivity.class);
		intent.putExtra("NAME", obj._file.getName());
		intent.putExtra("PATH", obj._file.getPath());
		setResult(RESULT_OK, intent);
		finish();
	}

	private void searchDirectory(File directory) {
		File[] files = directory.listFiles(new FileFilter() {
			public boolean accept(File file) {
				if (file.isDirectory())
					return false;
				String fname = file.getName().toLowerCase();
				return fname.endsWith(".pdf");
			}
		});
		if (files != null) {
			for (File f : files)
				_adapter.add(new FileSysObject(f, false, false));
		}
		File[] directories = directory.listFiles(new FileFilter() {
			public boolean accept(File file) {
				return file.isDirectory();
			}
		});
		if (directories != null) {
			for (File d : directories)
				searchDirectory(d);
		}
	}

	@Override
	public void onClick(View v) {
		_progressDialog = ProgressDialog.show(this, "Load", "Loading", true);
		new Thread(new Runnable() {
			@Override
			public void run()
			{
				_adapter.clear();
				File parent = _cd.getParentFile();
				if (parent != null)
					_adapter.add(new FileSysObject(parent, true, true));
				searchDirectory(_cd);
				_handler.post(_uiUpdator);
			}
		}).start();
	}
	
	private TextView		_path;
	private File			_cd = null;
	private Handler			_handler = new Handler();
	private Runnable		_updater = new Runnable() {
		@Override
		public void run() {
			_path.setText(_cd.getAbsolutePath());
			File parent = _cd.getParentFile();
			File[] directories = _cd.listFiles(new FileFilter() {
				public boolean accept(File file) {
					return file.isDirectory();
				}
			});
			if (directories == null)
				directories = new File[0];
			File[] files = _cd.listFiles(new FileFilter() {
				public boolean accept(File file) {
					if (file.isDirectory())
						return false;
					String fname = file.getName().toLowerCase();
					return fname.endsWith(".pdf");
				}
			});
			if (files == null)
				files = new File[0];
			Arrays.sort(files, new Comparator<File>() {
				public int compare(File arg0, File arg1) {
					return arg0.getName().compareToIgnoreCase(arg1.getName());
				}
			});
			Arrays.sort(directories, new Comparator<File>() {
				public int compare(File arg0, File arg1) {
					return arg0.getName().compareToIgnoreCase(arg1.getName());
				}
			});
			_adapter.clear();
			if (parent != null)
				_adapter.add(new FileSysObject(parent, true, true));
			for (File f : directories)
				_adapter.add(new FileSysObject(f, true, false));
			for (File f : files)
				_adapter.add(new FileSysObject(f, false, false));
			_adapter.notifyDataSetChanged();
		}
	};
	private FileSysAdapter	_adapter = null;
	private ProgressDialog	_progressDialog;
	private Runnable		_uiUpdator = new Runnable() {
		@Override
		public void run() {
			_adapter.notifyDataSetChanged();
			if (_progressDialog != null) {
				_progressDialog.dismiss();
				_progressDialog = null;
			}
		}
	};
	
	// FileSysObject
	private class FileSysObject {
		public File		_file;
		public boolean	_isDirectory;
		public boolean	_isParent;
		public FileSysObject(File file, boolean isDirectory, boolean isParent) {
			_file = file;
			_isDirectory = isDirectory;
{'fraction_non_alphanumeric': 0.11224300796443785, 'fraction_numerical': 0.0027782922763474717, 'mean_word_length': 4.086669806877061, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '4550952', 'n_tokens_mistral': 3707, 'n_tokens_neox': 3227, 'n_words': 921}