1
1
package com .termux .filepicker ;
2
2
3
3
import android .app .Activity ;
4
- import android .app .AlertDialog ;
5
4
import android .content .Intent ;
6
5
import android .database .Cursor ;
7
6
import android .net .Uri ;
8
7
import android .provider .OpenableColumns ;
9
8
import android .util .Patterns ;
10
9
11
10
import com .termux .R ;
11
+ import com .termux .shared .data .DataUtils ;
12
+ import com .termux .shared .data .IntentUtils ;
13
+ import com .termux .shared .interact .MessageDialogUtils ;
12
14
import com .termux .shared .interact .TextInputDialogUtils ;
13
15
import com .termux .shared .termux .TermuxConstants ;
14
16
import com .termux .shared .termux .TermuxConstants .TERMUX_APP .TERMUX_SERVICE ;
@@ -39,6 +41,8 @@ public class TermuxFileReceiverActivity extends Activity {
39
41
*/
40
42
boolean mFinishOnDismissNameDialog = true ;
41
43
44
+ private static final String API_TAG = TermuxConstants .TERMUX_APP_NAME + "FileReceiver" ;
45
+
42
46
private static final String LOG_TAG = "TermuxFileReceiverActivity" ;
43
47
44
48
static boolean isSharedTextAnUrl (String sharedText ) {
@@ -55,44 +59,66 @@ protected void onResume() {
55
59
final String type = intent .getType ();
56
60
final String scheme = intent .getScheme ();
57
61
62
+ Logger .logVerbose (LOG_TAG , "Intent Received:\n " + IntentUtils .getIntentString (intent ));
63
+
64
+ final String sharedTitle = IntentUtils .getStringExtraIfSet (intent , Intent .EXTRA_TITLE , null );
65
+
58
66
if (Intent .ACTION_SEND .equals (action ) && type != null ) {
59
67
final String sharedText = intent .getStringExtra (Intent .EXTRA_TEXT );
60
68
final Uri sharedUri = intent .getParcelableExtra (Intent .EXTRA_STREAM );
61
69
62
- if (sharedText != null ) {
70
+ if (sharedUri != null ) {
71
+ handleContentUri (sharedUri , sharedTitle );
72
+ } else if (sharedText != null ) {
63
73
if (isSharedTextAnUrl (sharedText )) {
64
74
handleUrlAndFinish (sharedText );
65
75
} else {
66
- String subject = intent . getStringExtra ( Intent .EXTRA_SUBJECT );
67
- if (subject == null ) subject = intent . getStringExtra ( Intent . EXTRA_TITLE ) ;
76
+ String subject = IntentUtils . getStringExtraIfSet ( intent , Intent .EXTRA_SUBJECT , null );
77
+ if (subject == null ) subject = sharedTitle ;
68
78
if (subject != null ) subject += ".txt" ;
69
79
promptNameAndSave (new ByteArrayInputStream (sharedText .getBytes (StandardCharsets .UTF_8 )), subject );
70
80
}
71
- } else if (sharedUri != null ) {
72
- handleContentUri (sharedUri , intent .getStringExtra (Intent .EXTRA_TITLE ));
73
81
} else {
74
82
showErrorDialogAndQuit ("Send action without content - nothing to save." );
75
83
}
76
- } else if ("content" .equals (scheme )) {
77
- handleContentUri (intent .getData (), intent .getStringExtra (Intent .EXTRA_TITLE ));
78
- } else if ("file" .equals (scheme )) {
79
- // When e.g. clicking on a downloaded apk:
80
- String path = intent .getData ().getPath ();
81
- File file = new File (path );
82
- try {
83
- FileInputStream in = new FileInputStream (file );
84
- promptNameAndSave (in , file .getName ());
85
- } catch (FileNotFoundException e ) {
86
- showErrorDialogAndQuit ("Cannot open file: " + e .getMessage () + "." );
87
- }
88
84
} else {
89
- showErrorDialogAndQuit ("Unable to receive any file or URL." );
85
+ Uri dataUri = intent .getData ();
86
+
87
+ if (dataUri == null ) {
88
+ showErrorDialogAndQuit ("Data uri not passed." );
89
+ return ;
90
+ }
91
+
92
+ if ("content" .equals (scheme )) {
93
+ handleContentUri (dataUri , sharedTitle );
94
+ } else if ("file" .equals (scheme )) {
95
+ // When e.g. clicking on a downloaded apk:
96
+ String path = dataUri .getPath ();
97
+ if (DataUtils .isNullOrEmpty (path )) {
98
+ showErrorDialogAndQuit ("File path from data uri is null, empty or invalid." );
99
+ return ;
100
+ }
101
+
102
+ File file = new File (path );
103
+ try {
104
+ FileInputStream in = new FileInputStream (file );
105
+ promptNameAndSave (in , file .getName ());
106
+ } catch (FileNotFoundException e ) {
107
+ showErrorDialogAndQuit ("Cannot open file: " + e .getMessage () + "." );
108
+ }
109
+ } else {
110
+ showErrorDialogAndQuit ("Unable to receive any file or URL." );
111
+ }
90
112
}
91
113
}
92
114
93
115
void showErrorDialogAndQuit (String message ) {
94
116
mFinishOnDismissNameDialog = false ;
95
- new AlertDialog .Builder (this ).setMessage (message ).setOnDismissListener (dialog -> finish ()).setPositiveButton (android .R .string .ok , (dialog , which ) -> finish ()).show ();
117
+ MessageDialogUtils .showMessage (this ,
118
+ API_TAG , message ,
119
+ null , (dialog , which ) -> finish (),
120
+ null , null ,
121
+ dialog -> finish ());
96
122
}
97
123
98
124
void handleContentUri (final Uri uri , String subjectFromIntent ) {
@@ -157,10 +183,17 @@ void promptNameAndSave(final InputStream in, final String attachmentFileName) {
157
183
158
184
public File saveStreamWithName (InputStream in , String attachmentFileName ) {
159
185
File receiveDir = new File (TERMUX_RECEIVEDIR );
186
+
187
+ if (DataUtils .isNullOrEmpty (attachmentFileName )) {
188
+ showErrorDialogAndQuit ("File name cannot be null or empty" );
189
+ return null ;
190
+ }
191
+
160
192
if (!receiveDir .isDirectory () && !receiveDir .mkdirs ()) {
161
193
showErrorDialogAndQuit ("Cannot create directory: " + receiveDir .getAbsolutePath ());
162
194
return null ;
163
195
}
196
+
164
197
try {
165
198
final File outFile = new File (receiveDir , attachmentFileName );
166
199
try (FileOutputStream f = new FileOutputStream (outFile )) {
@@ -182,7 +215,7 @@ void handleUrlAndFinish(final String url) {
182
215
final File urlOpenerProgramFile = new File (URL_OPENER_PROGRAM );
183
216
if (!urlOpenerProgramFile .isFile ()) {
184
217
showErrorDialogAndQuit ("The following file does not exist:\n $HOME/bin/termux-url-opener\n \n "
185
- + "Create this file as a script or a symlink - it will be called with the shared URL as only argument." );
218
+ + "Create this file as a script or a symlink - it will be called with the shared URL as the first argument." );
186
219
return ;
187
220
}
188
221
0 commit comments