From 6e5f546d4e3b96cfbc3365af28a05ef0219d7456 Mon Sep 17 00:00:00 2001 From: Charles Lombardo Date: Tue, 22 Nov 2022 15:47:41 -0500 Subject: [PATCH] Android: Fix GameDetailsDialog on leanback Previously the app would crash because Material Dividers inherit from AppCompat and the leanback activity does not. This creates a new layout file with leanback-specific accommodations and code is duplicated in GameDetailsDialog to prevent inflation crashes. --- .../dolphinemu/dialogs/GameDetailsDialog.java | 158 ++++++++---- .../res/layout/dialog_game_details_tv.xml | 240 ++++++++++++++++++ 2 files changed, 353 insertions(+), 45 deletions(-) create mode 100644 Source/Android/app/src/main/res/layout/dialog_game_details_tv.xml diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/GameDetailsDialog.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/GameDetailsDialog.java index dec25dbf2a..176f5b7610 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/GameDetailsDialog.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/dialogs/GameDetailsDialog.java @@ -7,6 +7,7 @@ import android.os.Bundle; import android.view.View; import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.DialogFragment; import com.google.android.material.dialog.MaterialAlertDialogBuilder; @@ -14,6 +15,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder; import org.dolphinemu.dolphinemu.NativeLibrary; import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.databinding.DialogGameDetailsBinding; +import org.dolphinemu.dolphinemu.databinding.DialogGameDetailsTvBinding; import org.dolphinemu.dolphinemu.model.GameFile; import org.dolphinemu.dolphinemu.services.GameFileCacheManager; import org.dolphinemu.dolphinemu.utils.GlideUtils; @@ -39,66 +41,132 @@ public final class GameDetailsDialog extends DialogFragment { GameFile gameFile = GameFileCacheManager.addOrGet(getArguments().getString(ARG_GAME_PATH)); - DialogGameDetailsBinding binding = DialogGameDetailsBinding.inflate(getLayoutInflater()); - String country = getResources().getStringArray(R.array.countryNames)[gameFile.getCountry()]; String description = gameFile.getDescription(); String fileSize = NativeLibrary.FormatSize(gameFile.getFileSize(), 2); - binding.textGameTitle.setText(gameFile.getTitle()); - binding.textDescription.setText(gameFile.getDescription()); - if (description.isEmpty()) + // TODO: Remove dialog_game_details_tv if we switch to an AppCompatActivity for leanback + DialogGameDetailsBinding binding; + DialogGameDetailsTvBinding tvBinding; + MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(requireContext()); + if (requireActivity() instanceof AppCompatActivity) { - binding.textDescription.setVisibility(View.GONE); - } + binding = DialogGameDetailsBinding.inflate(getLayoutInflater()); - binding.textCountry.setText(country); - binding.textCompany.setText(gameFile.getCompany()); - binding.textGameId.setText(gameFile.getGameId()); - binding.textRevision.setText(String.valueOf(gameFile.getRevision())); - - if (!gameFile.shouldShowFileFormatDetails()) - { - binding.labelFileFormat.setText(R.string.game_details_file_size); - binding.textFileFormat.setText(fileSize); - - binding.labelCompression.setVisibility(View.GONE); - binding.textCompression.setVisibility(View.GONE); - binding.labelBlockSize.setVisibility(View.GONE); - binding.textBlockSize.setVisibility(View.GONE); - } - else - { - long blockSize = gameFile.getBlockSize(); - String compression = gameFile.getCompressionMethod(); - - binding.textFileFormat.setText(getResources().getString(R.string.game_details_size_and_format, - gameFile.getFileFormatName(), fileSize)); - - if (compression.isEmpty()) + binding.textGameTitle.setText(gameFile.getTitle()); + binding.textDescription.setText(gameFile.getDescription()); + if (description.isEmpty()) { - binding.textCompression.setText(R.string.game_details_no_compression); - } - else - { - binding.textCompression.setText(gameFile.getCompressionMethod()); + binding.textDescription.setVisibility(View.GONE); } - if (blockSize > 0) - { - binding.textBlockSize.setText(NativeLibrary.FormatSize(blockSize, 0)); - } - else + binding.textCountry.setText(country); + binding.textCompany.setText(gameFile.getCompany()); + binding.textGameId.setText(gameFile.getGameId()); + binding.textRevision.setText(String.valueOf(gameFile.getRevision())); + + if (!gameFile.shouldShowFileFormatDetails()) { + binding.labelFileFormat.setText(R.string.game_details_file_size); + binding.textFileFormat.setText(fileSize); + + binding.labelCompression.setVisibility(View.GONE); + binding.textCompression.setVisibility(View.GONE); binding.labelBlockSize.setVisibility(View.GONE); binding.textBlockSize.setVisibility(View.GONE); } + else + { + long blockSize = gameFile.getBlockSize(); + String compression = gameFile.getCompressionMethod(); + + binding.textFileFormat.setText( + getResources().getString(R.string.game_details_size_and_format, + gameFile.getFileFormatName(), fileSize)); + + if (compression.isEmpty()) + { + binding.textCompression.setText(R.string.game_details_no_compression); + } + else + { + binding.textCompression.setText(gameFile.getCompressionMethod()); + } + + if (blockSize > 0) + { + binding.textBlockSize.setText(NativeLibrary.FormatSize(blockSize, 0)); + } + else + { + binding.labelBlockSize.setVisibility(View.GONE); + binding.textBlockSize.setVisibility(View.GONE); + } + } + + GlideUtils.loadGameBanner(binding.banner, gameFile); + + builder.setView(binding.getRoot()); } + else + { + tvBinding = DialogGameDetailsTvBinding.inflate(getLayoutInflater()); - GlideUtils.loadGameBanner(binding.banner, gameFile); + tvBinding.textGameTitle.setText(gameFile.getTitle()); + tvBinding.textDescription.setText(gameFile.getDescription()); + if (description.isEmpty()) + { + tvBinding.textDescription.setVisibility(View.GONE); + } - return new MaterialAlertDialogBuilder(requireActivity()) - .setView(binding.getRoot()) - .create(); + tvBinding.textCountry.setText(country); + tvBinding.textCompany.setText(gameFile.getCompany()); + tvBinding.textGameId.setText(gameFile.getGameId()); + tvBinding.textRevision.setText(String.valueOf(gameFile.getRevision())); + + if (!gameFile.shouldShowFileFormatDetails()) + { + tvBinding.labelFileFormat.setText(R.string.game_details_file_size); + tvBinding.textFileFormat.setText(fileSize); + + tvBinding.labelCompression.setVisibility(View.GONE); + tvBinding.textCompression.setVisibility(View.GONE); + tvBinding.labelBlockSize.setVisibility(View.GONE); + tvBinding.textBlockSize.setVisibility(View.GONE); + } + else + { + long blockSize = gameFile.getBlockSize(); + String compression = gameFile.getCompressionMethod(); + + tvBinding.textFileFormat.setText( + getResources().getString(R.string.game_details_size_and_format, + gameFile.getFileFormatName(), fileSize)); + + if (compression.isEmpty()) + { + tvBinding.textCompression.setText(R.string.game_details_no_compression); + } + else + { + tvBinding.textCompression.setText(gameFile.getCompressionMethod()); + } + + if (blockSize > 0) + { + tvBinding.textBlockSize.setText(NativeLibrary.FormatSize(blockSize, 0)); + } + else + { + tvBinding.labelBlockSize.setVisibility(View.GONE); + tvBinding.textBlockSize.setVisibility(View.GONE); + } + } + + GlideUtils.loadGameBanner(tvBinding.banner, gameFile); + + builder.setView(tvBinding.getRoot()); + } + return builder.create(); } } diff --git a/Source/Android/app/src/main/res/layout/dialog_game_details_tv.xml b/Source/Android/app/src/main/res/layout/dialog_game_details_tv.xml new file mode 100644 index 0000000000..bc919c75e8 --- /dev/null +++ b/Source/Android/app/src/main/res/layout/dialog_game_details_tv.xml @@ -0,0 +1,240 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +