From c581e9601b1d24f0a0db8dda6eb36244df7e65e3 Mon Sep 17 00:00:00 2001 From: Helium314 Date: Fri, 17 Jan 2025 20:10:16 +0100 Subject: [PATCH] allow setting background image for landscape and portrait separately fixes #798 --- .../main/java/helium314/keyboard/latin/App.kt | 4 +- .../settings/AppearanceSettingsFragment.kt | 48 +++++++++++++------ .../keyboard/latin/settings/Settings.java | 30 ++++++------ app/src/main/res/values/strings.xml | 4 ++ .../main/res/xml/prefs_screen_appearance.xml | 5 ++ 5 files changed, 58 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/App.kt b/app/src/main/java/helium314/keyboard/latin/App.kt index 149b62a22..089f8ebc5 100644 --- a/app/src/main/java/helium314/keyboard/latin/App.kt +++ b/app/src/main/java/helium314/keyboard/latin/App.kt @@ -163,12 +163,12 @@ private fun upgradesWhenComingFromOldAppName(context: Context) { try { val bgDay = File(context.filesDir, "custom_background_image") if (bgDay.isFile) { - bgDay.copyTo(Settings.getCustomBackgroundFile(context, false), true) + bgDay.copyTo(Settings.getCustomBackgroundFile(context, false, false), true) bgDay.delete() } val bgNight = File(context.filesDir, "custom_background_image_night") if (bgNight.isFile) { - bgNight.copyTo(Settings.getCustomBackgroundFile(context, true), true) + bgNight.copyTo(Settings.getCustomBackgroundFile(context, true, false), true) bgNight.delete() } } catch (_: Exception) {} diff --git a/app/src/main/java/helium314/keyboard/latin/settings/AppearanceSettingsFragment.kt b/app/src/main/java/helium314/keyboard/latin/settings/AppearanceSettingsFragment.kt index 4431ac95e..6ed61bab4 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/AppearanceSettingsFragment.kt +++ b/app/src/main/java/helium314/keyboard/latin/settings/AppearanceSettingsFragment.kt @@ -65,13 +65,25 @@ class AppearanceSettingsFragment : SubScreenFragment() { private val dayImageFilePicker = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { if (it.resultCode != Activity.RESULT_OK) return@registerForActivityResult val uri = it.data?.data ?: return@registerForActivityResult - loadImage(uri, false) + loadImage(uri, false, false) } private val nightImageFilePicker = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { if (it.resultCode != Activity.RESULT_OK) return@registerForActivityResult val uri = it.data?.data ?: return@registerForActivityResult - loadImage(uri, true) + loadImage(uri, true, false) + } + + private val dayImageFilePickerLandscape = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { + if (it.resultCode != Activity.RESULT_OK) return@registerForActivityResult + val uri = it.data?.data ?: return@registerForActivityResult + loadImage(uri, false, true) + } + + private val nightImageFilePickerLandscape = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { + if (it.resultCode != Activity.RESULT_OK) return@registerForActivityResult + val uri = it.data?.data ?: return@registerForActivityResult + loadImage(uri, true, true) } override fun onCreate(savedInstanceState: Bundle?) { @@ -92,7 +104,8 @@ class AppearanceSettingsFragment : SubScreenFragment() { true } } - findPreference("custom_background_image")?.setOnPreferenceClickListener { onClickLoadImage() } + findPreference("custom_background_image")?.setOnPreferenceClickListener { onClickLoadImage(false) } + findPreference("custom_background_image_landscape")?.setOnPreferenceClickListener { onClickLoadImage(true) } findPreference(Settings.PREF_CUSTOM_ICON_NAMES)?.setOnPreferenceClickListener { if (needsReload) KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(requireContext()) @@ -322,30 +335,35 @@ class AppearanceSettingsFragment : SubScreenFragment() { builder.show() } - private fun onClickLoadImage(): Boolean { + private fun onClickLoadImage(landscape: Boolean): Boolean { if (Settings.readDayNightPref(sharedPreferences, resources)) { AlertDialog.Builder(requireContext()) - .setMessage(R.string.day_or_night_image) - .setPositiveButton(R.string.day_or_night_day) { _, _ -> customImageDialog(false) } - .setNegativeButton(R.string.day_or_night_night) { _, _ -> customImageDialog(true) } + .setTitle(R.string.day_or_night_image) + .setPositiveButton(R.string.day_or_night_day) { _, _ -> customImageDialog(false, landscape) } + .setNegativeButton(R.string.day_or_night_night) { _, _ -> customImageDialog(true, landscape) } .setNeutralButton(android.R.string.cancel, null) .show() } else { - customImageDialog(false) + customImageDialog(false, landscape) } return true } - private fun customImageDialog(night: Boolean) { - val imageFile = Settings.getCustomBackgroundFile(requireContext(), night) + private fun customImageDialog(night: Boolean, landscape: Boolean) { + val imageFile = Settings.getCustomBackgroundFile(requireContext(), night, landscape) val builder = AlertDialog.Builder(requireContext()) - .setMessage(R.string.customize_background_image) + .setMessage(if (landscape) R.string.customize_background_image_landscape else R.string.customize_background_image) .setPositiveButton(R.string.button_load_custom) { _, _ -> val intent = Intent(Intent.ACTION_OPEN_DOCUMENT) .addCategory(Intent.CATEGORY_OPENABLE) .setType("image/*") - if (night) nightImageFilePicker.launch(intent) - else dayImageFilePicker.launch(intent) + if (landscape) { + if (night) nightImageFilePickerLandscape.launch(intent) + else dayImageFilePickerLandscape.launch(intent) + } else { + if (night) nightImageFilePicker.launch(intent) + else dayImageFilePicker.launch(intent) + } } .setNegativeButton(android.R.string.cancel, null) if (imageFile.exists()) { @@ -358,8 +376,8 @@ class AppearanceSettingsFragment : SubScreenFragment() { builder.show() } - private fun loadImage(uri: Uri, night: Boolean) { - val imageFile = Settings.getCustomBackgroundFile(requireContext(), night) + private fun loadImage(uri: Uri, night: Boolean, landscape: Boolean) { + val imageFile = Settings.getCustomBackgroundFile(requireContext(), night, landscape) FileUtils.copyContentUriToNewFile(uri, requireContext(), imageFile) try { BitmapFactory.decodeFile(imageFile.absolutePath) diff --git a/app/src/main/java/helium314/keyboard/latin/settings/Settings.java b/app/src/main/java/helium314/keyboard/latin/settings/Settings.java index 0f9d14118..3cdf55482 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/Settings.java +++ b/app/src/main/java/helium314/keyboard/latin/settings/Settings.java @@ -49,6 +49,7 @@ import helium314.keyboard.latin.utils.ToolbarUtilsKt; import java.io.File; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Locale; @@ -194,8 +195,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang private final ReentrantLock mSettingsValuesLock = new ReentrantLock(); // static cache for background images to avoid potentially slow reload on every settings reload - private static Drawable sCachedBackgroundDay; - private static Drawable sCachedBackgroundNight; + private final static Drawable[] sCachedBackgroundImages = new Drawable[4]; private Map mCustomToolbarKeyCodes = null; private Map mCustomToolbarLongpressCodes = null; @@ -562,25 +562,24 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang } @Nullable public static Drawable readUserBackgroundImage(final Context context, final boolean night) { - if (night && sCachedBackgroundNight != null) return sCachedBackgroundNight; - if (!night && sCachedBackgroundDay != null) return sCachedBackgroundDay; - final File image = getCustomBackgroundFile(context, night); + final boolean landscape = context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE; + final int index = (night ? 1 : 0) + (landscape ? 2 : 0); + if (sCachedBackgroundImages[index] != null) return sCachedBackgroundImages[index]; + + File image = getCustomBackgroundFile(context, night, landscape); + if (!image.isFile() && landscape) + image = getCustomBackgroundFile(context, night, false); // fall back to portrait image for historic reasons if (!image.isFile()) return null; try { - if (night) { - sCachedBackgroundNight = new BitmapDrawable(context.getResources(), BitmapFactory.decodeFile(image.getAbsolutePath())); - return sCachedBackgroundNight; - } else { - sCachedBackgroundDay = new BitmapDrawable(context.getResources(), BitmapFactory.decodeFile(image.getAbsolutePath())); - return sCachedBackgroundDay; - } + sCachedBackgroundImages[index] = new BitmapDrawable(context.getResources(), BitmapFactory.decodeFile(image.getAbsolutePath())); + return sCachedBackgroundImages[index]; } catch (Exception e) { return null; } } - public static File getCustomBackgroundFile(final Context context, final boolean night) { - return new File(DeviceProtectedUtils.getFilesDir(context), "custom_background_image" + (night ? "_night" : "")); + public static File getCustomBackgroundFile(final Context context, final boolean night, final boolean landscape) { + return new File(DeviceProtectedUtils.getFilesDir(context), "custom_background_image" + (landscape ? "_landscape" : "") + (night ? "_night" : "")); } public static boolean readDayNightPref(final SharedPreferences prefs, final Resources res) { @@ -588,8 +587,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang } public static void clearCachedBackgroundImages() { - sCachedBackgroundDay = null; - sCachedBackgroundNight = null; + Arrays.fill(sCachedBackgroundImages, null); } public static List getSecondaryLocales(final SharedPreferences prefs, final Locale mainLocale) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6e20f49a8..7bc3cc2fc 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -550,6 +550,10 @@ disposition rather than other common dispositions for Latin languages. [CHAR LIM Clipboard bottom row Set background image + + Set background image (landscape) + + If not set, portrait image will be used Customize currencies diff --git a/app/src/main/res/xml/prefs_screen_appearance.xml b/app/src/main/res/xml/prefs_screen_appearance.xml index cbcc4b6ae..45df45d8f 100644 --- a/app/src/main/res/xml/prefs_screen_appearance.xml +++ b/app/src/main/res/xml/prefs_screen_appearance.xml @@ -71,6 +71,11 @@ android:key="custom_background_image" android:title="@string/customize_background_image" /> + +