diff --git a/app/src/main/java/helium314/keyboard/latin/FloatingKeyboardManager.kt b/app/src/main/java/helium314/keyboard/latin/FloatingKeyboardManager.kt index 6886555f8..e2e5738d1 100644 --- a/app/src/main/java/helium314/keyboard/latin/FloatingKeyboardManager.kt +++ b/app/src/main/java/helium314/keyboard/latin/FloatingKeyboardManager.kt @@ -8,8 +8,11 @@ import android.content.SharedPreferences import android.content.res.Configuration import android.content.res.Resources import android.graphics.Color +import android.graphics.Outline import android.graphics.Rect import android.graphics.drawable.GradientDrawable +import androidx.core.graphics.ColorUtils +import helium314.keyboard.latin.utils.CenterCropDrawable import android.os.Build import android.view.Gravity import android.view.MotionEvent @@ -194,26 +197,10 @@ class FloatingKeyboardManager(private val context: Context, private val latinIME KeyboardLayoutSet.clearKeyboardCache() val colors = Settings.getValues().mColors - val bgColor = colors.get(ColorType.MAIN_BACKGROUND) val textColor = colors.get(ColorType.KEY_TEXT) val cornerRadius = CORNER_RADIUS_DP * density val bottomBarHeight = (BOTTOM_BAR_HEIGHT_DP * density).toInt() - // Configure or create bottom bar inside main_keyboard_frame - val bottomContainer = frame.findViewById(R.id.floating_bottom_bar) - if (bottomContainer != null) { - val bottomLp = bottomContainer.layoutParams as? LinearLayout.LayoutParams - ?: LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, bottomBarHeight) - bottomLp.width = LinearLayout.LayoutParams.MATCH_PARENT - bottomLp.height = bottomBarHeight - bottomContainer.layoutParams = bottomLp - bottomContainer.setPadding(0, 0, 0, 0) - bottomContainer.removeAllViews() - bottomBar = createBottomBar(bottomBarHeight, bgColor, textColor, density, cornerRadius) - bottomContainer.addView(bottomBar) - bottomContainer.visibility = View.VISIBLE - } - // Configure FrameLayout layoutParams val lp = frame.layoutParams as? FrameLayout.LayoutParams ?: FrameLayout.LayoutParams( floatingWidth, @@ -236,21 +223,37 @@ class FloatingKeyboardManager(private val context: Context, private val latinIME frame.translationX = posX.toFloat() frame.translationY = posY.toFloat() - // Background styling: rounded corners and elevation - val bgDrawable = GradientDrawable().apply { - setColor(bgColor) - cornerRadii = floatArrayOf( - cornerRadius, cornerRadius, - cornerRadius, cornerRadius, - cornerRadius, cornerRadius, - cornerRadius, cornerRadius - ) + // Background styling: the same background as docked (color or wallpaper), with rounded corners and elevation. + // The corners come from the outline, as a wallpaper has no rounded shape of its own. + frame.background = null + colors.setBackground(frame, ColorType.MAIN_BACKGROUND) + // a wallpaper can have any color, so the controls need their own backing to stay visible + val controlBacking = if (frame.background is CenterCropDrawable) + ColorUtils.setAlphaComponent(colors.get(ColorType.MAIN_BACKGROUND), 0xFF) + else Color.TRANSPARENT + frame.outlineProvider = object : ViewOutlineProvider() { + override fun getOutline(view: View, outline: Outline) { + outline.setRoundRect(0, 0, view.width, view.height, cornerRadius) + } } - frame.background = bgDrawable frame.clipToOutline = true - frame.outlineProvider = ViewOutlineProvider.BACKGROUND frame.elevation = 8f * density + // Configure or create bottom bar inside main_keyboard_frame + val bottomContainer = frame.findViewById(R.id.floating_bottom_bar) + if (bottomContainer != null) { + val bottomLp = bottomContainer.layoutParams as? LinearLayout.LayoutParams + ?: LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, bottomBarHeight) + bottomLp.width = LinearLayout.LayoutParams.MATCH_PARENT + bottomLp.height = bottomBarHeight + bottomContainer.layoutParams = bottomLp + bottomContainer.setPadding(0, 0, 0, 0) + bottomContainer.removeAllViews() + bottomBar = createBottomBar(bottomBarHeight, textColor, controlBacking, density) + bottomContainer.addView(bottomBar) + bottomContainer.visibility = View.VISIBLE + } + KeyboardSwitcher.getInstance().reloadKeyboard() (frame.findViewById(R.id.keyboard_view_wrapper) as? View)?.requestLayout() @@ -428,10 +431,9 @@ class FloatingKeyboardManager(private val context: Context, private val latinIME @SuppressLint("ClickableViewAccessibility") private fun createBottomBar( height: Int, - bgColor: Int, textColor: Int, - density: Float, - cornerRadius: Float + controlBacking: Int, + density: Float ): FrameLayout { val dm = context.resources.displayMetrics val minHeight = (120 * density).toInt() @@ -441,15 +443,6 @@ class FloatingKeyboardManager(private val context: Context, private val latinIME FrameLayout.LayoutParams.MATCH_PARENT, height ) - background = GradientDrawable().apply { - setColor(bgColor) - cornerRadii = floatArrayOf( - 0f, 0f, - 0f, 0f, - cornerRadius, cornerRadius, - cornerRadius, cornerRadius - ) - } } // Sleek drag handle pill in center @@ -462,8 +455,14 @@ class FloatingKeyboardManager(private val context: Context, private val latinIME this.cornerRadius = 2.5f * density setColor(defaultPillColor) } + // the pill is too thin to draw on a backing of its own, so it gets an outline in that color + val pillOutline = if (controlBacking == Color.TRANSPARENT) 0 else (2 * density).toInt() + if (pillOutline > 0) { + dragHandleBg.setStroke(pillOutline, controlBacking) + dragHandleBg.cornerRadius = pillHeight / 2f + pillOutline + } val dragHandle = View(context).apply { - layoutParams = FrameLayout.LayoutParams(pillWidth, pillHeight).apply { + layoutParams = FrameLayout.LayoutParams(pillWidth + 2 * pillOutline, pillHeight + 2 * pillOutline).apply { gravity = Gravity.CENTER } background = dragHandleBg @@ -485,7 +484,7 @@ class FloatingKeyboardManager(private val context: Context, private val latinIME background = GradientDrawable().apply { shape = GradientDrawable.RECTANGLE this.cornerRadius = keyboardCornerRadiusPx(context.resources) - setColor((textColor and 0x00FFFFFF) or 0x1A000000.toInt()) + setColor(ColorUtils.compositeColors((textColor and 0x00FFFFFF) or 0x1A000000, controlBacking)) } contentDescription = context.getString(R.string.floating_keyboard_dock_handle) scaleType = android.widget.ImageView.ScaleType.FIT_CENTER @@ -494,8 +493,8 @@ class FloatingKeyboardManager(private val context: Context, private val latinIME bottomBar.addView(dockBtn) // Corner Resize Handle (Bottom-Right / End) - val defaultBgColor = (textColor and 0x00FFFFFF) or 0x1A000000.toInt() - val activeBgColor = (textColor and 0x00FFFFFF) or 0x55000000.toInt() + val defaultBgColor = ColorUtils.compositeColors((textColor and 0x00FFFFFF) or 0x1A000000, controlBacking) + val activeBgColor = ColorUtils.compositeColors((textColor and 0x00FFFFFF) or 0x55000000, controlBacking) val resizeBg = GradientDrawable().apply { shape = GradientDrawable.RECTANGLE diff --git a/app/src/main/java/helium314/keyboard/latin/LatinIME.kt b/app/src/main/java/helium314/keyboard/latin/LatinIME.kt index dabd8b05f..7a99697e3 100644 --- a/app/src/main/java/helium314/keyboard/latin/LatinIME.kt +++ b/app/src/main/java/helium314/keyboard/latin/LatinIME.kt @@ -1,5 +1,6 @@ package helium314.keyboard.latin +import helium314.keyboard.latin.utils.CenterCropDrawable import helium314.keyboard.latin.utils.applyKeyboardStyle import android.Manifest import android.annotation.SuppressLint @@ -2288,7 +2289,10 @@ class LatinIME : InputMethodService(), window.isNavigationBarContrastEnforced = true } val settingsValues = settings.current - if (!settingsValues.mCustomNavBarColor) { + // from Android 15 the keyboard extends behind the navigation bar, so leave a wallpaper visible there + val showsWallpaper = Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM + && inputView?.findViewById(R.id.main_keyboard_frame)?.background is CenterCropDrawable + if (!settingsValues.mCustomNavBarColor || showsWallpaper) { setNavigationBarFrameBackground(null) clearNavigationBarColor() return diff --git a/app/src/main/java/helium314/keyboard/settings/preferences/BackgroundImagePreference.kt b/app/src/main/java/helium314/keyboard/settings/preferences/BackgroundImagePreference.kt index d8c31b41a..89c408379 100644 --- a/app/src/main/java/helium314/keyboard/settings/preferences/BackgroundImagePreference.kt +++ b/app/src/main/java/helium314/keyboard/settings/preferences/BackgroundImagePreference.kt @@ -33,6 +33,7 @@ import helium314.keyboard.settings.dialogs.ConfirmationDialog import helium314.keyboard.settings.dialogs.InfoDialog import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext @Composable fun BackgroundImagePref(setting: Setting, isLandscape: Boolean) { @@ -56,8 +57,12 @@ fun BackgroundImagePref(setting: Setting, isLandscape: Boolean) { showSelectionDialog = false showDayNightDialog = false scope.launch(Dispatchers.IO) { - if (!setBackgroundImage(ctx, uri, isNight, isLandscape)) - showErrorDialog = true + val success = setBackgroundImage(ctx, uri, isNight, isLandscape) + withContext(Dispatchers.Main) { + // rebuilds the keyboard views, so main thread only + KeyboardSwitcher.getInstance().setThemeNeedsReload() + if (!success) showErrorDialog = true + } } } val intent = Intent(Intent.ACTION_OPEN_DOCUMENT) @@ -119,13 +124,10 @@ fun BackgroundImagePref(setting: Setting, isLandscape: Boolean) { private fun setBackgroundImage(ctx: Context, uri: Uri, isNight: Boolean, isLandscape: Boolean): Boolean { val imageFile = Settings.getCustomBackgroundFile(ctx, isNight, isLandscape) FileUtils.copyContentUriToNewFile(uri, ctx, imageFile) - KeyboardSwitcher.getInstance().setThemeNeedsReload() val bm = BitmapUtils.decodeSampledBitmap(imageFile, maxDim = 2048, preferLowConfig = true) - if (bm == null) { - imageFile.delete() - return false - } - bm.recycle() + if (bm == null) imageFile.delete() + bm?.recycle() + // also after a failure, as the previous image file is already gone Settings.clearCachedBackgroundImages(ctx) - return true + return bm != null }