From 75899594c35e0bd055cedd40f695fd6583a6b2dd Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Tue, 4 Apr 2023 19:34:00 +0200 Subject: [PATCH] ui: use real M3 themeing on TV Signed-off-by: Jason A. Donenfeld --- .../java/com/wireguard/android/Application.kt | 6 +-- .../android/databinding/BindingAdapters.kt | 13 ++++++ .../wireguard/android/widget/TvCardView.kt | 44 +++++++++++++++++++ ui/src/main/res/color/tv_list_item_tint.xml | 7 +++ ui/src/main/res/layout/tv_activity.xml | 2 - ui/src/main/res/layout/tv_file_list_item.xml | 2 - .../main/res/layout/tv_tunnel_list_item.xml | 9 ++-- ui/src/main/res/values/attrs.xml | 4 ++ .../res/values/{md_colors.xml => colors.xml} | 0 ui/src/main/res/values/styles.xml | 7 ++- ui/src/main/res/values/tv_colors.xml | 8 ---- ui/src/main/res/values/tv_styles.xml | 31 ------------- 12 files changed, 80 insertions(+), 53 deletions(-) create mode 100644 ui/src/main/java/com/wireguard/android/widget/TvCardView.kt create mode 100644 ui/src/main/res/color/tv_list_item_tint.xml rename ui/src/main/res/values/{md_colors.xml => colors.xml} (100%) delete mode 100644 ui/src/main/res/values/tv_colors.xml delete mode 100644 ui/src/main/res/values/tv_styles.xml diff --git a/ui/src/main/java/com/wireguard/android/Application.kt b/ui/src/main/java/com/wireguard/android/Application.kt index 928ee346..94fb2ca7 100644 --- a/ui/src/main/java/com/wireguard/android/Application.kt +++ b/ui/src/main/java/com/wireguard/android/Application.kt @@ -17,8 +17,6 @@ import androidx.datastore.preferences.core.PreferenceDataStoreFactory import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.preferencesDataStoreFile import com.google.android.material.color.DynamicColors -import com.google.android.material.color.DynamicColorsOptions -import com.wireguard.android.activity.TvMainActivity import com.wireguard.android.backend.Backend import com.wireguard.android.backend.GoBackend import com.wireguard.android.backend.WgQuickBackend @@ -89,9 +87,7 @@ class Application : android.app.Application() { override fun onCreate() { Log.i(TAG, USER_AGENT) super.onCreate() - DynamicColors.applyToActivitiesIfAvailable(this, - // TODO: Remove this second argument once the TV theme has a proper M3 color palette. - DynamicColorsOptions.Builder().setPrecondition { activity, _ -> activity !is TvMainActivity }.build()) + DynamicColors.applyToActivitiesIfAvailable(this) rootShell = RootShell(applicationContext) toolsInstaller = ToolsInstaller(applicationContext, rootShell) preferencesDataStore = PreferenceDataStoreFactory.create { applicationContext.preferencesDataStoreFile("settings") } diff --git a/ui/src/main/java/com/wireguard/android/databinding/BindingAdapters.kt b/ui/src/main/java/com/wireguard/android/databinding/BindingAdapters.kt index d823fa8d..fd7bc72c 100644 --- a/ui/src/main/java/com/wireguard/android/databinding/BindingAdapters.kt +++ b/ui/src/main/java/com/wireguard/android/databinding/BindingAdapters.kt @@ -23,6 +23,7 @@ import com.wireguard.android.R import com.wireguard.android.databinding.ObservableKeyedRecyclerViewAdapter.RowConfigurationHandler import com.wireguard.android.widget.ToggleSwitch import com.wireguard.android.widget.ToggleSwitch.OnBeforeCheckedChangeListener +import com.wireguard.android.widget.TvCardView import com.wireguard.config.Attribute import com.wireguard.config.InetNetwork import java.net.InetAddress @@ -168,4 +169,16 @@ object BindingAdapters { 0 } } + + @JvmStatic + @BindingAdapter("isUp") + fun setIsUp(card: TvCardView, up: Boolean) { + card.isUp = up + } + + @JvmStatic + @BindingAdapter("isDeleting") + fun setIsDeleting(card: TvCardView, deleting: Boolean) { + card.isDeleting = deleting + } } diff --git a/ui/src/main/java/com/wireguard/android/widget/TvCardView.kt b/ui/src/main/java/com/wireguard/android/widget/TvCardView.kt new file mode 100644 index 00000000..9f7d7011 --- /dev/null +++ b/ui/src/main/java/com/wireguard/android/widget/TvCardView.kt @@ -0,0 +1,44 @@ +/* + * Copyright © 2017-2023 WireGuard LLC. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package com.wireguard.android.widget + +import android.content.Context +import android.util.AttributeSet +import android.view.View +import com.google.android.material.card.MaterialCardView +import com.wireguard.android.R + +class TvCardView(context: Context?, attrs: AttributeSet?) : MaterialCardView(context, attrs) { + var isUp: Boolean = false + set(value) { + field = value + refreshDrawableState() + } + var isDeleting: Boolean = false + set(value) { + field = value + refreshDrawableState() + } + + override fun onCreateDrawableState(extraSpace: Int): IntArray { + if (isUp || isDeleting) { + val drawableState = super.onCreateDrawableState(extraSpace + (if (isUp) 1 else 0) + (if (isDeleting) 1 else 0)) + if (isUp) { + View.mergeDrawableStates(drawableState, STATE_IS_UP) + } + if (isDeleting) { + View.mergeDrawableStates(drawableState, STATE_IS_DELETING) + } + return drawableState + } + return super.onCreateDrawableState(extraSpace) + } + + companion object { + private val STATE_IS_UP = intArrayOf(R.attr.state_isUp) + private val STATE_IS_DELETING = intArrayOf(R.attr.state_isDeleting) + } +} \ No newline at end of file diff --git a/ui/src/main/res/color/tv_list_item_tint.xml b/ui/src/main/res/color/tv_list_item_tint.xml new file mode 100644 index 00000000..08e833a7 --- /dev/null +++ b/ui/src/main/res/color/tv_list_item_tint.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/ui/src/main/res/layout/tv_activity.xml b/ui/src/main/res/layout/tv_activity.xml index 8e66c4e3..2ea80e41 100644 --- a/ui/src/main/res/layout/tv_activity.xml +++ b/ui/src/main/res/layout/tv_activity.xml @@ -135,7 +135,6 @@ android:visibility="@{isDeleting ? View.GONE : View.VISIBLE}" app:icon="@{filesRoot.isEmpty ? @drawable/ic_action_add_white : @drawable/ic_arrow_back}" app:iconPadding="0dp" - app:iconTint="?attr/colorOnPrimary" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> @@ -149,7 +148,6 @@ android:visibility="@{((tunnels.isEmpty && !isDeleting) || !filesRoot.isEmpty) ? View.GONE : View.VISIBLE}" app:icon="@{isDeleting ? @drawable/ic_arrow_back : @drawable/ic_action_delete}" app:iconPadding="0dp" - app:iconTint="?attr/colorOnPrimary" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" /> diff --git a/ui/src/main/res/layout/tv_file_list_item.xml b/ui/src/main/res/layout/tv_file_list_item.xml index 3fab318a..d5afa0a4 100644 --- a/ui/src/main/res/layout/tv_file_list_item.xml +++ b/ui/src/main/res/layout/tv_file_list_item.xml @@ -21,7 +21,6 @@ android:layout_margin="8dp" android:layout_marginTop="4dp" android:layout_marginBottom="0dp" - android:backgroundTint="@color/tv_card_background" android:checkable="true" android:focusable="true" app:contentPadding="8dp"> @@ -35,7 +34,6 @@ android:layout_height="wrap_content" android:text="@{key}" android:textAppearance="?attr/textAppearanceTitleLarge" - android:textColor="?attr/colorOnPrimary" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> diff --git a/ui/src/main/res/layout/tv_tunnel_list_item.xml b/ui/src/main/res/layout/tv_tunnel_list_item.xml index 29656f2e..2d6039f1 100644 --- a/ui/src/main/res/layout/tv_tunnel_list_item.xml +++ b/ui/src/main/res/layout/tv_tunnel_list_item.xml @@ -28,16 +28,18 @@ type="com.wireguard.android.model.ObservableTunnel" /> - + app:isDeleting="@{isDeleting}" + app:isUp="@{item.state == State.UP}"> @@ -76,6 +77,6 @@ - + diff --git a/ui/src/main/res/values/attrs.xml b/ui/src/main/res/values/attrs.xml index 903ac008..9ba1eea5 100644 --- a/ui/src/main/res/values/attrs.xml +++ b/ui/src/main/res/values/attrs.xml @@ -4,4 +4,8 @@ + + + + diff --git a/ui/src/main/res/values/md_colors.xml b/ui/src/main/res/values/colors.xml similarity index 100% rename from ui/src/main/res/values/md_colors.xml rename to ui/src/main/res/values/colors.xml diff --git a/ui/src/main/res/values/styles.xml b/ui/src/main/res/values/styles.xml index 74b4e80c..ea36784b 100644 --- a/ui/src/main/res/values/styles.xml +++ b/ui/src/main/res/values/styles.xml @@ -12,7 +12,7 @@ - + + diff --git a/ui/src/main/res/values/tv_colors.xml b/ui/src/main/res/values/tv_colors.xml deleted file mode 100644 index dc4e6a01..00000000 --- a/ui/src/main/res/values/tv_colors.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - #ff212121 - #ff0059c1 - @color/tv_primary_color - #b00020 - #ffeeeeee - diff --git a/ui/src/main/res/values/tv_styles.xml b/ui/src/main/res/values/tv_styles.xml deleted file mode 100644 index 07565a72..00000000 --- a/ui/src/main/res/values/tv_styles.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - -