Select one file with a single click

This commit is contained in:
rastik 2016-06-25 00:16:18 +02:00
parent 5074ab5e39
commit 16ac20ddb3
3 changed files with 29 additions and 6 deletions

View File

@ -57,6 +57,8 @@ public abstract class AbstractFilePickerActivity<T> extends AppCompatActivity
public static final String EXTRA_MODE = "nononsense.intent.MODE";
public static final String EXTRA_ALLOW_CREATE_DIR =
"nononsense.intent" + ".ALLOW_CREATE_DIR";
public static final String EXTRA_SINGLE_CLICK =
"nononsense.intent" + ".SINGLE_CLICK";
// For compatibility
public static final String EXTRA_ALLOW_MULTIPLE =
"android.intent.extra" + ".ALLOW_MULTIPLE";
@ -70,6 +72,7 @@ public abstract class AbstractFilePickerActivity<T> extends AppCompatActivity
protected int mode = AbstractFilePickerFragment.MODE_FILE;
protected boolean allowCreateDir = false;
protected boolean allowMultiple = false;
protected boolean singleClick = false;
@Override
@SuppressWarnings("unchecked")
@ -86,6 +89,8 @@ public abstract class AbstractFilePickerActivity<T> extends AppCompatActivity
allowCreateDir);
allowMultiple =
intent.getBooleanExtra(EXTRA_ALLOW_MULTIPLE, allowMultiple);
singleClick =
intent.getBooleanExtra(EXTRA_SINGLE_CLICK, singleClick);
}
FragmentManager fm = getSupportFragmentManager();
@ -94,7 +99,7 @@ public abstract class AbstractFilePickerActivity<T> extends AppCompatActivity
if (fragment == null) {
fragment =
getFragment(startPath, mode, allowMultiple, allowCreateDir);
getFragment(startPath, mode, allowMultiple, allowCreateDir, singleClick);
}
if (fragment != null) {
@ -108,7 +113,7 @@ public abstract class AbstractFilePickerActivity<T> extends AppCompatActivity
protected abstract AbstractFilePickerFragment<T> getFragment(
@Nullable final String startPath, final int mode, final boolean allowMultiple,
final boolean allowCreateDir);
final boolean allowCreateDir, final boolean singleClick);
@Override
public void onSaveInstanceState(Bundle b) {

View File

@ -59,6 +59,8 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
public static final String KEY_ALLOW_DIR_CREATE = "KEY_ALLOW_DIR_CREATE";
// Allow multiple items to be selected.
public static final String KEY_ALLOW_MULTIPLE = "KEY_ALLOW_MULTIPLE";
// If file can be selected by clicking only and checkboxes are not visible
public static final String KEY_SINGLE_CLICK = "KEY_SINGLE_CLICK";
// Used for saving state.
protected static final String KEY_CURRENT_PATH = "KEY_CURRENT_PATH";
protected final HashSet<T> mCheckedItems;
@ -67,6 +69,7 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
protected T mCurrentPath = null;
protected boolean allowCreateDir = false;
protected boolean allowMultiple = false;
protected boolean singleClick = false;
protected OnFilePickedListener mListener;
protected FileItemAdapter<T> mAdapter = null;
protected TextView mCurrentDirView;
@ -110,7 +113,8 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
* @param allowDirCreate can new directories be created?
*/
public void setArgs(@Nullable final String startPath, final int mode,
final boolean allowMultiple, final boolean allowDirCreate) {
final boolean allowMultiple, final boolean allowDirCreate,
final boolean singleClick) {
// There might have been arguments set elsewhere, if so do not overwrite them.
Bundle b = getArguments();
if (b == null) {
@ -122,6 +126,7 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
}
b.putBoolean(KEY_ALLOW_DIR_CREATE, allowDirCreate);
b.putBoolean(KEY_ALLOW_MULTIPLE, allowMultiple);
b.putBoolean(KEY_SINGLE_CLICK, singleClick);
b.putInt(KEY_MODE, mode);
setArguments(b);
}
@ -301,6 +306,8 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
.getBoolean(KEY_ALLOW_DIR_CREATE, allowCreateDir);
allowMultiple = savedInstanceState
.getBoolean(KEY_ALLOW_MULTIPLE, allowMultiple);
singleClick = savedInstanceState
.getBoolean(KEY_SINGLE_CLICK, singleClick);
String path = savedInstanceState.getString(KEY_CURRENT_PATH);
if (path != null) {
@ -312,6 +319,8 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
.getBoolean(KEY_ALLOW_DIR_CREATE, allowCreateDir);
allowMultiple = getArguments()
.getBoolean(KEY_ALLOW_MULTIPLE, allowMultiple);
singleClick = getArguments()
.getBoolean(KEY_SINGLE_CLICK, singleClick);
if (getArguments().containsKey(KEY_START_PATH)) {
String path = getArguments().getString(KEY_START_PATH);
if (path != null) {
@ -321,6 +330,8 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
}
}
if (singleClick) getActivity().findViewById(R.id.nnf_button_ok).setVisibility(View.GONE);
// If still null
if (mCurrentPath == null) {
mCurrentPath = getRoot();
@ -357,6 +368,7 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
b.putString(KEY_CURRENT_PATH, mCurrentPath.toString());
b.putBoolean(KEY_ALLOW_MULTIPLE, allowMultiple);
b.putBoolean(KEY_ALLOW_DIR_CREATE, allowCreateDir);
b.putBoolean(KEY_SINGLE_CLICK, singleClick);
b.putInt(KEY_MODE, mode);
}
@ -605,7 +617,12 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
if (isDir(viewHolder.file)) {
goToDir(viewHolder.file);
} else {
onLongClickCheckable(view, viewHolder);
if (!allowMultiple && singleClick) {
// Clear is necessary, in case user clicked some checkbox directly
mCheckedItems.clear();
mCheckedItems.add(viewHolder.file);
onClickOk(null);
} else onLongClickCheckable(view, viewHolder);
}
}
@ -729,6 +746,7 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
public CheckableViewHolder(View v) {
super(v);
checkbox = (CheckBox) v.findViewById(R.id.checkbox);
if (singleClick) checkbox.setVisibility(View.GONE);
checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

View File

@ -23,11 +23,11 @@ public class FilePickerActivity extends AbstractFilePickerActivity<File> {
@Override
protected AbstractFilePickerFragment<File> getFragment(
@Nullable final String startPath, final int mode, final boolean allowMultiple,
final boolean allowCreateDir) {
final boolean allowCreateDir, final boolean singleClick) {
AbstractFilePickerFragment<File> fragment = new FilePickerFragment();
// startPath is allowed to be null. In that case, default folder should be SD-card and not "/"
fragment.setArgs(startPath != null ? startPath : Environment.getExternalStorageDirectory().getPath(),
mode, allowMultiple, allowCreateDir);
mode, allowMultiple, allowCreateDir, singleClick);
return fragment;
}
}