|
@ -1,9 +1,11 @@ |
|
|
package awais.instagrabber.utils; |
|
|
package awais.instagrabber.utils; |
|
|
|
|
|
|
|
|
import android.Manifest; |
|
|
import android.Manifest; |
|
|
|
|
|
import android.content.ContentResolver; |
|
|
import android.content.Context; |
|
|
import android.content.Context; |
|
|
import android.content.DialogInterface; |
|
|
import android.content.DialogInterface; |
|
|
import android.os.Environment; |
|
|
import android.net.Uri; |
|
|
|
|
|
import android.provider.DocumentsContract; |
|
|
import android.util.Log; |
|
|
import android.util.Log; |
|
|
import android.webkit.MimeTypeMap; |
|
|
import android.webkit.MimeTypeMap; |
|
|
import android.widget.Toast; |
|
|
import android.widget.Toast; |
|
@ -11,6 +13,8 @@ import android.widget.Toast; |
|
|
import androidx.annotation.NonNull; |
|
|
import androidx.annotation.NonNull; |
|
|
import androidx.annotation.Nullable; |
|
|
import androidx.annotation.Nullable; |
|
|
import androidx.appcompat.app.AlertDialog; |
|
|
import androidx.appcompat.app.AlertDialog; |
|
|
|
|
|
import androidx.core.util.Pair; |
|
|
|
|
|
import androidx.documentfile.provider.DocumentFile; |
|
|
import androidx.work.Constraints; |
|
|
import androidx.work.Constraints; |
|
|
import androidx.work.Data; |
|
|
import androidx.work.Data; |
|
|
import androidx.work.NetworkType; |
|
|
import androidx.work.NetworkType; |
|
@ -21,9 +25,9 @@ import androidx.work.WorkRequest; |
|
|
import com.google.gson.Gson; |
|
|
import com.google.gson.Gson; |
|
|
|
|
|
|
|
|
import java.io.BufferedWriter; |
|
|
import java.io.BufferedWriter; |
|
|
import java.io.File; |
|
|
|
|
|
import java.io.FileWriter; |
|
|
|
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
|
|
|
import java.io.OutputStreamWriter; |
|
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.Collections; |
|
|
import java.util.Collections; |
|
|
import java.util.HashMap; |
|
|
import java.util.HashMap; |
|
|
import java.util.LinkedList; |
|
|
import java.util.LinkedList; |
|
@ -41,31 +45,50 @@ import awais.instagrabber.repositories.responses.User; |
|
|
import awais.instagrabber.repositories.responses.VideoVersion; |
|
|
import awais.instagrabber.repositories.responses.VideoVersion; |
|
|
import awais.instagrabber.workers.DownloadWorker; |
|
|
import awais.instagrabber.workers.DownloadWorker; |
|
|
|
|
|
|
|
|
|
|
|
import static awais.instagrabber.utils.Constants.FOLDER_PATH; |
|
|
|
|
|
|
|
|
public final class DownloadUtils { |
|
|
public final class DownloadUtils { |
|
|
private static final String TAG = DownloadUtils.class.getSimpleName(); |
|
|
private static final String TAG = DownloadUtils.class.getSimpleName(); |
|
|
|
|
|
|
|
|
public static final String WRITE_PERMISSION = Manifest.permission.WRITE_EXTERNAL_STORAGE; |
|
|
public static final String WRITE_PERMISSION = Manifest.permission.WRITE_EXTERNAL_STORAGE; |
|
|
public static final String[] PERMS = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}; |
|
|
public static final String[] PERMS = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}; |
|
|
public static final String DIR_BARINSTA = "Barinsta"; |
|
|
private static final String DIR_BARINSTA = "Barinsta"; |
|
|
public static final String DIR_DOWNLOADS = "Downloads"; |
|
|
private static final String DIR_DOWNLOADS = "Downloads"; |
|
|
public static final String DIR_CAMERA = "Camera"; |
|
|
private static final String DIR_CAMERA = "Camera"; |
|
|
public static final String DIR_EDIT = "Edit"; |
|
|
private static final String DIR_EDIT = "Edit"; |
|
|
|
|
|
private static final String TEMP_DIR = "Temp"; |
|
|
public static File getDownloadDir(final String... dirs) { |
|
|
private static DocumentFile root; |
|
|
final File parent = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS); |
|
|
public static void init(@NonNull final Context context) { |
|
|
File subDir = new File(parent, DIR_BARINSTA); |
|
|
// if (!Utils.settingsHelper.getBoolean(FOLDER_SAVE_TO)) return; |
|
|
|
|
|
final String customPath = Utils.settingsHelper.getString(FOLDER_PATH); |
|
|
|
|
|
if (TextUtils.isEmpty(customPath)) return; |
|
|
|
|
|
// dir = new File(customPath); |
|
|
|
|
|
root = DocumentFile.fromTreeUri(context, Uri.parse(customPath)); |
|
|
|
|
|
Log.d(TAG, "init: " + root); |
|
|
|
|
|
// final File parent = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); |
|
|
|
|
|
// final DocumentFile documentFile = DocumentFile.fromFile(parent); |
|
|
|
|
|
// Log.d(TAG, "init: " + documentFile); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static DocumentFile getDownloadDir(final String... dirs) { |
|
|
|
|
|
// final File parent = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS); |
|
|
|
|
|
// File subDir = new File(parent, DIR_BARINSTA); |
|
|
|
|
|
DocumentFile subDir = root; |
|
|
if (dirs != null) { |
|
|
if (dirs != null) { |
|
|
for (final String dir : dirs) { |
|
|
for (final String dir : dirs) { |
|
|
subDir = new File(subDir, dir); |
|
|
final DocumentFile subDirFile = subDir.findFile(dir); |
|
|
//noinspection ResultOfMethodCallIgnored |
|
|
if (subDirFile == null) { |
|
|
subDir.mkdirs(); |
|
|
subDir = subDir.createDirectory(dir); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return subDir; |
|
|
return subDir; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@NonNull |
|
|
@NonNull |
|
|
public static File getDownloadDir() { |
|
|
public static DocumentFile getDownloadDir() { |
|
|
// final File parent = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS); |
|
|
// final File parent = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS); |
|
|
// final File dir = new File(new File(parent, "barinsta"), "downloads"); |
|
|
// final File dir = new File(new File(parent, "barinsta"), "downloads"); |
|
|
// if (!dir.exists()) { |
|
|
// if (!dir.exists()) { |
|
@ -83,37 +106,63 @@ public final class DownloadUtils { |
|
|
return getDownloadDir(DIR_DOWNLOADS); |
|
|
return getDownloadDir(DIR_DOWNLOADS); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static File getCameraDir() { |
|
|
public static DocumentFile getCameraDir() { |
|
|
return getDownloadDir(DIR_CAMERA); |
|
|
return getDownloadDir(DIR_CAMERA); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static File getImageEditDir(final String sessionId) { |
|
|
public static DocumentFile getImageEditDir(final String sessionId) { |
|
|
return getDownloadDir(DIR_EDIT, sessionId); |
|
|
return getDownloadDir(DIR_EDIT, sessionId); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Nullable |
|
|
// @Nullable |
|
|
private static File getDownloadDir(@NonNull final Context context, @Nullable final String username) { |
|
|
// private static DocumentFile getDownloadDir(@NonNull final Context context, @Nullable final String username) { |
|
|
return getDownloadDir(context, username, false); |
|
|
// return getDownloadDir(context, username, false); |
|
|
} |
|
|
// } |
|
|
|
|
|
|
|
|
@Nullable |
|
|
@Nullable |
|
|
private static File getDownloadDir(final Context context, |
|
|
private static DocumentFile getDownloadDir(final Context context, |
|
|
@Nullable final String username, |
|
|
@Nullable final String username) { |
|
|
final boolean skipCreateDir) { |
|
|
final List<String> userFolderPaths = getSubPathForUserFolder(username); |
|
|
File dir = getDownloadDir(); |
|
|
DocumentFile dir = root; |
|
|
|
|
|
for (final String dirName : userFolderPaths) { |
|
|
if (Utils.settingsHelper.getBoolean(Constants.DOWNLOAD_USER_FOLDER) && !TextUtils.isEmpty(username)) { |
|
|
final DocumentFile file = dir.findFile(dirName); |
|
|
final String finaleUsername = username.startsWith("@") ? username.substring(1) : username; |
|
|
if (file != null) { |
|
|
dir = new File(dir, finaleUsername); |
|
|
dir = file; |
|
|
|
|
|
continue; |
|
|
} |
|
|
} |
|
|
|
|
|
dir = dir.createDirectory(dirName); |
|
|
if (context != null && !skipCreateDir && !dir.exists() && !dir.mkdirs()) { |
|
|
if (dir == null) break; |
|
|
|
|
|
} |
|
|
|
|
|
// final String joined = android.text.TextUtils.join("/", userFolderPaths); |
|
|
|
|
|
// final Uri userFolderUri = DocumentsContract.buildDocumentUriUsingTree(root.getUri(), joined); |
|
|
|
|
|
// final DocumentFile userFolder = DocumentFile.fromSingleUri(context, userFolderUri); |
|
|
|
|
|
if (context != null && (dir == null || !dir.exists())) { |
|
|
Toast.makeText(context, R.string.error_creating_folders, Toast.LENGTH_SHORT).show(); |
|
|
Toast.makeText(context, R.string.error_creating_folders, Toast.LENGTH_SHORT).show(); |
|
|
return null; |
|
|
return null; |
|
|
} |
|
|
} |
|
|
return dir; |
|
|
return dir; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static List<String> getSubPathForUserFolder(final String username) { |
|
|
|
|
|
final List<String> list = new ArrayList<>(); |
|
|
|
|
|
if (!Utils.settingsHelper.getBoolean(Constants.DOWNLOAD_USER_FOLDER) || TextUtils.isEmpty(username)) { |
|
|
|
|
|
list.add(DIR_DOWNLOADS); |
|
|
|
|
|
return list; |
|
|
|
|
|
} |
|
|
|
|
|
final String finalUsername = username.startsWith("@") ? username.substring(1) : username; |
|
|
|
|
|
list.add(DIR_DOWNLOADS); |
|
|
|
|
|
list.add(finalUsername); |
|
|
|
|
|
return list; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static DocumentFile getTempDir() { |
|
|
|
|
|
DocumentFile file = root.findFile(TEMP_DIR); |
|
|
|
|
|
if (file == null) { |
|
|
|
|
|
file = root.createDirectory(TEMP_DIR); |
|
|
|
|
|
} |
|
|
|
|
|
return file; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// public static void dmDownload(@NonNull final Context context, |
|
|
// public static void dmDownload(@NonNull final Context context, |
|
|
// @Nullable final String username, |
|
|
// @Nullable final String username, |
|
|
// final String modelId, |
|
|
// final String modelId, |
|
@ -126,59 +175,71 @@ public final class DownloadUtils { |
|
|
// } |
|
|
// } |
|
|
// } |
|
|
// } |
|
|
|
|
|
|
|
|
private static void dmDownloadImpl(@NonNull final Context context, |
|
|
// private static void dmDownloadImpl(@NonNull final Context context, |
|
|
@Nullable final String username, |
|
|
// @Nullable final String username, |
|
|
final String modelId, |
|
|
// final String modelId, |
|
|
final String url) { |
|
|
// final String url) { |
|
|
final File dir = getDownloadDir(context, username); |
|
|
// final DocumentFile dir = getDownloadDir(context, username); |
|
|
if (dir.exists() || dir.mkdirs()) { |
|
|
// if (dir != null && dir.exists()) { |
|
|
download(context, |
|
|
// download(context, url, getDownloadSavePaths(dir, modelId, url)); |
|
|
url, |
|
|
// return; |
|
|
getDownloadSaveFile(dir, modelId, url).getAbsolutePath()); |
|
|
// } |
|
|
return; |
|
|
// Toast.makeText(context, R.string.error_creating_folders, Toast.LENGTH_SHORT).show(); |
|
|
} |
|
|
// } |
|
|
Toast.makeText(context, R.string.error_creating_folders, Toast.LENGTH_SHORT).show(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@NonNull |
|
|
@NonNull |
|
|
private static File getDownloadSaveFile(final File finalDir, |
|
|
private static Pair<List<String>, String> getDownloadSavePaths(final List<String> paths, |
|
|
final String postId, |
|
|
final String postId, |
|
|
final String displayUrl) { |
|
|
final String displayUrl) { |
|
|
return getDownloadSaveFile(finalDir, postId, "", displayUrl); |
|
|
return getDownloadSavePaths(paths, postId, "", displayUrl); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static File getDownloadChildSaveFile(final File downloadDir, |
|
|
private static Pair<List<String>, String> getDownloadChildSaveFile(final List<String> paths, |
|
|
final String postId, |
|
|
final String postId, |
|
|
final int childPosition, |
|
|
final int childPosition, |
|
|
final String url) { |
|
|
final String url) { |
|
|
final String sliderPostfix = "_slide_" + childPosition; |
|
|
final String sliderPostfix = "_slide_" + childPosition; |
|
|
return getDownloadSaveFile(downloadDir, postId, sliderPostfix, url); |
|
|
return getDownloadSavePaths(paths, postId, sliderPostfix, url); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@NonNull |
|
|
@Nullable |
|
|
private static File getDownloadSaveFile(final File finalDir, |
|
|
private static Pair<List<String>, String> getDownloadSavePaths(final List<String> paths, |
|
|
final String postId, |
|
|
final String postId, |
|
|
final String sliderPostfix, |
|
|
final String sliderPostfix, |
|
|
final String displayUrl) { |
|
|
final String displayUrl) { |
|
|
final String fileName = postId + sliderPostfix + getFileExtensionFromUrl(displayUrl); |
|
|
if (paths == null) return null; |
|
|
return new File(finalDir, fileName); |
|
|
final String extension = getFileExtensionFromUrl(displayUrl); |
|
|
|
|
|
final String fileName = postId + sliderPostfix + extension; |
|
|
|
|
|
// return new File(finalDir, fileName); |
|
|
|
|
|
// DocumentFile file = finalDir.findFile(fileName); |
|
|
|
|
|
// if (file == null) { |
|
|
|
|
|
final String mimeType = Utils.mimeTypeMap.getMimeTypeFromExtension(extension.startsWith(".") ? extension.substring(1) : extension); |
|
|
|
|
|
// file = finalDir.createFile(mimeType, fileName); |
|
|
|
|
|
// } |
|
|
|
|
|
paths.add(fileName); |
|
|
|
|
|
return new Pair<>(paths, mimeType); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@NonNull |
|
|
public static DocumentFile getTempFile() { |
|
|
public static File getTempFile() { |
|
|
|
|
|
return getTempFile(null, null); |
|
|
return getTempFile(null, null); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static File getTempFile(final String fileName, final String extension) { |
|
|
public static DocumentFile getTempFile(final String fileName, final String extension) { |
|
|
final File dir = getDownloadDir(); |
|
|
final DocumentFile dir = getTempDir(); |
|
|
String name = fileName; |
|
|
String name = fileName; |
|
|
if (TextUtils.isEmpty(name)) { |
|
|
if (TextUtils.isEmpty(name)) { |
|
|
name = UUID.randomUUID().toString(); |
|
|
name = UUID.randomUUID().toString(); |
|
|
} |
|
|
} |
|
|
|
|
|
String mimeType = "application/octet-stream"; |
|
|
if (!TextUtils.isEmpty(extension)) { |
|
|
if (!TextUtils.isEmpty(extension)) { |
|
|
name += "." + extension; |
|
|
name += "." + extension; |
|
|
|
|
|
mimeType = Utils.mimeTypeMap.getMimeTypeFromExtension(extension); |
|
|
} |
|
|
} |
|
|
return new File(dir, name); |
|
|
DocumentFile file = dir.findFile(name); |
|
|
|
|
|
if (file == null) { |
|
|
|
|
|
file = dir.createFile(mimeType, name); |
|
|
|
|
|
} |
|
|
|
|
|
return file; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
@ -221,20 +282,21 @@ public final class DownloadUtils { |
|
|
return ""; |
|
|
return ""; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static List<Boolean> checkDownloaded(@NonNull final Media media) { |
|
|
public static List<Boolean> checkDownloaded(@NonNull final Context context, |
|
|
|
|
|
@NonNull final Media media) { |
|
|
final List<Boolean> checkList = new LinkedList<>(); |
|
|
final List<Boolean> checkList = new LinkedList<>(); |
|
|
final User user = media.getUser(); |
|
|
final User user = media.getUser(); |
|
|
String username = "username"; |
|
|
String username = "username"; |
|
|
if (user != null) { |
|
|
if (user != null) { |
|
|
username = user.getUsername(); |
|
|
username = user.getUsername(); |
|
|
} |
|
|
} |
|
|
final File downloadDir = getDownloadDir(null, "@" + username, true); |
|
|
final List<String> userFolderPaths = getSubPathForUserFolder(username); |
|
|
switch (media.getMediaType()) { |
|
|
switch (media.getMediaType()) { |
|
|
case MEDIA_TYPE_IMAGE: |
|
|
case MEDIA_TYPE_IMAGE: |
|
|
case MEDIA_TYPE_VIDEO: { |
|
|
case MEDIA_TYPE_VIDEO: { |
|
|
final String url = ResponseBodyUtils.getImageUrl(media); |
|
|
final String url = ResponseBodyUtils.getImageUrl(media); |
|
|
final File file = getDownloadSaveFile(downloadDir, media.getCode(), url); |
|
|
final Pair<List<String>, String> pair = getDownloadSavePaths(userFolderPaths, media.getCode(), url); |
|
|
checkList.add(file.exists()); |
|
|
checkList.add(checkPathExists(context, pair.first)); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
case MEDIA_TYPE_SLIDER: |
|
|
case MEDIA_TYPE_SLIDER: |
|
@ -243,8 +305,8 @@ public final class DownloadUtils { |
|
|
final Media child = sliderItems.get(i); |
|
|
final Media child = sliderItems.get(i); |
|
|
if (child == null) continue; |
|
|
if (child == null) continue; |
|
|
final String url = ResponseBodyUtils.getImageUrl(child); |
|
|
final String url = ResponseBodyUtils.getImageUrl(child); |
|
|
final File file = getDownloadChildSaveFile(downloadDir, media.getCode(), i + 1, url); |
|
|
final Pair<List<String>, String> pair = getDownloadChildSaveFile(userFolderPaths, media.getCode(), i + 1, url); |
|
|
checkList.add(file.exists()); |
|
|
checkList.add(checkPathExists(context, pair.first)); |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
default: |
|
|
default: |
|
@ -252,6 +314,14 @@ public final class DownloadUtils { |
|
|
return checkList; |
|
|
return checkList; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static boolean checkPathExists(@NonNull final Context context, |
|
|
|
|
|
@NonNull final List<String> paths) { |
|
|
|
|
|
final String joined = android.text.TextUtils.join("/", paths); |
|
|
|
|
|
final Uri userFolderUri = DocumentsContract.buildDocumentUriUsingTree(root.getUri(), joined); |
|
|
|
|
|
final DocumentFile userFolder = DocumentFile.fromSingleUri(context, userFolderUri); |
|
|
|
|
|
return userFolder != null && userFolder.exists(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public static void showDownloadDialog(@NonNull Context context, |
|
|
public static void showDownloadDialog(@NonNull Context context, |
|
|
@NonNull final Media feedModel, |
|
|
@NonNull final Media feedModel, |
|
|
final int childPosition) { |
|
|
final int childPosition) { |
|
@ -286,15 +356,20 @@ public final class DownloadUtils { |
|
|
|
|
|
|
|
|
public static void download(@NonNull final Context context, |
|
|
public static void download(@NonNull final Context context, |
|
|
@NonNull final StoryModel storyModel) { |
|
|
@NonNull final StoryModel storyModel) { |
|
|
final File downloadDir = getDownloadDir(context, "@" + storyModel.getUsername()); |
|
|
final DocumentFile downloadDir = getDownloadDir(context, "@" + storyModel.getUsername()); |
|
|
final String url = storyModel.getItemType() == MediaItemType.MEDIA_TYPE_VIDEO |
|
|
final String url = storyModel.getItemType() == MediaItemType.MEDIA_TYPE_VIDEO |
|
|
? storyModel.getVideoUrl() |
|
|
? storyModel.getVideoUrl() |
|
|
: storyModel.getStoryUrl(); |
|
|
: storyModel.getStoryUrl(); |
|
|
final File saveFile = new File(downloadDir, |
|
|
final String extension = DownloadUtils.getFileExtensionFromUrl(url); |
|
|
storyModel.getStoryMediaId() |
|
|
final String fileName = storyModel.getStoryMediaId() + "_" + storyModel.getTimestamp() + extension; |
|
|
+ "_" + storyModel.getTimestamp() |
|
|
DocumentFile saveFile = downloadDir.findFile(fileName); |
|
|
+ DownloadUtils.getFileExtensionFromUrl(url)); |
|
|
if (saveFile == null) { |
|
|
download(context, url, saveFile.getAbsolutePath()); |
|
|
saveFile = downloadDir.createFile( |
|
|
|
|
|
Utils.mimeTypeMap.getMimeTypeFromExtension(extension.startsWith(".") ? extension.substring(1) : extension), |
|
|
|
|
|
fileName); |
|
|
|
|
|
} |
|
|
|
|
|
// final File saveFile = new File(downloadDir, fileName); |
|
|
|
|
|
download(context, url, saveFile); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static void download(@NonNull final Context context, |
|
|
public static void download(@NonNull final Context context, |
|
@ -316,17 +391,19 @@ public final class DownloadUtils { |
|
|
private static void download(@NonNull final Context context, |
|
|
private static void download(@NonNull final Context context, |
|
|
@NonNull final List<Media> feedModels, |
|
|
@NonNull final List<Media> feedModels, |
|
|
final int childPositionIfSingle) { |
|
|
final int childPositionIfSingle) { |
|
|
final Map<String, String> map = new HashMap<>(); |
|
|
final Map<String, DocumentFile> map = new HashMap<>(); |
|
|
for (final Media media : feedModels) { |
|
|
for (final Media media : feedModels) { |
|
|
final User mediaUser = media.getUser(); |
|
|
final User mediaUser = media.getUser(); |
|
|
final File downloadDir = getDownloadDir(context, mediaUser == null ? "" : "@" + mediaUser.getUsername()); |
|
|
final List<String> userFolderPaths = getSubPathForUserFolder(mediaUser == null ? "" : "@" + mediaUser.getUsername()); |
|
|
if (downloadDir == null) return; |
|
|
// final DocumentFile downloadDir = getDownloadDir(context, mediaUser == null ? "" : "@" + mediaUser.getUsername()); |
|
|
switch (media.getMediaType()) { |
|
|
switch (media.getMediaType()) { |
|
|
case MEDIA_TYPE_IMAGE: |
|
|
case MEDIA_TYPE_IMAGE: |
|
|
case MEDIA_TYPE_VIDEO: { |
|
|
case MEDIA_TYPE_VIDEO: { |
|
|
final String url = getUrlOfType(media); |
|
|
final String url = getUrlOfType(media); |
|
|
final File file = getDownloadSaveFile(downloadDir, media.getCode(), url); |
|
|
final Pair<List<String>, String> pair = getDownloadSavePaths(userFolderPaths, media.getCode(), url); |
|
|
map.put(url, file.getAbsolutePath()); |
|
|
final DocumentFile file = createFile(pair); |
|
|
|
|
|
if (file == null) continue; |
|
|
|
|
|
map.put(url, file); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
case MEDIA_TYPE_VOICE: { |
|
|
case MEDIA_TYPE_VOICE: { |
|
@ -335,28 +412,48 @@ public final class DownloadUtils { |
|
|
if (mediaUser != null) { |
|
|
if (mediaUser != null) { |
|
|
fileName = mediaUser.getUsername() + "_" + fileName; |
|
|
fileName = mediaUser.getUsername() + "_" + fileName; |
|
|
} |
|
|
} |
|
|
final File file = getDownloadSaveFile(downloadDir, fileName, url); |
|
|
final Pair<List<String>, String> pair = getDownloadSavePaths(userFolderPaths, fileName, url); |
|
|
map.put(url, file.getAbsolutePath()); |
|
|
final DocumentFile file = createFile(pair); |
|
|
|
|
|
if (file == null) continue; |
|
|
|
|
|
map.put(url, file); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
case MEDIA_TYPE_SLIDER: |
|
|
case MEDIA_TYPE_SLIDER: |
|
|
final List<Media> sliderItems = media.getCarouselMedia(); |
|
|
final List<Media> sliderItems = media.getCarouselMedia(); |
|
|
for (int i = 0; i < sliderItems.size(); i++) { |
|
|
for (int i = 0; i < sliderItems.size(); i++) { |
|
|
if (childPositionIfSingle >= 0 && feedModels.size() == 1 && i != childPositionIfSingle) { |
|
|
if (childPositionIfSingle >= 0 && feedModels.size() == 1 && i != childPositionIfSingle) continue; |
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
final Media child = sliderItems.get(i); |
|
|
final Media child = sliderItems.get(i); |
|
|
final String url = getUrlOfType(child); |
|
|
final String url = getUrlOfType(child); |
|
|
final File file = getDownloadChildSaveFile(downloadDir, media.getCode(), i + 1, url); |
|
|
final Pair<List<String>, String> pair = getDownloadChildSaveFile(userFolderPaths, media.getCode(), i + 1, url); |
|
|
map.put(url, file.getAbsolutePath()); |
|
|
final DocumentFile file = createFile(pair); |
|
|
|
|
|
if (file == null) continue; |
|
|
|
|
|
map.put(url, file); |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
default: |
|
|
default: |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if (map.isEmpty()) return; |
|
|
download(context, map); |
|
|
download(context, map); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static DocumentFile createFile(@NonNull final Pair<List<String>, String> pair) { |
|
|
|
|
|
if (pair.first == null || pair.second == null) return null; |
|
|
|
|
|
DocumentFile dir = root; |
|
|
|
|
|
final List<String> first = pair.first; |
|
|
|
|
|
for (int i = 0; i < first.size(); i++) { |
|
|
|
|
|
final String name = first.get(i); |
|
|
|
|
|
final DocumentFile file = dir.findFile(name); |
|
|
|
|
|
if (file != null) { |
|
|
|
|
|
dir = file; |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
dir = i == first.size() - 1 ? dir.createFile(pair.second, name) : dir.createDirectory(name); |
|
|
|
|
|
if (dir == null) break; |
|
|
|
|
|
} |
|
|
|
|
|
return dir; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Nullable |
|
|
@Nullable |
|
|
private static String getUrlOfType(@NonNull final Media media) { |
|
|
private static String getUrlOfType(@NonNull final Media media) { |
|
|
switch (media.getMediaType()) { |
|
|
switch (media.getMediaType()) { |
|
@ -388,12 +485,13 @@ public final class DownloadUtils { |
|
|
|
|
|
|
|
|
public static void download(final Context context, |
|
|
public static void download(final Context context, |
|
|
final String url, |
|
|
final String url, |
|
|
final String filePath) { |
|
|
final DocumentFile filePath) { |
|
|
if (context == null || url == null || filePath == null) return; |
|
|
if (context == null || url == null || filePath == null) return; |
|
|
download(context, Collections.singletonMap(url, filePath)); |
|
|
download(context, Collections.singletonMap(url, filePath)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private static void download(final Context context, final Map<String, String> urlFilePathMap) { |
|
|
private static void download(final Context context, final Map<String, DocumentFile> urlFilePathMap) { |
|
|
|
|
|
if (context == null) return; |
|
|
final Constraints constraints = new Constraints.Builder() |
|
|
final Constraints constraints = new Constraints.Builder() |
|
|
.setRequiredNetworkType(NetworkType.CONNECTED) |
|
|
.setRequiredNetworkType(NetworkType.CONNECTED) |
|
|
.build(); |
|
|
.build(); |
|
@ -401,19 +499,25 @@ public final class DownloadUtils { |
|
|
.setUrlToFilePathMap(urlFilePathMap) |
|
|
.setUrlToFilePathMap(urlFilePathMap) |
|
|
.build(); |
|
|
.build(); |
|
|
final String requestJson = new Gson().toJson(request); |
|
|
final String requestJson = new Gson().toJson(request); |
|
|
final File tempFile = getTempFile(); |
|
|
final DocumentFile tempFile = getTempFile(null, "json"); |
|
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) { |
|
|
if (tempFile == null) { |
|
|
|
|
|
Log.e(TAG, "download: temp file is null"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
final Uri uri = tempFile.getUri(); |
|
|
|
|
|
final ContentResolver contentResolver = context.getContentResolver(); |
|
|
|
|
|
if (contentResolver == null) return; |
|
|
|
|
|
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(contentResolver.openOutputStream(uri)))) { |
|
|
writer.write(requestJson); |
|
|
writer.write(requestJson); |
|
|
} catch (IOException e) { |
|
|
} catch (IOException e) { |
|
|
Log.e(TAG, "download: Error writing request to file", e); |
|
|
Log.e(TAG, "download: Error writing request to file", e); |
|
|
//noinspection ResultOfMethodCallIgnored |
|
|
|
|
|
tempFile.delete(); |
|
|
tempFile.delete(); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
final WorkRequest downloadWorkRequest = new OneTimeWorkRequest.Builder(DownloadWorker.class) |
|
|
final WorkRequest downloadWorkRequest = new OneTimeWorkRequest.Builder(DownloadWorker.class) |
|
|
.setInputData( |
|
|
.setInputData( |
|
|
new Data.Builder() |
|
|
new Data.Builder() |
|
|
.putString(DownloadWorker.KEY_DOWNLOAD_REQUEST_JSON, tempFile.getAbsolutePath()) |
|
|
.putString(DownloadWorker.KEY_DOWNLOAD_REQUEST_JSON, tempFile.getUri().toString()) |
|
|
.build() |
|
|
.build() |
|
|
) |
|
|
) |
|
|
.setConstraints(constraints) |
|
|
.setConstraints(constraints) |
|
|
xxxxxxxxxx