Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 566215bd authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

UI: Port more to Material 3

This also activates Material 3 (with fixed colors) for older Android versions.
We don't want to maintain multiple designs, so the most reasonable approach is
to use a single design for all, but focus on the best UX for more recent
versions (as those are used by more people).
parent 38fb4b9c
Loading
Loading
Loading
Loading
+27 −16
Original line number Diff line number Diff line
@@ -6,22 +6,31 @@
package org.microg.gms.ui

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import androidx.appcompat.widget.SwitchCompat
import androidx.core.content.res.TypedArrayUtils
import androidx.preference.PreferenceViewHolder
import androidx.preference.TwoStatePreference
import org.microg.gms.base.core.R

// TODO
class SwitchBarPreference : TwoStatePreference {
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context) : super(context)
    private val frameId: Int
    private val backgroundOn: Drawable?
    private val backgroundOff: Drawable?
    private val backgroundDisabled: Drawable?

    init {
        layoutResource = R.layout.preference_switch_bar
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
        val a = context.obtainStyledAttributes(attrs, R.styleable.SwitchBarPreference, defStyleAttr, defStyleRes)
        frameId = a.getResourceId(R.styleable.SwitchBarPreference_switchBarFrameId, 0)
        backgroundOn = a.getDrawable(R.styleable.SwitchBarPreference_switchBarFrameBackgroundOn)
        backgroundOff = a.getDrawable(R.styleable.SwitchBarPreference_switchBarFrameBackgroundOff)
        backgroundDisabled = a.getDrawable(R.styleable.SwitchBarPreference_switchBarFrameBackgroundDisabled)
    }
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : this(context, attrs, defStyleAttr, R.style.Preference_SwitchBar)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, R.attr.switchBarPreferenceStyle)
    constructor(context: Context) : this(context, null)

    override fun onBindViewHolder(holder: PreferenceViewHolder) {
        super.onBindViewHolder(holder)
@@ -37,15 +46,17 @@ class SwitchBarPreference : TwoStatePreference {
            }
            this.isChecked = isChecked
        }
        holder.itemView.setBackgroundColorAttribute(when {
            isChecked -> androidx.appcompat.R.attr.colorControlActivated
            isEnabled -> androidx.appcompat.R.attr.colorButtonNormal
            else -> androidx.appcompat.R.attr.colorControlHighlight
        })
        val frame = if (frameId == 0) null else holder.findViewById(frameId)
        val backgroundView = frame ?: holder.itemView
        val (backgroundDrawable, backgroundColorAttribute) = when {
            !isEnabled -> Pair(backgroundDisabled, androidx.appcompat.R.attr.colorControlHighlight)
            isChecked -> Pair(backgroundOn, androidx.appcompat.R.attr.colorControlActivated)
            else -> Pair(backgroundOff, androidx.appcompat.R.attr.colorButtonNormal)
        }
        if (backgroundDrawable != null) {
            backgroundView.setBackgroundDrawable(backgroundDrawable)
        } else {
            backgroundView.setBackgroundColorAttribute(backgroundColorAttribute)
        }
    }

@Deprecated("Get rid")
interface PreferenceSwitchBarCallback {
    fun onChecked(newStatus: Boolean)
}
 No newline at end of file
+0 −1
Original line number Diff line number Diff line
@@ -8,7 +8,6 @@
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:paddingStart="?attr/listPreferredItemPaddingStart"
    android:paddingLeft="?attr/listPreferredItemPaddingLeft"
    android:paddingTop="24dp"
+18 −0
Original line number Diff line number Diff line
@@ -31,4 +31,22 @@
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
    </style>

    <declare-styleable name="PreferenceTheme">
        <attr format="reference" name="switchBarPreferenceStyle"/>
    </declare-styleable>

    <declare-styleable name="SwitchBarPreference">
        <attr format="reference" name="switchBarFrameId" />
        <attr format="reference" name="switchBarFrameBackgroundOn" />
        <attr format="reference" name="switchBarFrameBackgroundOff" />
        <attr format="reference" name="switchBarFrameBackgroundDisabled" />
    </declare-styleable>

    <style name="Preference.SwitchBar">
        <item name="layout">@layout/preference_switch_bar</item>
        <item name="switchBarFrameBackgroundOn">?attr/colorControlActivated</item>
        <item name="switchBarFrameBackgroundOff">?attr/colorButtonNormal</item>
        <item name="switchBarFrameBackgroundDisabled">?attr/colorControlHighlight</item>
    </style>
</resources>
+6 −2
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.android.gms.R;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import org.microg.gms.ui.settings.SettingsProvider;

import static org.microg.gms.ui.settings.SettingsProviderKt.getAllSettingsProviders;
@@ -33,14 +34,17 @@ public class MainSettingsActivity extends AppCompatActivity {
        }

        setContentView(R.layout.settings_root_activity);
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));

        Toolbar toolbar = findViewById(R.id.toolbar);
        CollapsingToolbarLayout toolbarLayout = findViewById(R.id.collapsing_toolbar);
        setSupportActionBar(toolbar);

        for (SettingsProvider settingsProvider : getAllSettingsProviders(this)) {
            settingsProvider.extendNavigation(getNavController());
        }

        appBarConfiguration = new AppBarConfiguration.Builder(getNavController().getGraph()).build();
        NavigationUI.setupActionBarWithNavController(this, getNavController(), appBarConfiguration);
        NavigationUI.setupWithNavController(toolbarLayout, toolbar, getNavController(), appBarConfiguration);
    }

    @Override
+1 −1
Original line number Diff line number Diff line
@@ -78,7 +78,7 @@ class SafetyNetFragment : PreferenceFragmentCompat() {
            val newStatus = newValue as Boolean
            SafetyNetPreferences.setEnabled(requireContext(), newStatus)
            DroidGuardPreferences.setEnabled(requireContext(), newStatus)
            droidguardUnsupported.isVisible = switchBarPreference.isChecked && !DroidGuardPreferences.isAvailable(requireContext())
            droidguardUnsupported.isVisible = newStatus && !DroidGuardPreferences.isAvailable(requireContext())
            true
        }
    }
Loading