1
0
mirror of https://github.com/danog/termux-api.git synced 2024-11-30 04:19:20 +01:00

Start a command to request a file from the system

Using the Storage Access Framework through something like

	termux-storage-get output-file

will allow one to get files from e.g. usb memory sticks.
This commit is contained in:
Fredrik Fornwall 2016-12-04 02:42:55 +01:00
parent 407681550a
commit 8927dd540b
3 changed files with 98 additions and 0 deletions

View File

@ -45,6 +45,10 @@
android:noHistory="true"
android:excludeFromRecents="true"
android:exported="false"/>
<activity android:name="com.termux.api.StorageGetAPI$StorageActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:excludeFromRecents="true"
android:exported="false"/>
<service android:name="com.termux.api.SpeechToTextAPI$SpeechToTextService"/>
<service android:name="com.termux.api.TextToSpeechAPI$TextToSpeechService" />
<provider android:authorities="com.termux.sharedfiles"

View File

@ -0,0 +1,89 @@
package com.termux.api;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import com.termux.api.util.ResultReturner;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
public class StorageGetAPI {
private static final String FILE_EXTRA = "com.termux.api.storage.file";
static void onReceive(TermuxApiReceiver apiReceiver, final Context context, final Intent intent) {
ResultReturner.returnData(apiReceiver, intent, new ResultReturner.ResultWriter() {
@Override
public void writeResult(PrintWriter out) throws Exception {
final String fileExtra = intent.getStringExtra("file");
if (fileExtra == null || !new File(fileExtra).getParentFile().canWrite()) {
out.println("ERROR: Not a writable folder: " + fileExtra);
return;
}
Intent intent = new Intent(context, StorageActivity.class);
intent.putExtra(FILE_EXTRA, fileExtra);
context.startActivity(intent);
}
});
}
public static class StorageActivity extends Activity {
private String outputFile;
@Override
public void onResume() {
super.onResume();
outputFile = getIntent().getStringExtra(FILE_EXTRA);
Log.e("termux-api", "output file: " + outputFile);
// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file browser.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
// Filter to only show results that can be "opened", such as a
// file (as opposed to a list of contacts or timezones)
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, 42);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
if (resultCode == RESULT_OK) {
Uri data = resultData.getData();
try {
try (InputStream in = getContentResolver().openInputStream(data)) {
try (OutputStream out = new FileOutputStream(outputFile)) {
byte[] buffer = new byte[8192];
while (true) {
int read = in.read(buffer);
if (read <= 0) {
break;
} else {
out.write(buffer, 0, read);
}
}
}
}
} catch (IOException e) {
Log.e("termux-api", "Error copying " + data + " to " + outputFile);
}
}
finish();
}
}
}

View File

@ -77,6 +77,11 @@ public class TermuxApiReceiver extends BroadcastReceiver {
SmsSendAPI.onReceive(this, intent);
}
break;
case "StorageGet":
StorageGetAPI.onReceive(this, context, intent);
break;
case "SpeechToText":
if (TermuxApiPermissionActivity.checkAndRequestPermissions(context, intent, Manifest.permission.RECORD_AUDIO)) {
SpeechToTextAPI.onReceive(context, intent);