Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T MAP 관련 코드 개선 #11

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion presentation/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import dagger.hilt.android.HiltAndroidApp
import javax.inject.Inject

@HiltAndroidApp
class TroubleShooterApplication : Application(), Configuration.Provider{
class TroubleShooterApplication : Application(), Configuration.Provider {

@Inject
lateinit var workerFactory : HiltWorkerFactory
lateinit var workerFactory: HiltWorkerFactory

@Inject
override lateinit var workManagerConfiguration: Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.stop.permission

import android.Manifest
import android.content.DialogInterface
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import com.stop.R

class PermissionManager(fragment: Fragment) {

private val locationPartialPermissionDialog: AlertDialog.Builder by lazy {
val context = fragment.requireContext()
AlertDialog.Builder(context)
.setTitle(context.getString(R.string.location_permission_dialog_title))
.setMessage(context.getString(R.string.location_partial_permission_dialog_message))
.setNegativeButton(context.getString(R.string.deny)) { _: DialogInterface, _: Int ->
}
.setPositiveButton(context.getString(R.string.allow)) { _, _ ->
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.parse("package:" + context.packageName)
}
context.startActivity(intent)
}
}
private val locationPermissionDeniedDialog: AlertDialog.Builder by lazy {
val context = fragment.requireContext()
AlertDialog.Builder(context)
.setTitle(context.getString(R.string.location_permission_dialog_title))
.setMessage(context.getString(R.string.location_permission_denied_dialog_message))
.setNegativeButton(context.getString(R.string.deny)) { _: DialogInterface, _: Int ->
}
.setPositiveButton(context.getString(R.string.allow)) { _, _ ->
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.parse("package:" + context.packageName)
}
context.startActivity(intent)
}
}

private var onGranted: () -> Unit = {}
private var isShowDialog = false
private var isOkayPartialGranted = false

private val locationPermissionRequest = fragment.registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
when {
permissions.getOrDefault(Manifest.permission.ACCESS_FINE_LOCATION, false) -> {
// Precise location access granted.
onGranted()
}

permissions.getOrDefault(Manifest.permission.ACCESS_COARSE_LOCATION, false) -> {
// Only approximate location access granted.
if (isOkayPartialGranted) {
onGranted()
}
if (isShowDialog) {
locationPartialPermissionDialog.show()
}
}

else -> {
if (isShowDialog) {
locationPermissionDeniedDialog.show()
}
}
}
}

fun getLocationPermission(
onGranted: () -> Unit,
isOkayPartialGranted: Boolean,
isShowDialog: Boolean,
) {
this.onGranted = onGranted
this.isOkayPartialGranted = isOkayPartialGranted
this.isShowDialog = isShowDialog
locationPermissionRequest.launch(LOCATION_PERMISSIONS)
}

