return activity as a listener instead of a target fragment

This commit is contained in:
contrudar 2016-10-04 14:24:53 +03:00
parent 69f9050c3c
commit f187c04368
2 changed files with 32 additions and 13 deletions

View File

@ -7,6 +7,7 @@
package com.nononsenseapps.filepicker;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.net.Uri;
@ -81,6 +82,7 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
protected boolean allowMultiple = false;
protected boolean allowExistingFile = true;
protected boolean singleClick = false;
protected OnFilePickedListener mListener;
protected FileItemAdapter<T> mAdapter = null;
protected TextView mCurrentDirView;
protected EditText mEditTextFileName;
@ -213,8 +215,8 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
* @param view which was clicked. Not used in default implementation.
*/
public void onClickCancel(@NonNull View view) {
if (getTargetFragment() != null) {
((OnFilePickedListener) getTargetFragment()).onCancelled();
if (mListener != null) {
mListener.onCancelled();
}
}
@ -224,7 +226,7 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
* @param view which was clicked. Not used in default implementation.
*/
public void onClickOk(@NonNull View view) {
if (getTargetFragment() == null) {
if (mListener == null) {
return;
}
@ -256,20 +258,20 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
// Append to current directory
result = toUri(getPath(appendPath(getFullPath(mCurrentPath), filename)));
}
((OnFilePickedListener) getTargetFragment()).onFilePicked(result);
mListener.onFilePicked(result);
} else if (allowMultiple) {
((OnFilePickedListener) getTargetFragment()).onFilesPicked(toUri(mCheckedItems));
mListener.onFilesPicked(toUri(mCheckedItems));
} else if (mode == MODE_FILE) {
//noinspection ConstantConditions
((OnFilePickedListener) getTargetFragment()).onFilePicked(toUri(getFirstCheckedItem()));
mListener.onFilePicked(toUri(getFirstCheckedItem()));
} else if (mode == MODE_DIR) {
((OnFilePickedListener) getTargetFragment()).onFilePicked(toUri(mCurrentPath));
mListener.onFilePicked(toUri(mCurrentPath));
} else {
// single FILE OR DIR
if (mCheckedItems.isEmpty()) {
((OnFilePickedListener) getTargetFragment()).onFilePicked(toUri(mCurrentPath));
mListener.onFilePicked(toUri(mCurrentPath));
} else {
((OnFilePickedListener) getTargetFragment()).onFilePicked(toUri(getFirstCheckedItem()));
mListener.onFilePicked(toUri(getFirstCheckedItem()));
}
}
}
@ -325,6 +327,23 @@ public abstract class AbstractFilePickerFragment<T> extends Fragment
return checkable;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (OnFilePickedListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() +
" must implement OnFilePickedListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

View File

@ -92,8 +92,8 @@ public abstract class FilePickerFragment extends AbstractFilePickerFragment<File
// If arrays are empty, then process was cancelled
if (permissions.length == 0) {
// Treat this as a cancel press
if (getTargetFragment() != null) {
((OnFilePickedListener) getTargetFragment()).onCancelled();
if (mListener != null) {
mListener.onCancelled();
}
} else { // if (requestCode == PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE) {
if (PackageManager.PERMISSION_GRANTED == grantResults[0]) {
@ -105,8 +105,8 @@ public abstract class FilePickerFragment extends AbstractFilePickerFragment<File
Toast.makeText(getContext(), R.string.nnf_permission_external_write_denied,
Toast.LENGTH_SHORT).show();
// Treat this as a cancel press
if (getTargetFragment() != null) {
((OnFilePickedListener) getTargetFragment()).onCancelled();
if (mListener != null) {
mListener.onCancelled();
}
}
}