+ *
+ * @param callingActivity activity opening the chooser
+ * @param requestCode request code used to identiefy the result
+ * @param filterIncludeExtensions file extensions to display
+ */
+ public static void startActivity (
+ final android.app.Activity callingActivity,
+ final int requestCode,
+ final java.util.ArrayList filterIncludeExtensions) {
+ //android.util.Log.d (TAG, "+ startActivity");
+ //android.util.Log.v (TAG, "> callingActivity = " + callingActivity);
+ //android.util.Log.v (TAG, "> requestCode = " + requestCode);
+ //android.util.Log.v (TAG, "> filterIncludeExtensions = " + filterIncludeExtensions);
+
+ final Intent intent = new Intent (callingActivity, FileChooserActivity.class);
+
+ intent.putStringArrayListExtra (
+ FileChooserActivity.EXTRA_FILTER_INCLUDE_EXTENSIONS,
+ filterIncludeExtensions);
+
+ //android.util.Log.v (TAG, "> intent = " + intent);
+
+ try {
+ callingActivity.startActivityForResult (intent, requestCode);
+ } catch (@NotNull final android.content.ActivityNotFoundException e) {
+ // The reason for the existence of aFileChooser
+ android.util.Log.e (TAG, "LOG02230:", e);
+ }
+
+ //android.util.Log.d (TAG, "- startActivity");
+ return;
+ } // startActivity
+
+ /**
+ *
start activity
+ *
+ * @param callingActivity activity opening the chooser
+ * @param requestCode request code used to identify the result
+ * @param baseDirectory base directory to show
+ * @param filterIncludeExtensions file extensions to display
+ */
+ public static void startActivity (
+ final android.app.Activity callingActivity,
+ final int requestCode,
+ final String baseDirectory,
+ final java.util.ArrayList filterIncludeExtensions) {
+ //android.util.Log.d (TAG, "+ startActivity");
+ //android.util.Log.v (TAG, "> callingActivity = " + callingActivity);
+ //android.util.Log.v (TAG, "> requestCode = " + requestCode);
+ //android.util.Log.v (TAG, "> baseDirectory = " + baseDirectory);
+ //android.util.Log.v (TAG, "> filterIncludeExtensions = " + filterIncludeExtensions);
+
+ final Intent intent = new Intent (callingActivity, FileChooserActivity.class);
+
+ intent.putExtra (
+ FileChooserActivity.EXTRA_FILTER_BASE_PATH,
+ baseDirectory);
+ intent.putStringArrayListExtra (
+ FileChooserActivity.EXTRA_FILTER_INCLUDE_EXTENSIONS,
+ filterIncludeExtensions);
+
+ //android.util.Log.v (TAG, "> intent = " + intent);
+
+ try {
+ callingActivity.startActivityForResult (intent, requestCode);
+ } catch (@NotNull final android.content.ActivityNotFoundException e) {
+ // The reason for the existence of aFileChooser
+ android.util.Log.e (TAG, "LOG02230:", e);
+ }
+
+ //android.util.Log.d (TAG, "- startActivity");
+ return;
+ } // startActivity
private FragmentManager mFragmentManager;
- private BroadcastReceiver mStorageListener = new BroadcastReceiver() {
+ @NotNull private final BroadcastReceiver mStorageListener = new BroadcastReceiver() {
@Override
- public void onReceive(Context context, Intent intent) {
+ public void onReceive(final Context context, final Intent intent) {
Toast.makeText(context, R.string.storage_removed, Toast.LENGTH_LONG).show();
finishWithResult(null);
}
};
-
+ /**
+ *
path to open first
+ */
private String mPath;
+ /**
+ *
extenstion to display
+ */
+ private ArrayList mFilterIncludeExtensions = new ArrayList ();
@Override
- protected void onCreate(Bundle savedInstanceState) {
+ protected void onCreate(@Nullable final Bundle savedInstanceState) {
+ //android.util.Log.d (TAG, "+ onCreate");
+ //android.util.Log.v (TAG, "> savedInstanceState = " + savedInstanceState);
+
super.onCreate(savedInstanceState);
+ final Intent intent = getIntent();
+
+ //android.util.Log.v (TAG, "> intent = " + intent);
+
+ if(intent != null){
+ mFilterIncludeExtensions = intent.getStringArrayListExtra (
+ EXTRA_FILTER_INCLUDE_EXTENSIONS);
+ mPath = intent.getStringExtra (
+ EXTRA_FILTER_BASE_PATH);
+ //android.util.Log.v (TAG, "> mFilterIncludeExtensions = " + mFilterIncludeExtensions);
+ //android.util.Log.v (TAG, "> mPath = " + mPath);
+ }
mFragmentManager = getSupportFragmentManager();
- mFragmentManager.addOnBackStackChangedListener(this);
+ mFragmentManager.addOnBackStackChangedListener (this);
if (savedInstanceState == null) {
- mPath = EXTERNAL_BASE_PATH;
+ if (mPath == null) {
+ mPath = EXTERNAL_BASE_PATH;
+ } // if
addFragment();
} else {
- mPath = savedInstanceState.getString(PATH);
+ mPath = savedInstanceState.getString(SAVE_INSTANCE_PATH);
}
- setTitle(mPath);
+ setTitle(mPath);
+
+ //android.util.Log.d (TAG, "- onCreate");
}
@Override
@@ -94,34 +208,37 @@ protected void onResume() {
}
@Override
- protected void onSaveInstanceState(Bundle outState) {
+ protected void onSaveInstanceState(@NotNull final Bundle outState) {
super.onSaveInstanceState(outState);
- outState.putString(PATH, mPath);
+ outState.putString(SAVE_INSTANCE_PATH, mPath);
}
+ @SuppressLint("NewApi") // Usages of New APIs are surrounded by sufficient conditional checks
@Override
public void onBackStackChanged() {
- int count = mFragmentManager.getBackStackEntryCount();
+ final int count = mFragmentManager.getBackStackEntryCount();
if (count > 0) {
- BackStackEntry fragment = mFragmentManager.getBackStackEntryAt(count - 1);
+ final BackStackEntry fragment = mFragmentManager.getBackStackEntryAt(count - 1);
mPath = fragment.getName();
} else {
mPath = EXTERNAL_BASE_PATH;
}
setTitle(mPath);
- if (HAS_ACTIONBAR)
- invalidateOptionsMenu();
+ if (HAS_ACTIONBAR) {
+ invalidateOptionsMenu ();
+ }
}
+ @SuppressLint("NewApi") // Usages of New APIs are surrounded by sufficient conditional checks
@Override
- public boolean onCreateOptionsMenu(Menu menu) {
+ public boolean onCreateOptionsMenu(final Menu menu) {
if (HAS_ACTIONBAR) {
- boolean hasBackStack = mFragmentManager.getBackStackEntryCount() > 0;
+ final boolean hasBackStack = mFragmentManager.getBackStackEntryCount() > 0;
+ final ActionBar actionBar = getActionBar();
- ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(hasBackStack);
actionBar.setHomeButtonEnabled(hasBackStack);
}
@@ -130,7 +247,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
}
@Override
- public boolean onOptionsItemSelected(MenuItem item) {
+ public boolean onOptionsItemSelected(@NotNull final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mFragmentManager.popBackStack();
@@ -144,7 +261,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
* Add the initial Fragment with given path.
*/
private void addFragment() {
- FileListFragment fragment = FileListFragment.newInstance(mPath);
+ final FileListFragment fragment = FileListFragment.newInstance(
+ mPath,
+ mFilterIncludeExtensions);
mFragmentManager.beginTransaction()
.add(android.R.id.content, fragment).commit();
}
@@ -155,10 +274,12 @@ private void addFragment() {
*
* @param file The file (directory) to display.
*/
- private void replaceFragment(File file) {
+ private void replaceFragment(@NotNull final File file) {
mPath = file.getAbsolutePath();
- FileListFragment fragment = FileListFragment.newInstance(mPath);
+ final FileListFragment fragment = FileListFragment.newInstance(
+ mPath,
+ mFilterIncludeExtensions);
mFragmentManager.beginTransaction()
.replace(android.R.id.content, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
@@ -170,9 +291,9 @@ private void replaceFragment(File file) {
*
* @param file The file selected.
*/
- private void finishWithResult(File file) {
+ private void finishWithResult(@Nullable final File file) {
if (file != null) {
- Uri uri = Uri.fromFile(file);
+ final Uri uri = Uri.fromFile(file);
setResult(RESULT_OK, new Intent().setData(uri));
finish();
} else {
@@ -187,7 +308,7 @@ private void finishWithResult(File file) {
* @param file The file that was selected
*/
@Override
- public void onFileSelected(File file) {
+ public void onFileSelected(@Nullable final File file) {
if (file != null) {
if (file.isDirectory()) {
replaceFragment(file);
@@ -204,7 +325,7 @@ public void onFileSelected(File file) {
* Register the external storage BroadcastReceiver.
*/
private void registerStorageListener() {
- IntentFilter filter = new IntentFilter();
+ final IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
registerReceiver(mStorageListener, filter);
}
diff --git a/aFileChooser/src/com/ipaulpro/afilechooser/FileListAdapter.java b/aFileChooser/src/com/ipaulpro/afilechooser/FileListAdapter.java
index 3480122..8c53586 100644
--- a/aFileChooser/src/com/ipaulpro/afilechooser/FileListAdapter.java
+++ b/aFileChooser/src/com/ipaulpro/afilechooser/FileListAdapter.java
@@ -29,7 +29,7 @@
/**
* List adapter for Files.
- *
+ *
* @version 2013-12-11
* @author paulburke (ipaulpro)
*/
@@ -42,8 +42,10 @@ public class FileListAdapter extends BaseAdapter {
private List mData = new ArrayList();
- public FileListAdapter(Context context) {
- mInflater = LayoutInflater.from(context);
+ public FileListAdapter(Context context)
+ {
+ super ();
+ mInflater = LayoutInflater.from (context);
}
public void add(File file) {
diff --git a/aFileChooser/src/com/ipaulpro/afilechooser/FileListFragment.java b/aFileChooser/src/com/ipaulpro/afilechooser/FileListFragment.java
index 5da363a..6d7dd57 100644
--- a/aFileChooser/src/com/ipaulpro/afilechooser/FileListFragment.java
+++ b/aFileChooser/src/com/ipaulpro/afilechooser/FileListFragment.java
@@ -24,19 +24,27 @@
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.ListView;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.List;
+import java.util.ArrayList;
/**
* Fragment that displays a list of Files in a given path.
- *
+ *
* @version 2013-12-11
* @author paulburke (ipaulpro)
*/
+@SuppressWarnings ("CollectionDeclaredAsConcreteClass")
public class FileListFragment extends ListFragment implements
LoaderManager.LoaderCallbacks> {
+ /**
+ * TAG for log messages.
+ * */
+ static final String TAG = FileListFragment.class.getName ();
/**
* Interface to listen for events.
*/
@@ -51,8 +59,15 @@ public interface Callbacks {
private static final int LOADER_ID = 0;
+ @NotNull
private FileListAdapter mAdapter;
+ /**
+ *
path to display. Not null after onCreate
+ */
+ @NotNull
private String mPath;
+ @Nullable
+ private ArrayList mFilterIncludeExtensions = new ArrayList();
private Callbacks mListener;
@@ -62,39 +77,67 @@ public interface Callbacks {
* @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) {
- FileListFragment fragment = new FileListFragment();
- Bundle args = new Bundle();
- args.putString(FileChooserActivity.PATH, path);
+ @NotNull public static FileListFragment newInstance(
+ @NotNull final String path,
+ @Nullable final ArrayList filterIncludeExtensions ) {
+ //android.util.Log.d (TAG, "+ newInstance");
+ //android.util.Log.v (TAG, "> path = " + path);
+ //android.util.Log.v (TAG, "> filterIncludeExtensions = " + filterIncludeExtensions);
+
+ final FileListFragment fragment = new FileListFragment();
+ final Bundle args = new Bundle();
+
+ args.putString(FileChooserActivity.SAVE_INSTANCE_PATH, path);
+ args.putStringArrayList (
+ FileChooserActivity.EXTRA_FILTER_INCLUDE_EXTENSIONS,
+ filterIncludeExtensions);
fragment.setArguments(args);
+ //android.util.Log.v (TAG, "> fragment = " + fragment);
+ //android.util.Log.d (TAG, "+ newInstance");
return fragment;
}
@Override
- public void onAttach(Activity activity) {
+ public void onAttach(@NotNull final Activity activity) {
super.onAttach(activity);
try {
mListener = (Callbacks) activity;
- } catch (ClassCastException e) {
+ } catch (@NotNull final ClassCastException e) {
+ android.util.Log.e (TAG, "LOG02240:", e);
throw new ClassCastException(activity.toString()
+ " must implement FileListFragment.Callbacks");
}
}
@Override
- public void onCreate(Bundle savedInstanceState) {
+ public void onCreate(final Bundle savedInstanceState) {
+ //android.util.Log.d (TAG, "+ onCreate");
+ //android.util.Log.v (TAG, "> savedInstanceState = " + savedInstanceState);
+
super.onCreate(savedInstanceState);
+ final android.os.Bundle arguments = getArguments ();
+
mAdapter = new FileListAdapter(getActivity());
- mPath = getArguments() != null ? getArguments().getString(
- FileChooserActivity.PATH) : Environment
- .getExternalStorageDirectory().getAbsolutePath();
+
+ //android.util.Log.v (TAG, "> mAdapter = " + mAdapter);
+ //android.util.Log.v (TAG, "> arguments = " + arguments);
+
+ mPath = arguments != null
+ ? arguments.getString (FileChooserActivity.SAVE_INSTANCE_PATH)
+ : Environment.getExternalStorageDirectory().getAbsolutePath();
+ if(arguments != null){
+ mFilterIncludeExtensions = arguments.getStringArrayList (
+ FileChooserActivity.EXTRA_FILTER_INCLUDE_EXTENSIONS);
+ }
+
+ //android.util.Log.d (TAG, "+ onCreate");
}
@Override
- public void onActivityCreated(Bundle savedInstanceState) {
+ public void onActivityCreated(final Bundle savedInstanceState) {
setEmptyText(getString(R.string.empty_directory));
setListAdapter(mAdapter);
setListShown(false);
@@ -105,22 +148,22 @@ public void onActivityCreated(Bundle savedInstanceState) {
}
@Override
- public void onListItemClick(ListView l, View v, int position, long id) {
- FileListAdapter adapter = (FileListAdapter) l.getAdapter();
+ public void onListItemClick(@NotNull final ListView l, final View v, final int position, final long id) {
+ final FileListAdapter adapter = (FileListAdapter) l.getAdapter();
if (adapter != null) {
- File file = (File) adapter.getItem(position);
+ final File file = adapter.getItem(position);
mPath = file.getAbsolutePath();
mListener.onFileSelected(file);
}
}
- @Override
- public Loader> onCreateLoader(int id, Bundle args) {
- return new FileLoader(getActivity(), mPath);
+ @Nullable @Override
+ public Loader> onCreateLoader(final int id, final Bundle args) {
+ return new FileLoader(getActivity(), mPath, mFilterIncludeExtensions);
}
@Override
- public void onLoadFinished(Loader> loader, List data) {
+ public void onLoadFinished(final Loader> loader, final List data) {
mAdapter.setListItems(data);
if (isResumed())
@@ -130,7 +173,7 @@ public void onLoadFinished(Loader> loader, List data) {
}
@Override
- public void onLoaderReset(Loader> loader) {
+ public void onLoaderReset(final Loader> loader) {
mAdapter.clear();
}
}
diff --git a/aFileChooser/src/com/ipaulpro/afilechooser/FileLoader.java b/aFileChooser/src/com/ipaulpro/afilechooser/FileLoader.java
index f8903ac..b60cc2b 100644
--- a/aFileChooser/src/com/ipaulpro/afilechooser/FileLoader.java
+++ b/aFileChooser/src/com/ipaulpro/afilechooser/FileLoader.java
@@ -21,6 +21,8 @@
import android.support.v4.content.AsyncTaskLoader;
import com.ipaulpro.afilechooser.utils.FileUtils;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.ArrayList;
@@ -29,7 +31,7 @@
/**
* Loader that returns a list of Files in a given file path.
- *
+ *
* @version 2013-12-11
* @author paulburke (ipaulpro)
*/
@@ -40,20 +42,25 @@ public class FileLoader extends AsyncTaskLoader> {
| FileObserver.MOVED_FROM | FileObserver.MOVED_TO
| FileObserver.MODIFY | FileObserver.MOVE_SELF;
- private FileObserver mFileObserver;
+ @Nullable private FileObserver mFileObserver;
- private List mData;
- private String mPath;
+ @Nullable private List mData;
+ private final String mPath;
+ @Nullable private final ArrayList mFilterIncludeExtensions;
- public FileLoader(Context context, String path) {
+ public FileLoader(
+ @NotNull final Context context,
+ final String path,
+ @Nullable final ArrayList filterIncludeExtensions) {
super(context);
this.mPath = path;
+ this.mFilterIncludeExtensions = filterIncludeExtensions;
}
- @Override
+ @NotNull @Override
public List loadInBackground() {
- ArrayList list = new ArrayList();
+ final ArrayList list = new ArrayList();
// Current directory File instance
final File pathDir = new File(mPath);
@@ -64,31 +71,30 @@ public List loadInBackground() {
// Sort the folders alphabetically
Arrays.sort(dirs, FileUtils.sComparator);
// Add each folder to the File list for the list adapter
- for (File dir : dirs)
- list.add(dir);
+ java.util.Collections.addAll (list, dirs);
}
// List file in this directory with the file filter
- final File[] files = pathDir.listFiles(FileUtils.sFileFilter);
+ final File[] files = pathDir.listFiles(
+ new FileUtils.FileExtensionFilter (mFilterIncludeExtensions));
if (files != null) {
// Sort the files alphabetically
Arrays.sort(files, FileUtils.sComparator);
// Add each file to the File list for the list adapter
- for (File file : files)
- list.add(file);
+ java.util.Collections.addAll (list, files);
}
return list;
}
@Override
- public void deliverResult(List data) {
+ public void deliverResult(final List data) {
if (isReset()) {
onReleaseResources(data);
return;
}
- List oldData = mData;
+ final List oldData = mData;
mData = data;
if (isStarted())
@@ -100,21 +106,23 @@ public void deliverResult(List data) {
@Override
protected void onStartLoading() {
- if (mData != null)
- deliverResult(mData);
+ if (mData != null) {
+ deliverResult (mData);
+ }
if (mFileObserver == null) {
mFileObserver = new FileObserver(mPath, FILE_OBSERVER_MASK) {
@Override
- public void onEvent(int event, String path) {
+ public void onEvent(final int event, final String path) {
onContentChanged();
}
};
}
mFileObserver.startWatching();
- if (takeContentChanged() || mData == null)
- forceLoad();
+ if (takeContentChanged() || mData == null) {
+ forceLoad ();
+ }
}
@Override
@@ -133,13 +141,13 @@ protected void onReset() {
}
@Override
- public void onCanceled(List data) {
+ public void onCanceled(final List data) {
super.onCanceled(data);
onReleaseResources(data);
}
- protected void onReleaseResources(List data) {
+ protected void onReleaseResources(final List data) {
if (mFileObserver != null) {
mFileObserver.stopWatching();
diff --git a/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java b/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
index 25c8008..c21f48d 100644
--- a/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
+++ b/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
@@ -32,10 +32,13 @@
import android.webkit.MimeTypeMap;
import com.ianhanniballake.localstorage.LocalStorageProvider;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.FileFilter;
import java.text.DecimalFormat;
+import java.util.ArrayList;
import java.util.Comparator;
/**
@@ -44,11 +47,14 @@
* @version 2013-12-11
* @author paulburke (ipaulpro)
*/
+@SuppressWarnings ("HardcodedFileSeparator")
public class FileUtils {
private FileUtils() {} //private constructor to enforce Singleton pattern
-
- /** TAG for log messages. */
- static final String TAG = "FileUtils";
+
+ /**
+ * TAG for log messages.
+ * */
+ static final String TAG = FileUtils.class.getName ();
private static final boolean DEBUG = false; // Set to true to enable logging
public static final String MIME_TYPE_AUDIO = "audio/*";
@@ -59,6 +65,47 @@ private FileUtils() {} //private constructor to enforce Singleton pattern
public static final String HIDDEN_PREFIX = ".";
+ /**
+ * File Filter that includes only files with the specified extensions to pass
+ * @author Kiran Rao
+ *
+ */
+ public static class FileExtensionFilter implements FileFilter{
+ /**
+ *
file extension filter
+ */
+ @Nullable private final ArrayList mFilterIncludeExtensions;
+
+ public FileExtensionFilter (@Nullable final ArrayList filterIncludeExtensions){
+ //android.util.Log.d (TAG, "+ FileExtensionFilter");
+ //android.util.Log.v (TAG, "> filterIncludeExtensions = " + filterIncludeExtensions);
+
+ this.mFilterIncludeExtensions = filterIncludeExtensions;
+
+ //android.util.Log.d (TAG, "+ FileExtensionFilter");
+ }
+
+ @Override
+ public boolean accept(@NotNull final File file) {
+ //android.util.Log.d (TAG, "+ accept");
+ //android.util.Log.v (TAG, "> file = " + file);
+
+ final String fileName = file.getName();
+ final android.net.Uri uri = android.net.Uri.fromFile (file);
+ final boolean passesExtensionsFilter = mFilterIncludeExtensions == null ||
+ mFilterIncludeExtensions.isEmpty () || mFilterIncludeExtensions.contains (
+ getExtension (uri.toString ()));
+ // Return files only (not directories) and skip hidden files
+ final boolean retval = file.isFile() && !fileName.startsWith(HIDDEN_PREFIX) &&
+ passesExtensionsFilter;
+
+ //android.util.Log.v (TAG, "> retval = " + retval);
+ //android.util.Log.d (TAG, "- accept");
+ return retval;
+ }
+
+ }
+
/**
* Gets the extension of a file name, like ".png" or ".jpg".
*
@@ -66,12 +113,13 @@ private FileUtils() {} //private constructor to enforce Singleton pattern
* @return Extension including the dot("."); "" if there is no extension;
* null if uri was null.
*/
- public static String getExtension(String uri) {
+ @Nullable
+ public static String getExtension(@Nullable final String uri) {
if (uri == null) {
return null;
}
- int dot = uri.lastIndexOf(".");
+ final int dot = uri.lastIndexOf(".");
if (dot >= 0) {
return uri.substring(dot);
} else {
@@ -83,7 +131,7 @@ public static String getExtension(String uri) {
/**
* @return Whether the URI is a local one.
*/
- public static boolean isLocal(String url) {
+ public static boolean isLocal(@Nullable final String url) {
if (url != null && !url.startsWith("http://") && !url.startsWith("https://")) {
return true;
}
@@ -94,7 +142,7 @@ public static boolean isLocal(String url) {
* @return True if Uri is a MediaStore Uri.
* @author paulburke
*/
- public static boolean isMediaUri(Uri uri) {
+ public static boolean isMediaUri(@NotNull final Uri uri) {
return "media".equalsIgnoreCase(uri.getAuthority());
}
@@ -104,7 +152,8 @@ public static boolean isMediaUri(Uri uri) {
* @param file
* @return uri
*/
- public static Uri getUri(File file) {
+ @Nullable
+ public static Uri getUri(@Nullable final File file) {
if (file != null) {
return Uri.fromFile(file);
}
@@ -117,14 +166,15 @@ public static Uri getUri(File file) {
* @param file
* @return
*/
- public static File getPathWithoutFilename(File file) {
+ @Nullable
+ public static File getPathWithoutFilename(@Nullable final File file) {
if (file != null) {
if (file.isDirectory()) {
// no file to be split off. Return everything
return file;
} else {
- String filename = file.getName();
- String filepath = file.getAbsolutePath();
+ final String filename = file.getName();
+ final String filepath = file.getAbsolutePath();
// Construct path without file name.
String pathwithoutname = filepath.substring(0,
@@ -141,9 +191,9 @@ public static File getPathWithoutFilename(File file) {
/**
* @return The MIME type for the given file.
*/
- public static String getMimeType(File file) {
+ public static String getMimeType(@NotNull final File file) {
- String extension = getExtension(file.getName());
+ final String extension = getExtension(file.getName());
if (extension.length() > 0)
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.substring(1));
@@ -154,8 +204,10 @@ public static String getMimeType(File file) {
/**
* @return The MIME type for the give Uri.
*/
- public static String getMimeType(Context context, Uri uri) {
- File file = new File(getPath(context, uri));
+ public static String getMimeType(
+ @NotNull final Context context, @NotNull final
+ Uri uri) {
+ final File file = new File(getPath(context, uri));
return getMimeType(file);
}
@@ -164,7 +216,7 @@ public static String getMimeType(Context context, Uri uri) {
* @return Whether the Uri authority is {@link LocalStorageProvider}.
* @author paulburke
*/
- public static boolean isLocalStorageDocument(Uri uri) {
+ public static boolean isLocalStorageDocument(@NotNull final Uri uri) {
return LocalStorageProvider.AUTHORITY.equals(uri.getAuthority());
}
@@ -173,7 +225,7 @@ public static boolean isLocalStorageDocument(Uri uri) {
* @return Whether the Uri authority is ExternalStorageProvider.
* @author paulburke
*/
- public static boolean isExternalStorageDocument(Uri uri) {
+ public static boolean isExternalStorageDocument(@NotNull final Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
@@ -182,7 +234,7 @@ public static boolean isExternalStorageDocument(Uri uri) {
* @return Whether the Uri authority is DownloadsProvider.
* @author paulburke
*/
- public static boolean isDownloadsDocument(Uri uri) {
+ public static boolean isDownloadsDocument(@NotNull final Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
@@ -191,7 +243,7 @@ public static boolean isDownloadsDocument(Uri uri) {
* @return Whether the Uri authority is MediaProvider.
* @author paulburke
*/
- public static boolean isMediaDocument(Uri uri) {
+ public static boolean isMediaDocument(@NotNull final Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
@@ -199,7 +251,7 @@ public static boolean isMediaDocument(Uri uri) {
* @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
- public static boolean isGooglePhotosUri(Uri uri) {
+ public static boolean isGooglePhotosUri(@NotNull final Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}
@@ -214,8 +266,11 @@ public static boolean isGooglePhotosUri(Uri uri) {
* @return The value of the _data column, which is typically a file path.
* @author paulburke
*/
- public static String getDataColumn(Context context, Uri uri, String selection,
- String[] selectionArgs) {
+ @Nullable public static String getDataColumn(
+ @NotNull final Context context,
+ final Uri uri,
+ final String selection,
+ final String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
@@ -247,14 +302,18 @@ public static String getDataColumn(Context context, Uri uri, String selection,
*
* Callers should check whether the path is local before assuming it
* represents a local file.
- *
+ *
* @param context The context.
* @param uri The Uri to query.
* @see #isLocal(String)
* @see #getFile(Context, Uri)
* @author paulburke
*/
- public static String getPath(final Context context, final Uri uri) {
+ @Nullable
+ @android.annotation.SuppressLint ("NewApi") // Usages of New APIs are surrounded by sufficient conditional checks
+ public static String getPath(
+ @NotNull final Context context,
+ @NotNull final Uri uri) {
if (DEBUG)
Log.d(TAG + " File -",
@@ -345,9 +404,12 @@ else if ("file".equalsIgnoreCase(uri.getScheme())) {
* @see #getPath(Context, Uri)
* @author paulburke
*/
- public static File getFile(Context context, Uri uri) {
+ @Nullable
+ public static File getFile(
+ @NotNull final Context context,
+ @Nullable final Uri uri) {
if (uri != null) {
- String path = getPath(context, uri);
+ final String path = getPath(context, uri);
if (path != null && isLocal(path)) {
return new File(path);
}
@@ -362,7 +424,7 @@ public static File getFile(Context context, Uri uri) {
* @return
* @author paulburke
*/
- public static String getReadableFileSize(int size) {
+ public static String getReadableFileSize(final int size) {
final int BYTES_IN_KILOBYTES = 1024;
final DecimalFormat dec = new DecimalFormat("###.#");
final String KILOBYTES = " KB";
@@ -395,7 +457,10 @@ public static String getReadableFileSize(int size) {
* @return
* @author paulburke
*/
- public static Bitmap getThumbnail(Context context, File file) {
+ @Nullable
+ public static Bitmap getThumbnail(
+ @NotNull final Context context,
+ @NotNull final File file) {
return getThumbnail(context, getUri(file), getMimeType(file));
}
@@ -408,7 +473,10 @@ public static Bitmap getThumbnail(Context context, File file) {
* @return
* @author paulburke
*/
- public static Bitmap getThumbnail(Context context, Uri uri) {
+ @Nullable
+ public static Bitmap getThumbnail(
+ @NotNull final Context context,
+ @NotNull final Uri uri) {
return getThumbnail(context, uri, getMimeType(context, uri));
}
@@ -422,7 +490,11 @@ public static Bitmap getThumbnail(Context context, Uri uri) {
* @return
* @author paulburke
*/
- public static Bitmap getThumbnail(Context context, Uri uri, String mimeType) {
+ @Nullable
+ public static Bitmap getThumbnail(
+ @NotNull final Context context,
+ @NotNull final Uri uri,
+ @NotNull final String mimeType) {
if (DEBUG)
Log.d(TAG, "Attempting to get thumbnail");
@@ -457,7 +529,7 @@ else if (mimeType.contains(FileUtils.MIME_TYPE_IMAGE)) {
null);
}
}
- } catch (Exception e) {
+ } catch (final Exception e) {
if (DEBUG)
Log.e(TAG, "getThumbnail", e);
} finally {
@@ -473,37 +545,26 @@ else if (mimeType.contains(FileUtils.MIME_TYPE_IMAGE)) {
*
* @author paulburke
*/
- public static Comparator sComparator = new Comparator() {
+ @NotNull public static Comparator sComparator = new Comparator() {
@Override
- public int compare(File f1, File f2) {
+ public int compare(
+ @NotNull final File f1, @NotNull final
+ File f2) {
// Sort alphabetically by lower case, which is much cleaner
- return f1.getName().toLowerCase().compareTo(
+ return f1.getName().toLowerCase(Locale.getDefault()).compareTo(
f2.getName().toLowerCase());
}
};
- /**
- * File (not directories) filter.
- *
- * @author paulburke
- */
- public static FileFilter sFileFilter = new FileFilter() {
- @Override
- public boolean accept(File file) {
- final String fileName = file.getName();
- // Return files only (not directories) and skip hidden files
- return file.isFile() && !fileName.startsWith(HIDDEN_PREFIX);
- }
- };
/**
* Folder (directories) filter.
*
* @author paulburke
*/
- public static FileFilter sDirFilter = new FileFilter() {
+ @NotNull public static FileFilter sDirFilter = new FileFilter() {
@Override
- public boolean accept(File file) {
+ public boolean accept(@NotNull final File file) {
final String fileName = file.getName();
// Return directories only and skip hidden directories
return file.isDirectory() && !fileName.startsWith(HIDDEN_PREFIX);
@@ -516,7 +577,7 @@ public boolean accept(File file) {
* @return The intent for opening a file with Intent.createChooser()
* @author paulburke
*/
- public static Intent createGetContentIntent() {
+ @NotNull public static Intent createGetContentIntent() {
// Implicitly allow the user to select a particular kind of data
final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// The MIME data type filter
diff --git a/aFileChooserExample/AndroidManifest.xml b/aFileChooserExample/AndroidManifest.xml
index e348b00..3ff8e02 100644
--- a/aFileChooserExample/AndroidManifest.xml
+++ b/aFileChooserExample/AndroidManifest.xml
@@ -30,7 +30,7 @@
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
@@ -54,7 +54,6 @@
-
-
\ No newline at end of file
+
diff --git a/aFileChooserExample/pom.xml b/aFileChooserExample/pom.xml
new file mode 100644
index 0000000..1d515f5
--- /dev/null
+++ b/aFileChooserExample/pom.xml
@@ -0,0 +1,276 @@
+
+
+
+
+
+
+ 4.0.0
+ afilechooser-expample
+ apk
+ aFileChooser Example
+ aFileChooser is an Android Library Project that simplifies the process of presenting a file chooser on Android 2.1+.
+
+ com.ipaulpro
+ afilechooser
+ 2.3
+
+ https://github.com/krischik/aFileChooser3
+
+
+ krischik@users.sourceforge.net
+ krischik
+ Martin Krischik
+ UIQ3 open-source software and tools
+ https://sourceforge.net/projects/uiq3/
+
+ Deployer
+
+ GMT+1
+ https://sourceforge.net/users/krischik
+
+
+
+
+ pache License, Version 2.0
+
+
+
+ scm:git:https://github.com/krischik/aFileChooser3
+ scm:git:https://github.com/krischik/aFileChooser3
+ main
+ scm:git:https://github.com/krischik/aFileChooser3
+
+
+
+
+
+
+
+ android.support
+ compatibility-v4
+ compile
+
+
+ android
+ android
+
+
+ com.ipaulpro
+ afilechooser-library
+ apklib
+
+
+ org.androidannotations
+ androidannotations
+ provided
+
+
+ org.androidannotations
+ androidannotations-api
+
+
+ com.intellij
+ annotations
+
+
+
+
+
+
+
+
+ src
+
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+
+ org.apache.maven.plugins
+ maven-jar-plugin
+
+
+ com.jayway.maven.plugins.android.generation2
+ android-maven-plugin
+
+ false
+ true
+
+ proguard.cfg
+
+ -Xms256m
+ -Xmx512m
+
+ true
+
+
+ true
+
+
+ true
+
+
+ org.apache.maven.plugins
+ maven-resources-plugin
+
+ ${project.build.sourceEncoding}
+
+
+
+ org.codehaus.mojo
+ build-helper-maven-plugin
+
+
+ add-source
+ generate-sources
+
+
+ gen
+ target/generated-sources/annotations
+ target/generated-sources/r
+
+
+
+ add-source
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-clean-plugin
+
+
+ org.apache.maven.plugins
+ maven-checkstyle-plugin
+
+
+ org.apache.maven.plugins
+ maven-antrun-plugin
+
+
+ Initialize
+ initialize
+
+
+
+
+
+
+
+
+
+
+ run
+
+
+
+
+
+ install
+
+
+
+
+
+
+
+
+ release
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+
+
+ 1
+ 1
+
+
+ false
+ none
+ true
+ true
+ true
+
+
+
+ com.jayway.maven.plugins.android.generation2
+ android-maven-plugin
+
+ true
+
+
+
+ org.apache.maven.plugins
+ maven-source-plugin
+
+
+ attach-sources
+
+ jar
+ test-jar
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/aFileChooserExample/proguard.cfg b/aFileChooserExample/proguard.cfg
index b1cdf17..54cbec1 100644
--- a/aFileChooserExample/proguard.cfg
+++ b/aFileChooserExample/proguard.cfg
@@ -2,6 +2,7 @@
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
+-dontoptimize
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
diff --git a/aFileChooserExample/project.properties b/aFileChooserExample/project.properties
index 6e983b1..22fdffb 100644
--- a/aFileChooserExample/project.properties
+++ b/aFileChooserExample/project.properties
@@ -10,3 +10,5 @@
# Project target.
target=android-19
android.library.reference.1=../aFileChooser
+
+
diff --git a/aFileChooserExample/res/layout/example.xml b/aFileChooserExample/res/layout/example.xml
new file mode 100644
index 0000000..8cb7fea
--- /dev/null
+++ b/aFileChooserExample/res/layout/example.xml
@@ -0,0 +1,44 @@
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/aFileChooserExample/res/values-v19/bools.xml b/aFileChooserExample/res/values-v19/bools.xml
new file mode 100644
index 0000000..9cdd78a
--- /dev/null
+++ b/aFileChooserExample/res/values-v19/bools.xml
@@ -0,0 +1,6 @@
+
+
+
+ true
+
+
\ No newline at end of file
diff --git a/aFileChooserExample/res/values/strings.xml b/aFileChooserExample/res/values/strings.xml
index 8de29c7..c02fad9 100644
--- a/aFileChooserExample/res/values/strings.xml
+++ b/aFileChooserExample/res/values/strings.xml
@@ -1,5 +1,5 @@
-
-
- aFileChooserExample
- Lorem ipsum
+ aFileChooserExample
+ Lorem ipsum
+ Choose from all files…
+ Choose pdf files…
+ Choose calculator files…
\ No newline at end of file
diff --git a/aFileChooserExample/src/com/ipaulpro/afilechooserexample/FileChooserExampleActivity.java b/aFileChooserExample/src/com/ipaulpro/afilechooserexample/FileChooserExampleActivity.java
index 8fd11a8..fcfaaea 100644
--- a/aFileChooserExample/src/com/ipaulpro/afilechooserexample/FileChooserExampleActivity.java
+++ b/aFileChooserExample/src/com/ipaulpro/afilechooserexample/FileChooserExampleActivity.java
@@ -20,58 +20,101 @@
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
-import android.os.Bundle;
import android.util.Log;
-import android.view.View;
-import android.view.View.OnClickListener;
-import android.widget.Button;
-import android.widget.Toast;
-
+import com.ipaulpro.afilechooser.FileChooserActivity;
import com.ipaulpro.afilechooser.utils.FileUtils;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.ArrayList;
/**
* @author paulburke (ipaulpro)
*/
+@SuppressWarnings ({"CollectionDeclaredAsConcreteClass", "HardcodedFileSeparator"})
+@org.androidannotations.annotations.EActivity (R.layout.example)
public class FileChooserExampleActivity extends Activity {
- private static final String TAG = "FileChooserExampleActivity";
-
- private static final int REQUEST_CODE = 6384; // onActivityResult request
- // code
+ private static final String TAG = FileChooserExampleActivity.class.getName ();
+ /**
+ *
onActivityResult request code
+ */
+ private static final int REQUEST_CODE = 6384;
+ @NotNull private static final ArrayList PDF_Files;
+ /**
+ *
File types for my calculator app. Replace it with whatever you want to test.
+ *
+ * author Martin Krischik"
+ */
+ @NotNull private static final ArrayList Calculator_Files;
+ /**
+ *
Directory why the unit tests of my calculator app stores there files. The
+ * real app uses {@link android.content.Context.getExternalFilesDir(java.lang.String)}