diff --git a/feature/onboarding/src/androidTest/java/com/hmh/hamyeonham/feature/onboarding/ExampleInstrumentedTest.kt b/feature/onboarding/src/androidTest/java/com/hmh/hamyeonham/feature/onboarding/ExampleInstrumentedTest.kt deleted file mode 100644 index 152c7bd4..00000000 --- a/feature/onboarding/src/androidTest/java/com/hmh/hamyeonham/feature/onboarding/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package com.hmh.hamyeonham.feature.onboarding - -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.ext.junit.runners.AndroidJUnit4 - -import org.junit.Test -import org.junit.runner.RunWith - -import org.junit.Assert.* - -/** - * Instrumented test, which will execute on an Android device. - * - * See [testing documentation](http://d.android.com/tools/testing). - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getInstrumentation().targetContext - assertEquals("com.hmh.hamyeonham.feature.onboarding.test", appContext.packageName) - } -} \ No newline at end of file diff --git a/feature/onboarding/src/main/AndroidManifest.xml b/feature/onboarding/src/main/AndroidManifest.xml index 0a100119..a2fcd80d 100644 --- a/feature/onboarding/src/main/AndroidManifest.xml +++ b/feature/onboarding/src/main/AndroidManifest.xml @@ -1,10 +1,29 @@ - + + + + + - + + + + + + + + diff --git a/feature/onboarding/src/main/java/com/hmh/hamyeonham/feature/onboarding/OnBoardingAccessibilityService.kt b/feature/onboarding/src/main/java/com/hmh/hamyeonham/feature/onboarding/OnBoardingAccessibilityService.kt new file mode 100644 index 00000000..774a15ed --- /dev/null +++ b/feature/onboarding/src/main/java/com/hmh/hamyeonham/feature/onboarding/OnBoardingAccessibilityService.kt @@ -0,0 +1,19 @@ +package com.hmh.hamyeonham.feature.onboarding + +import android.accessibilityservice.AccessibilityService +import android.util.Log +import android.view.accessibility.AccessibilityEvent +import com.hmh.hamyeonham.common.context.toast + +class OnBoardingAccessibilityService : AccessibilityService() { + override fun onAccessibilityEvent(event: AccessibilityEvent) { + if (event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) { + val packageName = event.packageName ?: return + Log.d("AccessibilityService", "현재 실행 중인 앱 패키지: $packageName") + } + } + + override fun onInterrupt() { + + } +} diff --git a/feature/onboarding/src/main/java/com/hmh/hamyeonham/feature/onboarding/OnBoardingActivity.kt b/feature/onboarding/src/main/java/com/hmh/hamyeonham/feature/onboarding/OnBoardingActivity.kt index 00e84cf4..3d88903b 100644 --- a/feature/onboarding/src/main/java/com/hmh/hamyeonham/feature/onboarding/OnBoardingActivity.kt +++ b/feature/onboarding/src/main/java/com/hmh/hamyeonham/feature/onboarding/OnBoardingActivity.kt @@ -1,21 +1,106 @@ 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.feature.onboarding.databinding.ActivityOnBoardingBinding class OnBoardingActivity : AppCompatActivity() { + private lateinit var binding: ActivityOnBoardingBinding + + private val accessibilitySettingsLauncher: ActivityResultLauncher = + registerForActivityResult( + ActivityResultContracts.StartActivityForResult(), + ) { + if (isAccessibilityServiceEnabled()) { + toast("접근성 서비스가 활성화되었습니다.") + } else { + toast("접근성 서비스가 활성화되지 않았습니다.") + } + } + override fun onCreate(savedInstanceState: Bundle?) { binding = ActivityOnBoardingBinding.inflate(layoutInflater) super.onCreate(savedInstanceState) setContentView(binding.root) + clickRequireAccessibilityBtn() + } - binding.run { - numberPicker.npCustomHours.minValue = 1 - numberPicker.npCustomHours.maxValue = 6 - numberPicker.npCustomMinutes.minValue = 1 - numberPicker.npCustomMinutes.maxValue = 59 + private fun clickRequireAccessibilityBtn() { + binding.btnAccessibility.setOnClickListener { + openAccessibilitySettingsIfNeeded() + } + binding.btnUsage.setOnClickListener { + requestUsageAccessPermission() } + binding.btnDrawOnOthers.setOnClickListener { + if (!hasOverlayPermission()) { + requestOverlayPermission() + } else { + toast("다른 앱 위에 그리기 권한이 이미 허용되어 있습니다.") + } + } + } + + 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) + } + } else { + toast("사용 정보 접근 권한이 이미 허용되어 있습니다.") + } + } + + private fun requestOverlayPermission() { + val packageUri = Uri.parse("package:$packageName") + val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, packageUri) + startActivity(intent) + } + + 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 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() } } diff --git a/feature/onboarding/src/main/res/layout/activity_on_boarding.xml b/feature/onboarding/src/main/res/layout/activity_on_boarding.xml index 4339e9b8..0be76867 100644 --- a/feature/onboarding/src/main/res/layout/activity_on_boarding.xml +++ b/feature/onboarding/src/main/res/layout/activity_on_boarding.xml @@ -1,16 +1,38 @@ - + android:layout_marginBottom="30dp"/> +