From 1e3d468c3371734680c221d79aff1f73a2fdf33f Mon Sep 17 00:00:00 2001 From: Chris Garrity Date: Mon, 13 Apr 2020 17:07:38 -0400 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20export=20as=20email=20message,?= =?UTF-8?q?=20and=20be=20less=20strict=20about=20import=20requirements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don’t export as email message. and be less strict about import requirements. Android intent filters are really only designed to work with standard mimetypes (like ‘image/png’). Even if you can share something with a custom mime type, it’s likely to get lost somewhere along the way, from email, or Gdrive, or Files app etc. Path matching patterns in intent filter only apply to `file` schemes, `content` scheme is likely to be some generated id in temp storage. `content` filters maintly go by mimetype, but as noted above, a custom mimetype has often gone AWOL. Generic application files are usually downloaded as `application/octet-stream`. Basically we have to trust the user not to try to load some other random file into ScratchJr. The import function will fail and give an error message if they do. Finally, fix the way we’re sharing ScratchJr files. While the button says ‘Share by Email’ for a long time Android has shown a selection of ways to share the file. However, we were setting the mime type as an email message so the saved file would always try to open in an email client instead of ScratchJr. --- .../app/src/main/AndroidManifest.xml | 21 ++++++++++++++++--- .../android/JavaScriptDirectInterface.java | 14 +++++++------ .../scratchjr/android/ScratchJrActivity.java | 11 +++++++--- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/android/ScratchJr/app/src/main/AndroidManifest.xml b/android/ScratchJr/app/src/main/AndroidManifest.xml index 42dff0d..68c2303 100644 --- a/android/ScratchJr/app/src/main/AndroidManifest.xml +++ b/android/ScratchJr/app/src/main/AndroidManifest.xml @@ -40,12 +40,17 @@ - + + + + + + - - @@ -56,6 +61,16 @@ + + + + + + + + + + 1; @@ -582,10 +580,13 @@ public class JavaScriptDirectInterface { File tempFile; String extension; + String mimetype; if (BuildConfig.APPLICATION_ID.equals("org.pbskids.scratchjr")) { extension = ".psjr"; + mimetype = "application/x-pbskids-scratchjr-project"; } else { extension = ".sjr"; + mimetype = "application/x-scratchjr-project"; } try { @@ -602,17 +603,18 @@ public class JavaScriptDirectInterface { } final Intent it = new Intent(Intent.ACTION_SEND); - it.setType("message/rfc822"); + it.setType(mimetype); it.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {}); - it.putExtra(android.content.Intent.EXTRA_SUBJECT, emailSubject); + it.putExtra(android.content.Intent.EXTRA_SUBJECT, fileName); it.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(emailBody)); // The stream data is a reference to the temporary file provided by our contentprovider it.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://" + ShareContentProvider.AUTHORITY + "/" + fileName)); + Intent shareIntent = Intent.createChooser(it, null); - _activity.startActivity(it); + _activity.startActivity(shareIntent); } // Analytics diff --git a/android/ScratchJr/app/src/main/java/org/scratchjr/android/ScratchJrActivity.java b/android/ScratchJr/app/src/main/java/org/scratchjr/android/ScratchJrActivity.java index afc7232..6d4f9ca 100644 --- a/android/ScratchJr/app/src/main/java/org/scratchjr/android/ScratchJrActivity.java +++ b/android/ScratchJr/app/src/main/java/org/scratchjr/android/ScratchJrActivity.java @@ -310,15 +310,20 @@ public class ScratchJrActivity String scheme = projectUri.getScheme(); Log.i(LOG_TAG, "receiveProject(scheme): " + scheme); Log.i(LOG_TAG, "receiveProject(path): " + projectUri.getPath()); - if (scheme == null || !(scheme.equals(ContentResolver.SCHEME_FILE) || scheme.equals(ContentResolver.SCHEME_CONTENT)) || - !projectUri.getPath().matches(PROJECT_EXTENSION)) { + + // if scheme isn't file or content, skip import + if (scheme == null || !(scheme.equals(ContentResolver.SCHEME_FILE) || scheme.equals(ContentResolver.SCHEME_CONTENT))) { + return; + } + // if scheme is file, then skip if filename doesn't have scratchjr project extension + if (scheme.equals(ContentResolver.SCHEME_FILE) && !projectUri.getPath().matches(PROJECT_EXTENSION)) { return; } // Read the project one byte at a time into a buffer ByteArrayOutputStream projectData = new ByteArrayOutputStream(); try { InputStream is = getContentResolver().openInputStream(projectUri); - + byte[] readByte = new byte[1]; while ((is.read(readByte)) == 1) { projectData.write(readByte[0]);