Skip to content

Commit d014923

Browse files
committed
ANDROID: fix permission issue with share command
1 parent 0a358f3 commit d014923

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

ChangeLog

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
2019-08-19 (0.12.15)
2-
ANDROID: fix issue with crash on minimise with googleos
3-
ANDROID: update to androidx
2+
ANDROID: fix issue with crash on minimise with ChromeOS
3+
ANDROID: fix permission issue with share command
4+
ANDROID: (internal) update to androidx
45

56
2019-07-27 (0.12.15)
67
SDL: added -n command line option to run then not pause for back key

src/platform/android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ android {
4747
}
4848

4949
dependencies {
50-
implementation 'androidx.core:core:1.0.0'
50+
implementation 'androidx.core:core:1.0.2'
5151
testImplementation 'junit:junit:4.12'
5252
}

src/platform/android/app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@
5252
<data android:scheme="smallbasic" />
5353
</intent-filter>
5454
</activity>
55+
<provider
56+
android:name="androidx.core.content.FileProvider"
57+
android:authorities="net.sourceforge.smallbasic.provider"
58+
android:exported="false"
59+
android:grantUriPermissions="true">
60+
<meta-data
61+
android:name="android.support.FILE_PROVIDER_PATHS"
62+
android:resource="@xml/file_paths"/>
63+
</provider>
5564
</application>
5665
<!-- Permissions -->
5766
<uses-permission android:name="android.permission.INTERNET" />

src/platform/android/app/src/main/java/net/sourceforge/smallbasic/MainActivity.java

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
import android.os.Bundle;
2525
import android.os.Environment;
2626
import android.os.Handler;
27-
import androidx.annotation.NonNull;
28-
import androidx.core.app.ActivityCompat;
29-
import androidx.core.content.ContextCompat;
3027
import android.util.Base64;
3128
import android.util.DisplayMetrics;
3229
import android.util.Log;
@@ -46,11 +43,15 @@
4643
import java.io.ByteArrayOutputStream;
4744
import java.io.DataInputStream;
4845
import java.io.File;
46+
import java.io.FileInputStream;
4947
import java.io.FileNotFoundException;
48+
import java.io.FileOutputStream;
5049
import java.io.FileReader;
5150
import java.io.FileWriter;
5251
import java.io.IOException;
52+
import java.io.InputStream;
5353
import java.io.InputStreamReader;
54+
import java.io.OutputStream;
5455
import java.io.UnsupportedEncodingException;
5556
import java.net.Inet4Address;
5657
import java.net.InetAddress;
@@ -72,6 +73,11 @@
7273
import java.util.concurrent.Semaphore;
7374
import java.util.zip.GZIPInputStream;
7475

76+
import androidx.annotation.NonNull;
77+
import androidx.core.app.ActivityCompat;
78+
import androidx.core.content.ContextCompat;
79+
import androidx.core.content.FileProvider;
80+
7581
/**
7682
* Extends NativeActivity to provide interface methods for runtime.cpp
7783
*
@@ -539,7 +545,8 @@ public void share(final byte[] pathBytes) {
539545
Intent sendIntent = new Intent();
540546
sendIntent.setAction(Intent.ACTION_SEND);
541547
sendIntent.putExtra(Intent.EXTRA_TEXT, buffer);
542-
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
548+
sendIntent.putExtra(Intent.EXTRA_STREAM, getSharedFile(file));
549+
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
543550
sendIntent.putExtra(Intent.EXTRA_SUBJECT, file.getName());
544551
sendIntent.setType("text/plain");
545552
startActivity(Intent.createChooser(sendIntent, "Share"));
@@ -669,6 +676,24 @@ public void run() {
669676
});
670677
}
671678

679+
private void copy(File src, File dst) throws IOException {
680+
InputStream in = new FileInputStream(src);
681+
try {
682+
OutputStream out = new FileOutputStream(dst);
683+
try {
684+
byte[] buf = new byte[1024];
685+
int len;
686+
while ((len = in.read(buf)) > 0) {
687+
out.write(buf, 0, len);
688+
}
689+
} finally {
690+
out.close();
691+
}
692+
} finally {
693+
in.close();
694+
}
695+
}
696+
672697
private String execBuffer(final String buffer, final String name, boolean run) throws IOException {
673698
File outputFile = getApplication().getFileStreamPath(name);
674699
BufferedWriter output = new BufferedWriter(new FileWriter(outputFile));
@@ -773,6 +798,23 @@ private Map<String, String> getPostData(DataInputStream inputStream, final Strin
773798
return result;
774799
}
775800

801+
private Uri getSharedFile(File file) {
802+
Uri result;
803+
try {
804+
File sharesPath = new File(getExternalFilesDir(null), "shares");
805+
if (sharesPath.mkdirs()) {
806+
Log.i(TAG, "created folder: " + sharesPath.toString());
807+
}
808+
File shareFile = new File(sharesPath, file.getName());
809+
copy(file, sharesPath);
810+
result = FileProvider.getUriForFile(this, "net.sourceforge.smallbasic.provider", shareFile);
811+
} catch (Exception e) {
812+
result = null;
813+
Log.e(TAG, "failed to create shared file", e);
814+
}
815+
return result;
816+
}
817+
776818
private String getString(final byte[] promptBytes) {
777819
try {
778820
return new String(promptBytes, CP1252);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<paths>
2+
<external-files-path name="shares" path="shares/" />
3+
</paths>

0 commit comments

Comments
 (0)