Skip to content

Commit 6cd5b35

Browse files
committed
Merge branch 'advisor/111-camerax-qr-scanner-and-smart-subscription-detection'
2 parents 14c03f8 + bd1c986 commit 6cd5b35

11 files changed

Lines changed: 665 additions & 38 deletions

File tree

‎android/app/build.gradle.kts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ dependencies {
237237
// ZXing for QR code generation and decoding
238238
implementation("com.google.zxing:core:3.5.3")
239239

240+
// CameraX for in-app real-time QR scanning
241+
val cameraxVersion = "1.4.1"
242+
implementation("androidx.camera:camera-core:$cameraxVersion")
243+
implementation("androidx.camera:camera-camera2:$cameraxVersion")
244+
implementation("androidx.camera:camera-lifecycle:$cameraxVersion")
245+
implementation("androidx.camera:camera-view:$cameraxVersion")
246+
240247
// Debug
241248
debugImplementation("androidx.compose.ui:ui-tooling")
242249
debugImplementation("androidx.compose.ui:ui-test-manifest")

‎android/app/src/main/AndroidManifest.xml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
tools:targetApi="34" />
1010
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
1111
<uses-permission android:name="android.permission.WAKE_LOCK" />
12+
<uses-permission android:name="android.permission.CAMERA" />
13+
<uses-feature android:name="android.hardware.camera" android:required="false" />
14+
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
1215

1316
<queries>
1417
<intent>
@@ -72,6 +75,12 @@
7275
</intent-filter>
7376
</activity>
7477

78+
<activity
79+
android:name=".ui.scanner.ScannerActivity"
80+
android:exported="false"
81+
android:screenOrientation="portrait"
82+
android:theme="@style/Theme.MasterDnsVPN" />
83+
7584
<service
7685
android:name=".service.MasterDnsVpnService"
7786
android:permission="android.permission.BIND_VPN_SERVICE"

‎android/app/src/main/java/com/masterdns/vpn/ui/profiles/ProfilesScreen.kt‎

Lines changed: 123 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.masterdns.vpn.ui.profiles
22

3+
import android.app.Activity
34
import android.content.ClipData
45
import android.content.ClipboardManager
56
import android.content.Context
@@ -53,6 +54,7 @@ import com.masterdns.vpn.ui.components.mdv.cards.MdvCardLow
5354
import com.masterdns.vpn.ui.components.mdv.controls.MdvBackTopAppBar
5455
import com.masterdns.vpn.ui.components.mdv.controls.MdvFilterChip
5556
import com.masterdns.vpn.ui.components.mdv.controls.MdvPrimaryActionButton
57+
import com.masterdns.vpn.ui.scanner.ScannerActivity
5658
import com.masterdns.vpn.ui.theme.ConnectedGreen
5759
import com.masterdns.vpn.ui.theme.MdvColor
5860
import 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

Comments
 (0)