Clicking on address copies it to the clipboard

This commit is contained in:
Felix Ableitner 2016-09-24 23:41:54 +09:00
parent f579608f6b
commit 0ccfd100d3
3 changed files with 22 additions and 7 deletions

View file

@ -107,11 +107,16 @@
<!-- Preference title --> <!-- Preference title -->
<string name="version">Version</string> <string name="version">Version</string>
<!-- IdenticonFragment -->
<!-- UserInfoFragment -->
<!-- Device address label and value --> <!-- Device address label and value -->
<string name="address_colon">Address: %1$s</string> <string name="address_colon">Address: %1$s</string>
<string name="ensichat_user_address">Ensichat User Address</string>
<string name="address_copied_to_clipboard">Address copied to clipboard</string>
<!-- ChatService --> <!-- ChatService -->

View file

@ -1,10 +1,12 @@
package com.nutomic.ensichat.fragments package com.nutomic.ensichat.fragments
import android.app.{AlertDialog, Dialog, DialogFragment} import android.app.{AlertDialog, Dialog, DialogFragment}
import android.content.{ClipData, ClipboardManager, Context}
import android.graphics.{Bitmap, Color} import android.graphics.{Bitmap, Color}
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.View.OnClickListener
import android.widget.{ImageView, TextView} import android.view.{LayoutInflater, View}
import android.widget.{ImageView, TextView, Toast}
import com.google.zxing.BarcodeFormat import com.google.zxing.BarcodeFormat
import com.google.zxing.common.BitMatrix import com.google.zxing.common.BitMatrix
import com.google.zxing.qrcode.QRCodeWriter import com.google.zxing.qrcode.QRCodeWriter
@ -23,7 +25,7 @@ object UserInfoFragment {
* *
* Use [[UserInfoFragment#getInstance]] to invoke. * Use [[UserInfoFragment#getInstance]] to invoke.
*/ */
class UserInfoFragment extends DialogFragment { class UserInfoFragment extends DialogFragment with OnClickListener {
private lazy val address = new Address(getArguments.getString(UserInfoFragment.ExtraAddress)) private lazy val address = new Address(getArguments.getString(UserInfoFragment.ExtraAddress))
private lazy val userName = getArguments.getString(UserInfoFragment.ExtraUserName) private lazy val userName = getArguments.getString(UserInfoFragment.ExtraUserName)
@ -35,9 +37,10 @@ class UserInfoFragment extends DialogFragment {
view.findViewById(R.id.identicon) view.findViewById(R.id.identicon)
.asInstanceOf[ImageView] .asInstanceOf[ImageView]
.setImageBitmap(IdenticonGenerator.generate(address, (150, 150), getActivity)) .setImageBitmap(IdenticonGenerator.generate(address, (150, 150), getActivity))
view.findViewById(R.id.address) val addressTextView = view.findViewById(R.id.address)
.asInstanceOf[TextView] .asInstanceOf[TextView]
.setText(getString(R.string.address_colon, address.toString())) addressTextView.setText(getString(R.string.address_colon, address.toString()))
addressTextView.setOnClickListener(this)
if (showQr) { if (showQr) {
val matrix = new QRCodeWriter().encode(address.toString(), BarcodeFormat.QR_CODE, 150, 150) val matrix = new QRCodeWriter().encode(address.toString(), BarcodeFormat.QR_CODE, 150, 150)
@ -53,6 +56,13 @@ class UserInfoFragment extends DialogFragment {
.create() .create()
} }
override def onClick (v: View): Unit = {
val cm = getContext.getSystemService(Context.CLIPBOARD_SERVICE).asInstanceOf[ClipboardManager]
val clip = ClipData.newPlainText(getContext.getString(R.string.ensichat_user_address), address.toString)
cm.setPrimaryClip(clip)
Toast.makeText(getContext, R.string.address_copied_to_clipboard, Toast.LENGTH_SHORT).show()
}
/** /**
* Converts a [[BitMatrix]] instance into a [[Bitmap]]. * Converts a [[BitMatrix]] instance into a [[Bitmap]].
*/ */

View file

@ -38,7 +38,7 @@ class UsersAdapter(activity: Activity) extends ArrayAdapter[User](activity, 0) w
view view
} }
override def onClick (v: View): Unit = { override def onClick(v: View): Unit = {
val user = v.getTag.asInstanceOf[User] val user = v.getTag.asInstanceOf[User]
val fragment = new UserInfoFragment() val fragment = new UserInfoFragment()
val bundle = new Bundle() val bundle = new Bundle()