also fix wrong mime-types for downloaded files
This commit is contained in:
aliveoutside
2022-09-06 13:39:39 +03:00
parent 99adb95816
commit 374b221f0d
@@ -10,6 +10,7 @@ import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import com.facebook.common.i.MediaUtils;
import com.facebook.x.g.EncodedImage;
@@ -20,6 +21,8 @@ import com.vk.metrics.eventtracking.VkTracker;
import com.vtosters.lite.R;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.HashSet;
@@ -47,52 +50,54 @@ public class DownloadUtils {
}
}
private static void b(final Context context, String str, String str2, boolean z) {
if (TextUtils.isEmpty(str2)) {
private static void b(final Context context, String str, String uri, boolean z) throws IOException {
if (TextUtils.isEmpty(uri)) {
return;
}
String file = a(str, str2);
File b2;
boolean pic = false;
boolean c2 = MediaUtils.c(MediaUtils.b(str2));
String fileName = a(str, uri);
File file;
if (file.endsWith(".jpg") || file.endsWith(".png") || file.endsWith(".jpeg")) {
b2 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath(), file);
pic = true;
} else if (file.endsWith(".mp3") || file.endsWith(".wav") || file.endsWith(".ogg") || file.endsWith(".aac") || file.endsWith(".flac") || file.endsWith(".m4a")) {
b2 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath(), file);
} else if (file.endsWith(".mp4") || file.endsWith(".mkv") || file.endsWith(".avi") || file.endsWith(".flv") || file.endsWith(".wmv") || file.endsWith(".mov") || file.endsWith(".3gp")) {
b2 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath(), file);
Log.v("DownloadUtils", "download: filename = " + fileName + " | str = " + str + "| str2 = " + uri);
if (fileName.endsWith(".jpg") || fileName.endsWith(".png") || fileName.endsWith(".jpeg")) {
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath(), fileName);
} else if (fileName.endsWith(".mp3") || fileName.endsWith(".wav") || fileName.endsWith(".ogg") || fileName.endsWith(".aac") || fileName.endsWith(".flac") || fileName.endsWith(".m4a")) {
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath(), fileName);
} else if (fileName.endsWith(".mp4") || fileName.endsWith(".mkv") || fileName.endsWith(".avi") || fileName.endsWith(".flv") || fileName.endsWith(".wmv") || fileName.endsWith(".mov") || fileName.endsWith(".3gp")) {
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath(), fileName);
} else {
b2 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), file);
file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), fileName);
}
if (c2) {
EncodedImage c3 = VKImageLoader.c(Uri.parse(str2));
final InputStream h = c3 == null ? null : c3.h();
boolean isImage = MediaUtils.c(FileUtils.f(file));
if (isImage) {
EncodedImage encodedImage = VKImageLoader.c(Uri.parse(uri));
final InputStream h = encodedImage == null ? null : encodedImage.h();
if (h != null) {
try {
if (b2.getParentFile() != null) {
b2.createNewFile();
if (file.getParentFile() != null) {
file.createNewFile();
}
if (b2.exists()) {
VkExecutors.x.a().execute(() -> ToastUtils.a(context.getString(R.string.file_saved, b2.getAbsoluteFile())));
if (file.exists()) {
FileUtils.b.a(h, new FileOutputStream(file), h.available(), null);
VkExecutors.x.a().execute(() -> ToastUtils.a(context.getString(R.string.file_saved, file.getAbsoluteFile())));
}
} catch (Exception e2) {
L.a(e2);
}
CameraUtils.a(context, b2, null);
CameraUtils.a(context, file, null);
return;
}
}
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(str2));
request.setDestinationUri(Uri.fromFile(b2));
request.setMimeType(FileUtils.f(new File(str)));
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(uri));
var mimeType = FileUtils.f(file);
request.setDestinationUri(Uri.fromFile(file));
request.setMimeType(mimeType);
request.setNotificationVisibility(1);
request.allowScanningByMediaScanner();
request.setTitle(str);
@@ -102,9 +107,6 @@ public class DownloadUtils {
}
try {
long enqueue = a2.enqueue(request);
if (pic) {
CameraUtils.a(context, b2, null);
}
if (!z) {
return;
}
@@ -184,10 +186,12 @@ public class DownloadUtils {
}
Intent intent = new Intent("android.intent.action.VIEW");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
String f2 = FileUtils.f(file);
if (!TextUtils.isEmpty(f2)) {
var mimeType = FileUtils.f(file);
var uri = FileUtils.uriFromFile(file);
Log.d("DownloadUtils", "openFile: " + file.getAbsolutePath() + "| mimetype " + mimeType + "| uri " + uri);
if (!TextUtils.isEmpty(mimeType)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(FileUtils.uriFromFile(file), f2);
intent.setDataAndType(uri, mimeType);
context.startActivity(intent);
return true;
}
@@ -208,9 +212,13 @@ public class DownloadUtils {
private static boolean a(Context context, File file) {
if (file != null && file.exists()) {
var mimeType = FileUtils.f(file);
var uri = FileUtils.uriFromFile(file);
Intent intent = new Intent("android.intent.action.VIEW");
intent.setDataAndType(Uri.parse(file.getAbsolutePath()), "application/*");
intent.setDataAndType(uri, mimeType);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d("DownloadUtils", "openFile: " + file.getAbsolutePath() + "| mimetype " + mimeType + "| uri " + uri);
try {
context.startActivity(intent);
return true;