<commit_before>package com.michaldabski.filemanager.folders;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ShareActionProvider;
import android.widget.TextView;
import android.widget.Toast;

import com.michaldabski.filemanager.AppPreferences;
import com.michaldabski.filemanager.FileManagerApplication;
import com.michaldabski.filemanager.R;
import com.michaldabski.filemanager.clipboard.Clipboard;
import com.michaldabski.filemanager.clipboard.FileOperationListener;
import com.michaldabski.filemanager.clipboard.Clipboard.FileAction;
import com.michaldabski.filemanager.favourites.FavouriteFolder;
import com.michaldabski.filemanager.favourites.FavouritesManager;
import com.michaldabski.filemanager.favourites.FavouritesManager.FolderAlreadyFavouriteException;
import com.michaldabski.filemanager.folders.FileAdapter.OnFileSelectedListener;
import com.michaldabski.utils.AsyncResult;
import com.michaldabski.utils.FileUtils;
import com.michaldabski.utils.FontApplicator;
import com.michaldabski.utils.IntentUtils;
import com.michaldabski.utils.OnResultListener;
import com.readystatesoftware.systembartint.SystemBarTintManager;

public class FolderFragment extends Fragment implements OnItemClickListener, OnScrollListener, OnItemLongClickListener, MultiChoiceModeListener, OnFileSelectedListener
{
	private static final String LOG_TAG = "FolderFragment";
	private final int DISTANCE_TO_HIDE_ACTIONBAR = 0; 
	public static final String 
		EXTRA_DIR = "directory",
		EXTRA_SELECTED_FILES = "selected_files",
		EXTRA_SCROLL_POSITION = "scroll_position";
	File currentDir,
		nextDir = null;
	int topVisibleItem=0;
	List<File> files = null;
	@SuppressWarnings("rawtypes")
	AsyncTask loadFilesTask=null;
	ListView listView = null;
	FileAdapter fileAdapter;
	private ActionMode actionMode = null;
	private final HashSet<File> selectedFiles = new HashSet<File>();
	private ShareActionProvider shareActionProvider;
	// set to true when selection shouldnt be cleared from switching out fragments
	boolean preserveSelection = false;
	
	public ListView getListView()
	{
		return listView;
	}
	
	private void setListAdapter(FileAdapter fileAdapter)
	{
		this.fileAdapter = fileAdapter;
		if (listView != null)
		{
			listView.setAdapter(fileAdapter);
			listView.setSelection(topVisibleItem);
			
			getView().findViewById(R.id.layoutMessage).setVisibility(View.GONE);
			listView.setVisibility(View.VISIBLE);
		}
	}
	
	FontApplicator getFontApplicator()
	{
		FolderActivity folderActivity = (FolderActivity) getActivity();
		return folderActivity.getFontApplicator();
	}
	
	void showProgress()
	{
		if (getView() != null)
		{
			getListView().setVisibility(View.GONE);
			getView().findViewById(R.id.layoutMessage).setVisibility(View.VISIBLE);
			getView().findViewById(R.id.tvMessage).setVisibility(View.GONE);
		}
	}
	
	FileManagerApplication getApplication()
	{
		if (getActivity() == null) return null;
		return (FileManagerApplication) getActivity().getApplication();
	}
	
	AppPreferences getPreferences()
	{
		if (getApplication() == null) return null;
		return getApplication().getAppPreferences();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
		
		Log.d(LOG_TAG, "Fragment created");
		
		if (savedInstanceState != null)
		{
			this.topVisibleItem = savedInstanceState.getInt(EXTRA_SCROLL_POSITION, 0);
			this.selectedFiles.addAll((HashSet<File>) savedInstanceState.getSerializable(EXTRA_SELECTED_FILES));
		}
		
		Bundle arguments = getArguments();
		
		if (arguments != null && arguments.containsKey(EXTRA_DIR))
			currentDir = new File(arguments.getString(EXTRA_DIR));
		else 
			currentDir = getPreferences().getStartFolder();
		
		setHasOptionsMenu(true);
		
		loadFileList();
	}
	
	void showMessage(CharSequence message)
	{
		View view = getView();
		if (view != null)
		{
			getListView().setVisibility(View.GONE);
			view.findViewById(R.id.layoutMessage).setVisibility(View.VISIBLE);
			view.findViewById(R.id.progress).setVisibility(View.GONE);
			TextView tvMessage = (TextView) view.findViewById(R.id.tvMessage);
			tvMessage.setText(message);
					
		}
	}
	
	void showMessage(int message)
	{
		showMessage(getString(message));
	}
	
