From: Soren Stoutner Date: Fri, 4 Nov 2022 00:13:41 +0000 (-0700) Subject: Use a coroutine to compress the bitmap in MoveToFolderDialog. https://redmine.stoutne... X-Git-Tag: v3.12~8 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=2b1677054aa2e63e86584ddcd3b3adff3cc5444f;p=PrivacyBrowserAndroid.git Use a coroutine to compress the bitmap in MoveToFolderDialog. https://redmine.stoutner.com/issues/766 --- diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java index fd190442..a21848a5 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -190,9 +190,9 @@ public class MainWebViewActivity extends AppCompatActivity implements CreateBook WebViewTabFragment.NewTabListener { // Define the public static variables. - public static ExecutorService executorService = Executors.newFixedThreadPool(4); + public static final ExecutorService executorService = Executors.newFixedThreadPool(4); public static String orbotStatus = "unknown"; - public static ArrayList pendingDialogsArrayList = new ArrayList<>(); + public static final ArrayList pendingDialogsArrayList = new ArrayList<>(); public static String proxyMode = ProxyHelper.NONE; // Declare the public static variables. diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/ViewSourceActivity.kt b/app/src/main/java/com/stoutner/privacybrowser/activities/ViewSourceActivity.kt index 691ff17f..3769a682 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/ViewSourceActivity.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/ViewSourceActivity.kt @@ -1,5 +1,5 @@ /* - * Copyright © 2017-2022 Soren Stoutner . + * Copyright 2017-2022 Soren Stoutner . * * This file is part of Privacy Browser Android . * @@ -274,10 +274,10 @@ class ViewSourceActivity: AppCompatActivity(), UntrustedSslCertificateListener { val webViewSourceFactory: ViewModelProvider.Factory = WebViewSourceFactory(currentUrl, userAgent, localeString, proxy, contentResolver, MainWebViewActivity.executorService) // Instantiate the WebView source view model class. - webViewSource = ViewModelProvider(this, webViewSourceFactory).get(WebViewSource::class.java) + webViewSource = ViewModelProvider(this, webViewSourceFactory)[WebViewSource::class.java] // Create a source observer. - webViewSource.observeSource().observe(this, { sourceStringArray: Array -> + webViewSource.observeSource().observe(this) { sourceStringArray: Array -> // Populate the text views. This can take a long time, and freezes the user interface, if the response body is particularly large. requestHeadersTextView.text = sourceStringArray[0] responseMessageTextView.text = sourceStringArray[1] @@ -290,10 +290,10 @@ class ViewSourceActivity: AppCompatActivity(), UntrustedSslCertificateListener { //Stop the swipe to refresh indicator if it is running swipeRefreshLayout.isRefreshing = false - }) + } // Create an error observer. - webViewSource.observeErrors().observe(this, { errorString: String -> + webViewSource.observeErrors().observe(this) { errorString: String -> // Display an error snackbar if the string is not `""`. if (errorString != "") { if (errorString.startsWith("javax.net.ssl.SSLHandshakeException")) { @@ -307,7 +307,7 @@ class ViewSourceActivity: AppCompatActivity(), UntrustedSslCertificateListener { Snackbar.make(swipeRefreshLayout, errorString, Snackbar.LENGTH_LONG).show() } } - }) + } // Implement swipe to refresh. swipeRefreshLayout.setOnRefreshListener { @@ -472,4 +472,4 @@ class ViewSourceActivity: AppCompatActivity(), UntrustedSslCertificateListener { responseBodyTitleTextView.setText(R.string.response_body) } } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/stoutner/privacybrowser/backgroundtasks/GetSourceBackgroundTask.java b/app/src/main/java/com/stoutner/privacybrowser/backgroundtasks/GetSourceBackgroundTask.java index d9da8bcc..a50f3dfc 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/backgroundtasks/GetSourceBackgroundTask.java +++ b/app/src/main/java/com/stoutner/privacybrowser/backgroundtasks/GetSourceBackgroundTask.java @@ -24,7 +24,6 @@ import android.content.ContentResolver; import android.database.Cursor; import android.graphics.Typeface; import android.net.Uri; -import android.os.Build; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.style.StyleSpan; @@ -233,8 +232,9 @@ public class GetSourceBackgroundTask { return true; }; - // Create a new trust manager. - TrustManager[] trustManager = new TrustManager[] { + // Create a new trust manager. Lint wants to warn us that it is hard to securely implement an X509 trust manager. + // But the point of this trust manager is that it should accept all certificates no matter what, so that isn't an issue in our case. + @SuppressLint("CustomX509TrustManager") TrustManager[] trustManager = new TrustManager[] { new X509TrustManager() { @SuppressLint("TrustAllX509TrustManager") @Override diff --git a/app/src/main/java/com/stoutner/privacybrowser/dialogs/MoveToFolderDialog.kt b/app/src/main/java/com/stoutner/privacybrowser/dialogs/MoveToFolderDialog.kt index 101e6b32..0b8891f5 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/dialogs/MoveToFolderDialog.kt +++ b/app/src/main/java/com/stoutner/privacybrowser/dialogs/MoveToFolderDialog.kt @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2022 Soren Stoutner . + * Copyright 2016-2022 Soren Stoutner . * * This file is part of Privacy Browser Android . * @@ -48,6 +48,11 @@ import androidx.preference.PreferenceManager import com.stoutner.privacybrowser.R import com.stoutner.privacybrowser.helpers.BookmarksDatabaseHelper +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext + import java.io.ByteArrayOutputStream import java.lang.StringBuilder @@ -188,8 +193,13 @@ class MoveToFolderDialog : DialogFragment() { // Create a home folder icon byte array output stream. val homeFolderIconByteArrayOutputStream = ByteArrayOutputStream() - // Convert the home folder bitmap to a byte array. `0` is for lossless compression (the only option for a PNG). - homeFolderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, homeFolderIconByteArrayOutputStream) + // Compress the bitmap using a coroutine with Dispatchers.Default. + CoroutineScope(Dispatchers.Main).launch { + withContext(Dispatchers.Default) { + // Convert the home folder bitmap to a byte array. `0` is for lossless compression (the only option for a PNG). + homeFolderIconBitmap.compress(Bitmap.CompressFormat.PNG, 0, homeFolderIconByteArrayOutputStream) + } + } // Convert the home folder icon byte array output stream to a byte array. val homeFolderIconByteArray = homeFolderIconByteArrayOutputStream.toByteArray() @@ -311,4 +321,4 @@ class MoveToFolderDialog : DialogFragment() { } } } -} \ No newline at end of file +}