MonkeyedTextInputEditText: introduce a new horror

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2020-03-28 13:50:11 -06:00
parent fb3fec299f
commit fe6b788f6b
4 changed files with 48 additions and 13 deletions

View File

@ -0,0 +1,30 @@
/*
* Copyright © 2020 WireGuard LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.wireguard.android.widget
import android.content.Context
import android.text.Editable
import android.text.SpannableStringBuilder
import android.util.AttributeSet
import com.google.android.material.R
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
class MonkeyedTextInputEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = R.attr.editTextStyle) : TextInputEditText(context, attrs, defStyleAttr) {
@Override
override fun getText(): Editable? {
val text = super.getText()
if (!text.isNullOrEmpty())
return text
/* We want this expression in TextInputLayout.java to be true:
* final boolean hasText = editText != null && !TextUtils.isEmpty(editText.getText());
* But for everyone else it should return the real value, so we check the caller.
*/
if (Thread.currentThread().stackTrace[3].className == TextInputLayout::class.qualifiedName)
return SpannableStringBuilder(" ")
return text
}
}

View File

@ -126,6 +126,7 @@
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/public_key_label_layout"
style="@style/TextInputLayoutBase"
android:hint="@string/public_key"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
@ -133,14 +134,14 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/private_key_text_layout">
<com.google.android.material.textfield.TextInputEditText
<com.wireguard.android.widget.MonkeyedTextInputEditText
android:id="@+id/public_key_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_generated"
android:editable="false"
android:ellipsize="end"
android:focusable="false"
android:hint="@string/public_key"
android:onClick="@{ClipboardUtils::copyTextView}"
android:singleLine="true"
android:text="@{config.interface.publicKey}" />
@ -170,6 +171,7 @@
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/listen_port_label_layout"
style="@style/TextInputLayoutBase"
android:hint="@string/listen_port"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
@ -178,10 +180,10 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/addresses_label_layout">
<com.google.android.material.textfield.TextInputEditText
<com.wireguard.android.widget.MonkeyedTextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/listen_port"
android:hint="@string/hint_random"
android:inputType="number"
android:text="@={config.interface.listenPort}"
android:textAlignment="center" />
@ -195,7 +197,7 @@
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="0.8"
app:layout_constraintHorizontal_weight="0.7"
app:layout_constraintEnd_toStartOf="@id/mtu_label_layout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/addresses_label_layout">
@ -215,17 +217,18 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
app:layout_constraintHorizontal_weight="0.2"
android:hint="@string/mtu"
app:layout_constraintHorizontal_weight="0.3"
app:layout_constraintStart_toEndOf="@id/dns_servers_label_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/dns_servers_label_layout">
<com.google.android.material.textfield.TextInputEditText
<com.wireguard.android.widget.MonkeyedTextInputEditText
android:id="@+id/mtu_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/mtu"
android:inputType="number"
android:hint="@string/hint_automatic"
android:text="@={config.interface.mtu}"
android:textAlignment="center" />
</com.google.android.material.textfield.TextInputLayout>

View File

@ -83,15 +83,16 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="@string/pre_shared_key"
app:layout_constraintTop_toBottomOf="@+id/public_key_label_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<com.google.android.material.textfield.TextInputEditText
<com.wireguard.android.widget.MonkeyedTextInputEditText
android:id="@+id/pre_shared_key_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/pre_shared_key"
android:hint="@string/hint_optional"
android:inputType="textNoSuggestions|textVisiblePassword"
android:text="@={item.preSharedKey}"/>
</com.google.android.material.textfield.TextInputLayout>
@ -102,15 +103,16 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:hint="@string/persistent_keepalive"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/pre_shared_key_label_layout"
app:layout_constraintEnd_toEndOf="parent">
<com.google.android.material.textfield.TextInputEditText
<com.wireguard.android.widget.MonkeyedTextInputEditText
android:id="@+id/persistent_keepalive_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/persistent_keepalive"
android:hint="@string/hint_optional"
android:inputType="number"
android:text="@={item.persistentKeepalive}" />
</com.google.android.material.textfield.TextInputLayout>

View File

@ -55,7 +55,7 @@
<style name="DetailText" parent="TextAppearance.MaterialComponents.Body1" />
<style name="TextInputLayoutBase" parent="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<style name="TextInputLayoutBase" parent="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">?attr/colorSecondary</item>
<item name="hintTextColor">?attr/colorOnPrimary</item>
</style>