diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4473c28..46dfa00 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -11,6 +11,8 @@ + + , grantResults: IntArray) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + + if (requestCode == REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION) { + if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { + shareQRScreenshot() + } else { + // TODO extract string resource + Toast.makeText(context!!, "Storage permission is necessary to share QR codes.", Toast.LENGTH_SHORT).show() + } + return + } + } + + /** + * This function takes a screenshot as a bitmap, 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 shareQRScreenshot() { + // Get Screenshot + val screenshot = Helper.loadBitmapFromView(container) + val imageUri = Helper.saveTemporalBitmap(context!!, screenshot) + + // Prepare information for share intent + val subject = getString(R.string.msg__invoice_subject, mUserAccount?.name) + val content = tvPleasePay.text.toString() + "\n" + + tvTo.text.toString() + + // 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, content) + shareIntent.type = "*/*" + startActivity(Intent.createChooser(shareIntent, getString(R.string.text__share_with))) + } + override fun onResume() { super.onResume() diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Helper.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Helper.kt new file mode 100644 index 0000000..bff3aef --- /dev/null +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Helper.kt @@ -0,0 +1,60 @@ +package cy.agorise.bitsybitshareswallet.utils + +import android.content.Context +import android.graphics.Bitmap +import android.graphics.Canvas +import android.net.Uri +import android.util.Log +import android.view.View +import androidx.core.content.FileProvider +import java.io.File +import java.io.FileOutputStream +import java.io.IOException + +/** + * Contains methods that are helpful in different parts of the app + */ +class Helper { + + companion object { + private val TAG = "Helper" + + /** + * Creates and returns a Bitmap from the contents of a View, does not matter + * if it is a simple view or a ViewGroup like a ConstraintLayout or a LinearLayout. + * + * @param view The view that is gonna be pictured. + * @return The generated image from the given view. + */ + fun loadBitmapFromView(view: View): Bitmap { + val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888) + val canvas = Canvas(bitmap) + view.draw(canvas) + + return bitmap + } + + fun saveTemporalBitmap(context: Context, bitmap: Bitmap): Uri { + // save bitmap to cache directory + try { + val cachePath = File(context.cacheDir, "images") + if (!cachePath.mkdirs()) + // don't forget to make the directory + Log.d(TAG, "shareBitmapImage creating cache images folder") + + val stream = FileOutputStream(cachePath.toString() + "/image.png") // overwrites this image every time + bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream) + stream.close() + } catch (e: IOException) { + Log.d(TAG, "shareBitmapImage error: " + e.message) + } + + // Send intent to share image+text + val imagePath = File(context.cacheDir, "images") + val newFile = File(imagePath, "image.png") + + // Create and return image uri + return FileProvider.getUriForFile(context, "cy.agorise.FileProvider", newFile) + } + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_share.xml b/app/src/main/res/drawable/ic_share.xml new file mode 100644 index 0000000..045bbc0 --- /dev/null +++ b/app/src/main/res/drawable/ic_share.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/fragment_receive_transaction.xml b/app/src/main/res/layout/fragment_receive_transaction.xml index b902424..0db1ef4 100644 --- a/app/src/main/res/layout/fragment_receive_transaction.xml +++ b/app/src/main/res/layout/fragment_receive_transaction.xml @@ -83,47 +83,57 @@ - - - + android:layout_height="0dp" + android:background="?android:windowBackground" + app:layout_constraintTop_toBottomOf="@+id/tilAsset" + app:layout_constraintBottom_toBottomOf="parent"> - + + + android:orientation="vertical" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintTop_toBottomOf="@+id/ivQR"> - + - + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/menu_receive_transaction.xml b/app/src/main/res/menu/menu_receive_transaction.xml new file mode 100644 index 0000000..5888d65 --- /dev/null +++ b/app/src/main/res/menu/menu_receive_transaction.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8499903..7e0568e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -56,6 +56,8 @@ Asset Please Pay: %1$s %2$s To: %1$s + BiTSy invoice from %1$s + Share with Settings @@ -79,5 +81,6 @@ Search Filter Export + Share