Skip to content

Commit

Permalink
[fix]: fix OnBoardingActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
kez-lab committed Jan 8, 2024
1 parent 51fbf46 commit d905266
Show file tree
Hide file tree
Showing 28 changed files with 280 additions and 389 deletions.
3 changes: 1 addition & 2 deletions app/src/main/java/com/hmh/hamyeonham/SampleActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.hmh.hamyeonham.common.view.viewBinding
import com.hmh.hamyeonham.databinding.ActivitySampleBinding
import com.hmh.hamyeonham.feature.onboarding.OnBoardingActivity
import com.hmh.hamyeonham.feature.onboarding.OnBoardingSelectDataActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand All @@ -22,7 +21,7 @@ class SampleActivity : AppCompatActivity() {
val splashScreen = installSplashScreen()
initSplashAnimation(splashScreen)
setContentView(binding.root)
startActivity(Intent(this, OnBoardingSelectDataActivity::class.java))
startActivity(Intent(this, OnBoardingActivity::class.java))
finish()
}

Expand Down
1 change: 1 addition & 0 deletions data/onboarding/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
10 changes: 10 additions & 0 deletions data/onboarding/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
hmh("feature")
}

android {
namespace = "com.hmh.hamyeonham.onboarding"
}

dependencies {}
Empty file.
21 changes: 21 additions & 0 deletions data/onboarding/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
4 changes: 4 additions & 0 deletions data/onboarding/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
2 changes: 1 addition & 1 deletion feature/onboarding/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
android:name=".OnBoardingDoneSingUpActivity"
android:exported="false" />
<activity
android:name=".OnBoardingSelectDataActivity"
android:name=".OnBoardingActivity"
android:exported="false" />
<activity
android:name=".OnBoardingActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,113 +1,57 @@
package com.hmh.hamyeonham.feature.onboarding

