From 5ed4ab34ea4836db4def453dafc5bd7fe2d6cfe3 Mon Sep 17 00:00:00 2001 From: curioustechizen Date: Wed, 14 Aug 2013 17:07:46 +0530 Subject: [PATCH] Added option to specify a list of file extensions for narrowing the list of files. --- README.markdown | 23 +++++++++++++++ .../afilechooser/FileChooserActivity.java | 17 +++++++++-- .../afilechooser/FileListFragment.java | 13 +++++++-- .../com/ipaulpro/afilechooser/FileLoader.java | 7 +++-- .../afilechooser/utils/FileUtils.java | 29 +++++++++++++++++-- .../FileChooserExampleActivity.java | 18 +++++++++++- 6 files changed, 98 insertions(+), 9 deletions(-) diff --git a/README.markdown b/README.markdown index cb4619c..5243878 100644 --- a/README.markdown +++ b/README.markdown @@ -66,6 +66,29 @@ A more robust example can be found in the aFileChooserExample folder. __Note__ The `FileUtils` class provides a helper method to construct an `ACTION_GET_CONTENT` Intent (`FileUtils.createGetContentIntent()`). It also contains a method to convert a `Uri` into a `File` (`FileUtils.getFile(Uri)`). +###Filtering by file extension + +Provide an extra `EXTRA_FILTER_INCLUDE_EXTENSIONS` which is an `ArrayList` containing all the extensions that must be included. Note that the extentions must begin with a dot character. The behavior of this extra is specified as follows: + + - If this extra is specified, then **only** files with the supplied extensions will be shown. All other files will be hidden. + - If this extra is not specified, or it is an empty `ArrayList`, then no filtering is performed. + +Example: + +``` + private static final ArrayList INCLUDE_EXTENSIONS_LIST = new ArrayList(); + static{ + INCLUDE_EXTENSIONS_LIST.add(".apk"); + INCLUDE_EXTENSIONS_LIST.add(".bin"); + } + //... + //... + Intent intent = new Intent(this, FileChooserActivity.class); + intent.putStringArrayListExtra(FileChooserActivity.EXTRA_FILTER_INCLUDE_EXTENSIONS, INCLUDE_EXTENSIONS_LIST); + //Use this intent in startActivityForResult() + +``` + ## Credits Developed by Paul Burke (iPaulPro) - [paulburke.co](http://paulburke.co/) diff --git a/aFileChooser/src/com/ipaulpro/afilechooser/FileChooserActivity.java b/aFileChooser/src/com/ipaulpro/afilechooser/FileChooserActivity.java index a7e63bb..8d69492 100644 --- a/aFileChooser/src/com/ipaulpro/afilechooser/FileChooserActivity.java +++ b/aFileChooser/src/com/ipaulpro/afilechooser/FileChooserActivity.java @@ -16,6 +16,7 @@ package com.ipaulpro.afilechooser; +import android.annotation.SuppressLint; import android.app.ActionBar; import android.content.BroadcastReceiver; import android.content.Context; @@ -35,6 +36,8 @@ import android.widget.Toast; import java.io.File; +import java.util.ArrayList; +import java.util.List; /** * Main Activity that handles the FileListFragments @@ -48,6 +51,9 @@ public class FileChooserActivity extends FragmentActivity implements OnBackStackChangedListener { public static final String PATH = "path"; + public static final String EXTRA_FILTER_INCLUDE_EXTENSIONS = + "com.ipaulpro.afilechooser.EXTRA_FILTER_INCLUDE_EXTENSIONS"; + private ArrayList mFilterIncludeExtensions = new ArrayList(); public static final String EXTERNAL_BASE_PATH = Environment .getExternalStorageDirectory().getAbsolutePath(); @@ -69,6 +75,11 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.chooser); + + Intent intent = getIntent(); + if(intent != null){ + mFilterIncludeExtensions = intent.getStringArrayListExtra(EXTRA_FILTER_INCLUDE_EXTENSIONS); + } mFragmentManager = getSupportFragmentManager(); mFragmentManager.addOnBackStackChangedListener(this); @@ -104,6 +115,7 @@ protected void onSaveInstanceState(Bundle outState) { outState.putString(PATH, mPath); } + @SuppressLint("NewApi") // Usages of New APIs are surrounded by sufficient conditional checks @Override public void onBackStackChanged() { @@ -119,6 +131,7 @@ public void onBackStackChanged() { if (HAS_ACTIONBAR) invalidateOptionsMenu(); } + @SuppressLint("NewApi") // Usages of New APIs are surrounded by sufficient conditional checks @Override public boolean onCreateOptionsMenu(Menu menu) { if (HAS_ACTIONBAR) { @@ -147,7 +160,7 @@ public boolean onOptionsItemSelected(MenuItem item) { * Add the initial Fragment with given path. */ private void addFragment() { - FileListFragment fragment = FileListFragment.newInstance(mPath); + FileListFragment fragment = FileListFragment.newInstance(mPath, mFilterIncludeExtensions); mFragmentManager.beginTransaction() .add(R.id.explorer_fragment, fragment).commit(); } @@ -161,7 +174,7 @@ private void addFragment() { private void replaceFragment(File file) { mPath = file.getAbsolutePath(); - FileListFragment fragment = FileListFragment.newInstance(mPath); + FileListFragment fragment = FileListFragment.newInstance(mPath, mFilterIncludeExtensions); mFragmentManager.beginTransaction() .replace(R.id.explorer_fragment, fragment) .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) diff --git a/aFileChooser/src/com/ipaulpro/afilechooser/FileListFragment.java b/aFileChooser/src/com/ipaulpro/afilechooser/FileListFragment.java index 59420ef..79ffc70 100644 --- a/aFileChooser/src/com/ipaulpro/afilechooser/FileListFragment.java +++ b/aFileChooser/src/com/ipaulpro/afilechooser/FileListFragment.java @@ -25,6 +25,7 @@ import android.widget.ListView; import java.io.File; +import java.util.ArrayList; import java.util.List; /** @@ -42,6 +43,7 @@ public class FileListFragment extends ListFragment implements private FileListAdapter mAdapter; private String mPath; + private ArrayList mFilterIncludeExtensions = new ArrayList(); /** * Create a new instance with the given file path. @@ -49,10 +51,13 @@ public class FileListFragment extends ListFragment implements * @param path The absolute path of the file (directory) to display. * @return A new Fragment with the given file path. */ - public static FileListFragment newInstance(String path) { + public static FileListFragment newInstance(String path, ArrayList + filterIncludeExtensions) { FileListFragment fragment = new FileListFragment(); Bundle args = new Bundle(); args.putString(FileChooserActivity.PATH, path); + args.putStringArrayList(FileChooserActivity.EXTRA_FILTER_INCLUDE_EXTENSIONS, + filterIncludeExtensions); fragment.setArguments(args); return fragment; @@ -66,6 +71,10 @@ public void onCreate(Bundle savedInstanceState) { mPath = getArguments() != null ? getArguments().getString( FileChooserActivity.PATH) : Environment .getExternalStorageDirectory().getAbsolutePath(); + if(getArguments() != null){ + mFilterIncludeExtensions = getArguments().getStringArrayList( + FileChooserActivity.EXTRA_FILTER_INCLUDE_EXTENSIONS); + } } @Override @@ -91,7 +100,7 @@ public void onListItemClick(ListView l, View v, int position, long id) { @Override public Loader> onCreateLoader(int id, Bundle args) { - return new FileLoader(getActivity(), mPath); + return new FileLoader(getActivity(), mPath, mFilterIncludeExtensions); } @Override diff --git a/aFileChooser/src/com/ipaulpro/afilechooser/FileLoader.java b/aFileChooser/src/com/ipaulpro/afilechooser/FileLoader.java index cb36a2b..5f86a49 100644 --- a/aFileChooser/src/com/ipaulpro/afilechooser/FileLoader.java +++ b/aFileChooser/src/com/ipaulpro/afilechooser/FileLoader.java @@ -17,6 +17,7 @@ package com.ipaulpro.afilechooser; import java.io.File; +import java.util.ArrayList; import java.util.List; import android.content.Context; @@ -44,15 +45,17 @@ public class FileLoader extends AsyncTaskLoader> { private List mData; private String mPath; + private ArrayList mFilterIncludeExtensions; - public FileLoader(Context context, String path) { + public FileLoader(Context context, String path, ArrayList filterIncludeExtensions) { super(context); this.mPath = path; + this.mFilterIncludeExtensions = filterIncludeExtensions; } @Override public List loadInBackground() { - return FileUtils.getFileList(mPath); + return FileUtils.getFileList(mPath, mFilterIncludeExtensions); } @Override diff --git a/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java b/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java index ab49760..d6eb993 100644 --- a/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java +++ b/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java @@ -407,6 +407,31 @@ public int compare(File f1, File f2) { } }; + /** + * File Filter that includes only files with the specified extensions to pass + * @author Kiran Rao + * + */ + private static class FileExtensionFilter implements FileFilter{ + private ArrayList mFilterIncludeExtensions; + + public FileExtensionFilter(ArrayList filterIncludeExtensions){ + this.mFilterIncludeExtensions = filterIncludeExtensions; + } + + @Override + public boolean accept(File file) { + final String fileName = file.getName(); + boolean passesExtensionsFilter = mFilterIncludeExtensions.isEmpty() + ? true: mFilterIncludeExtensions.contains(getExtension(Uri + .fromFile(file).toString())); + // Return files only (not directories) and skip hidden files + return file.isFile() && !fileName.startsWith(HIDDEN_PREFIX) && + passesExtensionsFilter; + } + + } + /** * File (not directories) filter. * @@ -441,7 +466,7 @@ public boolean accept(File file) { * @author paulburke */ - public static List getFileList(String path) { + public static List getFileList(String path, ArrayList filterIncludeExtensions) { ArrayList list = new ArrayList(); // Current directory File instance @@ -457,7 +482,7 @@ public static List getFileList(String path) { } // List file in this directory with the file filter - final File[] files = pathDir.listFiles(mFileFilter); + final File[] files = pathDir.listFiles(new FileExtensionFilter(filterIncludeExtensions)); if (files != null) { // Sort the files alphabetically Arrays.sort(files, mComparator); diff --git a/aFileChooserExample/src/com/ipaulpro/afilechooserexample/FileChooserExampleActivity.java b/aFileChooserExample/src/com/ipaulpro/afilechooserexample/FileChooserExampleActivity.java index f5e306d..c2de62c 100644 --- a/aFileChooserExample/src/com/ipaulpro/afilechooserexample/FileChooserExampleActivity.java +++ b/aFileChooserExample/src/com/ipaulpro/afilechooserexample/FileChooserExampleActivity.java @@ -17,6 +17,7 @@ package com.ipaulpro.afilechooserexample; import java.io.File; +import java.util.ArrayList; import android.app.Activity; import android.content.ActivityNotFoundException; @@ -29,6 +30,7 @@ import android.widget.Button; import android.widget.Toast; +import com.ipaulpro.afilechooser.FileChooserActivity; import com.ipaulpro.afilechooser.utils.FileUtils; /** @@ -37,6 +39,11 @@ public class FileChooserExampleActivity extends Activity { private static final int REQUEST_CODE = 6384; // onActivityResult request code + private static final ArrayList INCLUDE_EXTENSIONS_LIST = new ArrayList(); + static{ + INCLUDE_EXTENSIONS_LIST.add(".apk"); + INCLUDE_EXTENSIONS_LIST.add(".bin"); + } @Override public void onCreate(Bundle savedInstanceState) { @@ -49,7 +56,8 @@ public void onCreate(Bundle savedInstanceState) { @Override public void onClick(View v) { // Display the file chooser dialog - showChooser(); + //showChooser(); + startFileChooserActivity(); } }); @@ -69,6 +77,14 @@ private void showChooser() { } } + private void startFileChooserActivity(){ + startActivityForResult( + new Intent(this, FileChooserActivity.class).putStringArrayListExtra( + FileChooserActivity.EXTRA_FILTER_INCLUDE_EXTENSIONS, INCLUDE_EXTENSIONS_LIST), + REQUEST_CODE); + + } + @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) {