Skip to content

Commit

Permalink
Enable Sharing a file from Files app
Browse files Browse the repository at this point in the history
  • Loading branch information
martinwork committed Feb 29, 2024
1 parent 19d76d4 commit 5437277
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@
android:scheme="content"/>
</intent-filter>

<!-- This filter works, when sharing a hex file from Files app -->
<!-- https://developer.android.com/training/sharing/receive#update-manifest -->
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:mimeType="application/octet-stream" />
</intent-filter>

</activity>
<activity
android:name=".ui.activity.NotificationActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand Down Expand Up @@ -466,11 +467,18 @@ private void handleIncomingIntent(Intent intent) {
String fileName;

Project externalProject = null;
Uri uri = null;

if(intent.getData() != null && intent.getData().getEncodedPath() != null) {
isOpenByOtherApp = true;
String action = intent.getAction();
String type = intent.getType();
if ( Intent.ACTION_SEND.equals(action) && intent.hasExtra( Intent.EXTRA_STREAM)) {
uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
} else if(intent.getData() != null && intent.getData().getEncodedPath() != null) {
uri = intent.getData();
}

Uri uri = intent.getData();
if ( uri != null) {
isOpenByOtherApp = true;
String encodedPath = uri.getEncodedPath();

String scheme = uri.getScheme();
Expand All @@ -479,9 +487,7 @@ private void handleIncomingIntent(Intent intent) {
fileName = fileNameForFlashing(fullPathOfFile);
externalProject = fileName == null ? null : new Project(fileName, fullPathOfFile, 0, null, false);
} else if(scheme.equals("content")) {

Cursor cursor = null;

try {
cursor = getContentResolver().query(uri, null, null, null, null);

Expand Down Expand Up @@ -541,7 +547,7 @@ private void handleIncomingIntent(Intent intent) {
if ( mProgramToSend != null) {
startBluetoothForFlashing();
} else {
if (isOpenByOtherApp) {
if ( isOpenByOtherApp) {
Toast.makeText(this, "Not a micro:bit HEX file", Toast.LENGTH_LONG).show();
onFlashComplete();
}
Expand Down

0 comments on commit 5437277

Please sign in to comment.