Skip to content

Implementation guide

kamyar haqqani edited this page Nov 28, 2019 · 12 revisions

Adding the FileManager

the FileManager is a fragment that can be integrated inside of Activity;

there are two main modes of file managment:

  1. MODE_SINGLE_FILE_SELECTION:

    This mode allows the user to browse through files and folders and select the desired file or folder. by clicking, or long-clicking on each file or folder FileItem_onClickListener or FileItem_onLongClickListener would get called accordingly with the file clicked on passed as an argument and the developer can decide what happens to the file;

    This mode is good if you wanna let the user just select a single file or folder and complicated file operations isn't in mind;

implementation example:

public class MainActivity extends AppCompatActivity implements FileManager_states_listener {
    private easyFileManager easyfilemanager = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            easyfilemanager = new easyFileManager(this,"/sdcard",easyFileManager.MODE_SINGLE_FILE_SELECTION, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(easyfilemanager!=null) {
            FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
            tr.add(R.id.your_filemanager_fragment_view, easyfilemanager);
            tr.commit();
            easyfilemanager.add_on_FileItem_EventListener(new File_item_evens_listener() {
                @Override
                public void FileItem_onLongClickListener(File file) {
                    //decide what happens
                    Toast.makeText(getApplicationContext(),"the following "+(file.isFile()==true?"file":"folder")+" has been clicked on:\r\n"+file.getPath(),Toast.LENGTH_SHORT).show();
                }
                @Override
                public void FileItem_onClickListener(File file) {
                    //decide what happens
                    Toast.makeText(getApplicationContext(),"the following "+(file.isFile()==true?"file":"folder")+" has been clicked on:\r\n"+file.getPath(),Toast.LENGTH_SHORT).show();
                    if(easyFileManager.getFileType(file).contains("image"))easyfilemanager.show_pic(file);
                }
            });
        }
    }
    @Override
    public void onFile_Manager_Ready(easyFileManager fileManager) {

    }
}

  1. MODE_REGULAR_FILE_MANAGMENT;

    This mode allows the user to browse through files and folders and allows multiple files and folders to be selected; A list of selected files and folders can be acquired by calling get_selected_Files_list() on the easyFileManager instance;

    This mode has an options bar which allows the developer to define new file operations to be performed when they are clicked on;

implementation example:

public class MainActivity extends AppCompatActivity implements FileManager_states_listener {
    private easyFileManager easyfilemanager = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            easyfilemanager = new easyFileManager(this,"/sdcard",easyFileManager.MODE_SINGLE_FILE_SELECTION, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(easyfilemanager !=null) {
            FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
            tr.add(R.id.linear, easyfilemanager);
            tr.commit();
        }
    }
    @Override
    public void onFile_Manager_Ready(easyFileManager fileManager) {

    }
}

Adding file operation option to OptionsBar

Developers can add new file operation options to the OptionsBar by calling add_Option_to_OptionsBar(); Developer must assign a unique id to each option; Adding an option with the same id as another option would override the previous option; This function accepts bitmap as the option's icon;

Options can be set to become visible only when at least one file or folder is selected; This can be set by passing True for is_available_only_when_files_are_selected;

There are 4 already existing options which perform copy, cut, delete, create new folder operations and their assigned option ids are stored in easyFileManager's static final variables OPTION_COPY_ID, OPTION_CUT_ID, OPTION_DELETE_ID and OPTION_NEW_FOLDER_ID accordingly; you can add the by calling add_common_options_and_operations function after the filemanager fragment is fully commited and brought to view; If these common options are used the File manager might become unavailable at times that user using these options; the state variable can be checked to see whether filemanager is usable or not.

Example of adding a new file operation option OptionsBar:


@Override
public void onFile_Manager_Ready(easyFileManager fileManager) {
        try {
            // if(fileManager.state==easyFileManager.STATE_BROWSING_DIRS)continue to the following code
            fileManager.add_Option_to_OptionsBar(BitmapFactory.decodeResource(getApplicationContext().getResources(),
                    R.drawable.my_options_icon_image), new Option_item_events_listener() {
                @Override
                public void Option_item_OnClickListener(List<File> selected_Files_list) {
                    String s="";
                    for (File file : selected_Files_list) {
                        s=s+file.getName()+" ";
                    }
                    s=s+"are selected.";
                    Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
                }

                @Override
                public void Option_item_OnlongClickListener(List<File> selected_Files_list) {

                }
            }, "option_id_0",false);
        }catch (Exception e){
            e.printStackTrace();
        }
}

Options can be removed by calling remove_Option_from_OptionsBar and passing the option's id as argument; A list of all the options in the OptionsBar can be acquired by calling get_list_of_option_IDs;

Example of removing an option:


        if(easyfileManager.FileManager_is_ready) {
            try {
                easyfileManager.remove_Option_from_OptionsBar("option_id_0");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

Adding icon for certain file extentions

The developer can select the icon for all file extensions except the ones are used for images; The file manager would automatically create a lower quality icon from the image; The icon for each file extension can be selected by calling add_Icon_for_file_type().

        easyfilemanager.add_Icon_for_file_type(BitmapFactory.decodeResource(getResources(),R.drawable.icon_image_for_movies),"mp4");
        easyfilemanager.add_Icon_for_file_type(BitmapFactory.decodeResource(getResources(),R.drawable.icon_image_for_sounds),"mp3");
        easyfilemanager.add_Icon_for_file_type(BitmapFactory.decodeResource(getResources(),R.drawable.icon_image_for_movies),"mkv");

Adding and removing file and folder extension filters

Files and folders can be filtered in two ways; either only certain name extensions are allowed or all extensions are allowed but certain are removed;

Example for excluding all file name extensions except jpg and png:

easyfilemanager.allowed_file_extentions_list.add("png");
easyfilemanager.allowed_file_extentions_list.add("jpg");

Example of including all folder name extension except ".thumbnail":

easyfilemanager.excluded_folder_extentions_list.add("thumbnails");

Navigating to a directory

After the filemanager is ready you can direct the filemanager to browse a specific diretory by calling navigate_to_directory function and passing the directory file as an argument; navigate_to_parent_directory can be called if you want to just navigate to the previous(parent) directory;

Navigating to the previous (parent) directory or state( if internal file operation options are added) after pressing the back button;

The file manager does not listen directly for the back button click event; By calling handle_Back_button_pressed_event function the file manager would perform either of these:

  1. go back to the default state if the file manager choosing is a folder for paste operation
  2. unselect all selected file items if any is selected
  3. navigate to the previous ( parent) directory.

So this funtion can be called in back pressed event listener like below:

public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            try {
                if(easyfilemanager !=null) easyfilemanager.handle_Back_button_pressed_event();
                return false;
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
        }
        return super.onKeyDown(keyCode, event);
}