|
| 1 | +package com.htc.zion.demoapp |
| 2 | + |
| 3 | +import android.app.Activity.RESULT_OK |
| 4 | +import android.content.Context |
| 5 | +import android.content.Intent |
| 6 | +import android.os.Bundle |
| 7 | +import android.util.Log |
| 8 | +import android.view.LayoutInflater |
| 9 | +import android.view.View |
| 10 | +import android.view.ViewGroup |
| 11 | +import androidx.fragment.app.Fragment |
| 12 | +import com.google.android.material.snackbar.Snackbar |
| 13 | +import com.google.gson.Gson |
| 14 | +import com.htc.htcwalletsdk.Native.Type.ByteArrayHolder |
| 15 | +import com.htc.zion.demoapp.Utils.Companion.LOG_TAG |
| 16 | +import com.htc.zion.demoapp.Utils.Companion.convertToHex |
| 17 | +import com.htc.zion.demoapp.Utils.Companion.sha256 |
| 18 | +import com.htc.zion.demoapp.data.EthereumJsonTemplate |
| 19 | +import com.htc.zion.demoapp.data.Message |
| 20 | +import com.htc.zion.demoapp.databinding.FragmentFirstBinding |
| 21 | +import kotlinx.android.synthetic.main.fragment_first.* |
| 22 | +import java.util.concurrent.Executors |
| 23 | + |
| 24 | +/** |
| 25 | + * A simple [Fragment] subclass as the default destination in the navigation. |
| 26 | + */ |
| 27 | +class FirstFragment : Fragment() { |
| 28 | + val SELECT_PHOTO = 1 |
| 29 | + val COIN_TYPE_ETHEREUM = 60 |
| 30 | + |
| 31 | + private val tzExecutor by lazy { |
| 32 | + Executors.newSingleThreadExecutor() |
| 33 | + } |
| 34 | + |
| 35 | + override fun onCreateView( |
| 36 | + inflater: LayoutInflater, |
| 37 | + container: ViewGroup?, |
| 38 | + savedInstanceState: Bundle? |
| 39 | + ): View? { |
| 40 | + // Inflate the layout for this fragment |
| 41 | + val binding = FragmentFirstBinding.inflate(inflater, container, false) |
| 42 | + return binding.root |
| 43 | + } |
| 44 | + |
| 45 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 46 | + super.onCreate(savedInstanceState) |
| 47 | + initZkmaSdk(this.requireContext()) |
| 48 | + } |
| 49 | + |
| 50 | + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| 51 | + super.onViewCreated(view, savedInstanceState) |
| 52 | + |
| 53 | + btn_create_seed.setOnClickListener { |
| 54 | + tzExecutor.execute { |
| 55 | + val uniqueId = Utils.getZkmaSdkUniqueId(view.context) |
| 56 | + if (ZkmaSdkHelper.isSeedExists(uniqueId)) { |
| 57 | + activity?.runOnUiThread { |
| 58 | + Snackbar.make(view, R.string.msg_seed_exists, Snackbar.LENGTH_LONG) |
| 59 | + .setAction("Action", null).show() |
| 60 | + } |
| 61 | + } else { |
| 62 | + val result = ZkmaSdkHelper.createSeed(uniqueId) |
| 63 | + Log.i(LOG_TAG, "createSeed(), result=$result") |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + btn_restore_seed.setOnClickListener { |
| 69 | + tzExecutor.execute { |
| 70 | + val uniqueId = Utils.getZkmaSdkUniqueId(view.context) |
| 71 | + if (ZkmaSdkHelper.isSeedExists(uniqueId)) { |
| 72 | + activity?.runOnUiThread { |
| 73 | + Snackbar.make(view, R.string.msg_seed_exists, Snackbar.LENGTH_LONG) |
| 74 | + .setAction("Action", null).show() |
| 75 | + } |
| 76 | + } else { |
| 77 | + val result = ZkmaSdkHelper.restoreSeed(uniqueId) |
| 78 | + Log.i(LOG_TAG, "restoreSeed(), result=$result") |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + btn_sign_photo.setOnClickListener { |
| 84 | + val intent = Intent( |
| 85 | + Intent.ACTION_PICK, |
| 86 | + android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI |
| 87 | + ) |
| 88 | + intent.type = "image/*" |
| 89 | + startActivityForResult(intent, SELECT_PHOTO) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { |
| 94 | + super.onActivityResult(requestCode, resultCode, data) |
| 95 | + |
| 96 | + if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK) { |
| 97 | + data?.data?.let { uri -> |
| 98 | + tzExecutor.execute { |
| 99 | + Utils.loadImage(this.requireContext(), uri)?.let { bitmap -> |
| 100 | + bitmap.convertToHex() |
| 101 | + }?.let { bitmapHex -> |
| 102 | + bitmapHex.sha256() |
| 103 | + }?.let { bitmapHash -> |
| 104 | + val photoData = EthereumJsonTemplate(Message(bitmapHash)) |
| 105 | + Log.i(LOG_TAG, "EthereumJson=$photoData") |
| 106 | + val sig = ByteArrayHolder() |
| 107 | + val result = ZkmaSdkHelper.signMessage( |
| 108 | + Utils.getZkmaSdkUniqueId(this.requireContext()), |
| 109 | + COIN_TYPE_ETHEREUM, |
| 110 | + Gson().toJson(photoData), |
| 111 | + sig |
| 112 | + ) |
| 113 | + Log.i( |
| 114 | + LOG_TAG, |
| 115 | + "signMessage(), result=$result, sig=${sig.byteArray.toString()}" |
| 116 | + ) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private fun initZkmaSdk(context: Context) { |
| 124 | + tzExecutor.execute { |
| 125 | + ZkmaSdkHelper.initSdk(context) |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments