2017-09-18 12:29:43 +00:00
|
|
|
package org.schabi.newpipe.util;
|
|
|
|
|
2018-03-08 14:50:46 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2019-08-17 16:38:33 +00:00
|
|
|
import android.net.Uri;
|
2017-09-18 12:29:43 +00:00
|
|
|
import android.os.Bundle;
|
2018-03-08 14:50:46 +00:00
|
|
|
import android.os.Environment;
|
|
|
|
import android.util.TypedValue;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.loader.content.Loader;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
import androidx.recyclerview.widget.SortedList;
|
|
|
|
|
2018-03-08 14:50:46 +00:00
|
|
|
import com.nononsenseapps.filepicker.AbstractFilePickerFragment;
|
|
|
|
import com.nononsenseapps.filepicker.FilePickerFragment;
|
|
|
|
|
2017-09-18 12:29:43 +00:00
|
|
|
import org.schabi.newpipe.R;
|
|
|
|
|
2018-03-08 14:50:46 +00:00
|
|
|
import java.io.File;
|
|
|
|
|
2017-09-18 12:29:43 +00:00
|
|
|
public class FilePickerActivityHelper extends com.nononsenseapps.filepicker.FilePickerActivity {
|
2018-03-08 14:50:46 +00:00
|
|
|
private CustomFilePickerFragment currentFragment;
|
|
|
|
|
2020-03-31 17:20:15 +00:00
|
|
|
public static Intent chooseSingleFile(@NonNull final Context context) {
|
|
|
|
return new Intent(context, FilePickerActivityHelper.class)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, false)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_SINGLE_CLICK, true)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Intent chooseFileToSave(@NonNull final Context context,
|
|
|
|
@Nullable final String startPath) {
|
|
|
|
return new Intent(context, FilePickerActivityHelper.class)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, true)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_EXISTING_FILE, true)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_START_PATH, startPath)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_MODE,
|
|
|
|
FilePickerActivityHelper.MODE_NEW_FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isOwnFileUri(@NonNull final Context context, @NonNull final Uri uri) {
|
|
|
|
if (uri.getAuthority() == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return uri.getAuthority().startsWith(context.getPackageName());
|
|
|
|
}
|
|
|
|
|
2017-09-18 12:29:43 +00:00
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void onCreate(final Bundle savedInstanceState) {
|
2019-08-17 16:38:33 +00:00
|
|
|
if (ThemeHelper.isLightThemeSelected(this)) {
|
2017-09-18 12:29:43 +00:00
|
|
|
this.setTheme(R.style.FilePickerThemeLight);
|
|
|
|
} else {
|
|
|
|
this.setTheme(R.style.FilePickerThemeDark);
|
|
|
|
}
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
}
|
2018-03-08 14:50:46 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
// If at top most level, normal behaviour
|
|
|
|
if (currentFragment.isBackTop()) {
|
|
|
|
super.onBackPressed();
|
|
|
|
} else {
|
|
|
|
// Else go up
|
|
|
|
currentFragment.goUp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
protected AbstractFilePickerFragment<File> getFragment(@Nullable final String startPath,
|
|
|
|
final int mode,
|
|
|
|
final boolean allowMultiple,
|
|
|
|
final boolean allowCreateDir,
|
|
|
|
final boolean allowExistingFile,
|
|
|
|
final boolean singleClick) {
|
2018-03-08 14:50:46 +00:00
|
|
|
final CustomFilePickerFragment fragment = new CustomFilePickerFragment();
|
2020-03-31 17:20:15 +00:00
|
|
|
fragment.setArgs(startPath != null ? startPath
|
|
|
|
: Environment.getExternalStorageDirectory().getPath(),
|
2018-03-08 14:50:46 +00:00
|
|
|
mode, allowMultiple, allowCreateDir, allowExistingFile, singleClick);
|
2020-03-31 17:20:15 +00:00
|
|
|
currentFragment = fragment;
|
|
|
|
return currentFragment;
|
2019-08-17 16:38:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-08 14:50:46 +00:00
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Internal
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
|
|
public static class CustomFilePickerFragment extends FilePickerFragment {
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
|
|
|
|
final Bundle savedInstanceState) {
|
2018-03-08 14:50:46 +00:00
|
|
|
return super.onCreateView(inflater, container, savedInstanceState);
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent,
|
|
|
|
final int viewType) {
|
2018-03-08 14:50:46 +00:00
|
|
|
final RecyclerView.ViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
|
|
|
|
|
|
|
|
final View view = viewHolder.itemView.findViewById(android.R.id.text1);
|
|
|
|
if (view instanceof TextView) {
|
2020-03-31 17:20:15 +00:00
|
|
|
((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX,
|
|
|
|
getResources().getDimension(R.dimen.file_picker_items_text_size));
|
2018-03-08 14:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return viewHolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void onClickOk(@NonNull final View view) {
|
2018-03-08 14:50:46 +00:00
|
|
|
if (mode == MODE_NEW_FILE && getNewFileName().isEmpty()) {
|
2020-03-31 17:20:15 +00:00
|
|
|
if (mToast != null) {
|
|
|
|
mToast.cancel();
|
|
|
|
}
|
|
|
|
mToast = Toast.makeText(getActivity(), R.string.file_name_empty_error,
|
|
|
|
Toast.LENGTH_SHORT);
|
2018-03-08 14:50:46 +00:00
|
|
|
mToast.show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
super.onClickOk(view);
|
|
|
|
}
|
|
|
|
|
2019-09-01 20:35:42 +00:00
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
protected boolean isItemVisible(@NonNull final File file) {
|
|
|
|
if (file.isDirectory() && file.isHidden()) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-01 20:35:42 +00:00
|
|
|
return super.isItemVisible(file);
|
|
|
|
}
|
|
|
|
|
2018-03-08 14:50:46 +00:00
|
|
|
public File getBackTop() {
|
2020-03-31 17:20:15 +00:00
|
|
|
if (getArguments() == null) {
|
|
|
|
return Environment.getExternalStorageDirectory();
|
|
|
|
}
|
2018-03-08 14:50:46 +00:00
|
|
|
|
|
|
|
final String path = getArguments().getString(KEY_START_PATH, "/");
|
|
|
|
if (path.contains(Environment.getExternalStorageDirectory().getPath())) {
|
|
|
|
return Environment.getExternalStorageDirectory();
|
|
|
|
}
|
|
|
|
|
|
|
|
return getPath(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isBackTop() {
|
2020-03-31 17:20:15 +00:00
|
|
|
return compareFiles(mCurrentPath,
|
|
|
|
getBackTop()) == 0 || compareFiles(mCurrentPath, new File("/")) == 0;
|
2018-03-08 14:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void onLoadFinished(final Loader<SortedList<File>> loader,
|
|
|
|
final SortedList<File> data) {
|
2018-03-08 14:50:46 +00:00
|
|
|
super.onLoadFinished(loader, data);
|
|
|
|
layoutManager.scrollToPosition(0);
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 12:29:43 +00:00
|
|
|
}
|