X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyBrowserAndroid.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacybrowser%2Fcoroutines%2FSaveUrlCoroutine.kt;h=f6b7a83235d8f181f9f3c0ec18848d553e957c4f;hp=c6fd36a284478ff7e6bdfcd38e0267829572257e;hb=2f0d40bbfc1aca5d88e711422cec58d912b66884;hpb=5186b668274b09e37b371c0a134e53255c98ad98 diff --git a/app/src/main/java/com/stoutner/privacybrowser/coroutines/SaveUrlCoroutine.kt b/app/src/main/java/com/stoutner/privacybrowser/coroutines/SaveUrlCoroutine.kt index c6fd36a2..f6b7a832 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/coroutines/SaveUrlCoroutine.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/coroutines/SaveUrlCoroutine.kt @@ -27,11 +27,12 @@ import android.provider.OpenableColumns import android.util.Base64 import android.webkit.CookieManager +import androidx.viewpager2.widget.ViewPager2 + import com.google.android.material.snackbar.Snackbar import com.stoutner.privacybrowser.R import com.stoutner.privacybrowser.helpers.ProxyHelper -import com.stoutner.privacybrowser.views.NoSwipeViewPager import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -46,6 +47,9 @@ import java.text.NumberFormat class SaveUrlCoroutine { fun save(context: Context, activity: Activity, urlString: String, fileUri: Uri, userAgent: String, cookiesEnabled: Boolean) { + // Create a canceled boolean. + var canceled = false + // Use a coroutine to save the URL. CoroutineScope(Dispatchers.Main).launch { // Create a file name string. @@ -70,10 +74,11 @@ class SaveUrlCoroutine { } // Get a handle for the no swipe view pager. - val noSwipeViewPager = activity.findViewById(R.id.webviewpager) + val webViewViewPager2 = activity.findViewById(R.id.webview_viewpager2) // Create a saving file snackbar. - val savingFileSnackbar = Snackbar.make(noSwipeViewPager, activity.getString(R.string.saving_file, 0, fileNameString), Snackbar.LENGTH_INDEFINITE) + val savingFileSnackbar = Snackbar.make(webViewViewPager2, activity.getString(R.string.saving_file, 0, fileNameString), Snackbar.LENGTH_INDEFINITE) + .setAction(R.string.cancel) { canceled = true } // Display the saving file snackbar. savingFileSnackbar.show() @@ -159,7 +164,7 @@ class SaveUrlCoroutine { var bufferLength: Int // Attempt to read data from the input stream and store it in the output stream. Also store the amount of data read in the buffer length variable. - while (inputStream.read(conversionBufferByteArray).also { bufferLength = it } > 0) { // Proceed while the amount of data stored in the buffer in > 0. + while ((inputStream.read(conversionBufferByteArray).also { bufferLength = it } > 0) && !canceled) { // Proceed while the amount of data stored in the buffer in > 0. // Write the contents of the conversion buffer to the file output stream. outputStream.write(conversionBufferByteArray, 0, bufferLength) @@ -203,8 +208,13 @@ class SaveUrlCoroutine { // Dismiss the saving file snackbar. savingFileSnackbar.dismiss() - // Display the file saved snackbar. - Snackbar.make(noSwipeViewPager, activity.getString(R.string.saved, fileNameString), Snackbar.LENGTH_LONG).show() + // Display a final disposition snackbar. + if (canceled) + // Display the download cancelled snackbar. + Snackbar.make(webViewViewPager2, activity.getString(R.string.download_cancelled), Snackbar.LENGTH_SHORT).show() + else + // Display the file saved snackbar. + Snackbar.make(webViewViewPager2, activity.getString(R.string.saved, fileNameString), Snackbar.LENGTH_LONG).show() } } catch (exception: Exception) { // Update the UI. @@ -213,7 +223,7 @@ class SaveUrlCoroutine { savingFileSnackbar.dismiss() // Display the file saving error. - Snackbar.make(noSwipeViewPager, activity.getString(R.string.error_saving_file, fileNameString, exception), Snackbar.LENGTH_INDEFINITE).show() + Snackbar.make(webViewViewPager2, activity.getString(R.string.error_saving_file, fileNameString, exception), Snackbar.LENGTH_INDEFINITE).show() } } }