Skip to content

Commit

Permalink
[feat] #14 권한 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
l2zh committed Jan 5, 2024
1 parent 0344382 commit 690c296
Show file tree
Hide file tree
Showing 12 changed files with 427 additions and 67 deletions.
16 changes: 16 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-feature
android:name="android.hardware.camera"
android:required="true" />

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

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

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

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

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

<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />

<application
android:name=".MotivooApplication"
android:allowBackup="true"
Expand Down
10 changes: 1 addition & 9 deletions app/src/main/java/sopt/motivoo/presentation/LoginFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@ package sopt.motivoo.presentation

import android.os.Bundle
import android.view.View
import androidx.navigation.fragment.findNavController
import sopt.motivoo.R
import sopt.motivoo.databinding.FragmentLoginBinding
import sopt.motivoo.util.binding.BindingFragment

class LoginFragment : BindingFragment<FragmentLoginBinding>(R.layout.fragment_login) {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.btnLogin.setOnClickListener {
navigateToHome()
}
}

private fun navigateToHome() {
findNavController().navigate(R.id.action_loginFragment_to_homeFragment)
}
}
19 changes: 0 additions & 19 deletions app/src/main/java/sopt/motivoo/presentation/SplashFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sopt.motivoo.presentation

import android.os.Bundle
import android.view.View
import androidx.navigation.fragment.findNavController
import sopt.motivoo.R
import sopt.motivoo.databinding.FragmentSplashBinding
import sopt.motivoo.util.binding.BindingFragment
Expand All @@ -11,23 +10,5 @@ class SplashFragment : BindingFragment<FragmentSplashBinding>(R.layout.fragment_

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.btnSplash.setOnClickListener {
navigateToLogin()
}

binding.btnWebview.setOnClickListener {
navigateToWebView()
}
}

private fun navigateToLogin() {
findNavController().navigate(R.id.action_splashFragment_to_loginFragment)
}

private fun navigateToWebView() {
val action = SplashFragmentDirections
.actionSplashFragmentToWebViewFragment("https://velog.io/@gnwjd309/GitHub-commit-history")
findNavController().navigate(action)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package sopt.motivoo.presentation.intro

import android.Manifest
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.navigation.fragment.findNavController
import sopt.motivoo.R
import sopt.motivoo.databinding.FragmentPermissionBinding
import sopt.motivoo.util.binding.BindingFragment

class PermissionFragment :
BindingFragment<FragmentPermissionBinding>(R.layout.fragment_permission) {

private val deniedPermissions = mutableSetOf<String>()
private var requiredPermissions = mutableSetOf<String>()

private val requestMultiplePermissionsLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
permissions.entries.forEach { (permission, granted) ->
if (granted) {
requiredPermissions.remove(permission)
} else if (shouldShowRequestPermissionRationale(permission)) {
deniedPermissions.add(permission)
requiredPermissions.remove(permission)
}
}
if (requiredPermissions.isEmpty()) {
navigateToLogin()
}
return@registerForActivityResult
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

initRequiredPermissions()
if (checkPermissionsStatus()) {
navigateToLogin()
} else {
getPermission()
}
}

private fun initRequiredPermissions() {
val isAndroidTOrAbove = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU
val isAndroidQOrAbove = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q

val storagePermission = if (isAndroidTOrAbove) {
Manifest.permission.READ_MEDIA_IMAGES
} else {
Manifest.permission.READ_EXTERNAL_STORAGE
}

val activityRecognitionPermission = if (isAndroidQOrAbove) {
Manifest.permission.ACTIVITY_RECOGNITION
} else {
Manifest.permission.BODY_SENSORS
}
val permissions = listOf(
Manifest.permission.CAMERA,
storagePermission,
activityRecognitionPermission
)

for (permission in permissions) {
if (!shouldShowRequestPermissionRationale(permission)) {
requiredPermissions.add(permission)
}
}
}

private fun checkPermissionsStatus(): Boolean {
for (permission in requiredPermissions) {
if (!shouldShowRequestPermissionRationale(permission)) {
return false
}
}
return true
}

private fun getPermission() {
binding.btnPermissionDone.setOnClickListener { checkAndRequestPermissions() }
}

private fun checkAndRequestPermissions() {
val permissionsToRequest = requiredPermissions.filterNot { deniedPermissions.contains(it) }

if (permissionsToRequest.isNotEmpty()) {
requestPermissions(permissionsToRequest.toTypedArray())
}
}

private fun requestPermissions(permissions: Array<String>) {
requestMultiplePermissionsLauncher.launch(permissions)
}

private fun navigateToLogin() {
findNavController().navigate(R.id.action_permissionFragment_to_loginFragment)
}
}
16 changes: 16 additions & 0 deletions app/src/main/res/drawable/ic_health.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="40dp"
android:height="40dp"
android:viewportWidth="40"
android:viewportHeight="40">
<path
android:fillColor="#F4F5F9"
android:pathData="M20,0L20,0A20,20 0,0 1,40 20L40,20A20,20 0,0 1,20 40L20,40A20,20 0,0 1,0 20L0,20A20,20 0,0 1,20 0z" />
<path
android:fillColor="#00000000"
android:pathData="M29,20H26.038C25.827,20 25.722,20 25.633,20.042C25.554,20.079 25.485,20.138 25.431,20.215C25.371,20.303 25.34,20.42 25.277,20.656L23.46,27.501C23.227,28.376 23.111,28.814 22.936,28.927C22.785,29.025 22.601,29.024 22.451,28.925C22.276,28.81 22.163,28.371 21.937,27.494L18.063,12.506C17.837,11.629 17.724,11.19 17.549,11.075C17.399,10.976 17.215,10.975 17.064,11.073C16.889,11.186 16.773,11.624 16.54,12.499L14.723,19.344C14.66,19.58 14.629,19.697 14.568,19.785C14.515,19.862 14.446,19.921 14.367,19.958C14.278,20 14.173,20 13.962,20H11"
android:strokeWidth="1.5"
android:strokeColor="#303031"
android:strokeLineCap="round"
android:strokeLineJoin="round" />
</vector>
16 changes: 0 additions & 16 deletions app/src/main/res/drawable/ic_helth.xml

This file was deleted.

19 changes: 19 additions & 0 deletions app/src/main/res/drawable/selector_button_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true">
<ripple android:color="@color/gray_800_303031">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/gray_900_161616" />
<corners android:radius="8dp" />
</shape>
</item>
</ripple>
</item>
<item android:state_enabled="false">
<shape android:shape="rectangle">
<solid android:color="@color/gray_100_F4F5F9" />
<corners android:radius="8dp" />
</shape>
</item>
</selector>
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/selector_text_color.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white_FFFFFF" android:state_enabled="true" />

<item android:color="@color/black_090909" android:state_enabled="false" />
</selector>
Loading

0 comments on commit 690c296

Please sign in to comment.