LobbyDisplayFragment.kt (2974B)
1 /* 2 This file is part of GNUnet. 3 Copyright (C) 2021--2025 GNUnet e.V. 4 5 GNUnet is free software: you can redistribute it and/or modify it 6 under the terms of the GNU Affero General Public License as published 7 by the Free Software Foundation, either version 3 of the License, 8 or (at your option) any later version. 9 10 GNUnet is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Affero General Public License for more details. 14 15 You should have received a copy of the GNU Affero General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 SPDX-License-Identifier: AGPL3.0-or-later 19 */ 20 /* 21 * @author t3sserakt 22 * @file GNUnetMessenger/app/src/main/java/org/gnunet/gnunetmessenger/ui/account/LobbyDisplayFragment.kt 23 */ 24 25 package org.gnunet.gnunetmessenger.ui.account 26 27 import android.content.ClipboardManager 28 import android.content.ClipData 29 import android.graphics.Bitmap 30 import android.graphics.Color 31 import android.os.Bundle 32 import android.view.LayoutInflater 33 import android.view.View 34 import android.view.ViewGroup 35 import android.widget.Button 36 import android.widget.TextView 37 import android.widget.ImageView 38 import androidx.core.content.ContextCompat 39 import androidx.fragment.app.Fragment 40 import androidx.navigation.fragment.findNavController 41 import com.google.zxing.BarcodeFormat 42 import com.google.zxing.qrcode.QRCodeWriter 43 import org.gnunet.gnunetmessenger.R 44 45 class LobbyDisplayFragment : Fragment() { 46 47 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { 48 val view = inflater.inflate(R.layout.fragment_lobby_display, container, false) 49 50 val lobbyId = arguments?.getString("lobbyId") ?: "N/A" 51 52 view.findViewById<TextView>(R.id.lobby_id_text).text = "Lobby ID: $lobbyId" 53 54 val qrBitmap = generateQrCode(lobbyId) 55 view.findViewById<ImageView>(R.id.lobby_qr).setImageBitmap(qrBitmap) 56 57 view.findViewById<Button>(R.id.btn_cancel).setOnClickListener { 58 findNavController().popBackStack() 59 } 60 61 view.findViewById<Button>(R.id.btn_copy).setOnClickListener { 62 val clipboard = ContextCompat.getSystemService(requireContext(), ClipboardManager::class.java) 63 val clip = ClipData.newPlainText("Lobby ID", lobbyId) 64 clipboard?.setPrimaryClip(clip) 65 } 66 67 return view 68 } 69 70 private fun generateQrCode(text: String): Bitmap { 71 val size = 512 72 val bits = QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size) 73 val bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565) 74 for (x in 0 until size) { 75 for (y in 0 until size) { 76 bitmap.setPixel(x, y, if (bits[x, y]) Color.BLACK else Color.WHITE) 77 } 78 } 79 return bitmap 80 } 81 }