companion object {
private val LOCATION_PERMISSIONS = arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ class AlarmSettingFragment : Fragment() {
private fun initView() {
binding.textViewStartPosition.text = routeResultViewModel.origin.value?.name
binding.textViewEndPosition.text = routeResultViewModel.destination.value?.name
binding.textViewLastTimeInfo.text = routeResultViewModel.lastTimes.value?.filterNotNull()?.sortedBy {
it.timeToBoard
}?.first()?.timeToBoard
val expectedWalkingTime = (routeResultViewModel.itinerary.value!!.routes.first().sectionTime.div(60)).roundToInt().toString() + "분"
binding.textViewLastTimeInfo.text =
routeResultViewModel.lastTimes.value?.filterNotNull()?.sortedBy {
it.timeToBoard
}?.first()?.timeToBoard
val expectedWalkingTime =
(routeResultViewModel.itinerary.value!!.routes.first().sectionTime.div(60)).roundToInt()
.toString() + "분"
binding.textViewWalkTimeInfo.text = expectedWalkingTime

with(binding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import androidx.lifecycle.viewModelScope
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import androidx.work.workDataOf
import com.stop.util.AlarmFunctions
import com.stop.util.convertTimeMillisToString
import com.stop.domain.model.alarm.AlarmUseCaseItem
import com.stop.domain.usecase.alarm.DeleteAlarmUseCase
import com.stop.domain.usecase.alarm.GetAlarmUseCase
import com.stop.domain.usecase.alarm.SaveAlarmUseCase
import com.stop.util.makeFullTime
import com.stop.model.alarm.AlarmStatus
import com.stop.model.mission.MissionStatus
import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.ALARM_TIME
import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.LAST_TIME
import com.stop.util.AlarmFunctions
import com.stop.util.convertTimeMillisToString
import com.stop.util.makeFullTime
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import java.util.*
import java.util.UUID
import javax.inject.Inject

@HiltViewModel
Expand Down Expand Up @@ -53,7 +53,12 @@ class AlarmSettingViewModel @Inject constructor(

fun saveAlarm(alarmUseCaseItem: AlarmUseCaseItem) {
viewModelScope.launch(Dispatchers.IO) {
saveAlarmUseCase(alarmUseCaseItem.copy(alarmTime = alarmTime.value ?: 0, alarmMethod = alarmMethod))
saveAlarmUseCase(
alarmUseCaseItem.copy(
alarmTime = alarmTime.value ?: 0,
alarmMethod = alarmMethod
)
)
}
}

Expand All @@ -67,9 +72,11 @@ class AlarmSettingViewModel @Inject constructor(
MissionStatus.BEFORE -> {
alarmStatus.value = AlarmStatus.EXIST
}

MissionStatus.ONGOING -> {
alarmStatus.value = AlarmStatus.MISSION
}

MissionStatus.OVER -> {
alarmStatus.value = AlarmStatus.EXIST
}
Expand Down Expand Up @@ -117,7 +124,8 @@ class AlarmSettingViewModel @Inject constructor(
fun startCountDownTimer(time: String) {
val lastTimeMillis = makeFullTime(time).timeInMillis
val nowTimeMillis = System.currentTimeMillis()
var diffTimeMillis = if (lastTimeMillis > nowTimeMillis) lastTimeMillis - nowTimeMillis else 0L
var diffTimeMillis =
if (lastTimeMillis > nowTimeMillis) lastTimeMillis - nowTimeMillis else 0L

viewModelScope.launch(Dispatchers.IO) {
var oldTimeMillis = System.currentTimeMillis()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import androidx.work.ForegroundInfo
import androidx.work.WorkManager
import androidx.work.WorkerParameters
import com.stop.R
import com.stop.util.AlarmFunctions
import com.stop.domain.usecase.nearplace.GetNearPlacesUseCase
import com.stop.util.isMoreThanOreo
import com.stop.util.makeFullTime
import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.ALARM_TIME
import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.LAST_TIME
import com.stop.util.AlarmFunctions
import com.stop.util.isMoreThanOreo
import com.stop.util.makeFullTime
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -44,7 +44,7 @@ class LastTimeCheckWorker @AssistedInject constructor(
return Result.success()
}

private fun initData(){
private fun initData() {
lastTime = inputData.getString(LAST_TIME) ?: ""
}

Expand Down Expand Up @@ -83,7 +83,7 @@ class LastTimeCheckWorker @AssistedInject constructor(

private suspend fun checkLastTransportTime() {
while (isStopped.not()) {
//TODO 막차시간 가져오는 API로 변경 해야합니다.
//TODO 막차시간 가져오는 API로 변경 해야합니다.
getNearPlacesUseCase(
"아남타워",
126.969652,
Expand All @@ -92,7 +92,7 @@ class LastTimeCheckWorker @AssistedInject constructor(

val resultLastTime = "21:04:00"

if(lastTime != resultLastTime && resultLastTime != null){
if (lastTime != resultLastTime && resultLastTime != null) {
lastTime = resultLastTime
alarmFunctions.cancelAlarm()
alarmFunctions.callAlarm(resultLastTime, alarmTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import android.content.Context
import android.content.Intent
import com.stop.AlarmActivity
import com.stop.R
import com.stop.util.isMoreThanOreo
import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.ALARM_CODE
import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.ALARM_NOTIFICATION_HIGH_ID
import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.ALARM_NOTIFICATION_ID
import com.stop.util.getActivityPendingIntent
import com.stop.util.getAlarmHighNotification
import com.stop.util.isMoreThanOreo

class AlarmReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(ALARM_NOTIFICATION_ID)

val content = context.getString(R.string.alarm_content_text)
Expand All @@ -28,7 +29,8 @@ class AlarmReceiver : BroadcastReceiver() {
val alarmStartPendingIntent = context.getActivityPendingIntent(
Intent(context, AlarmActivity::class.java).apply {
putExtra("ALARM_CODE", ALARM_CODE)
flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
flags =
Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
},
ALARM_CODE
)
Expand All @@ -44,7 +46,8 @@ class AlarmReceiver : BroadcastReceiver() {

Intent(context, AlarmActivity::class.java).apply {
putExtra("ALARM_CODE", ALARM_CODE)
flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
flags =
Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
context.startActivity(this)
}
}
Expand Down
18 changes: 13 additions & 5 deletions presentation/src/main/java/com/stop/ui/alarmstart/SoundService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import android.content.Context
import android.content.Intent
import android.media.MediaPlayer
import android.media.RingtoneManager
import android.os.*
import android.os.CombinedVibration
import android.os.PowerManager
import android.os.VibrationEffect
import android.os.Vibrator
import android.os.VibratorManager
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import com.stop.domain.usecase.alarm.GetAlarmUseCase
import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.ALARM_CODE
import com.stop.util.getBroadcastPendingIntent
import com.stop.util.isMoreThanOreoUnderRedVelVet
import com.stop.util.isMoreThanSnow
import com.stop.util.isUnderOreo
import com.stop.ui.alarmsetting.AlarmSettingFragment.Companion.ALARM_CODE
import com.stop.util.getBroadcastPendingIntent
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
Expand All @@ -37,7 +41,10 @@ class SoundService : LifecycleService() {
getAlarmUseCase().collectLatest { alarmData ->
alarmData?.let {
if (it.alarmMethod) {
mediaPlayer = MediaPlayer.create(this@SoundService, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)).apply {
mediaPlayer = MediaPlayer.create(
this@SoundService,
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
).apply {
setWakeMode(this@SoundService, PowerManager.PARTIAL_WAKE_LOCK)
isLooping = true
start()
Expand All @@ -55,7 +62,8 @@ class SoundService : LifecycleService() {
val effect = VibrationEffect.createWaveform(pattern, amplitude, 0)
vibrator?.vibrate(effect)
} else if (isMoreThanSnow()) {
vibratorManager = getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
vibratorManager =
getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager
val effect = VibrationEffect.createWaveform(pattern, amplitude, 0)
val combinedVibration = CombinedVibration.createParallel(effect)
vibratorManager?.vibrate(combinedVibration)
Expand Down
Loading