Code clean up
This commit is contained in:
@@ -17,11 +17,6 @@ import com.google.zxing.WriterException
|
||||
import timber.log.Timber
|
||||
import java.nio.charset.StandardCharsets
|
||||
|
||||
/**
|
||||
* Based on:
|
||||
* https://android.googlesource.com/platform/packages/apps/Settings/+/0d706f0/src/com/android/settings/wifi/qrcode/QrCodeGenerator.java
|
||||
* https://android.googlesource.com/platform/packages/apps/Settings/+/8a9ccfd/src/com/android/settings/wifi/dpp/WifiDppQrCodeGeneratorFragment.java#153
|
||||
*/
|
||||
class QRCodeDialog : DialogFragment() {
|
||||
companion object {
|
||||
private const val KEY_ARG = "arg"
|
||||
@@ -31,18 +26,23 @@ class QRCodeDialog : DialogFragment() {
|
||||
fun withArg(arg: String) = apply { arguments = bundleOf(KEY_ARG to arg) }
|
||||
private val arg get() = arguments?.getString(KEY_ARG)
|
||||
|
||||
/**
|
||||
* Based on:
|
||||
* https://android.googlesource.com/platform/packages/apps/Settings/+/0d706f0/src/com/android/settings/wifi/qrcode/QrCodeGenerator.java
|
||||
* https://android.googlesource.com/platform/packages/apps/Settings/+/8a9ccfd/src/com/android/settings/wifi/dpp/WifiDppQrCodeGeneratorFragment.java#153
|
||||
*/
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?) = try {
|
||||
ImageView(context).apply {
|
||||
val hints = HashMap<EncodeHintType, Any>()
|
||||
if (!iso88591.canEncode(arg)) hints[EncodeHintType.CHARACTER_SET] = StandardCharsets.UTF_8.name()
|
||||
val size = resources.getDimensionPixelSize(R.dimen.qrcode_size)
|
||||
val hints = mutableMapOf<EncodeHintType, Any>()
|
||||
if (!iso88591.canEncode(arg)) hints[EncodeHintType.CHARACTER_SET] = StandardCharsets.UTF_8.name()
|
||||
val qrBits = MultiFormatWriter().encode(arg, BarcodeFormat.QR_CODE, size, size, hints)
|
||||
val bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565)
|
||||
for (x in 0 until size) for (y in 0 until size) {
|
||||
bitmap.setPixel(x, y, if (qrBits.get(x, y)) Color.BLACK else Color.WHITE)
|
||||
}
|
||||
ImageView(context).apply {
|
||||
layoutParams = ViewGroup.LayoutParams(size, size)
|
||||
setImageBitmap(bitmap)
|
||||
setImageBitmap(Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565).apply {
|
||||
for (x in 0 until size) for (y in 0 until size) {
|
||||
setPixel(x, y, if (qrBits.get(x, y)) Color.BLACK else Color.WHITE)
|
||||
}
|
||||
})
|
||||
}
|
||||
} catch (e: WriterException) {
|
||||
Timber.w(e)
|
||||
|
||||
Reference in New Issue
Block a user