11package com.masterdns.vpn.ui.profiles
22
3+ import android.app.Activity
34import android.content.ClipData
45import android.content.ClipboardManager
56import android.content.Context
@@ -53,6 +54,7 @@ import com.masterdns.vpn.ui.components.mdv.cards.MdvCardLow
5354import com.masterdns.vpn.ui.components.mdv.controls.MdvBackTopAppBar
5455import com.masterdns.vpn.ui.components.mdv.controls.MdvFilterChip
5556import com.masterdns.vpn.ui.components.mdv.controls.MdvPrimaryActionButton
57+ import com.masterdns.vpn.ui.scanner.ScannerActivity
5658import com.masterdns.vpn.ui.theme.ConnectedGreen
5759import com.masterdns.vpn.ui.theme.MdvColor
5860import com.masterdns.vpn.ui.theme.MdvRadius
@@ -112,17 +114,25 @@ fun ProfilesScreen(
112114 val snackbarHostState = remember { SnackbarHostState () }
113115 val scope = rememberCoroutineScope()
114116 val qrScanLauncher = rememberLauncherForActivityResult(
115- contract = ActivityResultContracts .TakePicturePreview ()
116- ) { bitmap ->
117- if (bitmap == null ) {
117+ contract = ActivityResultContracts .StartActivityForResult ()
118+ ) { result ->
119+ if (result.resultCode != Activity . RESULT_OK ) {
118120 scope.launch { snackbarHostState.showSnackbar(context.getString(R .string.profiles_qr_scan_cancelled)) }
119121 return @rememberLauncherForActivityResult
120122 }
121- val decodedText = QrCodeUtil .decodeFromBitmap(bitmap).getOrNull ()
122- if (decodedText.isNullOrBlank ()) {
123+ val decodedText = result.data?.getStringExtra( ScannerActivity . EXTRA_SCAN_RESULT )?.trim().orEmpty ()
124+ if (decodedText.isBlank ()) {
123125 scope.launch { snackbarHostState.showSnackbar(context.getString(R .string.profiles_qr_scan_no_code)) }
124126 return @rememberLauncherForActivityResult
125127 }
128+ val subUrl = ProfileUrlCodec .findSubscriptionUrl(decodedText)
129+ if (subUrl != null ) {
130+ prefilledSubscriptionUrl = subUrl
131+ addSubscriptionError = null
132+ addSubscriptionSession++
133+ showAddSubscriptionDialog = true
134+ return @rememberLauncherForActivityResult
135+ }
126136 val drafts = runCatching { ProfileUrlCodec .decodeMultiple(decodedText) }.getOrNull()
127137 if (drafts.isNullOrEmpty()) {
128138 scope.launch { snackbarHostState.showSnackbar(context.getString(R .string.profiles_invalid_link_msg)) }
@@ -249,7 +259,7 @@ fun ProfilesScreen(
249259 leadingIcon = { Icon (Icons .Filled .QrCodeScanner , contentDescription = null ) },
250260 onClick = {
251261 showAddMenu = false
252- qrScanLauncher.launch(null )
262+ qrScanLauncher.launch(Intent (context, ScannerActivity :: class .java) )
253263 }
254264 )
255265 DropdownMenuItem (
@@ -997,7 +1007,7 @@ private fun ShareProfileDialog(
9971007 Text (profile.name, style = MaterialTheme .typography.titleSmall, fontWeight = FontWeight .SemiBold )
9981008 if (shareUrl != null ) {
9991009 val qrBitmap = remember(shareUrl) {
1000- runCatching { QrCodeUtil .encodeAsBitmap(shareUrl, sizePx = 512 ) }.getOrNull()
1010+ runCatching { QrCodeUtil .encodeAsBitmap(shareUrl, sizePx = 800 ) }.getOrNull()
10011011 }
10021012 if (qrBitmap != null ) {
10031013 Box (
@@ -1079,11 +1089,11 @@ private fun ImportProfilesDialog(
10791089 val context = LocalContext .current
10801090
10811091 val dialogQrScanner = rememberLauncherForActivityResult(
1082- contract = ActivityResultContracts .TakePicturePreview ()
1083- ) { bitmap ->
1084- if (bitmap == null ) return @rememberLauncherForActivityResult
1085- val decodedText = QrCodeUtil .decodeFromBitmap(bitmap).getOrNull ()
1086- if (decodedText.isNullOrBlank ()) {
1092+ contract = ActivityResultContracts .StartActivityForResult ()
1093+ ) { result ->
1094+ if (result.resultCode != Activity . RESULT_OK ) return @rememberLauncherForActivityResult
1095+ val decodedText = result.data?.getStringExtra( ScannerActivity . EXTRA_SCAN_RESULT )?.trim().orEmpty ()
1096+ if (decodedText.isBlank ()) {
10871097 errorMessage = context.getString(R .string.profiles_qr_scan_no_code)
10881098 } else {
10891099 rawText = if (rawText.isBlank()) decodedText else " $rawText \n $decodedText "
@@ -1186,7 +1196,7 @@ private fun ImportProfilesDialog(
11861196 )
11871197 }
11881198 OutlinedButton (
1189- onClick = { dialogQrScanner.launch(null ) },
1199+ onClick = { dialogQrScanner.launch(Intent (context, ScannerActivity :: class .java) ) },
11901200 modifier = Modifier .weight(1f )
11911201 ) {
11921202 Icon (Icons .Filled .QrCodeScanner , contentDescription = null , modifier = Modifier .size(16 .dp))
@@ -1242,10 +1252,24 @@ private fun AddSubscriptionDialog(
12421252 isLoading : Boolean ,
12431253 externalError : String? = null
12441254) {
1255+ val context = LocalContext .current
12451256 var name by rememberSaveable { mutableStateOf(" " ) }
12461257 var url by rememberSaveable(initialUrl) { mutableStateOf(initialUrl) }
12471258 var localError by rememberSaveable { mutableStateOf<String ?>(null ) }
12481259
1260+ val subQrLauncher = rememberLauncherForActivityResult(
1261+ contract = ActivityResultContracts .StartActivityForResult ()
1262+ ) { result ->
1263+ if (result.resultCode == Activity .RESULT_OK ) {
1264+ val scanned = result.data?.getStringExtra(ScannerActivity .EXTRA_SCAN_RESULT )?.trim().orEmpty()
1265+ if (scanned.isNotBlank()) {
1266+ val detected = ProfileUrlCodec .findSubscriptionUrl(scanned) ? : scanned
1267+ url = detected
1268+ localError = null
1269+ }
1270+ }
1271+ }
1272+
12491273 LaunchedEffect (externalError) {
12501274 localError = externalError
12511275 }
@@ -1272,6 +1296,17 @@ private fun AddSubscriptionDialog(
12721296 },
12731297 label = { Text (stringResource(R .string.profiles_subscription_url_label)) },
12741298 placeholder = { Text (stringResource(R .string.profiles_subscription_url_hint)) },
1299+ trailingIcon = {
1300+ IconButton (
1301+ onClick = { subQrLauncher.launch(Intent (context, ScannerActivity ::class .java)) },
1302+ enabled = ! isLoading
1303+ ) {
1304+ Icon (
1305+ Icons .Filled .QrCodeScanner ,
1306+ contentDescription = stringResource(R .string.profiles_subscription_scan_qr)
1307+ )
1308+ }
1309+ },
12751310 singleLine = true ,
12761311 isError = localError != null ,
12771312 enabled = ! isLoading,
@@ -1322,6 +1357,7 @@ private fun ManageSubscriptionsDialog(
13221357 isRefreshing : Boolean
13231358) {
13241359 var pendingDeleteSub by remember { mutableStateOf<SubscriptionEntity ?>(null ) }
1360+ var sharingSub by remember { mutableStateOf<SubscriptionEntity ?>(null ) }
13251361
13261362 AlertDialog (
13271363 onDismissRequest = onDismiss,
@@ -1365,6 +1401,12 @@ private fun ManageSubscriptionsDialog(
13651401 )
13661402 }
13671403 Row {
1404+ IconButton (onClick = { sharingSub = sub }, enabled = ! isRefreshing) {
1405+ Icon (
1406+ Icons .Filled .QrCode2 ,
1407+ contentDescription = stringResource(R .string.profiles_subscription_share_qr)
1408+ )
1409+ }
13681410 IconButton (onClick = { onRefresh(sub.id) }, enabled = ! isRefreshing) {
13691411 Icon (
13701412 Icons .Filled .Sync ,
@@ -1414,6 +1456,74 @@ private fun ManageSubscriptionsDialog(
14141456 }
14151457 )
14161458 }
1459+
1460+ if (sharingSub != null ) {
1461+ SubscriptionQrDialog (
1462+ subscription = sharingSub!! ,
1463+ onDismiss = { sharingSub = null }
1464+ )
1465+ }
1466+ }
1467+
1468+ @Composable
1469+ private fun SubscriptionQrDialog (
1470+ subscription : SubscriptionEntity ,
1471+ onDismiss : () -> Unit
1472+ ) {
1473+ val context = LocalContext .current
1474+ val qrBitmap = remember(subscription.url) {
1475+ runCatching { QrCodeUtil .encodeAsBitmap(subscription.url, sizePx = 800 ) }.getOrNull()
1476+ }
1477+
1478+ AlertDialog (
1479+ onDismissRequest = onDismiss,
1480+ title = { Text (subscription.name) },
1481+ text = {
1482+ Column (
1483+ modifier = Modifier
1484+ .fillMaxWidth()
1485+ .verticalScroll(rememberScrollState()),
1486+ verticalArrangement = Arrangement .spacedBy(MdvSpace .S3 ),
1487+ horizontalAlignment = Alignment .CenterHorizontally
1488+ ) {
1489+ if (qrBitmap != null ) {
1490+ Image (
1491+ bitmap = qrBitmap.asImageBitmap(),
1492+ contentDescription = stringResource(R .string.profiles_subscription_share_qr),
1493+ modifier = Modifier
1494+ .size(220 .dp)
1495+ .clip(RoundedCornerShape (MdvRadius .Lg ))
1496+ .background(MdvColor .QrBackground )
1497+ .padding(MdvSpace .S2 )
1498+ )
1499+ }
1500+ CompositionLocalProvider (LocalLayoutDirection provides LayoutDirection .Ltr ) {
1501+ OutlinedTextField (
1502+ value = subscription.url,
1503+ onValueChange = {},
1504+ readOnly = true ,
1505+ modifier = Modifier .fillMaxWidth().height(100 .dp),
1506+ maxLines = 4 ,
1507+ textStyle = LocalTextStyle .current.copy(textDirection = TextDirection .Ltr , textAlign = TextAlign .Start )
1508+ )
1509+ }
1510+ }
1511+ },
1512+ confirmButton = {
1513+ TextButton (onClick = {
1514+ val clipboard = context.getSystemService(Context .CLIPBOARD_SERVICE ) as ClipboardManager
1515+ clipboard.setPrimaryClip(ClipData .newPlainText(subscription.name, subscription.url))
1516+ Toast .makeText(context, context.getString(R .string.profiles_share_copied), Toast .LENGTH_SHORT ).show()
1517+ }) {
1518+ Text (stringResource(R .string.btn_copy))
1519+ }
1520+ },
1521+ dismissButton = {
1522+ TextButton (onClick = onDismiss) {
1523+ Text (stringResource(R .string.action_close))
1524+ }
1525+ }
1526+ )
14171527}
14181528
14191529@Composable
0 commit comments