From 23770f79a93f7d3d16c3676ec923e4deffd26d31 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Tue, 7 Jan 2020 09:35:14 +1100 Subject: [PATCH] Fix QR code sharing --- .../redesign/activities/QRCodeActivity.kt | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/org/thoughtcrime/securesms/loki/redesign/activities/QRCodeActivity.kt b/src/org/thoughtcrime/securesms/loki/redesign/activities/QRCodeActivity.kt index b6b6e76209..6a9790c673 100644 --- a/src/org/thoughtcrime/securesms/loki/redesign/activities/QRCodeActivity.kt +++ b/src/org/thoughtcrime/securesms/loki/redesign/activities/QRCodeActivity.kt @@ -1,5 +1,6 @@ package org.thoughtcrime.securesms.loki.redesign.activities +import android.Manifest import android.content.Intent import android.graphics.Bitmap import android.graphics.Typeface @@ -14,6 +15,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast +import com.tbruyelle.rxpermissions2.RxPermissions import kotlinx.android.synthetic.main.activity_qr_code.* import kotlinx.android.synthetic.main.fragment_view_my_qr_code.* import network.loki.messenger.R @@ -129,21 +131,33 @@ class ViewMyQRCodeFragment : Fragment() { } private fun shareQRCode() { - val directory = File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_PICTURES) - val fileName = "$hexEncodedPublicKey.png" - val file = File(directory, fileName) - file.createNewFile() - val fos = FileOutputStream(file) - val size = toPx(240, resources) - val qrCode = QRCodeUtilities.encode(hexEncodedPublicKey, size, false) - qrCode.compress(Bitmap.CompressFormat.PNG, 100, fos) - fos.flush() - fos.close() - val intent = Intent(Intent.ACTION_SEND) - intent.putExtra(Intent.EXTRA_STREAM, FileProviderUtil.getUriFor(activity!!, file)) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - intent.type = "image/png" - startActivity(Intent.createChooser(intent, "Share QR Code")) + fun proceed() { + val directory = File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_PICTURES) + val fileName = "$hexEncodedPublicKey.png" + val file = File(directory, fileName) + file.createNewFile() + val fos = FileOutputStream(file) + val size = toPx(240, resources) + val qrCode = QRCodeUtilities.encode(hexEncodedPublicKey, size, false) + qrCode.compress(Bitmap.CompressFormat.PNG, 100, fos) + fos.flush() + fos.close() + val intent = Intent(Intent.ACTION_SEND) + intent.putExtra(Intent.EXTRA_STREAM, FileProviderUtil.getUriFor(activity!!, file)) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + intent.type = "image/png" + startActivity(Intent.createChooser(intent, "Share QR Code")) + } + if (RxPermissions(this).isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { + proceed() + } else { + @SuppressWarnings("unused") + val unused = RxPermissions(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE).subscribe { isGranted -> + if (isGranted) { + proceed() + } + } + } } } // endregion \ No newline at end of file