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

Select one file with a single click #92

Merged
merged 1 commit into from
Jun 25, 2016
Merged
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
@@ -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 @@ 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<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) {
Original file line number Diff line number Diff line change
@@ -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 @@ protected FileItemAdapter<T> 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) {
Original file line number Diff line number Diff line change
@@ -23,11 +23,11 @@ public FilePickerActivity() {
@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;
}
}