taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

UriInputFragment.kt (3253B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2020 Taler Systems S.A.
      4  *
      5  * GNU Taler is free software; you can redistribute it and/or modify it under the
      6  * terms of the GNU General Public License as published by the Free Software
      7  * Foundation; either version 3, or (at your option) any later version.
      8  *
      9  * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License along with
     14  * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 
     17 package net.taler.wallet
     18 
     19 import android.content.ClipboardManager
     20 import android.os.Bundle
     21 import android.view.LayoutInflater
     22 import android.view.View
     23 import android.view.ViewGroup
     24 import android.widget.Toast
     25 import android.widget.Toast.LENGTH_LONG
     26 import androidx.core.content.getSystemService
     27 import androidx.core.os.bundleOf
     28 import androidx.core.view.ViewCompat
     29 import androidx.core.view.WindowInsetsCompat
     30 import androidx.core.view.updatePadding
     31 import androidx.fragment.app.Fragment
     32 import androidx.navigation.fragment.findNavController
     33 import net.taler.wallet.databinding.FragmentUriInputBinding
     34 
     35 class UriInputFragment : Fragment() {
     36 
     37     private lateinit var ui: FragmentUriInputBinding
     38 
     39     override fun onCreateView(
     40         inflater: LayoutInflater,
     41         container: ViewGroup?,
     42         savedInstanceState: Bundle?
     43     ): View {
     44         ui = FragmentUriInputBinding.inflate(inflater, container, false)
     45         return ui.root
     46     }
     47 
     48     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     49         setupInsets()
     50 
     51         val clipboard = requireContext().getSystemService<ClipboardManager>()
     52 
     53         ui.pasteButton.setOnClickListener {
     54             val item = clipboard?.primaryClip?.getItemAt(0)
     55             if (item?.text != null) {
     56                 ui.uriView.setText(item.text)
     57             } else {
     58                 if (item?.uri != null) {
     59                     ui.uriView.setText(item.uri.toString())
     60                 } else {
     61                     Toast.makeText(requireContext(), R.string.paste_invalid, LENGTH_LONG).show()
     62                 }
     63             }
     64         }
     65         ui.okButton.setOnClickListener {
     66             val trimmedText = ui.uriView.text?.trim()
     67             if (trimmedText?.startsWith("taler://", ignoreCase = true) == true ||
     68                 trimmedText?.startsWith("payto://", ignoreCase = true) == true) {
     69                 ui.uriLayout.error = null
     70                 val args = bundleOf("uri" to trimmedText.toString(), "from" to "URI input")
     71                 findNavController().navigate(R.id.action_global_handleUri, args)
     72             } else {
     73                 ui.uriLayout.error = getString(R.string.uri_invalid)
     74             }
     75         }
     76     }
     77 
     78     private fun setupInsets() {
     79         ViewCompat.setOnApplyWindowInsetsListener(ui.root) { v, windowInsets ->
     80             val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
     81             v.updatePadding(left = insets.left, right = insets.right, bottom = insets.bottom)
     82             WindowInsetsCompat.CONSUMED
     83         }
     84     }
     85 }