Don’t export as email message, and be less strict about import requirements

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.
This commit is contained in:
Chris Garrity 2020-04-13 17:07:38 -04:00
parent 098b2dc68a
commit 1e3d468c33
3 changed files with 34 additions and 12 deletions

View file

@ -40,12 +40,17 @@
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" />
<data android:scheme="content" />
<data android:mimeType="*/*" />
<data android:pathPattern="@string/share_extension_filter" />
<!-- These additional pathPattern blocks are to allow for paths with
additional periods in them. See:
http://stackoverflow.com/questions/3400072/pathpattern-to-match-file-extension-does-not-work-if-a-period-exists-elsewhere-i/8599921 -->
<data android:pathPattern="@{`.*\\.` + @string/share_extension_filter}"/>
<data android:pathPattern="@{`.*\\..*\\.` + @string/share_extension_filter}"/>
<data android:pathPattern="@{`.*\\..*\\..*\\.` + @string/share_extension_filter}"/>
<data android:pathPattern="@{`.*\\..*\\..*\\..*\\.` + @string/share_extension_filter}"/>
<data android:pathPattern="@{`.*\\..*\\..*\\..*\\..*\\.` + @string/share_extension_filter}"/>
<data android:host="*" />
<data android:scheme="file" />
<data android:scheme="content" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
@ -56,6 +61,16 @@
<data android:mimeType="@string/share_mimetype" />
<data android:host="*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" />
<data android:scheme="content" />
<data android:mimeType="application/octet-stream" />
<data android:host="*" />
</intent-filter>
</activity>
<receiver android:name="com.google.android.gms.analytics.AnalyticsReceiver"

View file

@ -3,8 +3,6 @@ package org.scratchjr.android;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -350,7 +348,7 @@ public class JavaScriptDirectInterface {
public String scratchjr_cameracheck() {
return _activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY) ? "1" : "0";
}
@JavascriptInterface
public boolean scratchjr_has_multiple_cameras() {
return Camera.getNumberOfCameras() > 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

View file

@ -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]);