Skip to content

fix apk file open fails in some cases #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri
int storage = ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
PendingIntent pendingIntent = null;
if (status == DownloadStatus.COMPLETE && clickToOpenDownloadedFile && storage == PackageManager.PERMISSION_GRANTED) {
Intent intent = IntentUtils.getOpenFileIntent(getApplicationContext(), saveFilePath, contentType);
if (IntentUtils.validateIntent(getApplicationContext(), intent)) {
Intent intent = IntentUtils.validatedFileIntent(getApplicationContext(), saveFilePath, contentType);
if (intent != null) {
Log.d(TAG, "Setting an intent to open the file " + saveFilePath);
pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ private void open(MethodCall call, MethodChannel.Result result) {
filename = fileURL.substring(fileURL.lastIndexOf("/") + 1, fileURL.length());
}
String saveFilePath = savedDir + File.separator + filename;
Intent intent = IntentUtils.getOpenFileIntent(context, saveFilePath, task.mimeType);
if (IntentUtils.validateIntent(context, intent)) {
Intent intent = IntentUtils.validatedFileIntent(context, saveFilePath, task.mimeType);
if (intent!=null) {
context.startActivity(intent);
result.success(true);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,73 @@
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;

import androidx.core.content.FileProvider;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLConnection;
import java.util.List;

import android.util.Log;
import android.os.Build;

public class IntentUtils {

public static synchronized Intent getOpenFileIntent(Context context, String path, String contentType) {
File file = new File(path);

private static Intent buildIntent(Context context, File file, String mime){
Intent intent = new Intent(Intent.ACTION_VIEW);

if (Build.VERSION.SDK_INT >= 24) {
Uri uri = FileProvider.getUriForFile(
context,
context.getPackageName() + ".flutter_downloader.provider", file);
intent.setDataAndType(uri, contentType);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".flutter_downloader.provider", file);
intent.setDataAndType(uri, mime);
} else {
intent.setDataAndType(Uri.fromFile(file), contentType);
intent.setDataAndType(Uri.fromFile(file), mime);
}

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
return intent;
}

public static synchronized boolean validateIntent(Context context, Intent intent) {
public static synchronized Intent validatedFileIntent(Context context, String path, String contentType) {
File file = new File(path);
Intent intent = buildIntent(context,file,contentType);
if(validateIntent(context, intent))
return intent;
String mime = null;
try {
FileInputStream inputFile = new FileInputStream(path);
mime = URLConnection.guessContentTypeFromStream(inputFile);//does not work in some target sdk version
if(mime==null){
mime = URLConnection.guessContentTypeFromName(path);//works fine

}
} catch (Exception ignored){

}
if(mime!=null) {
intent = buildIntent(context,file,mime);
if(validateIntent(context, intent))
return intent;
}
return null;
}

// public static synchronized Intent getOpenFileIntent(Context context, String path, String contentType) {
// File file = new File(path);
// Intent intent = new Intent(Intent.ACTION_VIEW);
// if (Build.VERSION.SDK_INT >= 24) {
// Uri uri = FileProvider.getUriForFile(
// context,
// context.getPackageName() + ".flutter_downloader.provider", file);
// intent.setDataAndType(uri, contentType);
// } else {
// intent.setDataAndType(Uri.fromFile(file), contentType);
// }

// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// return intent;
// }

private static boolean validateIntent(Context context, Intent intent) {
PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
if (infos.size() > 0) {
Expand Down