	void showList()
	{
		getListView().setVisibility(View.VISIBLE);
		getView().findViewById(R.id.layoutMessage).setVisibility(View.GONE);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_list, container, false);
		this.listView = (ListView) view.findViewById(android.R.id.list);
		return view;
	}
	
	void loadFileList()
	{
		if (loadFilesTask != null) return;
		this.loadFilesTask = new AsyncTask<File, Void, AsyncResult<File[]>>()
		{
			@Override
			protected AsyncResult<File[]> doInBackground(File... params)
			{
				try
				{
					File[] files =params[0].listFiles(FileUtils.DEFAULT_FILE_FILTER);
					if (files == null)
						throw new NullPointerException(getString(R.string.cannot_read_directory_s, params[0].getName()));
					if (isCancelled())<commit_msg>Keep fastscroll visible in KitKat<commit_after>package com.michaldabski.filemanager.folders;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ShareActionProvider;
import android.widget.TextView;
import android.widget.Toast;

import com.michaldabski.filemanager.AppPreferences;
import com.michaldabski.filemanager.FileManagerApplication;
import com.michaldabski.filemanager.R;
import com.michaldabski.filemanager.clipboard.Clipboard;
import com.michaldabski.filemanager.clipboard.Clipboard.FileAction;
import com.michaldabski.filemanager.clipboard.FileOperationListener;
import com.michaldabski.filemanager.favourites.FavouriteFolder;
import com.michaldabski.filemanager.favourites.FavouritesManager;
import com.michaldabski.filemanager.favourites.FavouritesManager.FolderAlreadyFavouriteException;
import com.michaldabski.filemanager.folders.FileAdapter.OnFileSelectedListener;
import com.michaldabski.utils.AsyncResult;
import com.michaldabski.utils.FileUtils;
import com.michaldabski.utils.FontApplicator;
import com.michaldabski.utils.IntentUtils;
import com.michaldabski.utils.OnResultListener;
import com.readystatesoftware.systembartint.SystemBarTintManager;

public class FolderFragment extends Fragment implements OnItemClickListener, OnScrollListener, OnItemLongClickListener, MultiChoiceModeListener, OnFileSelectedListener
{
	private static final String LOG_TAG = "FolderFragment";
	private final int DISTANCE_TO_HIDE_ACTIONBAR = 0; 
	public static final String 
		EXTRA_DIR = "directory",
		EXTRA_SELECTED_FILES = "selected_files",
		EXTRA_SCROLL_POSITION = "scroll_position";
	File currentDir,
		nextDir = null;
	int topVisibleItem=0;
	List<File> files = null;
	@SuppressWarnings("rawtypes")
	AsyncTask loadFilesTask=null;
	ListView listView = null;
	FileAdapter fileAdapter;
	private ActionMode actionMode = null;
	private final HashSet<File> selectedFiles = new HashSet<File>();
	private ShareActionProvider shareActionProvider;
	// set to true when selection shouldnt be cleared from switching out fragments
	boolean preserveSelection = false;
	
	public ListView getListView()
	{
		return listView;
	}
	
	private void setListAdapter(FileAdapter fileAdapter)
	{
		this.fileAdapter = fileAdapter;
		if (listView != null)
		{
			listView.setAdapter(fileAdapter);
			listView.setSelection(topVisibleItem);
			
			getView().findViewById(R.id.layoutMessage).setVisibility(View.GONE);
			listView.setVisibility(View.VISIBLE);
		}
	}
	
	FontApplicator getFontApplicator()
	{
		FolderActivity folderActivity = (FolderActivity) getActivity();
		return folderActivity.getFontApplicator();
	}
	
	void showProgress()
	{
		if (getView() != null)
		{
			getListView().setVisibility(View.GONE);
			getView().findViewById(R.id.layoutMessage).setVisibility(View.VISIBLE);
			getView().findViewById(R.id.tvMessage).setVisibility(View.GONE);
		}
	}
	
	FileManagerApplication getApplication()
	{
		if (getActivity() == null) return null;
		return (FileManagerApplication) getActivity().getApplication();
	}
	
	AppPreferences getPreferences()
	{
		if (getApplication() == null) return null;
		return getApplication().getAppPreferences();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
		
		Log.d(LOG_TAG, "Fragment created");
		
		if (savedInstanceState != null)
		{
			this.topVisibleItem = savedInstanceState.getInt(EXTRA_SCROLL_POSITION, 0);
			this.selectedFiles.addAll((HashSet<File>) savedInstanceState.getSerializable(EXTRA_SELECTED_FILES));
		}
		
		Bundle arguments = getArguments();
		
		if (arguments != null && arguments.containsKey(EXTRA_DIR))
			currentDir = new File(arguments.getString(EXTRA_DIR));
		else 
			currentDir = getPreferences().getStartFolder();
		
		setHasOptionsMenu(true);
		
		loadFileList();
	}
	
	void showMessage(CharSequence message)
	{
		View view = getView();
		if (view != null)
		{
			getListView().setVisibility(View.GONE);
			view.findViewById(R.id.layoutMessage).setVisibility(View.VISIBLE);
			view.findViewById(R.id.progress).setVisibility(View.GONE);
			TextView tvMessage = (TextView) view.findViewById(R.id.tvMessage);
			tvMessage.setText(message);
					
		}
	}
	
	void showMessage(int message)
	{
		showMessage(getString(message));
	}
	
	void showList()
	{
		getListView().setVisibility(View.VISIBLE);
		getView().findViewById(R.id.layoutMessage).setVisibility(View.GONE);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_list, container, false);
		this.listView = (ListView) view.findViewById(android.R.id.list);
		
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
			listView.setFastScrollAlwaysVisible(true);
		return view;
	}
	
	void loadFileList()
	{
		if (loadFilesTask != null) return;
		this.loadFilesTask = new AsyncTask<File, Void, AsyncResult<File[]>>()
		{
			@Override
			protected AsyncResult<File[]> doInBackground(File... params)
			{
				try
				{
					File[] files =params[0].listFiles(FileUtils.DEFAULT_FILE_FILTER);
					if (files == null)
						throw new NullPointerException(getString(R.string.cannot_read_directory_s, params[0].getName()));
					if (isCancelled())
{'fraction_non_alphanumeric': 0.10317460317460317, 'fraction_numerical': 0.000778089013383131, 'mean_word_length': 5.876939539860889, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, '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': '7057790', 'n_tokens_mistral': 4247, 'n_tokens_neox': 3824, 'n_words': 705}