summaryrefslogtreecommitdiffhomepage
path: root/android/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'android/src/main')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/MtuCell.kt91
-rw-r--r--android/src/main/res/layout/mtu_edit_text.xml13
-rw-r--r--android/src/main/res/values/dimensions.xml2
3 files changed, 106 insertions, 0 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/MtuCell.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/MtuCell.kt
new file mode 100644
index 0000000000..eb1463cd45
--- /dev/null
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/widget/MtuCell.kt
@@ -0,0 +1,91 @@
+package net.mullvad.mullvadvpn.ui.widget
+
+import android.content.Context
+import android.text.Editable
+import android.text.TextWatcher
+import android.util.AttributeSet
+import android.view.LayoutInflater
+import android.widget.EditText
+import android.widget.TextView
+import kotlin.properties.Delegates.observable
+import net.mullvad.mullvadvpn.R
+
+private const val MIN_MTU_VALUE = 1280
+private const val MAX_MTU_VALUE = 1420
+
+class MtuCell : Cell {
+ private val input =
+ (LayoutInflater.from(context).inflate(R.layout.mtu_edit_text, null) as EditText).apply {
+ val width = resources.getDimensionPixelSize(R.dimen.cell_input_width)
+ val height = resources.getDimensionPixelSize(R.dimen.cell_input_height)
+
+ layoutParams = LayoutParams(width, height, 0.0f)
+
+ addTextChangedListener(InputWatcher())
+ setOnFocusChangeListener { _, newHasFocus -> hasFocus = newHasFocus }
+ }
+
+ private val validInputColor = context.getColor(R.color.white)
+ private val invalidInputColor = context.getColor(R.color.red)
+
+ var value: Int?
+ get() = input.text.toString().trim().toIntOrNull()
+ set(value) = input.setText(value?.toString() ?: "")
+
+ var onSubmit: ((Int?) -> Unit)? = null
+
+ var hasFocus by observable(false) { _, oldValue, newValue ->
+ if (oldValue == true && newValue == false) {
+ val mtu = value
+
+ if (mtu == null || (mtu >= MIN_MTU_VALUE && mtu <= MAX_MTU_VALUE)) {
+ onSubmit?.invoke(mtu)
+ }
+ }
+ }
+
+ constructor(context: Context) : super(context, TextView(context)) {}
+
+ constructor(context: Context, attributes: AttributeSet) :
+ super(context, attributes, TextView(context)) {}
+
+ constructor(context: Context, attributes: AttributeSet, defaultStyleAttribute: Int) :
+ super(context, attributes, defaultStyleAttribute, TextView(context)) {}
+
+ constructor(
+ context: Context,
+ attributes: AttributeSet,
+ defaultStyleAttribute: Int,
+ defaultStyleResource: Int
+ ) : super(
+ context,
+ attributes,
+ defaultStyleAttribute,
+ defaultStyleResource,
+ TextView(context)
+ ) {}
+
+ init {
+ cell.setEnabled(false)
+ cell.addView(input)
+
+ footer?.text =
+ context.getString(R.string.wireguard_mtu_footer, MIN_MTU_VALUE, MAX_MTU_VALUE)
+ }
+
+ inner class InputWatcher : TextWatcher {
+ override fun beforeTextChanged(text: CharSequence, start: Int, count: Int, after: Int) {}
+
+ override fun onTextChanged(text: CharSequence, start: Int, count: Int, after: Int) {}
+
+ override fun afterTextChanged(text: Editable) {
+ val value = text.toString().trim().toIntOrNull()
+
+ if (value != null && value >= MIN_MTU_VALUE && value <= MAX_MTU_VALUE) {
+ input.setTextColor(validInputColor)
+ } else {
+ input.setTextColor(invalidInputColor)
+ }
+ }
+ }
+}
diff --git a/android/src/main/res/layout/mtu_edit_text.xml b/android/src/main/res/layout/mtu_edit_text.xml
new file mode 100644
index 0000000000..05fce42e7d
--- /dev/null
+++ b/android/src/main/res/layout/mtu_edit_text.xml
@@ -0,0 +1,13 @@
+<EditText xmlns:android="http://schemas.android.com/apk/res/android"
+ android:paddingHorizontal="4dp"
+ android:background="@drawable/cell_input_background"
+ android:digits="0123456789"
+ android:inputType="number"
+ android:singleLine="true"
+ android:imeOptions="flagNoPersonalizedLearning"
+ android:textCursorDrawable="@drawable/cell_input_cursor"
+ android:gravity="center"
+ android:hint="@string/hint_default"
+ android:textColorHint="@color/white80"
+ android:textColor="@color/white"
+ android:textSize="20sp" />
diff --git a/android/src/main/res/values/dimensions.xml b/android/src/main/res/values/dimensions.xml
index 2e60d27cec..c4ebc722ad 100644
--- a/android/src/main/res/values/dimensions.xml
+++ b/android/src/main/res/values/dimensions.xml
@@ -18,6 +18,8 @@
<dimen name="cell_horizontal_padding">16dp</dimen>
<dimen name="cell_label_horizontal_padding">8dp</dimen>
<dimen name="cell_label_vertical_padding">17dp</dimen>
+ <dimen name="cell_input_width">80dp</dimen>
+ <dimen name="cell_input_height">34dp</dimen>
<dimen name="cell_footer_top_padding">8dp</dimen>
<dimen name="cell_footer_horizontal_padding">24dp</dimen>
<dimen name="chevron_width">14dp</dimen>