import android.app.usage.UsageStatsManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.hmh.hamyeonham.common.context.toast
import com.hmh.hamyeonham.common.navigation.NavigationProvider
import com.hmh.hamyeonham.common.view.viewBinding
import com.hmh.hamyeonham.feature.onboarding.databinding.ActivityOnBoardingBinding
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class OnBoardingActivity : AppCompatActivity() {

private val binding by viewBinding(ActivityOnBoardingBinding::inflate)

@Inject
lateinit var navigationProvider: NavigationProvider

private val accessibilitySettingsLauncher: ActivityResultLauncher<Intent> =
registerForActivityResult(
ActivityResultContracts.StartActivityForResult(),
) {
if (isAccessibilityServiceEnabled()) {
toast("접근성 서비스가 활성화되었습니다.")
} else {
toast("접근성 서비스가 활성화되지 않았습니다.")
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
clickRequireAccessibilityBtn()
initViewPager()
}

private fun clickRequireAccessibilityBtn() {
binding.btnAccessibility.setOnClickListener {
openAccessibilitySettingsIfNeeded()
}
binding.btnUsage.setOnClickListener {
requestUsageAccessPermission()
}
binding.btnDrawOnOthers.setOnClickListener {
if (!hasOverlayPermission()) {
requestOverlayPermission()
} else {
toast("다른 앱 위에 그리기 권한이 이미 허용되어 있습니다.")
}
private fun initViewPager() {
val pagerAdapter = setOnboardingPageAdapter()
binding.btnOnboardingNext.setOnClickListener {
navigateToNextOnboardingStep(pagerAdapter)
}
}

private fun requestUsageAccessPermission() {
if (!hasUsageStatsPermission()) {
try {
val packageUri = Uri.parse("package:$packageName")
val intent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS, packageUri)
startActivity(intent)
} catch (e: Exception) {
val intent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
startActivity(intent)
private fun navigateToNextOnboardingStep(pagerAdapter: OnBoardingFragmentStateAdapter) {
binding.vpOnboardingContainer.let { viewPager ->
val currentItem = viewPager.currentItem
val lastItem = pagerAdapter.itemCount - 1
when {
currentItem < lastItem -> viewPager.currentItem = currentItem + 1
currentItem == lastItem -> startOnBoardingDoneSignUpActivity()
}
} else {
toast("사용 정보 접근 권한이 이미 허용되어 있습니다.")
}
}

private fun requestOverlayPermission() {
val packageUri = Uri.parse("package:$packageName")
val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, packageUri)
private fun startOnBoardingDoneSignUpActivity() {
val intent = Intent(this, OnBoardingDoneSingUpActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
startActivity(intent)
finish()
}

private fun hasOverlayPermission(): Boolean {
return Settings.canDrawOverlays(this)
}

private fun isAccessibilityServiceEnabled(): Boolean {
val service = packageName + "/" + OnBoardingAccessibilityService::class.java.canonicalName
val enabledServicesSetting = Settings.Secure.getString(
contentResolver,
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
)
return enabledServicesSetting?.contains(service) == true
}

private fun openAccessibilitySettingsIfNeeded() {
if (!isAccessibilityServiceEnabled()) {
val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
accessibilitySettingsLauncher.launch(intent)
} else {
toast("접근성 권한이 이미 허용되어 있습니다.")
private fun setOnboardingPageAdapter(): OnBoardingFragmentStateAdapter {
val pagerAdapter = OnBoardingFragmentStateAdapter(this)
binding.vpOnboardingContainer.run {
adapter = pagerAdapter
isUserInputEnabled = false
}
}

private fun hasUsageStatsPermission(): Boolean {
val usageStatsManager = getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager
val time = System.currentTimeMillis()
val stats = usageStatsManager.queryUsageStats(
UsageStatsManager.INTERVAL_DAILY,
time - 1000 * 60,
time,
)
return stats != null && stats.isNotEmpty()
return pagerAdapter
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,41 @@ package com.hmh.hamyeonham.feature.onboarding
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.hmh.hamyeonham.feature.onboarding.fragment.OnBoardingRequestPermissionFragment
import com.hmh.hamyeonham.feature.onboarding.fragment.OnBoardingSelectAppFragment
import com.hmh.hamyeonham.feature.onboarding.fragment.OnBoardingSelectDataFragment
import com.hmh.hamyeonham.feature.onboarding.fragment.OnBoardingSelectScreenTimeFragment

enum class OnBoardingFragmentType {
REQUEST_PERMISSION,
SELECT_APP,
SELECT_SCREEN_TIME_GOAL,
SELECT_DATA_TIME,
SELECT_DATA_PROBLEM,
SELECT_DATA_PERIOD,
SELECT_USE_TIME_GOAL,
}

class OnBoardingFragmentStateAdapter(fragmentActivity: FragmentActivity) :
FragmentStateAdapter(fragmentActivity) {
var onBoardingFragments: ArrayList<Fragment> = ArrayList()
override fun getItemCount(): Int {
return onBoardingFragments.size
return OnBoardingFragmentType.entries.size
}

override fun createFragment(position: Int): Fragment {
return onBoardingFragments[position]
}

fun removeOnboardingFragments() {
onBoardingFragments.removeLast()
}

fun addOnboardingFragment(fragmentType: OnBoardingFragmentType) {
val fragment = when (fragmentType) {
return when (val fragmentType = position.toOnBoardingFragmentType()) {
OnBoardingFragmentType.SELECT_DATA_TIME -> OnBoardingSelectDataFragment.newInstance(fragmentType)
OnBoardingFragmentType.SELECT_DATA_PROBLEM -> OnBoardingSelectDataFragment.newInstance(fragmentType)
OnBoardingFragmentType.SELECT_DATA_PERIOD -> OnBoardingSelectDataFragment.newInstance(fragmentType)
OnBoardingFragmentType.SELECT_SCREEN_TIME_GOAL -> OnBoardingSelectScreenTimeFragment()
OnBoardingFragmentType.REQUEST_PERMISSION -> OnBoardingRequestPermissionFragment()
OnBoardingFragmentType.SELECT_APP -> OnBoardingSelectAppFragment()
OnBoardingFragmentType.SELECT_SCREENTIME_GOAL -> OnBoardingSelectScreentimeGoalFragment()
OnBoardingFragmentType.SELECT_DATA_TIME -> OnBoardingSelectDataFragment.newInstance(
OnBoardingQuestionList.OnBoardingQuestionTime[0],
OnBoardingQuestionList.OnBoardingQuestionTime[1],
OnBoardingQuestionList.OnBoardingQuestionTime[2],
OnBoardingQuestionList.OnBoardingQuestionTime[3],
OnBoardingQuestionList.OnBoardingQuestionTime[4],
OnBoardingQuestionList.OnBoardingQuestionTime[5],
)

OnBoardingFragmentType.SELECT_DATA_PROBLEM -> OnBoardingSelectDataFragment.newInstance(
OnBoardingQuestionList.OnBoardingQuestionProblem[0],
OnBoardingQuestionList.OnBoardingQuestionProblem[1],
OnBoardingQuestionList.OnBoardingQuestionProblem[2],
OnBoardingQuestionList.OnBoardingQuestionProblem[3],
OnBoardingQuestionList.OnBoardingQuestionProblem[4],
OnBoardingQuestionList.OnBoardingQuestionProblem[5],
)

OnBoardingFragmentType.SELECT_DATA_PERIOD -> OnBoardingSelectDataFragment.newInstance(
OnBoardingQuestionList.OnBoardingChallengePeriod[0],
OnBoardingQuestionList.OnBoardingChallengePeriod[1],
OnBoardingQuestionList.OnBoardingChallengePeriod[2],
OnBoardingQuestionList.OnBoardingChallengePeriod[3],
OnBoardingQuestionList.OnBoardingChallengePeriod[4],
OnBoardingQuestionList.OnBoardingChallengePeriod[5],
)

OnBoardingFragmentType.SELECT_USE_TIME_GOAL -> OnBoardingSelectUseTimeGoalFragment()
else -> {
throw IllegalArgumentException("Unknown fragment type")
}
OnBoardingFragmentType.SELECT_USE_TIME_GOAL -> OnBoardingSelectScreenTimeFragment()
}
onBoardingFragments.add(fragment)
}

private fun Int.toOnBoardingFragmentType(): OnBoardingFragmentType {
return OnBoardingFragmentType.entries.getOrNull(this)
?: OnBoardingFragmentType.SELECT_DATA_TIME
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit d905266

Please sign in to comment.