From d2f435b265d9fb532a824d2e29edf01257638f62 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sat, 28 Mar 2020 12:40:47 +0530 Subject: ClipboardUtils: Update to handle TextInputEditText Signed-off-by: Harsh Shandilya --- .../com/wireguard/android/util/ClipboardUtils.kt | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'ui/src/main/java/com') diff --git a/ui/src/main/java/com/wireguard/android/util/ClipboardUtils.kt b/ui/src/main/java/com/wireguard/android/util/ClipboardUtils.kt index c1ddaa4c..51f6486f 100644 --- a/ui/src/main/java/com/wireguard/android/util/ClipboardUtils.kt +++ b/ui/src/main/java/com/wireguard/android/util/ClipboardUtils.kt @@ -6,10 +6,11 @@ package com.wireguard.android.util import android.content.ClipData import android.content.ClipboardManager -import android.content.Context import android.view.View import android.widget.TextView +import androidx.core.content.getSystemService import com.google.android.material.snackbar.Snackbar +import com.google.android.material.textfield.TextInputEditText /** * Standalone utilities for interacting with the system clipboard. @@ -17,14 +18,16 @@ import com.google.android.material.snackbar.Snackbar object ClipboardUtils { @JvmStatic fun copyTextView(view: View) { - if (view !is TextView) + val data = when (view) { + is TextInputEditText -> Pair(view.editableText, view.hint) + is TextView -> Pair(view.text, view.contentDescription) + else -> return + } + if (data.first == null || data.first.isEmpty()) { return - val text = view.text - if (text == null || text.length == 0) return - val service = view.getContext().getSystemService(Context.CLIPBOARD_SERVICE) as? ClipboardManager - ?: return - val description = view.getContentDescription() - service.setPrimaryClip(ClipData.newPlainText(description, text)) - Snackbar.make(view, "$description copied to clipboard", Snackbar.LENGTH_LONG).show() + } + val service = view.context.getSystemService() ?: return + service.setPrimaryClip(ClipData.newPlainText(data.second, data.first)) + Snackbar.make(view, "${data.second} copied to clipboard", Snackbar.LENGTH_LONG).show() } } -- cgit v1.2.3