Skip to content

Commit

Permalink
[add/#135] health foreground service type
Browse files Browse the repository at this point in the history
  • Loading branch information
Jokwanhee committed Mar 30, 2024
1 parent b4269b7 commit ad59372
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 38 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_HEALTH" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

Expand Down
53 changes: 17 additions & 36 deletions app/src/main/java/sopt/motivoo/presentation/home/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,43 +38,20 @@ import timber.log.Timber
@AndroidEntryPoint
class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home) {
private val viewModel: HomeViewModel by activityViewModels()
val alarmManager = context?.getSystemService(Context.ALARM_SERVICE) as AlarmManager

lateinit var alarmManager: AlarmManager

private val requestHomePermissionRequest = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { permissions ->
var permissionGranted = true
var educationGranted = false

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
Timber.e("aaa can do it : ${alarmManager.canScheduleExactAlarms()} ")
}
permissions.entries.forEach {
Timber.e("aaa it.key : ${it.key} / it.value : ${it.value}")
if (it.key in Manifest.permission.POST_NOTIFICATIONS && it.value == false) {
permissionGranted = false
}
if (it.key in Manifest.permission.ACTIVITY_RECOGNITION && it.value == false) {
if (it.key in HOME_REQUIRED_PERMISSIONS && it.value == false) {
permissionGranted = false
}

if (!shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS) && it.value == false) {
educationGranted = true
}
if (!shouldShowRequestPermissionRationale(Manifest.permission.ACTIVITY_RECOGNITION) && it.value == false) {
if (!shouldShowRequestPermissionRationale(it.key) && it.value == false) {
educationGranted = true
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (!alarmManager.canScheduleExactAlarms()) {
permissionGranted = false
educationGranted = false
} else {
permissionGranted = true
educationGranted = true
}
}
}

if (!permissionGranted) {
Expand All @@ -88,6 +65,7 @@ class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.vm = viewModel
alarmManager = context?.getSystemService(Context.ALARM_SERVICE) as AlarmManager
backPressed()

initHomePermissionsState()
Expand Down Expand Up @@ -199,15 +177,7 @@ class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home
if (checkPermission()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (!alarmManager.canScheduleExactAlarms()) {
requireContext().showSnackbar(
binding.root,
"자정마다 걸음 수를 초기화하려면 알림 및 리마인더를 허용해주세요.",
"설정으로 이동",
true
) {
val intent = Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM)
startActivity(intent)
}
requestScheduleExactAlarm()
} else {
startStepCountService(homeState.homeData.userId.toInt())
}
Expand All @@ -217,6 +187,18 @@ class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home
}
}

private fun requestScheduleExactAlarm() {
requireContext().showSnackbar(
binding.root,
getString(R.string.home_exact_alarm_denied_reminder),
getString(R.string.home_setting),
true
) {
val intent = Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM)
startActivity(intent)
}
}

private fun handleBackNavStack() {
findNavController().currentBackStackEntry?.savedStateHandle?.getLiveData<Int>(
HOME_STATE_CONFIRM
Expand Down Expand Up @@ -341,7 +323,6 @@ class HomeFragment : BindingFragment<FragmentHomeBinding>(R.layout.fragment_home
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
add(Manifest.permission.POST_NOTIFICATIONS)
add(Manifest.permission.SCHEDULE_EXACT_ALARM)
}
}.toTypedArray()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class StepCountService : LifecycleService() {
getString(R.string.notification_title, stepCount),
getString(R.string.notification_content)
),
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
ServiceInfo.FOREGROUND_SERVICE_TYPE_HEALTH
)
} else {
startForeground(
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
<string name="home_parent">부모</string>
<string name="verify_exercise">운동 인증이 완료되었어요!</string>
<string name="verify_exercise_description">오늘도 멋지게 운동을 해내셨군요</string>
<string name="home_exact_alarm_denied_reminder">자정마다 걸음 수를 초기화하려면 알림 및 리마인더를 허용해주세요.</string>
<string name="home_setting">설정으로 이동</string>
<string name="motivoo_logo">motivoo_logo</string>

<!-- my page -->
Expand Down

0 comments on commit ad59372

Please sign in to comment.