....
This commit is contained in:
parent
70869d7e1e
commit
1ba808fcf0
@ -186,6 +186,10 @@ class SystemStatusFragment : Fragment() {
|
||||
binding.btnQuickFlash.setOnClickListener {
|
||||
toggleFlashlight()
|
||||
}
|
||||
binding.btnQuickFlash.setOnLongClickListener {
|
||||
toggleFlashlight(120)
|
||||
true
|
||||
}
|
||||
binding.btnQuickMirror.setOnClickListener {
|
||||
if (allPermissionsGranted()) {
|
||||
showMirrorDialog() // 권한이 있으면 거울 실행
|
||||
@ -213,47 +217,72 @@ class SystemStatusFragment : Fragment() {
|
||||
|
||||
private var flashTimerJob: Job? = null
|
||||
|
||||
private fun toggleFlashlight() {
|
||||
private fun toggleFlashlight(time : Int = 30) {
|
||||
try {
|
||||
val cameraId = cameraManager.cameraIdList[0]
|
||||
isFlashOn = !isFlashOn
|
||||
cameraManager.setTorchMode(cameraId, isFlashOn)
|
||||
|
||||
// 기존 타이머가 있다면 취소
|
||||
flashTimerJob?.cancel()
|
||||
flashTimerJob?.cancel() // 기존 타이머 중지
|
||||
|
||||
if (isFlashOn) {
|
||||
binding.btnQuickFlash.text = "🔦 ON"
|
||||
binding.btnQuickFlash.setTextColor(Color.YELLOW)
|
||||
|
||||
// 30초 후 자동 종료 타이머 시작
|
||||
// 30초 카운트다운 시작
|
||||
flashTimerJob = CoroutineScope(Dispatchers.Main).launch {
|
||||
delay(30000L) // 30초
|
||||
if (isFlashOn) {
|
||||
isFlashOn = false
|
||||
cameraManager.setTorchMode(cameraId, false)
|
||||
binding.btnQuickFlash.text = "🔦 조명"
|
||||
binding.btnQuickFlash.setTextColor(Color.WHITE)
|
||||
Toast.makeText(context, "배터리 보호를 위해 조명을 껐습니다.", Toast.LENGTH_SHORT).show()
|
||||
for (i in time downTo 1) {
|
||||
binding.btnQuickFlash.text = "🔦 $i" + "s"
|
||||
delay(1000L)
|
||||
}
|
||||
// 시간 종료 후 초기화
|
||||
isFlashOn = false
|
||||
cameraManager.setTorchMode(cameraId, false)
|
||||
resetFlashButton()
|
||||
}
|
||||
} else {
|
||||
binding.btnQuickFlash.text = "🔦 조명"
|
||||
binding.btnQuickFlash.setTextColor(Color.WHITE)
|
||||
resetFlashButton()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
private fun resetFlashButton() {
|
||||
binding.btnQuickFlash.text = "🔦 조명"
|
||||
binding.btnQuickFlash.setTextColor(Color.WHITE)
|
||||
}
|
||||
|
||||
// 2. 권한 확인 함수
|
||||
private fun allPermissionsGranted() = androidx.core.content.ContextCompat.checkSelfPermission(
|
||||
requireContext(), CAMERA_PERMISSION) == android.content.pm.PackageManager.PERMISSION_GRANTED
|
||||
|
||||
private fun showMirrorDialog() {
|
||||
val dialog = android.app.Dialog(requireContext(), android.R.style.Theme_Black_NoTitleBar_Fullscreen)
|
||||
|
||||
// 레이아웃 구성을 위한 FrameLayout 생성
|
||||
val frameLayout = android.widget.FrameLayout(requireContext())
|
||||
val previewView = androidx.camera.view.PreviewView(requireContext())
|
||||
dialog.setContentView(previewView)
|
||||
|
||||
// 타이머용 TextView 생성
|
||||
val timerText = TextView(requireContext()).apply {
|
||||
setTextColor(Color.WHITE)
|
||||
setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f)
|
||||
setBackgroundColor(Color.parseColor("#88000000")) // 반투명 배경
|
||||
setPadding(32, 16, 32, 16)
|
||||
gravity = android.view.Gravity.CENTER
|
||||
}
|
||||
|
||||
val params = android.widget.FrameLayout.LayoutParams(
|
||||
android.widget.FrameLayout.LayoutParams.WRAP_CONTENT,
|
||||
android.widget.FrameLayout.LayoutParams.WRAP_CONTENT
|
||||
).apply {
|
||||
gravity = android.view.Gravity.TOP or android.view.Gravity.CENTER_HORIZONTAL
|
||||
topMargin = 100
|
||||
}
|
||||
|
||||
frameLayout.addView(previewView)
|
||||
frameLayout.addView(timerText, params)
|
||||
dialog.setContentView(frameLayout)
|
||||
|
||||
val cameraProviderFuture = androidx.camera.lifecycle.ProcessCameraProvider.getInstance(requireContext())
|
||||
|
||||
@ -274,10 +303,14 @@ class SystemStatusFragment : Fragment() {
|
||||
}
|
||||
}, androidx.core.content.ContextCompat.getMainExecutor(requireContext()))
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
delay(30000L)
|
||||
for (i in 30 downTo 1) {
|
||||
if (!dialog.isShowing) break
|
||||
timerText.text = "거울 모드 종료까지 $i" + "초"
|
||||
delay(1000L)
|
||||
}
|
||||
if (dialog.isShowing) {
|
||||
dialog.dismiss()
|
||||
Toast.makeText(context, "거울을 종료합니다.", Toast.LENGTH_SHORT).show()
|
||||
context?.toast("배터리 절약을 위해 거울을 껐습니다.")
|
||||
}
|
||||
}
|
||||
// 화면 터치 시 종료
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user