From 9ca39ff78b248edcb97baa0ec90c20166f8313a0 Mon Sep 17 00:00:00 2001 From: Artem Umerov Date: Tue, 10 Jan 2023 17:39:48 +0300 Subject: [PATCH] 6 6 --- app_fenrir/src/main/AndroidManifest.xml | 3 +- .../fenrir/crypt/KeyExchangeService.kt | 1 + .../friends/requests/IRequestsView.kt | 1 + .../friends/requests/RequestsFragment.kt | 14 ++- .../friends/requests/RequestsPresenter.kt | 3 +- .../fenrir/picasso/transforms/ImageHelper.kt | 74 +++++++++------ .../transforms/MonochromeTransformation.kt | 42 +++++---- .../stroke/ImageWithStrokeHelper.kt | 94 +++++++++++-------- .../ragnarok/fenrir/service/ErrorLocalizer.kt | 3 +- .../fragment_abs_friends_with_toolbar.xml | 15 +++ .../src/main/res/layout/fragment_requests.xml | 59 ++++++++++++ app_filegallery/src/main/AndroidManifest.xml | 3 +- .../picasso/transforms/ImageHelper.kt | 76 +++++++++------ .../transforms/MonochromeTransformation.kt | 42 +++++---- .../filegallery/util/ErrorLocalizer.kt | 3 +- 15 files changed, 291 insertions(+), 142 deletions(-) create mode 100644 app_fenrir/src/main/res/layout/fragment_requests.xml diff --git a/app_fenrir/src/main/AndroidManifest.xml b/app_fenrir/src/main/AndroidManifest.xml index 8eeb41598..5758d5a7a 100644 --- a/app_fenrir/src/main/AndroidManifest.xml +++ b/app_fenrir/src/main/AndroidManifest.xml @@ -56,6 +56,7 @@ android:allowAudioPlaybackCapture="true" android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" + android:enableOnBackInvokedCallback="true" android:fullBackupContent="true" android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" @@ -66,7 +67,7 @@ android:supportsRtl="false" android:theme="@style/App.DayNight" android:usesCleartextTraffic="true" - tools:targetApi="s"> + tools:targetApi="tiramisu"> , accountId: Int, ownerId: Int) + fun updateCount(count: Int) } \ No newline at end of file diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/fragment/friends/requests/RequestsFragment.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/fragment/friends/requests/RequestsFragment.kt index 23afa11cd..b67e225bd 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/fragment/friends/requests/RequestsFragment.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/fragment/friends/requests/RequestsFragment.kt @@ -10,6 +10,7 @@ import android.view.inputmethod.InputMethodManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import androidx.swiperefreshlayout.widget.SwipeRefreshLayout +import com.google.android.material.textview.MaterialTextView import dev.ragnarok.fenrir.Constants import dev.ragnarok.fenrir.Extra import dev.ragnarok.fenrir.R @@ -31,14 +32,16 @@ class RequestsFragment : BaseMvpFragment(), FriendsRecycleAdapter.Listener, IRequestsView { private var mAdapter: FriendsRecycleAdapter? = null private var mSwipeRefreshLayout: SwipeRefreshLayout? = null + private var mCount: MaterialTextView? = null override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { requireActivity().window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) - val root = inflater.inflate(R.layout.fragment_friends, container, false) + val root = inflater.inflate(R.layout.fragment_requests, container, false) val mRecyclerView: RecyclerView = root.findViewById(R.id.list) + mCount = root.findViewById(R.id.count_data) mSwipeRefreshLayout = root.findViewById(R.id.refresh) mSwipeRefreshLayout?.setOnRefreshListener { presenter?.fireRefresh() @@ -79,6 +82,15 @@ class RequestsFragment : BaseMvpFragment(), return root } + override fun updateCount(count: Int) { + if (count <= 0) { + mCount?.visibility = View.GONE + } else { + mCount?.visibility = View.VISIBLE + } + mCount?.text = getString(R.string.people_count, count) + } + override fun getPresenterFactory(saveInstanceState: Bundle?): IPresenterFactory { return object : IPresenterFactory { override fun create(): RequestsPresenter { diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/fragment/friends/requests/RequestsPresenter.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/fragment/friends/requests/RequestsPresenter.kt index 8bdf3ecab..5055583e8 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/fragment/friends/requests/RequestsPresenter.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/fragment/friends/requests/RequestsPresenter.kt @@ -69,6 +69,7 @@ class RequestsPresenter(accountId: Int, private val userId: Int, savedInstanceSt override fun onGuiCreated(viewHost: IRequestsView) { super.onGuiCreated(viewHost) viewHost.displayData(data, isSearchNow) + viewHost.updateCount(allData.size) resolveSwipeRefreshAvailability() } @@ -129,6 +130,7 @@ class RequestsPresenter(accountId: Int, private val userId: Int, savedInstanceSt } offset += if (isNotFriendShow) 1000 else 200 resolveRefreshingView() + view?.updateCount(allData.size) } private fun loadAllCachedData() { @@ -335,7 +337,6 @@ class RequestsPresenter(accountId: Int, private val userId: Int, savedInstanceSt private const val SEARCH_CACHE = 1 private const val SEARCH_WEB = 2 private const val WEB_SEARCH_DELAY = 1000 - private const val WEB_SEARCH_COUNT_PER_LOAD = 100 internal fun allow(user: User, preparedQ: String): Boolean { val full = user.fullName.lowercase(Locale.getDefault()) return full.contains(preparedQ) diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/picasso/transforms/ImageHelper.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/picasso/transforms/ImageHelper.kt index 8c8ca8c24..cd9def42e 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/picasso/transforms/ImageHelper.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/picasso/transforms/ImageHelper.kt @@ -6,55 +6,69 @@ import android.os.Build object ImageHelper { fun getRoundedBitmap(workBitmap: Bitmap?): Bitmap? { workBitmap ?: return null - var bitmap = workBitmap - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && bitmap.config == Bitmap.Config.HARDWARE) { - val tmpBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true) - bitmap.recycle() - bitmap = tmpBitmap - if (bitmap == null) { - return null - } + val bitmapWidth = workBitmap.width + val bitmapHeight = workBitmap.height + val isHardware = + Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && workBitmap.config == Bitmap.Config.HARDWARE + + var output: Bitmap? = null + val canvas: Canvas + var obj: Picture? = null + if (isHardware) { + obj = Picture() + canvas = obj.beginRecording(bitmapWidth, bitmapHeight) + } else { + output = Bitmap.createBitmap(bitmapWidth, bitmapHeight, workBitmap.config) + canvas = Canvas(output) } - val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888) - val canvas = Canvas(output) canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR) val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG) - paint.shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) - canvas.drawOval(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat(), paint) - if (bitmap != output) { - bitmap.recycle() + paint.shader = BitmapShader(workBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) + canvas.drawOval(0f, 0f, bitmapWidth.toFloat(), bitmapHeight.toFloat(), paint) + workBitmap.recycle() + if (isHardware && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + obj?.endRecording() + output = + obj?.let { Bitmap.createBitmap(it, it.width, it.height, Bitmap.Config.HARDWARE) } } return output } fun getEllipseBitmap(workBitmap: Bitmap?, angle: Float): Bitmap? { workBitmap ?: return null - var bitmap = workBitmap - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && bitmap.config == Bitmap.Config.HARDWARE) { - val tmpBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true) - bitmap.recycle() - bitmap = tmpBitmap - if (bitmap == null) { - return null - } + val bitmapWidth = workBitmap.width + val bitmapHeight = workBitmap.height + val isHardware = + Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && workBitmap.config == Bitmap.Config.HARDWARE + + var output: Bitmap? = null + val canvas: Canvas + var obj: Picture? = null + if (isHardware) { + obj = Picture() + canvas = obj.beginRecording(bitmapWidth, bitmapHeight) + } else { + output = Bitmap.createBitmap(bitmapWidth, bitmapHeight, workBitmap.config) + canvas = Canvas(output) } - val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888) - val canvas = Canvas(output) canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR) val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG) - paint.shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) - val pth = (bitmap.width + bitmap.height).toFloat() / 2 + paint.shader = BitmapShader(workBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) + val pth = (bitmapWidth + bitmapHeight).toFloat() / 2 canvas.drawRoundRect( 0f, 0f, - bitmap.width.toFloat(), - bitmap.height.toFloat(), + bitmapWidth.toFloat(), + bitmapHeight.toFloat(), pth * angle, pth * angle, paint ) - if (bitmap != output) { - bitmap.recycle() + workBitmap.recycle() + if (isHardware && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + obj?.endRecording() + output = + obj?.let { Bitmap.createBitmap(it, it.width, it.height, Bitmap.Config.HARDWARE) } } return output } diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/picasso/transforms/MonochromeTransformation.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/picasso/transforms/MonochromeTransformation.kt index abb9257a0..b243a7b4c 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/picasso/transforms/MonochromeTransformation.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/picasso/transforms/MonochromeTransformation.kt @@ -11,29 +11,37 @@ class MonochromeTransformation : Transformation { } fun transform(source: Bitmap): Bitmap? { - var bitmap = source - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && bitmap.config == Bitmap.Config.HARDWARE) { - val tmpBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true) - bitmap.recycle() - bitmap = tmpBitmap - if (bitmap == null) { - return null - } + val bitmapWidth = source.width + val bitmapHeight = source.height + val isHardware = + Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && source.config == Bitmap.Config.HARDWARE + + var output: Bitmap? = null + val canvas: Canvas + var obj: Picture? = null + if (isHardware) { + obj = Picture() + canvas = obj.beginRecording(bitmapWidth, bitmapHeight) + } else { + output = Bitmap.createBitmap(bitmapWidth, bitmapHeight, source.config) + canvas = Canvas(output) } val matrix = ColorMatrix() matrix.setSaturation(0f) - val bitmapCopy = Bitmap.createBitmap( - bitmap.width, - bitmap.height, Bitmap.Config.ARGB_8888 - ) - val canvas = Canvas(bitmapCopy) - val paint = Paint() + val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG) val colorFilter = ColorMatrixColorFilter(matrix) paint.colorFilter = colorFilter - canvas.drawBitmap(bitmap, 0f, 0f, paint) - bitmap.recycle() - return bitmapCopy + paint.shader = BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) + + canvas.drawRect(0f, 0f, bitmapWidth.toFloat(), bitmapHeight.toFloat(), paint) + source.recycle() + if (isHardware && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + obj?.endRecording() + output = + obj?.let { Bitmap.createBitmap(it, it.width, it.height, Bitmap.Config.HARDWARE) } + } + return output } override fun localTransform(source: Bitmap?): Bitmap? { diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/picasso/transforms/stroke/ImageWithStrokeHelper.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/picasso/transforms/stroke/ImageWithStrokeHelper.kt index cc6fd83f0..1a38d1597 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/picasso/transforms/stroke/ImageWithStrokeHelper.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/picasso/transforms/stroke/ImageWithStrokeHelper.kt @@ -10,24 +10,28 @@ object ImageWithStrokeHelper { workBitmap: Bitmap? ): Bitmap? { workBitmap ?: return null - var bitmap = workBitmap - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && bitmap.config == Bitmap.Config.HARDWARE) { - val tmpBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true) - bitmap.recycle() - bitmap = tmpBitmap - if (bitmap == null) { - return null - } + val bitmapWidth = workBitmap.width + val bitmapHeight = workBitmap.height + val isHardware = + Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && workBitmap.config == Bitmap.Config.HARDWARE + + var output: Bitmap? = null + val canvas: Canvas + var obj: Picture? = null + if (isHardware) { + obj = Picture() + canvas = obj.beginRecording(bitmapWidth, bitmapHeight) + } else { + output = Bitmap.createBitmap(bitmapWidth, bitmapHeight, workBitmap.config) + canvas = Canvas(output) } - val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888) - val canvas = Canvas(output) canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR) val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG) - paint.shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) - canvas.drawOval(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat(), paint) + paint.shader = BitmapShader(workBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) + canvas.drawOval(0f, 0f, bitmapWidth.toFloat(), bitmapHeight.toFloat(), paint) paint.style = Paint.Style.STROKE - val pth = (bitmap.width + bitmap.height).toFloat() / 2 + val pth = (bitmapWidth + bitmapHeight).toFloat() / 2 var rdd = 0.066f * pth paint.strokeWidth = rdd paint.shader = null @@ -37,8 +41,8 @@ object ImageWithStrokeHelper { canvas.drawOval( rdd / 2, rdd / 2, - (bitmap.width - rdd / 2), - (bitmap.height - rdd / 2), + (bitmapWidth - rdd / 2), + (bitmapHeight - rdd / 2), paint ) @@ -50,13 +54,15 @@ object ImageWithStrokeHelper { canvas.drawOval( rdd / 2, rdd / 2, - (bitmap.width - rdd / 2), - (bitmap.height - rdd / 2), + (bitmapWidth - rdd / 2), + (bitmapHeight - rdd / 2), paint ) - - if (bitmap != output) { - bitmap.recycle() + workBitmap.recycle() + if (isHardware && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + obj?.endRecording() + output = + obj?.let { Bitmap.createBitmap(it, it.width, it.height, Bitmap.Config.HARDWARE) } } return output } @@ -67,26 +73,30 @@ object ImageWithStrokeHelper { angle: Float ): Bitmap? { workBitmap ?: return null - var bitmap = workBitmap - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && bitmap.config == Bitmap.Config.HARDWARE) { - val tmpBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true) - bitmap.recycle() - bitmap = tmpBitmap - if (bitmap == null) { - return null - } + val bitmapWidth = workBitmap.width + val bitmapHeight = workBitmap.height + val isHardware = + Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && workBitmap.config == Bitmap.Config.HARDWARE + + var output: Bitmap? = null + val canvas: Canvas + var obj: Picture? = null + if (isHardware) { + obj = Picture() + canvas = obj.beginRecording(bitmapWidth, bitmapHeight) + } else { + output = Bitmap.createBitmap(bitmapWidth, bitmapHeight, workBitmap.config) + canvas = Canvas(output) } - val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888) - val canvas = Canvas(output) canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR) val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG) - paint.shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) - val pth = (bitmap.width + bitmap.height).toFloat() / 2 + paint.shader = BitmapShader(workBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) + val pth = (bitmapWidth + bitmapHeight).toFloat() / 2 canvas.drawRoundRect( 0f, 0f, - bitmap.width.toFloat(), - bitmap.height.toFloat(), + bitmapWidth.toFloat(), + bitmapHeight.toFloat(), pth * angle, pth * angle, paint @@ -101,8 +111,8 @@ object ImageWithStrokeHelper { canvas.drawRoundRect( rdd / 2, rdd / 2, - (bitmap.width - rdd / 2), - (bitmap.height - rdd / 2), + (bitmapWidth - rdd / 2), + (bitmapHeight - rdd / 2), pth * angle, pth * angle, paint @@ -116,15 +126,17 @@ object ImageWithStrokeHelper { canvas.drawRoundRect( rdd / 2, rdd / 2, - (bitmap.width - rdd / 2), - (bitmap.height - rdd / 2), + (bitmapWidth - rdd / 2), + (bitmapHeight - rdd / 2), pth * angle, pth * angle, paint ) - - if (bitmap != output) { - bitmap.recycle() + workBitmap.recycle() + if (isHardware && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + obj?.endRecording() + output = + obj?.let { Bitmap.createBitmap(it, it.width, it.height, Bitmap.Config.HARDWARE) } } return output } diff --git a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/service/ErrorLocalizer.kt b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/service/ErrorLocalizer.kt index 116d5eee3..732c6dc97 100644 --- a/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/service/ErrorLocalizer.kt +++ b/app_fenrir/src/main/kotlin/dev/ragnarok/fenrir/service/ErrorLocalizer.kt @@ -6,6 +6,7 @@ import dev.ragnarok.fenrir.api.ApiException import dev.ragnarok.fenrir.api.rest.HttpException import dev.ragnarok.fenrir.exception.NotFoundException import dev.ragnarok.fenrir.nonNullNoEmpty +import java.net.ConnectException import java.net.SocketTimeoutException import java.net.UnknownHostException @@ -22,7 +23,7 @@ object ErrorLocalizer { is SocketTimeoutException -> { context.getString(R.string.error_timeout_message) } - is UnknownHostException -> { + is ConnectException, is UnknownHostException -> { context.getString(R.string.error_unknown_host) } is NotFoundException -> { diff --git a/app_fenrir/src/main/res/layout/fragment_abs_friends_with_toolbar.xml b/app_fenrir/src/main/res/layout/fragment_abs_friends_with_toolbar.xml index 7ddb1d4f9..e90b1b822 100644 --- a/app_fenrir/src/main/res/layout/fragment_abs_friends_with_toolbar.xml +++ b/app_fenrir/src/main/res/layout/fragment_abs_friends_with_toolbar.xml @@ -1,11 +1,26 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/app_filegallery/src/main/AndroidManifest.xml b/app_filegallery/src/main/AndroidManifest.xml index ce3be6a71..cebb48dc9 100644 --- a/app_filegallery/src/main/AndroidManifest.xml +++ b/app_filegallery/src/main/AndroidManifest.xml @@ -38,6 +38,7 @@ android:allowAudioPlaybackCapture="true" android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" + android:enableOnBackInvokedCallback="true" android:fullBackupContent="true" android:hardwareAccelerated="true" android:icon="@mipmap/ic_launcher" @@ -48,7 +49,7 @@ android:supportsRtl="false" android:theme="@style/App.DayNight" android:usesCleartextTraffic="true" - tools:targetApi="s"> + tools:targetApi="tiramisu"> = Build.VERSION_CODES.P && bitmap.config == Bitmap.Config.HARDWARE) { - val tmpBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true) - bitmap.recycle() - bitmap = tmpBitmap - if (bitmap == null) { - return null - } + val bitmapWidth = workBitmap.width + val bitmapHeight = workBitmap.height + val isHardware = + Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && workBitmap.config == Bitmap.Config.HARDWARE + + var output: Bitmap? = null + val canvas: Canvas + var obj: Picture? = null + if (isHardware) { + obj = Picture() + canvas = obj.beginRecording(bitmapWidth, bitmapHeight) + } else { + output = Bitmap.createBitmap(bitmapWidth, bitmapHeight, workBitmap.config) + canvas = Canvas(output) } - val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888) - val canvas = Canvas(output) canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR) val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG) - paint.shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) - canvas.drawOval(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat(), paint) - if (bitmap != output) { - bitmap.recycle() + paint.shader = BitmapShader(workBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) + canvas.drawOval(0f, 0f, bitmapWidth.toFloat(), bitmapHeight.toFloat(), paint) + workBitmap.recycle() + if (isHardware && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + obj?.endRecording() + output = + obj?.let { Bitmap.createBitmap(it, it.width, it.height, Bitmap.Config.HARDWARE) } } return output } fun getEllipseBitmap(workBitmap: Bitmap?, angle: Float): Bitmap? { workBitmap ?: return null - var bitmap = workBitmap - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && bitmap.config == Bitmap.Config.HARDWARE) { - val tmpBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true) - bitmap.recycle() - bitmap = tmpBitmap - if (bitmap == null) { - return null - } + val bitmapWidth = workBitmap.width + val bitmapHeight = workBitmap.height + val isHardware = + Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && workBitmap.config == Bitmap.Config.HARDWARE + + var output: Bitmap? = null + val canvas: Canvas + var obj: Picture? = null + if (isHardware) { + obj = Picture() + canvas = obj.beginRecording(bitmapWidth, bitmapHeight) + } else { + output = Bitmap.createBitmap(bitmapWidth, bitmapHeight, workBitmap.config) + canvas = Canvas(output) } - val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888) - val canvas = Canvas(output) canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR) val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG) - paint.shader = BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) - val pth = (bitmap.width + bitmap.height).toFloat() / 2 + paint.shader = BitmapShader(workBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) + val pth = (bitmapWidth + bitmapHeight).toFloat() / 2 canvas.drawRoundRect( 0f, 0f, - bitmap.width.toFloat(), - bitmap.height.toFloat(), + bitmapWidth.toFloat(), + bitmapHeight.toFloat(), pth * angle, pth * angle, paint ) - if (bitmap != output) { - bitmap.recycle() + workBitmap.recycle() + if (isHardware && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + obj?.endRecording() + output = + obj?.let { Bitmap.createBitmap(it, it.width, it.height, Bitmap.Config.HARDWARE) } } return output } -} \ No newline at end of file +} diff --git a/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/picasso/transforms/MonochromeTransformation.kt b/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/picasso/transforms/MonochromeTransformation.kt index 67c554274..8818ee2d3 100644 --- a/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/picasso/transforms/MonochromeTransformation.kt +++ b/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/picasso/transforms/MonochromeTransformation.kt @@ -11,29 +11,37 @@ class MonochromeTransformation : Transformation { } fun transform(source: Bitmap): Bitmap? { - var bitmap = source - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && bitmap.config == Bitmap.Config.HARDWARE) { - val tmpBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true) - bitmap.recycle() - bitmap = tmpBitmap - if (bitmap == null) { - return null - } + val bitmapWidth = source.width + val bitmapHeight = source.height + val isHardware = + Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && source.config == Bitmap.Config.HARDWARE + + var output: Bitmap? = null + val canvas: Canvas + var obj: Picture? = null + if (isHardware) { + obj = Picture() + canvas = obj.beginRecording(bitmapWidth, bitmapHeight) + } else { + output = Bitmap.createBitmap(bitmapWidth, bitmapHeight, source.config) + canvas = Canvas(output) } val matrix = ColorMatrix() matrix.setSaturation(0f) - val bitmapCopy = Bitmap.createBitmap( - bitmap.width, - bitmap.height, Bitmap.Config.ARGB_8888 - ) - val canvas = Canvas(bitmapCopy) - val paint = Paint() + val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG) val colorFilter = ColorMatrixColorFilter(matrix) paint.colorFilter = colorFilter - canvas.drawBitmap(bitmap, 0f, 0f, paint) - bitmap.recycle() - return bitmapCopy + paint.shader = BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP) + + canvas.drawRect(0f, 0f, bitmapWidth.toFloat(), bitmapHeight.toFloat(), paint) + source.recycle() + if (isHardware && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + obj?.endRecording() + output = + obj?.let { Bitmap.createBitmap(it, it.width, it.height, Bitmap.Config.HARDWARE) } + } + return output } override fun localTransform(source: Bitmap?): Bitmap? { diff --git a/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/util/ErrorLocalizer.kt b/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/util/ErrorLocalizer.kt index f95ba997a..6ac50b507 100644 --- a/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/util/ErrorLocalizer.kt +++ b/app_filegallery/src/main/kotlin/dev/ragnarok/filegallery/util/ErrorLocalizer.kt @@ -4,6 +4,7 @@ import android.content.Context import dev.ragnarok.filegallery.R import dev.ragnarok.filegallery.api.rest.HttpException import dev.ragnarok.filegallery.nonNullNoEmpty +import java.net.ConnectException import java.net.SocketTimeoutException import java.net.UnknownHostException @@ -14,7 +15,7 @@ object ErrorLocalizer { is SocketTimeoutException -> { context.getString(R.string.error_timeout_message) } - is UnknownHostException -> { + is ConnectException, is UnknownHostException -> { context.getString(R.string.error_unknown_host) } is HttpException -> {