Browse Source

improve downloading (creating files)

* creating files is offloaded to task thread so it doesn't hang the ui
* add notification to inform the above happening
renovate/org.robolectric-robolectric-4.x
Austin Huang 3 years ago
parent
commit
0e788c9889
No known key found for this signature in database GPG Key ID: 84C23AA04587A91F
  1. 25
      app/src/main/java/awais/instagrabber/utils/DownloadUtils.kt
  2. 1
      app/src/main/res/values/strings.xml

25
app/src/main/java/awais/instagrabber/utils/DownloadUtils.kt

@ -9,6 +9,8 @@ import android.view.MenuItem
import android.view.View import android.view.View
import androidx.appcompat.view.ContextThemeWrapper import androidx.appcompat.view.ContextThemeWrapper
import androidx.appcompat.widget.PopupMenu import androidx.appcompat.widget.PopupMenu
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.work.* import androidx.work.*
import awais.instagrabber.R import awais.instagrabber.R
@ -16,6 +18,7 @@ import awais.instagrabber.fragments.settings.PreferenceKeys
import awais.instagrabber.models.enums.MediaItemType import awais.instagrabber.models.enums.MediaItemType
import awais.instagrabber.repositories.responses.Media import awais.instagrabber.repositories.responses.Media
import awais.instagrabber.repositories.responses.stories.StoryMedia import awais.instagrabber.repositories.responses.stories.StoryMedia
import awais.instagrabber.utils.AppExecutors.tasksThread
import awais.instagrabber.utils.TextUtils.isEmpty import awais.instagrabber.utils.TextUtils.isEmpty
import awais.instagrabber.workers.DownloadWorker import awais.instagrabber.workers.DownloadWorker
import com.google.gson.Gson import com.google.gson.Gson
@ -24,6 +27,7 @@ import java.io.IOException
import java.io.OutputStreamWriter import java.io.OutputStreamWriter
import java.util.* import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
import kotlin.math.abs
object DownloadUtils { object DownloadUtils {
@ -211,7 +215,6 @@ object DownloadUtils {
return Pair(fileName, mimeType!!) return Pair(fileName, mimeType!!)
} }
// can't convert to checkFiles() due to lack of Context
fun getTempFile(fileName: String?, extension: String): DocumentFile? { fun getTempFile(fileName: String?, extension: String): DocumentFile? {
val dir = tempDir val dir = tempDir
var name = fileName var name = fileName
@ -382,12 +385,28 @@ object DownloadUtils {
download(context, listOf(feedModel), position) download(context, listOf(feedModel), position)
} }
// this must be used for bulk download, but ONLY bulk download
@JvmStatic @JvmStatic
fun download( fun download(
context: Context, context: Context,
feedModels: List<Media> feedModels: List<Media>
) { ) {
val builder = NotificationCompat.Builder(context, Constants.DOWNLOAD_CHANNEL_ID)
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
.setSmallIcon(R.drawable.ic_download)
.setOngoing(true)
.setProgress(1, 0, true)
.setAutoCancel(false)
.setOnlyAlertOnce(true)
.setContentTitle(context.getString(R.string.downloader_preparing))
val notification = builder.build()
val nid = abs(UUID.randomUUID().hashCode())
val nManager = NotificationManagerCompat.from(context.applicationContext)
nManager.notify(nid, notification)
tasksThread.execute {
download(context, feedModels, -1) download(context, feedModels, -1)
nManager.cancel(nid)
}
} }
private fun download( private fun download(
@ -438,9 +457,7 @@ object DownloadUtils {
val url = getUrlOfType(child) val url = getUrlOfType(child)
val usernamePrepend = val usernamePrepend =
if (Utils.settingsHelper.getBoolean(PreferenceKeys.DOWNLOAD_PREPEND_USER_NAME) && mediaUser != null) mediaUser.username else "" if (Utils.settingsHelper.getBoolean(PreferenceKeys.DOWNLOAD_PREPEND_USER_NAME) && mediaUser != null) mediaUser.username else ""
val pair = getDownloadChildSavePaths(
media.code, i + 1, url, usernamePrepend
)
val pair = getDownloadChildSavePaths(media.code, i + 1, url, usernamePrepend)
map[url!!] = pair map[url!!] = pair
i++ i++
} }

1
app/src/main/res/values/strings.xml

@ -205,6 +205,7 @@
<string name="direct_download_desc">Downloads posts directly to the phone!</string> <string name="direct_download_desc">Downloads posts directly to the phone!</string>
<string name="direct_download_loading">Fetching post(s)</string> <string name="direct_download_loading">Fetching post(s)</string>
<string name="downloader_complete">Download completed</string> <string name="downloader_complete">Download completed</string>
<string name="downloader_preparing">Preparing to download…</string>
<string name="downloader_downloading_post">Downloading post…</string> <string name="downloader_downloading_post">Downloading post…</string>
<string name="downloader_downloading_media">Downloading media</string> <string name="downloader_downloading_media">Downloading media</string>
<string name="downloader_downloading_pfp">Downloading profile picture</string> <string name="downloader_downloading_pfp">Downloading profile picture</string>

Loading…
Cancel
Save