Add checks for filetype, skip unnecessary FileIO

This PR was 95% of the way there, and I didn’t want to ask for changes after not getting to review it for so long, so I just made the changes.

Thanks for submitting!
This commit is contained in:
Chris Garrity 2020-04-07 14:49:55 -04:00
parent bbba46c143
commit 9a12f98cf9
3 changed files with 11 additions and 14 deletions

View file

@ -19,7 +19,7 @@ android {
free {
applicationId "org.scratchjr.androidfree"
minSdkVersion 19
targetSdkVersion 27
targetSdkVersion 28
versionCode 21
versionName "1.2.0"
}

View file

@ -41,6 +41,7 @@
<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" />
<data android:host="*" />
@ -49,8 +50,9 @@
<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="@string/share_mimetype" />
<data android:pathPattern="@string/share_extension_filter" />
<data android:host="*" />
</intent-filter>
</activity>

View file

@ -38,8 +38,6 @@ import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@ -310,21 +308,18 @@ public class ScratchJrActivity
}
private void receiveProject(Uri projectUri) {
File projectFile = null;
String PROJECT_EXTENSION = getApplicationContext().getString(R.string.share_extension_filter);
String scheme = projectUri.getScheme();
if (scheme != null) {
if (scheme.equals(ContentResolver.SCHEME_FILE)) {
String filePath = projectUri.getPath();
projectFile = filePath != null ? new File(filePath) : null;
} else if (!scheme.equals(ContentResolver.SCHEME_CONTENT)) {
return;
}
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)) {
return;
}
// Read the project one byte at a time into a buffer
ByteArrayOutputStream projectData = new ByteArrayOutputStream();
try {
InputStream is = (projectFile != null) ? new FileInputStream(projectFile) :
getContentResolver().openInputStream(projectUri);
InputStream is = getContentResolver().openInputStream(projectUri);
byte[] readByte = new byte[1];
while ((is.read(readByte)) == 1) {