mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-08 03:24:04 +08:00
Add a file picker to pick files from $HOME
This allows e.g. files from the $HOME folder to be attached to emails or uploaded when in other apps. Also bump version to 0.18.
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
package com.termux.filepicker;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.ListActivity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ListView;
|
||||
|
||||
import com.termux.R;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/** Activity allowing picking files from the $HOME folder. */
|
||||
public class TermuxFilePickerActivity extends ListActivity {
|
||||
|
||||
@SuppressLint("SdCardPath")
|
||||
final String TERMUX_HOME = "/data/data/com.termux/files/home";
|
||||
|
||||
private File mCurrentDirectory;
|
||||
private final List<File> mFiles = new ArrayList<>();
|
||||
private final List<String> mFileNames = new ArrayList<>();
|
||||
private ArrayAdapter mAdapter;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.file_picker);
|
||||
|
||||
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mFileNames);
|
||||
|
||||
enterDirectory(new File(TERMUX_HOME));
|
||||
setListAdapter(mAdapter);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
int id = item.getItemId();
|
||||
if (id == android.R.id.home) {
|
||||
enterDirectory(mCurrentDirectory.getParentFile());
|
||||
return true;
|
||||
} else {
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onListItemClick(ListView l, View v, int position, long id) {
|
||||
super.onListItemClick(l, v, position, id);
|
||||
File requestFile = mFiles.get(position);
|
||||
if (requestFile.isDirectory()) {
|
||||
enterDirectory(requestFile);
|
||||
} else {
|
||||
Uri returnUri = Uri.withAppendedPath(Uri.parse("content://com.termux.filepicker.provider/"), requestFile.getAbsolutePath());
|
||||
Intent returnIntent = new Intent().setData(returnUri);
|
||||
returnIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
setResult(Activity.RESULT_OK, returnIntent);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
void enterDirectory(File directory) {
|
||||
getActionBar().setDisplayHomeAsUpEnabled(!directory.getAbsolutePath().equals(TERMUX_HOME));
|
||||
|
||||
String title = directory.getAbsolutePath() + "/";
|
||||
if (title.startsWith(TERMUX_HOME)) {
|
||||
title = "~" + title.substring(TERMUX_HOME.length(), title.length());
|
||||
}
|
||||
setTitle(title);
|
||||
|
||||
mCurrentDirectory = directory;
|
||||
mFiles.clear();
|
||||
mFileNames.clear();
|
||||
mFiles.addAll(Arrays.asList(mCurrentDirectory.listFiles()));
|
||||
|
||||
Collections.sort(mFiles, new Comparator<File>() {
|
||||
@Override
|
||||
public int compare(File f1, File f2) {
|
||||
final String n1 = f1.getName();
|
||||
final String n2 = f2.getName();
|
||||
// Display dot folders last:
|
||||
if (n1.startsWith(".") && !n2.startsWith(".")) {
|
||||
return 1;
|
||||
} else if (n2.startsWith(".") && !n1.startsWith(".")) {
|
||||
return -1;
|
||||
}
|
||||
return n1.compareToIgnoreCase(n2);
|
||||
}
|
||||
});
|
||||
|
||||
for (File file : mFiles) {
|
||||
mFileNames.add(file.getName() + (file.isDirectory() ? "/" : ""));
|
||||
}
|
||||
mAdapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
package com.termux.filepicker;
|
||||
|
||||
|
||||
import android.content.ContentProvider;
|
||||
import android.content.ContentValues;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
/** Provider of files content uris picked from {@link com.termux.filepicker.TermuxFilePickerActivity}. */
|
||||
public class TermuxFilePickerProvider extends ContentProvider {
|
||||
@Override
|
||||
public boolean onCreate() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType(Uri uri) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Uri insert(Uri uri, ContentValues values) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
|
||||
File file = new File(uri.getPath());
|
||||
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user