Skip to content

Commit

Permalink
Bug fix: fix crash on share file for Android 8; resolve #186
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishiranu committed Jan 6, 2018
1 parent 2fffd44 commit f9e1f00
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 27 deletions.
7 changes: 4 additions & 3 deletions src/com/mishiranu/dashchan/content/CacheManager.java
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 Fukurou Mishiranu
* Copyright 2014-2018 Fukurou Mishiranu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -983,7 +983,7 @@ private void handleGalleryShareFiles() {
}
}

public Pair<File, String> prepareFileForShare(File file, String fileName) {
public Pair<Uri, String> prepareFileForShare(File file, String fileName) {
File tempDirectory = getExternalTempDirectory();
if (tempDirectory == null) {
return null;
Expand All @@ -998,6 +998,7 @@ public Pair<File, String> prepareFileForShare(File file, String fileName) {
fileName = GALLERY_SHARE_FILE_NAME_START + System.currentTimeMillis() + "." + extension;
File shareFile = new File(tempDirectory, fileName);
IOUtils.copyInternalFile(file, shareFile);
return new Pair<>(shareFile, mimeType);
Uri uri = FileProvider.convertShareFile(tempDirectory, shareFile, mimeType);
return new Pair<>(uri, mimeType);
}
}
80 changes: 62 additions & 18 deletions src/com/mishiranu/dashchan/content/FileProvider.java
@@ -1,5 +1,5 @@
/*
* Copyright 2018 Fukurou Mishiranu
* Copyright 2017-2018 Fukurou Mishiranu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,16 +40,19 @@ public class FileProvider extends ContentProvider {
private static final String AUTHORITY = "com.mishiranu.providers.dashchan";
private static final String PATH_UPDATES = "updates";
private static final String PATH_DOWNLOADS = "downloads";
private static final String PATH_SHARE = "share";

private static final int URI_UPDATES = 1;
private static final int URI_DOWNLOADS = 2;
private static final int URI_SHARE = 3;

private static final UriMatcher URI_MATCHER;

static {
URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
URI_MATCHER.addURI(AUTHORITY, PATH_UPDATES + "/*", URI_UPDATES);
URI_MATCHER.addURI(AUTHORITY, PATH_DOWNLOADS + "/*", URI_DOWNLOADS);
URI_MATCHER.addURI(AUTHORITY, PATH_SHARE + "/*", URI_SHARE);
}

@Override
Expand Down Expand Up @@ -89,27 +92,53 @@ public static Uri convertUpdatesUri(Context context, Uri uri) {
return uri;
}

private static File lastDownloadsFile;
private static Uri lastDownloadsUri;
private static String lastDownloadsFileType;
private static class InternalFile {
public final File file;
public final String type;
public final Uri uri;

public static Uri convertDownloadsFile(File file, String type) {
public InternalFile(File file, String type, Uri uri) {
this.file = file;
this.uri = uri;
this.type = type;
}
}

private static InternalFile downloadsFile;
private static InternalFile shareFile;

private static InternalFile convertFile(File directory, File file, String type, String providerPath) {
if (C.API_NOUGAT) {
String filePath = file.getAbsolutePath();
String directoryPath = Preferences.getDownloadDirectory().getAbsolutePath();
String directoryPath = directory.getAbsolutePath();
if (filePath.startsWith(directoryPath)) {
filePath = filePath.substring(directoryPath.length());
if (filePath.startsWith("/")) {
filePath = filePath.substring(1);
}
// Allow only one URI for current notification
lastDownloadsFile = file;
lastDownloadsFileType = type;
lastDownloadsUri = new Uri.Builder().scheme("content").authority(AUTHORITY)
.appendPath(PATH_DOWNLOADS).appendEncodedPath(filePath).build();
return lastDownloadsUri;
Uri uri = new Uri.Builder().scheme("content").authority(AUTHORITY)
.appendPath(providerPath).appendEncodedPath(filePath).build();
return new InternalFile(file, type, uri);
}
}
return null;
}

public static Uri convertDownloadsFile(File file, String type) {
InternalFile internalFile = convertFile(Preferences.getDownloadDirectory(), file, type, PATH_DOWNLOADS);
if (internalFile != null) {
downloadsFile = internalFile;
return internalFile.uri;
}
return Uri.fromFile(file);
}

public static Uri convertShareFile(File directory, File file, String type) {
InternalFile internalFile = convertFile(directory, file, type, PATH_SHARE);
if (internalFile != null) {
shareFile = internalFile;
return internalFile.uri;
}
return Uri.fromFile(file);
}

Expand All @@ -124,8 +153,13 @@ public String getType(Uri uri) {
return "application/vnd.android.package-archive";
}
case URI_DOWNLOADS: {
if (lastDownloadsFileType != null) {
return lastDownloadsFileType;
if (downloadsFile != null) {
return downloadsFile.type;
}
}
case URI_SHARE: {
if (shareFile != null) {
return shareFile.type;
}
}
default: {
Expand All @@ -147,8 +181,13 @@ public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundEx
}
}
case URI_DOWNLOADS: {
if (uri.equals(lastDownloadsUri)) {
return ParcelFileDescriptor.open(lastDownloadsFile, ParcelFileDescriptor.MODE_READ_ONLY);
if (downloadsFile != null && uri.equals(downloadsFile.uri)) {
return ParcelFileDescriptor.open(downloadsFile.file, ParcelFileDescriptor.MODE_READ_ONLY);
}
}
case URI_SHARE: {
if (shareFile != null && uri.equals(shareFile.uri)) {
return ParcelFileDescriptor.open(shareFile.file, ParcelFileDescriptor.MODE_READ_ONLY);
}
}
default: {
Expand All @@ -164,7 +203,8 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel
int matchResult = URI_MATCHER.match(uri);
switch (URI_MATCHER.match(uri)) {
case URI_UPDATES:
case URI_DOWNLOADS: {
case URI_DOWNLOADS:
case URI_SHARE: {
if (projection == null) {
projection = PROJECTION;
}
Expand All @@ -184,7 +224,11 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel
break;
}
case URI_DOWNLOADS: {
file = lastDownloadsFile;
file = downloadsFile != null ? downloadsFile.file : null;
break;
}
case URI_SHARE: {
file = shareFile != null ? shareFile.file : null;
break;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/com/mishiranu/dashchan/util/NavigationUtils.java
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2017 Fukurou Mishiranu
* Copyright 2014-2018 Fukurou Mishiranu
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,6 +53,7 @@
import com.mishiranu.dashchan.C;
import com.mishiranu.dashchan.R;
import com.mishiranu.dashchan.content.CacheManager;
import com.mishiranu.dashchan.content.FileProvider;
import com.mishiranu.dashchan.content.model.GalleryItem;
import com.mishiranu.dashchan.content.net.CloudFlarePasser;
import com.mishiranu.dashchan.content.service.AudioPlayerService;
Expand Down Expand Up @@ -426,15 +427,14 @@ public static void shareLink(Context context, String subject, Uri uri) {
}

public static void shareFile(Context context, File file, String fileName) {
Pair<File, String> data = CacheManager.getInstance().prepareFileForShare(file, fileName);
Pair<Uri, String> data = CacheManager.getInstance().prepareFileForShare(file, fileName);
if (data == null) {
ToastUtils.show(context, R.string.message_cache_unavailable);
return;
}
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(data.second);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(data.first));
context.startActivity(Intent.createChooser(intent, null));
int intentFlags = FileProvider.getIntentFlags();
context.startActivity(Intent.createChooser(new Intent(Intent.ACTION_SEND)
.setType(data.second).setFlags(intentFlags).putExtra(Intent.EXTRA_STREAM, data.first), null));
}

public static void restartApplication(Context context) {
Expand Down

0 comments on commit f9e1f00

Please sign in to comment.