diff options
| author | 2023-08-12 19:09:30 -0400 | |
|---|---|---|
| committer | 2023-08-30 10:19:05 -0400 | |
| commit | 270f430f70d9325cf4dc3520ad359f14b9da6880 (patch) | |
| tree | f96cdc52e0e2f897aae32ecd4a045e4acf3cbb47 /src | |
| parent | Merge pull request #11380 from t895/settings-integration (diff) | |
| download | yuzu-270f430f70d9325cf4dc3520ad359f14b9da6880.tar.gz yuzu-270f430f70d9325cf4dc3520ad359f14b9da6880.tar.xz yuzu-270f430f70d9325cf4dc3520ad359f14b9da6880.zip | |
android: Create custom game icon loader
Diffstat (limited to '')
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt | 24 | ||||
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameIconUtils.kt | 77 |
2 files changed, 79 insertions, 22 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt index e91277d35..13359ef36 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt | |||
| @@ -3,8 +3,6 @@ | |||
| 3 | 3 | ||
| 4 | package org.yuzu.yuzu_emu.adapters | 4 | package org.yuzu.yuzu_emu.adapters |
| 5 | 5 | ||
| 6 | import android.graphics.Bitmap | ||
| 7 | import android.graphics.BitmapFactory | ||
| 8 | import android.net.Uri | 6 | import android.net.Uri |
| 9 | import android.text.TextUtils | 7 | import android.text.TextUtils |
| 10 | import android.view.LayoutInflater | 8 | import android.view.LayoutInflater |
| @@ -15,23 +13,20 @@ import android.widget.Toast | |||
| 15 | import androidx.appcompat.app.AppCompatActivity | 13 | import androidx.appcompat.app.AppCompatActivity |
| 16 | import androidx.documentfile.provider.DocumentFile | 14 | import androidx.documentfile.provider.DocumentFile |
| 17 | import androidx.lifecycle.ViewModelProvider | 15 | import androidx.lifecycle.ViewModelProvider |
| 18 | import androidx.lifecycle.lifecycleScope | ||
| 19 | import androidx.navigation.findNavController | 16 | import androidx.navigation.findNavController |
| 20 | import androidx.preference.PreferenceManager | 17 | import androidx.preference.PreferenceManager |
| 21 | import androidx.recyclerview.widget.AsyncDifferConfig | 18 | import androidx.recyclerview.widget.AsyncDifferConfig |
| 22 | import androidx.recyclerview.widget.DiffUtil | 19 | import androidx.recyclerview.widget.DiffUtil |
| 23 | import androidx.recyclerview.widget.ListAdapter | 20 | import androidx.recyclerview.widget.ListAdapter |
| 24 | import androidx.recyclerview.widget.RecyclerView | 21 | import androidx.recyclerview.widget.RecyclerView |
| 25 | import coil.load | ||
| 26 | import kotlinx.coroutines.launch | ||
| 27 | import org.yuzu.yuzu_emu.HomeNavigationDirections | 22 | import org.yuzu.yuzu_emu.HomeNavigationDirections |
| 28 | import org.yuzu.yuzu_emu.NativeLibrary | ||
| 29 | import org.yuzu.yuzu_emu.R | 23 | import org.yuzu.yuzu_emu.R |
| 30 | import org.yuzu.yuzu_emu.YuzuApplication | 24 | import org.yuzu.yuzu_emu.YuzuApplication |
| 31 | import org.yuzu.yuzu_emu.adapters.GameAdapter.GameViewHolder | 25 | import org.yuzu.yuzu_emu.adapters.GameAdapter.GameViewHolder |
| 32 | import org.yuzu.yuzu_emu.databinding.CardGameBinding | 26 | import org.yuzu.yuzu_emu.databinding.CardGameBinding |
| 33 | import org.yuzu.yuzu_emu.model.Game | 27 | import org.yuzu.yuzu_emu.model.Game |
| 34 | import org.yuzu.yuzu_emu.model.GamesViewModel | 28 | import org.yuzu.yuzu_emu.model.GamesViewModel |
| 29 | import org.yuzu.yuzu_emu.utils.GameIconUtils | ||
| 35 | 30 | ||
| 36 | class GameAdapter(private val activity: AppCompatActivity) : | 31 | class GameAdapter(private val activity: AppCompatActivity) : |
| 37 | ListAdapter<Game, GameViewHolder>(AsyncDifferConfig.Builder(DiffCallback()).build()), | 32 | ListAdapter<Game, GameViewHolder>(AsyncDifferConfig.Builder(DiffCallback()).build()), |
| @@ -98,12 +93,7 @@ class GameAdapter(private val activity: AppCompatActivity) : | |||
| 98 | this.game = game | 93 | this.game = game |
| 99 | 94 | ||
| 100 | binding.imageGameScreen.scaleType = ImageView.ScaleType.CENTER_CROP | 95 | binding.imageGameScreen.scaleType = ImageView.ScaleType.CENTER_CROP |
| 101 | activity.lifecycleScope.launch { | 96 | GameIconUtils.loadGameIcon(game, binding.imageGameScreen) |
| 102 | val bitmap = decodeGameIcon(game.path) | ||
| 103 | binding.imageGameScreen.load(bitmap) { | ||
| 104 | error(R.drawable.default_icon) | ||
| 105 | } | ||
| 106 | } | ||
| 107 | 97 | ||
| 108 | binding.textGameTitle.text = game.title.replace("[\\t\\n\\r]+".toRegex(), " ") | 98 | binding.textGameTitle.text = game.title.replace("[\\t\\n\\r]+".toRegex(), " ") |
| 109 | 99 | ||
| @@ -126,14 +116,4 @@ class GameAdapter(private val activity: AppCompatActivity) : | |||
| 126 | return oldItem == newItem | 116 | return oldItem == newItem |
| 127 | } | 117 | } |
| 128 | } | 118 | } |
| 129 | |||
| 130 | private fun decodeGameIcon(uri: String): Bitmap? { | ||
| 131 | val data = NativeLibrary.getIcon(uri) | ||
| 132 | return BitmapFactory.decodeByteArray( | ||
| 133 | data, | ||
| 134 | 0, | ||
| 135 | data.size, | ||
| 136 | BitmapFactory.Options() | ||
| 137 | ) | ||
| 138 | } | ||
| 139 | } | 119 | } |
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameIconUtils.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameIconUtils.kt new file mode 100644 index 000000000..c0fe596d7 --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameIconUtils.kt | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | // SPDX-FileCopyrightText: 2023 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | package org.yuzu.yuzu_emu.utils | ||
| 5 | |||
| 6 | import android.graphics.Bitmap | ||
| 7 | import android.graphics.BitmapFactory | ||
| 8 | import android.widget.ImageView | ||
| 9 | import androidx.core.graphics.drawable.toDrawable | ||
| 10 | import coil.ImageLoader | ||
| 11 | import coil.decode.DataSource | ||
| 12 | import coil.fetch.DrawableResult | ||
| 13 | import coil.fetch.FetchResult | ||
| 14 | import coil.fetch.Fetcher | ||
| 15 | import coil.key.Keyer | ||
| 16 | import coil.memory.MemoryCache | ||
| 17 | import coil.request.ImageRequest | ||
| 18 | import coil.request.Options | ||
| 19 | import org.yuzu.yuzu_emu.NativeLibrary | ||
| 20 | import org.yuzu.yuzu_emu.R | ||
| 21 | import org.yuzu.yuzu_emu.YuzuApplication | ||
| 22 | import org.yuzu.yuzu_emu.model.Game | ||
| 23 | |||
| 24 | class GameIconFetcher( | ||
| 25 | private val game: Game, | ||
| 26 | private val options: Options | ||
| 27 | ) : Fetcher { | ||
| 28 | override suspend fun fetch(): FetchResult { | ||
| 29 | return DrawableResult( | ||
| 30 | drawable = decodeGameIcon(game.path)!!.toDrawable(options.context.resources), | ||
| 31 | isSampled = false, | ||
| 32 | dataSource = DataSource.DISK | ||
| 33 | ) | ||
| 34 | } | ||
| 35 | |||
| 36 | private fun decodeGameIcon(uri: String): Bitmap? { | ||
| 37 | val data = NativeLibrary.getIcon(uri) | ||
| 38 | return BitmapFactory.decodeByteArray( | ||
| 39 | data, | ||
| 40 | 0, | ||
| 41 | data.size, | ||
| 42 | BitmapFactory.Options() | ||
| 43 | ) | ||
| 44 | } | ||
| 45 | |||
| 46 | class Factory : Fetcher.Factory<Game> { | ||
| 47 | override fun create(data: Game, options: Options, imageLoader: ImageLoader): Fetcher = | ||
| 48 | GameIconFetcher(data, options) | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | class GameIconKeyer : Keyer<Game> { | ||
| 53 | override fun key(data: Game, options: Options): String = data.path | ||
| 54 | } | ||
| 55 | |||
| 56 | object GameIconUtils { | ||
| 57 | private val imageLoader = ImageLoader.Builder(YuzuApplication.appContext) | ||
| 58 | .components { | ||
| 59 | add(GameIconKeyer()) | ||
| 60 | add(GameIconFetcher.Factory()) | ||
| 61 | } | ||
| 62 | .memoryCache { | ||
| 63 | MemoryCache.Builder(YuzuApplication.appContext) | ||
| 64 | .maxSizePercent(0.25) | ||
| 65 | .build() | ||
| 66 | } | ||
| 67 | .build() | ||
| 68 | |||
| 69 | fun loadGameIcon(game: Game, imageView: ImageView) { | ||
| 70 | val request = ImageRequest.Builder(YuzuApplication.appContext) | ||
| 71 | .data(game) | ||
| 72 | .target(imageView) | ||
| 73 | .error(R.drawable.default_icon) | ||
| 74 | .build() | ||
| 75 | imageLoader.enqueue(request) | ||
| 76 | } | ||
| 77 | } | ||