Skip to content

Commit 743b5e7

Browse files
committed
Refactoring get url address
1 parent 61f79e1 commit 743b5e7

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

lib/src/main/java/com/omegar/libs/testconnect/SocketClient.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.java_websocket.client.WebSocketClient
1010
import org.java_websocket.handshake.ServerHandshake
1111
import java.io.ByteArrayOutputStream
1212
import java.net.URI
13+
import java.nio.ByteBuffer
1314
import kotlin.coroutines.CoroutineContext
1415

1516
/**
@@ -54,13 +55,37 @@ internal class SocketClient(
5455
}
5556

5657
fun sendLog(logText: String) {
57-
if (!isClosed) {
58+
if (isOpen) {
5859
send("$HEADER_LOG:$logText")
5960
}
6061
}
6162

63+
override fun send(text: String?) {
64+
try {
65+
super.send(text)
66+
} catch (e: Throwable) {
67+
e.printStackTrace()
68+
}
69+
}
70+
71+
override fun send(data: ByteArray?) {
72+
try {
73+
super.send(data)
74+
} catch (e: Throwable) {
75+
e.printStackTrace()
76+
}
77+
}
78+
79+
override fun send(bytes: ByteBuffer?) {
80+
try {
81+
super.send(bytes)
82+
} catch (e: Throwable) {
83+
e.printStackTrace()
84+
}
85+
}
86+
6287
fun sendScreenshot(bitmap: Bitmap) {
63-
if (!isClosed) {
88+
if (isOpen) {
6489
val stream = ByteArrayOutputStream()
6590
stream.write("$HEADER_SEND_SCREENSHOT:".toByteArray())
6691
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, stream)

lib/src/main/java/com/omegar/libs/testconnect/TestConnector.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.content.pm.PackageManager.NameNotFoundException
66
import android.graphics.Bitmap
77
import android.graphics.Canvas
88
import android.graphics.Color
9+
import android.net.Uri
910
import android.os.Build
1011
import android.os.Build.VERSION
1112
import android.os.Build.VERSION_CODES
@@ -62,10 +63,11 @@ internal object TestConnector : Callback, CoroutineScope {
6263
}
6364

6465
fun init(context: Context) {
65-
if (BuildConfig.DEBUG) {
66+
val serverAddress = if (BuildConfig.DEBUG) context.getServerAddress() else null
67+
if (serverAddress != null) {
6668
logCatcher = LogCatcher()
6769
socketClient = SocketClient(
68-
url = "ws://192.168.10.57:8080/ws",
70+
url = serverAddress,
6971
deviceName = context.deviceName,
7072
appName = context.appName,
7173
appVersion = context.appVersion,
@@ -77,6 +79,22 @@ internal object TestConnector : Callback, CoroutineScope {
7779
}
7880
}
7981

82+
private fun Context.getServerAddress(): String? {
83+
try {
84+
val serverAddressUri: Uri = OmegaTestConnectContract.CONTENT_URI
85+
val projection = arrayOf(OmegaTestConnectContract.COLUMN_SERVER_ADDRESS)
86+
val cursor = contentResolver.query(serverAddressUri, projection, null, null, null)
87+
if (cursor != null && cursor.moveToFirst()) {
88+
val serverAddress = cursor.getString(cursor.getColumnIndexOrThrow(OmegaTestConnectContract.COLUMN_SERVER_ADDRESS))
89+
cursor.close()
90+
return serverAddress
91+
}
92+
} catch (e: Throwable) {
93+
e.printStackTrace()
94+
}
95+
return null
96+
}
97+
8098
override fun startLog() {
8199
logJob = launch {
82100
logCatcher?.flow?.collect {
@@ -116,4 +134,11 @@ internal object TestConnector : Callback, CoroutineScope {
116134
null
117135
}
118136
}
137+
138+
object OmegaTestConnectContract {
139+
140+
const val COLUMN_SERVER_ADDRESS = "server_address"
141+
const val AUTHORITY = "com.omega.testconnectprovider"
142+
val CONTENT_URI = Uri.parse("content://$AUTHORITY/server_address")
143+
}
119144
}

0 commit comments

Comments
 (0)