-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
427 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
app/src/main/java/sopt/motivoo/presentation/intro/PermissionFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.