From 94f4eaca0f9d01ad9f83d9748d91b8908ad39641 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Tue, 5 Feb 2019 20:40:29 -0600 Subject: [PATCH] Add the share icon in the eReceipt toolbar, which when tapped creates a screenshot of the eReceipt and sends an intent to the system so it shows all the possible options for the user to share the image. --- .../fragments/EReceiptFragment.kt | 76 ++++++++++++++++++- app/src/main/res/menu/menu_e_receipt.xml | 11 +++ 2 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 app/src/main/res/menu/menu_e_receipt.xml diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/EReceiptFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/EReceiptFragment.kt index 26bc73a..a8f31f9 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/EReceiptFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/EReceiptFragment.kt @@ -1,12 +1,14 @@ package cy.agorise.bitsybitshareswallet.fragments +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager import android.os.Bundle import android.preference.PreferenceManager import android.text.Html import android.text.method.LinkMovementMethod -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup +import android.view.* +import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProviders @@ -15,6 +17,8 @@ import androidx.navigation.fragment.navArgs import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.database.joins.TransferDetail import cy.agorise.bitsybitshareswallet.utils.Constants +import cy.agorise.bitsybitshareswallet.utils.Helper +import cy.agorise.bitsybitshareswallet.utils.toast import cy.agorise.bitsybitshareswallet.viewmodels.EReceiptViewModel import kotlinx.android.synthetic.main.fragment_e_receipt.* import java.math.RoundingMode @@ -25,6 +29,10 @@ import java.util.* class EReceiptFragment : Fragment() { + companion object { + private const val REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION = 100 + } + private val args: EReceiptFragmentArgs by navArgs() private lateinit var mEReceiptViewModel: EReceiptViewModel @@ -97,4 +105,66 @@ class EReceiptFragment : Fragment() { tvTransferID.text = tx tvTransferID.movementMethod = LinkMovementMethod.getInstance() } + + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + inflater.inflate(R.menu.menu_e_receipt, menu) + } + + override fun onOptionsItemSelected(item: MenuItem?): Boolean { + if (item?.itemId == R.id.menu_share) { + verifyStoragePermission() + return true + } + return super.onOptionsItemSelected(item) + } + + /** Verifies if the storage permission is already granted, if that is the case then it takes the screenshot and + * shares it but if it is not then it asks the user for that permission */ + private fun verifyStoragePermission() { + if (ContextCompat.checkSelfPermission(activity!!, Manifest.permission.WRITE_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED) { + // Permission is not already granted + requestPermissions(arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), + REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION) + } else { + // Permission is already granted + shareEReceiptScreenshot() + } + } + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + + if (requestCode == REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION) { + if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { + shareEReceiptScreenshot() + } else { + context?.toast(getString(R.string.msg__storage_permission_necessary_share)) + } + return + } + } + + /** Takes a screenshot as a bitmap (hiding the tx hyperlink), saves it into a temporal cache image and then + * sends an intent so the user can select the desired method to share the image. */ + private fun shareEReceiptScreenshot() { + // Get Screenshot + tvTransferID.text = getString(R.string.template__tx, args.transferId) + val screenshot = Helper.loadBitmapFromView(container) + formatTransferTextView(args.transferId) + val imageUri = context?.let { Helper.saveTemporalBitmap(it, screenshot) } + + // Prepare information for share intent + val subject = "${getString(R.string.app_name)} ${getString(R.string.title_e_receipt)}" + + // Create share intent and call it + val shareIntent = Intent() + shareIntent.action = Intent.ACTION_SEND + shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) // temp permission for receiving app to read this file + shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri) + shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject) + shareIntent.putExtra(Intent.EXTRA_TEXT, subject) + shareIntent.type = "*/*" + startActivity(Intent.createChooser(shareIntent, getString(R.string.text__share_with))) + } } \ No newline at end of file diff --git a/app/src/main/res/menu/menu_e_receipt.xml b/app/src/main/res/menu/menu_e_receipt.xml new file mode 100644 index 0000000..5888d65 --- /dev/null +++ b/app/src/main/res/menu/menu_e_receipt.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file