-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
5 changed files
with
212 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
app/src/main/java/com/termux/filepicker/TermuxFilePickerActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
app/src/main/java/com/termux/filepicker/TermuxFilePickerProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:paddingLeft="8dp" | ||
android:paddingRight="8dp"> | ||
|
||
<ListView android:id="@android:id/list" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_weight="1" | ||
android:drawSelectorOnTop="false"/> | ||
|
||
<TextView android:id="@android:id/empty" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:text="$HOME is empty!"/> | ||
</LinearLayout> |