diff --git a/app/src/main/kotlin/bums/lunatic/launcher/helpers/ForeGroundService.kt b/app/src/main/kotlin/bums/lunatic/launcher/helpers/ForeGroundService.kt index 2cf0f422..6efe36ae 100644 --- a/app/src/main/kotlin/bums/lunatic/launcher/helpers/ForeGroundService.kt +++ b/app/src/main/kotlin/bums/lunatic/launcher/helpers/ForeGroundService.kt @@ -765,47 +765,33 @@ class ForeGroundService : Service() { fun sendToI(msg: String) { if (PrefString.telegramSendTarget.get().length > 5) { - CoroutineScope(Dispatchers.IO).launch { - OkHttpClient.Builder() - .connectionPool(ConnectionPool(5, 60, TimeUnit.SECONDS)) - .build().newCall(Request.Builder().url("https://api.telegram.org/bot7934509464:AAE_xUbICxMdywLGnxo7BkeIqA1nVza4P9w/sendMessage?chat_id=${PrefString.telegramSendTarget.get()}&text=${msg}") - .addHeader("Content-Type", "application/json").get().build()).execute()?.let { response -> - if (response.isSuccessful) { - val body: ResponseBody? = response.body - if (body != null) { } - } else Blog.LOGE("sendToI telegram Error Occurred") + serviceScope.launch(Dispatchers.IO) { + try { + val request = Request.Builder() + .url("https://api.telegram.org/bot7934509464:AAE_xUbICxMdywLGnxo7BkeIqA1nVza4P9w/sendMessage?chat_id=${PrefString.telegramSendTarget.get()}&text=${msg}") + .addHeader("Content-Type", "application/json") + .get() + .build() + bums.lunatic.launcher.utils.NetworkClient.client.newCall(request).execute().use { response -> + if (!response.isSuccessful) { + Blog.LOGE("sendToI telegram Error Occurred: ${response.code}") + } } + } catch (e: Exception) { + Blog.LOGE("sendToI telegram Exception: ${e.message}") + } } } } fun sendToI(boolean: Boolean) { - if (PrefString.telegramSendTarget.get().length > 5) { - CoroutineScope(Dispatchers.IO).launch { - val url = - "https://api.telegram.org/bot7934509464:AAE_xUbICxMdywLGnxo7BkeIqA1nVza4P9w/sendMessage?chat_id=${PrefString.telegramSendTarget.get()}&text=${if(boolean) { - "돼지가 ${PrefString.carName.get()}에 탔다요." - }else { - "${PrefString.carName.get()}의 시동이 꺼졌다요." - }}" - //7068729507 - val response: Response = OkHttpClient.Builder() - .connectionPool(ConnectionPool(5, 60, TimeUnit.SECONDS)) - .build().newCall(Request.Builder().url(url) - .addHeader("Content-Type", "application/json").get().build()).execute() - if (response.isSuccessful) { - // 응답 받아서 처리 - val body: ResponseBody? = response.body - if (body != null) { - - } -// BLog.LOGE("sendToI telegram response isSuccessful ${body}") - } else Blog.LOGE("sendToI telegram Error Occurred") - } + val text = if (boolean) { + "돼지가 ${PrefString.carName.get()}에 탔다요." + } else { + "${PrefString.carName.get()}의 시동이 꺼졌다요." } - - + sendToI(text) } @SuppressLint("MissingPermission") fun isConnected(device: BluetoothDevice): Boolean { diff --git a/app/src/main/kotlin/bums/lunatic/launcher/player/PlayerActivity.kt b/app/src/main/kotlin/bums/lunatic/launcher/player/PlayerActivity.kt index 695d851f..bdcee239 100644 --- a/app/src/main/kotlin/bums/lunatic/launcher/player/PlayerActivity.kt +++ b/app/src/main/kotlin/bums/lunatic/launcher/player/PlayerActivity.kt @@ -659,28 +659,28 @@ class PlayerActivity : AppCompatActivity(), TextureView.SurfaceTextureListener { // 필요 시 Referer 추가 (GeckoWeb에서 넘겨받은 값 활용 가능) .build() - val response = okhttp3.OkHttpClient().newCall(request).execute() - - if (response.isSuccessful) { - response.body?.byteStream()?.use { input -> - newSubFile.outputStream().use { output -> - input.copyTo(output) + bums.lunatic.launcher.utils.NetworkClient.client.newCall(request).execute().use { response -> + if (response.isSuccessful) { + response.body?.byteStream()?.use { input -> + newSubFile.outputStream().use { output -> + input.copyTo(output) + } } - } - withContext(Dispatchers.Main) { - geckoWeb.visibility = View.GONE - Toast.makeText(this@PlayerActivity, "자막 저장 완료: ${newSubFile.name}", Toast.LENGTH_SHORT).show() + withContext(Dispatchers.Main) { + geckoWeb.visibility = View.GONE + Toast.makeText(this@PlayerActivity, "자막 저장 완료: ${newSubFile.name}", Toast.LENGTH_SHORT).show() - // 1. 플레이어의 자막 경로 업데이트 - subtitlePath = newSubFile.absolutePath + // 1. 플레이어의 자막 경로 업데이트 + subtitlePath = newSubFile.absolutePath - // 2. 앞서 만든 스마트 인코딩 및 번역 로직 트리거 - // 이 안에서 인코딩 복구 -> 언어 감지 -> 번역 순으로 진행됩니다. - detectLanguageAndTranslate() + // 2. 앞서 만든 스마트 인코딩 및 번역 로직 트리거 + // 이 안에서 인코딩 복구 -> 언어 감지 -> 번역 순으로 진행됩니다. + detectLanguageAndTranslate() - // 3. 자막 리스트 갱신 및 표시 - loadAvailableSubtitles() + // 3. 자막 리스트 갱신 및 표시 + loadAvailableSubtitles() + } } } } catch (e: Exception) { diff --git a/app/src/main/kotlin/bums/lunatic/launcher/utils/NetworkClient.kt b/app/src/main/kotlin/bums/lunatic/launcher/utils/NetworkClient.kt new file mode 100644 index 00000000..3c7ac063 --- /dev/null +++ b/app/src/main/kotlin/bums/lunatic/launcher/utils/NetworkClient.kt @@ -0,0 +1,19 @@ +package bums.lunatic.launcher.utils + +import okhttp3.ConnectionPool +import okhttp3.OkHttpClient +import java.util.concurrent.TimeUnit + +/** + * Shared OkHttpClient singleton to avoid creating per-call connection pools and thread leaks. + */ +object NetworkClient { + val client: OkHttpClient by lazy { + OkHttpClient.Builder() + .connectionPool(ConnectionPool(5, 60, TimeUnit.SECONDS)) + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) + .build() + } +}