summaryrefslogtreecommitdiffhomepage
path: root/android/src/main/kotlin
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-08-02 20:29:57 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2019-08-06 13:53:14 +0000
commitdbd2071458ca24ecbe401cb19e8362f1edb4a948 (patch)
tree4b223343a88550ccfb8d1b19a565bf5e2348c60c /android/src/main/kotlin
parent00e6702f059d06c60f015993e44a0eca304b248e (diff)
downloadmullvadvpn-dbd2071458ca24ecbe401cb19e8362f1edb4a948.tar.xz
mullvadvpn-dbd2071458ca24ecbe401cb19e8362f1edb4a948.zip
Allow "Switch location" button to grow taller
Diffstat (limited to 'android/src/main/kotlin')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt2
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt41
2 files changed, 41 insertions, 2 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt
index a0c94873f4..09aadb0a7a 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ConnectFragment.kt
@@ -86,7 +86,7 @@ class ConnectFragment : Fragment() {
onDisconnect = { connectionProxy.disconnect() }
}
- switchLocationButton = SwitchLocationButton(view)
+ switchLocationButton = SwitchLocationButton(view, resources)
switchLocationButton.onClick = { openSwitchLocationScreen() }
updateKeyStatusJob = updateKeyStatus(keyStatusListener.keyStatus)
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt
index 4d3dae8159..f86fac6768 100644
--- a/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/SwitchLocationButton.kt
@@ -5,18 +5,26 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Job
+import android.content.res.Resources
import android.graphics.drawable.Drawable
+import android.text.TextUtils.TruncateAt
import android.view.View
+import android.view.ViewGroup.MarginLayoutParams
import android.widget.Button
import net.mullvad.mullvadvpn.model.ActionAfterDisconnect
import net.mullvad.mullvadvpn.model.TunnelState
import net.mullvad.mullvadvpn.relaylist.RelayItem
-class SwitchLocationButton(val parentView: View) {
+class SwitchLocationButton(val parentView: View, val resources: Resources) {
private val button: Button = parentView.findViewById(R.id.switch_location)
private val chevron: Drawable = button.compoundDrawables[2]
+ private val normalButtonHeight = resources.getDimensionPixelSize(R.dimen.normal_button_height)
+ private val tallButtonHeight = resources.getDimensionPixelSize(R.dimen.tall_button_height)
+ private val topMargin = tallButtonHeight - normalButtonHeight
+
+ private var tall = false
private var updateJob: Job? = null
var location: RelayItem? = null
@@ -35,6 +43,7 @@ class SwitchLocationButton(val parentView: View) {
init {
button.setOnClickListener { onClick?.invoke() }
+ button.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> resizeIfNecessary() }
}
fun onDestroy() {
@@ -65,6 +74,7 @@ class SwitchLocationButton(val parentView: View) {
private fun showLabel() {
button.setText(R.string.switch_location)
button.setCompoundDrawables(null, null, null, null)
+ resizeIfNecessary()
}
private fun showLocation() {
@@ -75,6 +85,35 @@ class SwitchLocationButton(val parentView: View) {
} else {
button.setText(locationName)
button.setCompoundDrawables(null, null, chevron, null)
+ resizeIfNecessary()
+ }
+ }
+
+ private fun resizeIfNecessary() {
+ val layoutParams = button.layoutParams
+
+ if (button.lineCount > 1 && !tall) {
+ tall = true
+
+ if (layoutParams is MarginLayoutParams) {
+ layoutParams.height = tallButtonHeight
+ layoutParams.topMargin = 0
+ }
+
+ button.maxLines = 2
+ button.ellipsize = TruncateAt.END
+ button.requestLayout()
+ } else if (button.lineCount <= 1 && tall) {
+ tall = false
+
+ if (layoutParams is MarginLayoutParams) {
+ layoutParams.height = normalButtonHeight
+ layoutParams.topMargin = topMargin
+ }
+
+ button.maxLines = -1
+ button.ellipsize = null
+ button.requestLayout()
}
}
}