refactor: Task 8 완료 - 공용 NetworkClient(OkHttpClient) 도입 및 Response.use/예외 처리 보강
- utils/NetworkClient.kt 신규 생성: ConnectionPool(5, 60s) 및 타임아웃(30s)이 설정된 공용 OkHttpClient 싱글톤 제공
- ForeGroundService.kt:
* sendToI(msg)와 sendToI(boolean)의 중복 OkHttp 호출을 단일 sendToI(msg)로 통합
* serviceScope.launch(Dispatchers.IO) 사용 및 try-catch + Response.use {} 적용으로 커넥션/예외 누수 방지
- PlayerActivity.kt downloadSubtitle(): 매 호출 OkHttpClient() 신규 생성 제거, NetworkClient.client 및 Response.use {} 적용
- 검증: ./gradlew :app:compileDebugKotlin 빌드 성공 확인 (3s)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user