Skip to content

Commit 8e32343

Browse files
committed
Long press qs tile to open scanner when disconnected
1 parent 12c389a commit 8e32343

1 file changed

Lines changed: 95 additions & 4 deletions

File tree

app/src/main/java/com/sameerasw/airsync/MainActivity.kt

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.sameerasw.airsync
33
import android.Manifest
44
import android.os.Build
55
import android.os.Bundle
6+
import android.util.Log
67
import androidx.activity.ComponentActivity
78
import androidx.activity.compose.setContent
89
import androidx.activity.enableEdgeToEdge
@@ -27,10 +28,9 @@ import androidx.compose.ui.graphics.ColorFilter
2728
import androidx.core.net.toUri
2829
import androidx.compose.ui.input.nestedscroll.nestedScroll
2930
import com.sameerasw.airsync.data.local.DataStoreManager
30-
import kotlinx.coroutines.flow.collect
31-
import kotlinx.coroutines.flow.flowOf
32-
import kotlinx.coroutines.flow.map
33-
import kotlinx.coroutines.flow.Flow
31+
import android.content.Intent
32+
import com.sameerasw.airsync.presentation.ui.activities.QRScannerActivity
33+
import com.sameerasw.airsync.utils.WebSocketUtil
3434

3535
class MainActivity : ComponentActivity() {
3636

@@ -40,9 +40,35 @@ class MainActivity : ComponentActivity() {
4040
) { isGranted ->
4141
}
4242

43+
private val qrScannerLauncher = registerForActivityResult(
44+
ActivityResultContracts.StartActivityForResult()
45+
) { result ->
46+
if (result.resultCode == RESULT_OK) {
47+
val qrCode = result.data?.getStringExtra("QR_CODE")
48+
if (qrCode != null) {
49+
// Parse and connect with the QR code data
50+
handleQRCodeResult(qrCode)
51+
}
52+
} else {
53+
// User cancelled or invalid QR, do nothing - app remains open
54+
}
55+
}
56+
4357
@OptIn(ExperimentalMaterial3Api::class)
4458
override fun onCreate(savedInstanceState: Bundle?) {
4559
super.onCreate(savedInstanceState)
60+
61+
// Check if this is a QS tile long-press intent and device is not connected
62+
if (intent?.action == "android.service.quicksettings.action.QS_TILE_PREFERENCES") {
63+
if (!WebSocketUtil.isConnected()) {
64+
// Not connected, open QR scanner instead
65+
val qrScannerIntent = Intent(this, QRScannerActivity::class.java)
66+
qrScannerLauncher.launch(qrScannerIntent)
67+
return
68+
}
69+
}
70+
71+
// ...existing code...
4672
// Enable full edge-to-edge drawing for both status and navigation bars
4773
enableEdgeToEdge(
4874
statusBarStyle = SystemBarStyle.auto(
@@ -181,4 +207,69 @@ class MainActivity : ComponentActivity() {
181207
}
182208
}
183209
}
210+
211+
private fun handleQRCodeResult(qrCode: String) {
212+
try {
213+
val uri = android.net.Uri.parse(qrCode)
214+
215+
// Validate QR code format: must be airsync scheme with host and port
216+
if (uri.scheme != "airsync") {
217+
Log.w("MainActivity", "Invalid QR code scheme: ${uri.scheme}")
218+
android.widget.Toast.makeText(
219+
this,
220+
"Invalid QR code format",
221+
android.widget.Toast.LENGTH_SHORT
222+
).show()
223+
finish()
224+
return
225+
}
226+
227+
val host = uri.host
228+
val port = uri.port
229+
230+
if (host.isNullOrEmpty() || port == -1) {
231+
Log.w("MainActivity", "Invalid QR code: missing host or port")
232+
android.widget.Toast.makeText(
233+
this,
234+
"Invalid QR code format",
235+
android.widget.Toast.LENGTH_SHORT
236+
).show()
237+
finish()
238+
return
239+
}
240+
241+
// Valid QR code, proceed with connection
242+
val mainIntent = Intent(this, MainActivity::class.java).apply {
243+
data = uri
244+
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
245+
}
246+
startActivity(mainIntent)
247+
finish()
248+
} catch (e: Exception) {
249+
Log.e("MainActivity", "Error handling QR code result: ${e.message}", e)
250+
android.widget.Toast.makeText(
251+
this,
252+
"Invalid QR code format",
253+
android.widget.Toast.LENGTH_SHORT
254+
).show()
255+
finish()
256+
}
257+
}
258+
259+
override fun onNewIntent(intent: Intent?) {
260+
super.onNewIntent(intent)
261+
262+
// Check if this is a QS tile long-press intent
263+
if (intent?.action == "android.service.quicksettings.action.QS_TILE_PREFERENCES") {
264+
// Check if device is connected
265+
if (!WebSocketUtil.isConnected()) {
266+
// Not connected, open QR scanner
267+
val qrScannerIntent = Intent(this, QRScannerActivity::class.java).apply {
268+
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
269+
}
270+
startActivity(qrScannerIntent)
271+
finish()
272+
}
273+
}
274+
}
184275
}

0 commit comments

Comments
 (0)