diff options
3 files changed, 70 insertions, 1 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/CopyableInformationView.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/CopyableInformationView.kt new file mode 100644 index 0000000000..e3ceb47d5a --- /dev/null +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/CopyableInformationView.kt @@ -0,0 +1,65 @@ +package net.mullvad.mullvadvpn.ui + +import android.content.ClipData +import android.content.ClipboardManager +import android.content.Context +import android.util.AttributeSet +import android.widget.Toast +import net.mullvad.mullvadvpn.R + +class CopyableInformationView : InformationView { + var clipboardLabel: String? = null + set(value) { + field = value + super.setEnabled(value != null) + } + + var copiedToast: String? = null + + constructor(context: Context) : super(context) {} + + constructor(context: Context, attributes: AttributeSet) : super(context, attributes) { + loadAttributes(attributes) + } + + constructor(context: Context, attributes: AttributeSet, defaultStyleAttribute: Int) : + super(context, attributes, defaultStyleAttribute) { + loadAttributes(attributes) + } + + constructor( + context: Context, + attributes: AttributeSet, + defaultStyleAttribute: Int, + defaultStyleResource: Int + ) : super(context, attributes, defaultStyleAttribute, defaultStyleResource) { + loadAttributes(attributes) + } + + init { + onClick = { copyToClipboard() } + } + + private fun loadAttributes(attributes: AttributeSet) { + val styleableId = R.styleable.CopyableInformationView + + context.theme.obtainStyledAttributes(attributes, styleableId, 0, 0).apply { + try { + clipboardLabel = getString(R.styleable.CopyableInformationView_clipboardLabel) + copiedToast = getString(R.styleable.CopyableInformationView_copiedToast) + } finally { + recycle() + } + } + } + + private fun copyToClipboard() { + val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager + val clipData = ClipData.newPlainText(clipboardLabel, information) + val toastMessage = copiedToast ?: context.getString(R.string.copied_to_clipboard) + + clipboard.primaryClip = clipData + + Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show() + } +} diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/InformationView.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/InformationView.kt index 32721421bf..45d5c78ba1 100644 --- a/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/InformationView.kt +++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/ui/InformationView.kt @@ -8,7 +8,7 @@ import android.widget.LinearLayout import android.widget.TextView import net.mullvad.mullvadvpn.R -class InformationView : LinearLayout { +open class InformationView : LinearLayout { private val container = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE).let { service -> val inflater = service as LayoutInflater diff --git a/android/src/main/res/values/attrs.xml b/android/src/main/res/values/attrs.xml index 62c7d275b7..d8406f4c61 100644 --- a/android/src/main/res/values/attrs.xml +++ b/android/src/main/res/values/attrs.xml @@ -1,4 +1,8 @@ <resources> + <declare-styleable name="CopyableInformationView"> + <attr name="clipboardLabel" format="reference|string"/> + <attr name="copiedToast" format="reference|string"/> + </declare-styleable> <declare-styleable name="InformationView"> <attr name="description" format="reference|string"/> </declare-styleable> |
