From 16ac20ddb30cca6ca123cbc221ba2748c32d7036 Mon Sep 17 00:00:00 2001 From: rastik Date: Sat, 25 Jun 2016 00:16:18 +0200 Subject: [PATCH] Select one file with a single click --- .../AbstractFilePickerActivity.java | 9 ++++++-- .../AbstractFilePickerFragment.java | 22 +++++++++++++++++-- .../filepicker/FilePickerActivity.java | 4 ++-- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/library/src/main/java/com/nononsenseapps/filepicker/AbstractFilePickerActivity.java b/library/src/main/java/com/nononsenseapps/filepicker/AbstractFilePickerActivity.java index 6f7fd08b..49e5359b 100644 --- a/library/src/main/java/com/nononsenseapps/filepicker/AbstractFilePickerActivity.java +++ b/library/src/main/java/com/nononsenseapps/filepicker/AbstractFilePickerActivity.java @@ -57,6 +57,8 @@ public abstract class AbstractFilePickerActivity 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 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 @@ protected void onCreate(Bundle savedInstanceState) { allowCreateDir); allowMultiple = intent.getBooleanExtra(EXTRA_ALLOW_MULTIPLE, allowMultiple); + singleClick = + intent.getBooleanExtra(EXTRA_SINGLE_CLICK, singleClick); } FragmentManager fm = getSupportFragmentManager(); @@ -94,7 +99,7 @@ protected void onCreate(Bundle savedInstanceState) { if (fragment == null) { fragment = - getFragment(startPath, mode, allowMultiple, allowCreateDir); + getFragment(startPath, mode, allowMultiple, allowCreateDir, singleClick); } if (fragment != null) { @@ -108,7 +113,7 @@ protected void onCreate(Bundle savedInstanceState) { protected abstract AbstractFilePickerFragment 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) { diff --git a/library/src/main/java/com/nononsenseapps/filepicker/AbstractFilePickerFragment.java b/library/src/main/java/com/nononsenseapps/filepicker/AbstractFilePickerFragment.java index 2e954bdd..352ab157 100644 --- a/library/src/main/java/com/nononsenseapps/filepicker/AbstractFilePickerFragment.java +++ b/library/src/main/java/com/nononsenseapps/filepicker/AbstractFilePickerFragment.java @@ -59,6 +59,8 @@ public abstract class AbstractFilePickerFragment 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 mCheckedItems; @@ -67,6 +69,7 @@ public abstract class AbstractFilePickerFragment extends Fragment protected T mCurrentPath = null; protected boolean allowCreateDir = false; protected boolean allowMultiple = false; + protected boolean singleClick = false; protected OnFilePickedListener mListener; protected FileItemAdapter mAdapter = null; protected TextView mCurrentDirView; @@ -110,7 +113,8 @@ protected FileItemAdapter getDummyAdapter() { * @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 void setArgs(@Nullable final String startPath, final int mode, } 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 void onActivityCreated(Bundle savedInstanceState) { .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 void onActivityCreated(Bundle savedInstanceState) { .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 void onActivityCreated(Bundle savedInstanceState) { } } + 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 void onSaveInstanceState(Bundle b) { 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 void onClickCheckable(@NonNull View view, @NonNull CheckableViewHolder vi 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 class CheckableViewHolder extends DirViewHolder { 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) { diff --git a/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java b/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java index 02309d6d..6551933d 100644 --- a/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java +++ b/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java @@ -23,11 +23,11 @@ public FilePickerActivity() { @Override protected AbstractFilePickerFragment getFragment( @Nullable final String startPath, final int mode, final boolean allowMultiple, - final boolean allowCreateDir) { + final boolean allowCreateDir, final boolean singleClick) { AbstractFilePickerFragment 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; } }