Use more Compat utilities (#5786)

* Use ActivityCompat.recreate

* Use more KTX extensions

* Use PackageInfoCompat.getLongVersionCode

* Remove unnecessary compat usages
This commit is contained in:
Taco 2021-08-27 16:33:12 -04:00 committed by GitHub
parent edcfa28b0b
commit 12a9d0575d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 76 additions and 80 deletions

View File

@ -1,6 +1,6 @@
package eu.kanade.tachiyomi.data.database.resolvers
import android.content.ContentValues
import androidx.core.content.contentValuesOf
import com.pushtorefresh.storio.sqlite.StorIOSQLite
import com.pushtorefresh.storio.sqlite.operations.put.PutResolver
import com.pushtorefresh.storio.sqlite.operations.put.PutResult
@ -25,7 +25,7 @@ class MangaNextUpdatedPutResolver : PutResolver<Manga>() {
.whereArgs(manga.id)
.build()
fun mapToContentValues(manga: Manga) = ContentValues(1).apply {
put(MangaTable.COL_NEXT_UPDATE, manga.next_update)
}
fun mapToContentValues(manga: Manga) = contentValuesOf(
MangaTable.COL_NEXT_UPDATE to manga.next_update
)
}

View File

@ -14,6 +14,7 @@ import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.int
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.long
import uy.kohesive.injekt.injectLazy
import java.util.Date
@ -67,7 +68,7 @@ internal class ExtensionGithubApi {
val pkgName = element.jsonObject["pkg"]!!.jsonPrimitive.content
val apkName = element.jsonObject["apk"]!!.jsonPrimitive.content
val versionName = element.jsonObject["version"]!!.jsonPrimitive.content
val versionCode = element.jsonObject["code"]!!.jsonPrimitive.int
val versionCode = element.jsonObject["code"]!!.jsonPrimitive.long
val lang = element.jsonObject["lang"]!!.jsonPrimitive.content
val nsfw = element.jsonObject["nsfw"]!!.jsonPrimitive.int == 1
val icon = "${REPO_URL_PREFIX}icon/${apkName.replace(".apk", ".png")}"

View File

@ -7,7 +7,7 @@ sealed class Extension {
abstract val name: String
abstract val pkgName: String
abstract val versionName: String
abstract val versionCode: Int
abstract val versionCode: Long
abstract val lang: String?
abstract val isNsfw: Boolean
@ -15,7 +15,7 @@ sealed class Extension {
override val name: String,
override val pkgName: String,
override val versionName: String,
override val versionCode: Int,
override val versionCode: Long,
override val lang: String,
override val isNsfw: Boolean,
val pkgFactory: String?,
@ -29,7 +29,7 @@ sealed class Extension {
override val name: String,
override val pkgName: String,
override val versionName: String,
override val versionCode: Int,
override val versionCode: Long,
override val lang: String,
override val isNsfw: Boolean,
val apkName: String,
@ -40,7 +40,7 @@ sealed class Extension {
override val name: String,
override val pkgName: String,
override val versionName: String,
override val versionCode: Int,
override val versionCode: Long,
val signatureHash: String,
override val lang: String? = null,
override val isNsfw: Boolean = false

View File

@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import androidx.core.content.pm.PackageInfoCompat
import dalvik.system.PathClassLoader
import eu.kanade.tachiyomi.annotations.Nsfw
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
@ -103,7 +104,7 @@ internal object ExtensionLoader {
val extName = pkgManager.getApplicationLabel(appInfo).toString().substringAfter("Tachiyomi: ")
val versionName = pkgInfo.versionName
val versionCode = pkgInfo.versionCode
val versionCode = PackageInfoCompat.getLongVersionCode(pkgInfo)
if (versionName.isNullOrEmpty()) {
val exception = Exception("Missing versionName for extension $extName")

View File

@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.ui.browse.source.filter
import android.view.View
import android.widget.CheckedTextView
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.view.updatePadding
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.R
import eu.davidea.flexibleadapter.FlexibleAdapter
@ -72,7 +73,7 @@ open class TriStateItem(val filter: Filter.TriState) : AbstractFlexibleItem<TriS
init {
// Align with native checkbox
text.setPadding(4.dpToPx, 0, 0, 0)
text.updatePadding(left = 4.dpToPx)
text.compoundDrawablePadding = 20.dpToPx
}
}

View File

@ -9,7 +9,6 @@ import android.view.MenuItem
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.ActionMode
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.view.isVisible
import com.bluelinelabs.conductor.ControllerChangeHandler
import com.bluelinelabs.conductor.ControllerChangeType
@ -417,7 +416,7 @@ class LibraryController(
// Tint icon if there's a filter active
if (settingsSheet.filters.hasActiveFilters()) {
val filterColor = activity!!.getResourceColor(R.attr.colorFilterActive)
DrawableCompat.setTint(filterItem.icon, filterColor)
filterItem.icon.setTint(filterColor)
}
}

View File

@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.manga.chapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.graphics.drawable.DrawableCompat
import androidx.recyclerview.widget.RecyclerView
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.databinding.MangaChaptersHeaderBinding
@ -60,7 +59,7 @@ class MangaChaptersHeaderAdapter(
} else {
view.context.getResourceColor(R.attr.colorOnBackground)
}
DrawableCompat.setTint(binding.btnChaptersFilter.drawable, filterColor)
binding.btnChaptersFilter.drawable.setTint(filterColor)
merge(view.clicks(), binding.btnChaptersFilter.clicks())
.onEach { controller.showSettingsSheet() }

View File

@ -8,7 +8,7 @@ import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.ViewPropertyAnimator
import androidx.core.content.ContextCompat
import androidx.core.graphics.withSave
import androidx.core.view.isVisible
import eu.kanade.tachiyomi.ui.reader.viewer.ViewerNavigation
import kotlin.math.abs
@ -62,30 +62,27 @@ class ReaderNavigationOverlayView(context: Context, attributeSet: AttributeSet)
navigation?.regions?.forEach { region ->
val rect = region.rectF
canvas?.save()
// Scale rect from 1f,1f to screen width and height
canvas?.scale(width.toFloat(), height.toFloat())
regionPaint.color = ContextCompat.getColor(context, region.type.colorRes)
canvas?.drawRect(rect, regionPaint)
canvas?.restore()
canvas?.withSave {
// Scale rect from 1f,1f to screen width and height
scale(width.toFloat(), height.toFloat())
regionPaint.color = context.getColor(region.type.colorRes)
drawRect(rect, regionPaint)
}
// Don't want scale anymore because it messes with drawText
canvas?.save()
canvas?.withSave {
// Translate origin to rect start (left, top)
canvas?.translate((width * rect.left), (height * rect.top))
// Translate origin to rect start (left, top)
translate((width * rect.left), (height * rect.top))
// Calculate center of rect width on screen
val x = width * (abs(rect.left - rect.right) / 2)
// Calculate center of rect width on screen
val x = width * (abs(rect.left - rect.right) / 2)
// Calculate center of rect height on screen
val y = height * (abs(rect.top - rect.bottom) / 2)
// Calculate center of rect height on screen
val y = height * (abs(rect.top - rect.bottom) / 2)
canvas?.drawText(context.getString(region.type.nameRes), x, y, textBorderPaint)
canvas?.drawText(context.getString(region.type.nameRes), x, y, textPaint)
canvas?.restore()
drawText(context.getString(region.type.nameRes), x, y, textBorderPaint)
drawText(context.getString(region.type.nameRes), x, y, textPaint)
}
}
}

View File

@ -6,6 +6,7 @@ import android.os.Looper
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.ViewConfiguration
import androidx.core.os.postDelayed
import kotlin.math.abs
/**
@ -43,7 +44,7 @@ open class GestureDetectorWithLongTap(
if (ev.downTime - lastUp > doubleTapTime) {
downX = ev.rawX
downY = ev.rawY
handler.postDelayed(longTapFn, longTapTime)
handler.postDelayed(longTapTime) { longTapFn }
}
}
MotionEvent.ACTION_MOVE -> {

View File

@ -16,6 +16,7 @@ import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.core.view.setMargins
import androidx.core.view.updateLayoutParams
import coil.imageLoader
import coil.request.CachePolicy
@ -457,7 +458,7 @@ class PagerPageHolder(
TextView(context).apply {
layoutParams = LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT).apply {
setMargins(margins, margins, margins, margins)
setMargins(margins)
}
gravity = Gravity.CENTER
setText(R.string.decode_image_error)
@ -467,7 +468,7 @@ class PagerPageHolder(
PagerButton(context, viewer).apply {
layoutParams = LayoutParams(WRAP_CONTENT, WRAP_CONTENT).apply {
setMargins(margins, margins, margins, margins)
setMargins(margins)
}
setText(R.string.action_retry)
setOnClickListener {
@ -481,7 +482,7 @@ class PagerPageHolder(
if (imageUrl.orEmpty().startsWith("http", true)) {
PagerButton(context, viewer).apply {
layoutParams = LayoutParams(WRAP_CONTENT, WRAP_CONTENT).apply {
setMargins(margins, margins, margins, margins)
setMargins(margins)
}
setText(R.string.action_open_in_web_view)
setOnClickListener {

View File

@ -6,6 +6,7 @@ import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import androidx.core.app.ActivityCompat
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
@ -139,7 +140,7 @@ class WebtoonViewer(val activity: ReaderActivity, val isContinuous: Boolean = tr
}
config.themeChangedListener = {
activity.recreate()
ActivityCompat.recreate(activity)
}
config.navigationModeChangedListener = {

View File

@ -1,6 +1,7 @@
package eu.kanade.tachiyomi.ui.setting
import android.os.Build
import androidx.core.app.ActivityCompat
import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.asImmediateFlow
@ -74,7 +75,7 @@ class SettingsAppearanceController : SettingsController() {
defaultValue = appThemes[0].name
onChange {
activity?.recreate()
activity?.let { ActivityCompat.recreate(it) }
true
}
}
@ -87,7 +88,7 @@ class SettingsAppearanceController : SettingsController() {
.launchIn(viewScope)
onChange {
activity?.recreate()
activity?.let { ActivityCompat.recreate(it) }
true
}
}

View File

@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.util.preference
import androidx.annotation.StringRes
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.graphics.drawable.DrawableCompat
import androidx.preference.CheckBoxPreference
import androidx.preference.DialogPreference
import androidx.preference.EditTextPreference
@ -150,7 +149,7 @@ var Preference.summaryRes: Int
var Preference.iconTint: Int
get() = 0 // set only
set(value) {
DrawableCompat.setTint(icon, value)
icon.setTint(value)
}
var ListPreference.entriesRes: Array<Int>

View File

@ -7,7 +7,6 @@ import android.os.Environment
import android.os.StatFs
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import androidx.core.os.EnvironmentCompat
import com.hippo.unifile.UniFile
import eu.kanade.tachiyomi.util.lang.Hash
import java.io.File
@ -51,7 +50,7 @@ object DiskUtil {
.filterNotNull()
.mapNotNull {
val file = File(it.absolutePath.substringBefore("/Android/"))
val state = EnvironmentCompat.getStorageState(file)
val state = Environment.getExternalStorageState(file)
if (state == Environment.MEDIA_MOUNTED || state == Environment.MEDIA_MOUNTED_READ_ONLY) {
file
} else {

View File

@ -102,7 +102,7 @@ fun Context.copyToClipboard(label: String, content: String) {
*/
fun Context.notificationBuilder(channelId: String, block: (NotificationCompat.Builder.() -> Unit)? = null): NotificationCompat.Builder {
val builder = NotificationCompat.Builder(this, channelId)
.setColor(ContextCompat.getColor(this, R.color.accent_blue))
.setColor(getColor(R.color.accent_blue))
if (block != null) {
builder.block()
}
@ -152,7 +152,7 @@ fun Context.hasPermission(permission: String) = ContextCompat.checkSelfPermissio
val tv = TypedValue()
return if (this.theme.resolveAttribute(attr, tv, true)) {
if (tv.resourceId != 0) {
ContextCompat.getColor(this, tv.resourceId)
getColor(tv.resourceId)
} else {
tv.data
}

View File

@ -4,7 +4,6 @@ import android.content.Context
import android.content.res.Configuration
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
@ -12,6 +11,7 @@ import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.os.Build
import androidx.core.graphics.alpha
import androidx.core.graphics.applyCanvas
import androidx.core.graphics.blue
import androidx.core.graphics.createBitmap
import androidx.core.graphics.green
@ -132,8 +132,9 @@ object ImageUtil {
Side.RIGHT -> Rect(width - width / 2, 0, width, height)
Side.LEFT -> Rect(0, 0, width / 2, height)
}
val canvas = Canvas(half)
canvas.drawBitmap(imageBitmap, part, singlePage, null)
half.applyCanvas {
drawBitmap(imageBitmap, part, singlePage, null)
}
val output = ByteArrayOutputStream()
half.compress(Bitmap.CompressFormat.JPEG, 100, output)
@ -151,21 +152,22 @@ object ImageUtil {
val width = imageBitmap.width
val result = createBitmap(width / 2, height * 2)
val canvas = Canvas(result)
// right -> upper
val rightPart = when (upperSide) {
Side.RIGHT -> Rect(width - width / 2, 0, width, height)
Side.LEFT -> Rect(0, 0, width / 2, height)
result.applyCanvas {
// right -> upper
val rightPart = when (upperSide) {
Side.RIGHT -> Rect(width - width / 2, 0, width, height)
Side.LEFT -> Rect(0, 0, width / 2, height)
}
val upperPart = Rect(0, 0, width / 2, height)
drawBitmap(imageBitmap, rightPart, upperPart, null)
// left -> bottom
val leftPart = when (upperSide) {
Side.LEFT -> Rect(width - width / 2, 0, width, height)
Side.RIGHT -> Rect(0, 0, width / 2, height)
}
val bottomPart = Rect(0, height, width / 2, height * 2)
drawBitmap(imageBitmap, leftPart, bottomPart, null)
}
val upperPart = Rect(0, 0, width / 2, height)
canvas.drawBitmap(imageBitmap, rightPart, upperPart, null)
// left -> bottom
val leftPart = when (upperSide) {
Side.LEFT -> Rect(width - width / 2, 0, width, height)
Side.RIGHT -> Rect(0, 0, width / 2, height)
}
val bottomPart = Rect(0, height, width / 2, height * 2)
canvas.drawBitmap(imageBitmap, leftPart, bottomPart, null)
val output = ByteArrayOutputStream()
result.compress(Bitmap.CompressFormat.JPEG, 100, output)

View File

@ -13,10 +13,10 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.MenuRes
import androidx.annotation.StringRes
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.widget.PopupMenu
import androidx.appcompat.widget.TooltipCompat
import androidx.core.content.ContextCompat
import androidx.core.view.children
import androidx.core.view.descendants
import androidx.core.view.forEach
@ -123,10 +123,10 @@ inline fun View.popupMenu(
if (selectedItemId != null) {
(popup.menu as? MenuBuilder)?.setOptionalIconsVisible(true)
val emptyIcon = ContextCompat.getDrawable(context, R.drawable.ic_blank_24dp)
val emptyIcon = AppCompatResources.getDrawable(context, R.drawable.ic_blank_24dp)
popup.menu.forEach { item ->
item.icon = when (item.itemId) {
selectedItemId -> ContextCompat.getDrawable(context, R.drawable.ic_check_24dp)?.mutate()?.apply {
selectedItemId -> AppCompatResources.getDrawable(context, R.drawable.ic_check_24dp)?.mutate()?.apply {
setTint(context.getResourceColor(android.R.attr.textColorPrimary))
}
else -> emptyIcon

View File

@ -7,7 +7,6 @@ import android.view.ViewGroup
import androidx.annotation.AttrRes
import androidx.annotation.CallSuper
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import eu.kanade.tachiyomi.R
@ -102,7 +101,7 @@ open class ExtendedNavigationView @JvmOverloads constructor(
return when (state) {
SORT_ASC -> tintVector(context, R.drawable.ic_arrow_up_white_32dp)
SORT_DESC -> tintVector(context, R.drawable.ic_arrow_down_white_32dp)
SORT_NONE -> ContextCompat.getDrawable(context, R.drawable.empty_drawable_32dp)
SORT_NONE -> AppCompatResources.getDrawable(context, R.drawable.empty_drawable_32dp)
else -> null
}
}

View File

@ -8,9 +8,9 @@ import android.view.LayoutInflater
import android.view.MenuItem
import android.widget.FrameLayout
import androidx.annotation.ArrayRes
import androidx.appcompat.content.res.AppCompatResources
import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.widget.PopupMenu
import androidx.core.content.ContextCompat
import androidx.core.view.forEach
import androidx.core.view.get
import com.tfcporciuncula.flow.Preference
@ -38,10 +38,10 @@ class MaterialSpinnerView @JvmOverloads constructor(context: Context, attrs: Att
}
private val emptyIcon by lazy {
ContextCompat.getDrawable(context, R.drawable.ic_blank_24dp)
AppCompatResources.getDrawable(context, R.drawable.ic_blank_24dp)
}
private val checkmarkIcon by lazy {
ContextCompat.getDrawable(context, R.drawable.ic_check_24dp)?.mutate()?.apply {
AppCompatResources.getDrawable(context, R.drawable.ic_check_24dp)?.mutate()?.apply {
setTint(context.getResourceColor(android.R.attr.textColorPrimary))
}
}

View File

@ -13,7 +13,6 @@ import android.widget.RadioButton
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.widget.TintTypedArray
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.R
@ -44,14 +43,10 @@ open class SimpleNavigationView @JvmOverloads constructor(
R.style.Widget_Design_NavigationView
)
ViewCompat.setBackground(
this,
a.getDrawable(R.styleable.NavigationView_android_background)
)
setBackground(a.getDrawable(R.styleable.NavigationView_android_background))
if (a.hasValue(R.styleable.NavigationView_elevation)) {
ViewCompat.setElevation(
this,
setElevation(
a.getDimensionPixelSize(
R.styleable.NavigationView_elevation,
0