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) {
|
fun sendToI(msg: String) {
|
||||||
if (PrefString.telegramSendTarget.get().length > 5) {
|
if (PrefString.telegramSendTarget.get().length > 5) {
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
serviceScope.launch(Dispatchers.IO) {
|
||||||
OkHttpClient.Builder()
|
try {
|
||||||
.connectionPool(ConnectionPool(5, 60, TimeUnit.SECONDS))
|
val request = Request.Builder()
|
||||||
.build().newCall(Request.Builder().url("https://api.telegram.org/bot7934509464:AAE_xUbICxMdywLGnxo7BkeIqA1nVza4P9w/sendMessage?chat_id=${PrefString.telegramSendTarget.get()}&text=${msg}")
|
.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 ->
|
.addHeader("Content-Type", "application/json")
|
||||||
if (response.isSuccessful) {
|
.get()
|
||||||
val body: ResponseBody? = response.body
|
.build()
|
||||||
if (body != null) { }
|
bums.lunatic.launcher.utils.NetworkClient.client.newCall(request).execute().use { response ->
|
||||||
} else Blog.LOGE("sendToI telegram Error Occurred")
|
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) {
|
fun sendToI(boolean: Boolean) {
|
||||||
if (PrefString.telegramSendTarget.get().length > 5) {
|
val text = if (boolean) {
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
"돼지가 ${PrefString.carName.get()}에 탔다요."
|
||||||
val url =
|
} else {
|
||||||
"https://api.telegram.org/bot7934509464:AAE_xUbICxMdywLGnxo7BkeIqA1nVza4P9w/sendMessage?chat_id=${PrefString.telegramSendTarget.get()}&text=${if(boolean) {
|
"${PrefString.carName.get()}의 시동이 꺼졌다요."
|
||||||
"돼지가 ${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")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
sendToI(text)
|
||||||
|
|
||||||
}
|
}
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
fun isConnected(device: BluetoothDevice): Boolean {
|
fun isConnected(device: BluetoothDevice): Boolean {
|
||||||
|
|||||||
@@ -659,28 +659,28 @@ class PlayerActivity : AppCompatActivity(), TextureView.SurfaceTextureListener {
|
|||||||
// 필요 시 Referer 추가 (GeckoWeb에서 넘겨받은 값 활용 가능)
|
// 필요 시 Referer 추가 (GeckoWeb에서 넘겨받은 값 활용 가능)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
val response = okhttp3.OkHttpClient().newCall(request).execute()
|
bums.lunatic.launcher.utils.NetworkClient.client.newCall(request).execute().use { response ->
|
||||||
|
if (response.isSuccessful) {
|
||||||
if (response.isSuccessful) {
|
response.body?.byteStream()?.use { input ->
|
||||||
response.body?.byteStream()?.use { input ->
|
newSubFile.outputStream().use { output ->
|
||||||
newSubFile.outputStream().use { output ->
|
input.copyTo(output)
|
||||||
input.copyTo(output)
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
geckoWeb.visibility = View.GONE
|
geckoWeb.visibility = View.GONE
|
||||||
Toast.makeText(this@PlayerActivity, "자막 저장 완료: ${newSubFile.name}", Toast.LENGTH_SHORT).show()
|
Toast.makeText(this@PlayerActivity, "자막 저장 완료: ${newSubFile.name}", Toast.LENGTH_SHORT).show()
|
||||||
|
|
||||||
// 1. 플레이어의 자막 경로 업데이트
|
// 1. 플레이어의 자막 경로 업데이트
|
||||||
subtitlePath = newSubFile.absolutePath
|
subtitlePath = newSubFile.absolutePath
|
||||||
|
|
||||||
// 2. 앞서 만든 스마트 인코딩 및 번역 로직 트리거
|
// 2. 앞서 만든 스마트 인코딩 및 번역 로직 트리거
|
||||||
// 이 안에서 인코딩 복구 -> 언어 감지 -> 번역 순으로 진행됩니다.
|
// 이 안에서 인코딩 복구 -> 언어 감지 -> 번역 순으로 진행됩니다.
|
||||||
detectLanguageAndTranslate()
|
detectLanguageAndTranslate()
|
||||||
|
|
||||||
// 3. 자막 리스트 갱신 및 표시
|
// 3. 자막 리스트 갱신 및 표시
|
||||||
loadAvailableSubtitles()
|
loadAvailableSubtitles()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} 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