Enable sharing QRCode
This commit is contained in:
parent
388bf65499
commit
5f152e4763
5 changed files with 72 additions and 3 deletions
|
|
@ -32,6 +32,16 @@
|
||||||
<data android:mimeType="message/*" />
|
<data android:mimeType="message/*" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
|
<provider
|
||||||
|
android:name="androidx.core.content.FileProvider"
|
||||||
|
android:authorities="org.fnives.android.qrcodetransfer.fileprovider"
|
||||||
|
android:grantUriPermissions="true"
|
||||||
|
android:exported="false">
|
||||||
|
<meta-data
|
||||||
|
android:name="android.support.FILE_PROVIDER_PATHS"
|
||||||
|
android:resource="@xml/filepaths" />
|
||||||
|
</provider>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|
@ -14,11 +14,14 @@ import android.graphics.Color
|
||||||
import android.icu.text.MessageFormat
|
import android.icu.text.MessageFormat
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS
|
import android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
import androidx.core.content.FileProvider
|
||||||
import com.google.zxing.BinaryBitmap
|
import com.google.zxing.BinaryBitmap
|
||||||
import com.google.zxing.LuminanceSource
|
import com.google.zxing.LuminanceSource
|
||||||
import com.google.zxing.RGBLuminanceSource
|
import com.google.zxing.RGBLuminanceSource
|
||||||
import com.google.zxing.common.BitMatrix
|
import com.google.zxing.common.BitMatrix
|
||||||
import com.google.zxing.common.HybridBinarizer
|
import com.google.zxing.common.HybridBinarizer
|
||||||
|
import java.io.File
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -60,8 +63,8 @@ fun Int.toOrdinal(): String {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.copyToClipboard(text: String) {
|
fun Context.copyToClipboard(text: String) {
|
||||||
val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager;
|
val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
|
||||||
val clipData = ClipData.newPlainText("label", text);
|
val clipData = ClipData.newPlainText("label", text)
|
||||||
clipboard.setPrimaryClip(clipData)
|
clipboard.setPrimaryClip(clipData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -76,4 +79,50 @@ fun Context.openLink(link: String) {
|
||||||
} catch (ignored: Throwable) {
|
} catch (ignored: Throwable) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private val Context.sharedDir: File
|
||||||
|
get() {
|
||||||
|
// must be the same as in filepaths.xml!
|
||||||
|
val cachePath = File(cacheDir, "shared")
|
||||||
|
cachePath.mkdirs()
|
||||||
|
return cachePath
|
||||||
|
}
|
||||||
|
|
||||||
|
private val Context.sharedFile: File
|
||||||
|
get() {
|
||||||
|
// must be the same as in filepaths.xml!
|
||||||
|
return File(sharedDir, "qrcode.jpg")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun Context.bitmapToSharedFile(bitmap: Bitmap): Boolean {
|
||||||
|
try {
|
||||||
|
sharedFile.createNewFile()
|
||||||
|
sharedFile.outputStream().use { stream ->
|
||||||
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
} catch (e: Throwable) {
|
||||||
|
e.printStackTrace()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun Context.shareQRCodeImageFile() {
|
||||||
|
// must be the same as in manifest.xml!
|
||||||
|
val contentUri = FileProvider.getUriForFile(this, "${packageName}.fileprovider", sharedFile)
|
||||||
|
?: return
|
||||||
|
|
||||||
|
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.setDataAndType(contentUri, contentResolver.getType(contentUri))
|
||||||
|
shareIntent.putExtra(Intent.EXTRA_STREAM, contentUri)
|
||||||
|
val chooserIntent = Intent.createChooser(shareIntent, ContextCompat.getString(this, R.string.share))
|
||||||
|
ContextCompat.startActivity(this, chooserIntent, null)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun Context.shareBitmap(bitmap: Bitmap) {
|
||||||
|
bitmapToSharedFile(bitmap)
|
||||||
|
shareQRCodeImageFile()
|
||||||
}
|
}
|
||||||
|
|
@ -2,6 +2,7 @@ package org.fnives.android.qrcodetransfer.create
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import androidx.compose.foundation.Image
|
import androidx.compose.foundation.Image
|
||||||
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
|
|
@ -34,6 +35,7 @@ import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.alpha
|
import androidx.compose.ui.draw.alpha
|
||||||
import androidx.compose.ui.graphics.asImageBitmap
|
import androidx.compose.ui.graphics.asImageBitmap
|
||||||
import androidx.compose.ui.layout.ContentScale
|
import androidx.compose.ui.layout.ContentScale
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import androidx.compose.ui.text.input.ImeAction
|
import androidx.compose.ui.text.input.ImeAction
|
||||||
|
|
@ -45,6 +47,7 @@ import kotlinx.coroutines.withContext
|
||||||
import org.fnives.android.qrcodetransfer.R
|
import org.fnives.android.qrcodetransfer.R
|
||||||
import org.fnives.android.qrcodetransfer.SequenceProtocol
|
import org.fnives.android.qrcodetransfer.SequenceProtocol
|
||||||
import org.fnives.android.qrcodetransfer.intent.LocalIntentText
|
import org.fnives.android.qrcodetransfer.intent.LocalIntentText
|
||||||
|
import org.fnives.android.qrcodetransfer.shareBitmap
|
||||||
import org.fnives.android.qrcodetransfer.storage.LocalAppPreferences
|
import org.fnives.android.qrcodetransfer.storage.LocalAppPreferences
|
||||||
import org.fnives.android.qrcodetransfer.toBitmap
|
import org.fnives.android.qrcodetransfer.toBitmap
|
||||||
|
|
||||||
|
|
@ -87,6 +90,7 @@ fun QRCodeCarousel(
|
||||||
bitmapIndex: Int,
|
bitmapIndex: Int,
|
||||||
setBitmapIndex: (Int) -> Unit
|
setBitmapIndex: (Int) -> Unit
|
||||||
) {
|
) {
|
||||||
|
val context = LocalContext.current
|
||||||
Column(Modifier.fillMaxSize()) {
|
Column(Modifier.fillMaxSize()) {
|
||||||
val imageBitmap = remember(bitmaps, bitmapIndex) {
|
val imageBitmap = remember(bitmaps, bitmapIndex) {
|
||||||
if (bitmapIndex < bitmaps.size) {
|
if (bitmapIndex < bitmaps.size) {
|
||||||
|
|
@ -100,7 +104,8 @@ fun QRCodeCarousel(
|
||||||
Image(
|
Image(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.weight(1f)
|
.weight(1f)
|
||||||
.fillMaxWidth(),
|
.fillMaxWidth()
|
||||||
|
.clickable { context.shareBitmap(bitmaps[bitmapIndex]) },
|
||||||
bitmap = imageBitmap,
|
bitmap = imageBitmap,
|
||||||
contentDescription = "",
|
contentDescription = "",
|
||||||
contentScale = ContentScale.Fit
|
contentScale = ContentScale.Fit
|
||||||
|
|
|
||||||
|
|
@ -19,4 +19,5 @@
|
||||||
<string name="open">Open</string>
|
<string name="open">Open</string>
|
||||||
<string name="read_qr_code">Scan QR Code</string>
|
<string name="read_qr_code">Scan QR Code</string>
|
||||||
<string name="create_qr_code">Create QR Code</string>
|
<string name="create_qr_code">Create QR Code</string>
|
||||||
|
<string name="share">Share QR Code</string>
|
||||||
</resources>
|
</resources>
|
||||||
4
app/src/main/res/xml/filepaths.xml
Normal file
4
app/src/main/res/xml/filepaths.xml
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<paths>
|
||||||
|
<cache-path name="qrcode" path="shared"/>
|
||||||
|
</paths>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue