diff --git a/app/src/main/java/com/topjohnwu/magisk/arch/UIActivity.kt b/app/src/main/java/com/topjohnwu/magisk/arch/UIActivity.kt index c011e5ef9..147a57730 100644 --- a/app/src/main/java/com/topjohnwu/magisk/arch/UIActivity.kt +++ b/app/src/main/java/com/topjohnwu/magisk/arch/UIActivity.kt @@ -14,6 +14,7 @@ import com.google.android.material.snackbar.Snackbar import com.topjohnwu.magisk.BR import com.topjohnwu.magisk.core.Config import com.topjohnwu.magisk.core.base.BaseActivity +import com.topjohnwu.magisk.widget.Pre23CardViewBackgroundColorFixLayoutInflaterListener import rikka.insets.WindowInsetsHelper import rikka.layoutinflater.view.LayoutInflaterFactory @@ -33,6 +34,11 @@ abstract class UIActivity : BaseActivity(), ViewModel override fun onCreate(savedInstanceState: Bundle?) { layoutInflater.factory2 = LayoutInflaterFactory(delegate) .addOnViewCreatedListener(WindowInsetsHelper.LISTENER) + .apply { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { + this.addOnViewCreatedListener(Pre23CardViewBackgroundColorFixLayoutInflaterListener.getInstance()) + } + } super.onCreate(savedInstanceState) diff --git a/app/src/main/java/com/topjohnwu/magisk/widget/Pre23CardViewBackgroundColorFixLayoutInflaterListener.java b/app/src/main/java/com/topjohnwu/magisk/widget/Pre23CardViewBackgroundColorFixLayoutInflaterListener.java new file mode 100644 index 000000000..2d9b9d661 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/widget/Pre23CardViewBackgroundColorFixLayoutInflaterListener.java @@ -0,0 +1,41 @@ +package com.topjohnwu.magisk.widget; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.content.res.ColorStateList; +import android.util.AttributeSet; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.widget.TintTypedArray; +import androidx.cardview.widget.CardView; + +import rikka.layoutinflater.view.LayoutInflaterFactory; + +public class Pre23CardViewBackgroundColorFixLayoutInflaterListener implements LayoutInflaterFactory.OnViewCreatedListener { + + private final static Pre23CardViewBackgroundColorFixLayoutInflaterListener INSTANCE = new Pre23CardViewBackgroundColorFixLayoutInflaterListener(); + + public static Pre23CardViewBackgroundColorFixLayoutInflaterListener getInstance() { + return INSTANCE; + } + + @SuppressLint("RestrictedApi") + @Override + public void onViewCreated(@NonNull View view, @Nullable View parent, @NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) { + if (!(view instanceof CardView)) { + return; + } + + TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, androidx.cardview.R.styleable.CardView); + if (a.hasValue(androidx.cardview.R.styleable.CardView_cardBackgroundColor)) { + ColorStateList colorStateList = a.getColorStateList(androidx.cardview.R.styleable.CardView_cardBackgroundColor); + if (colorStateList != null) { + ((CardView) view).setCardBackgroundColor(colorStateList); + } + } + a.recycle(); + } +} +