Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Added MIME type filter to FileChooserActivity and company. #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.MimeTypeMap;
import android.widget.Toast;

import java.io.File;
Expand All @@ -46,8 +47,10 @@ public class FileChooserActivity extends FragmentActivity implements
OnBackStackChangedListener, FileListFragment.Callbacks {

public static final String PATH = "path";
public static final String MIME_TYPE = "mime_type";
public static final String EXTERNAL_BASE_PATH = Environment
.getExternalStorageDirectory().getAbsolutePath();
public static final String MIME_TYPE_DEFAULT = "*/*";

private static final boolean HAS_ACTIONBAR = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;

Expand All @@ -61,11 +64,14 @@ public void onReceive(Context context, Intent intent) {
};

private String mPath;
private String mMimeType;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mMimeType = (getIntent() != null) ? getIntent().getType() : MIME_TYPE_DEFAULT;

mFragmentManager = getSupportFragmentManager();
mFragmentManager.addOnBackStackChangedListener(this);

Expand Down Expand Up @@ -144,7 +150,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, mMimeType);
mFragmentManager.beginTransaction()
.add(android.R.id.content, fragment).commit();
}
Expand All @@ -158,7 +164,7 @@ private void addFragment() {
private void replaceFragment(File file) {
mPath = file.getAbsolutePath();

FileListFragment fragment = FileListFragment.newInstance(mPath);
FileListFragment fragment = FileListFragment.newInstance(mPath, mMimeType);
mFragmentManager.beginTransaction()
.replace(android.R.id.content, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
Expand Down
26 changes: 25 additions & 1 deletion aFileChooser/src/com/ipaulpro/afilechooser/FileListAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@
package com.ipaulpro.afilechooser;

import android.content.Context;
import android.provider.DocumentsContract.Document;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.ipaulpro.afilechooser.utils.FileUtils;

/**
* List adapter for Files.
*
Expand All @@ -41,9 +45,11 @@ public class FileListAdapter extends BaseAdapter {
private final LayoutInflater mInflater;

private List<File> mData = new ArrayList<File>();
private String mMimeType;

public FileListAdapter(Context context) {
public FileListAdapter(Context context, String mimeType) {
mInflater = LayoutInflater.from(context);
mMimeType = mimeType;
}

public void add(File file) {
Expand Down Expand Up @@ -115,7 +121,25 @@ public View getView(int position, View convertView, ViewGroup parent) {
int icon = file.isDirectory() ? ICON_FOLDER : ICON_FILE;
view.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);

// Disable visually
row.setEnabled(isEnabled(position));

return row;
}

@Override
public boolean isEnabled(int position) {
boolean enable = true;

File file = getItem(position);

// Always enabled if a folder
if (!file.isDirectory()) {
// Set enabled if the MIME types match
String concreteType = FileUtils.getMimeType(file);
enable = FileUtils.compareMimeTypes(concreteType, mMimeType);
}

return enable;
}
}
10 changes: 8 additions & 2 deletions aFileChooser/src/com/ipaulpro/afilechooser/FileListFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ public interface Callbacks {
* Create a new instance with the given file path.
*
* @param path The absolute path of the file (directory) to display.
* @param mimeType The MIME type for the file.
* @return A new Fragment with the given file path.
*/
public static FileListFragment newInstance(String path) {
public static FileListFragment newInstance(String path, String mimeType) {
FileListFragment fragment = new FileListFragment();
Bundle args = new Bundle();
args.putString(FileChooserActivity.PATH, path);
args.putString(FileChooserActivity.MIME_TYPE, mimeType);
fragment.setArguments(args);

return fragment;
Expand All @@ -86,11 +88,15 @@ public void onAttach(Activity activity) {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String mimeType = getArguments() != null ? getArguments().getString(
FileChooserActivity.MIME_TYPE)
: FileChooserActivity.MIME_TYPE_DEFAULT;

mAdapter = new FileListAdapter(getActivity());
mAdapter = new FileListAdapter(getActivity(), mimeType);
mPath = getArguments() != null ? getArguments().getString(
FileChooserActivity.PATH) : Environment
.getExternalStorageDirectory().getAbsolutePath();

}

@Override
Expand Down
27 changes: 27 additions & 0 deletions aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,33 @@ public static String getMimeType(Context context, Uri uri) {
return getMimeType(file);
}

/**
* Helper to compare two MIME types, where one may be a pattern.
*
* @param concreteType A fully-specified MIME type.
* @param desiredType A desired MIME type that may be a pattern such as *\/*.
* @return Returns true if the two MIME types match.
*/
public static boolean compareMimeTypes(String concreteType, String desiredType) {
final int typeLength = desiredType.length();
if (typeLength == 3 && desiredType.equals("*/*")) {
return true;
}

final int slashpos = desiredType.indexOf('/');
if (slashpos > 0) {
if (typeLength == slashpos+2 && desiredType.charAt(slashpos+1) == '*') {
if (desiredType.regionMatches(0, concreteType, 0, slashpos+1)) {
return true;
}
} else if (desiredType.equals(concreteType)) {
return true;
}
}

return false;
}

/**
* @param uri The Uri to check.
* @return Whether the Uri authority is {@link LocalStorageProvider}.
Expand Down