Skip to content

Commit

Permalink
fix: 예외 처리 및 권한 확인
Browse files Browse the repository at this point in the history
  • Loading branch information
easyhz committed Apr 11, 2024
1 parent e6a092d commit 740a253
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 45 deletions.
32 changes: 18 additions & 14 deletions app/src/main/java/com/easyhz/picly/provider/PiclyFileProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@ class PiclyFileProvider: FileProvider(R.xml.file_paths) {
* @return 캐시 파일 Content Uri
*/
suspend fun getIncomingImageUri(context: Context, uri: Uri): Uri {
val directory = File(context.cacheDir, "images")
directory.mkdirs()
val file = File.createTempFile(
"incoming_image_",
".jpeg",
directory,
)
context.saveImage(uri, file)
val authority = context.packageName + ".fileprovider"
return getUriForFile(
context,
authority,
file,
)
try {
val directory = File(context.cacheDir, "images")
directory.mkdirs()
val file = File.createTempFile(
"incoming_image_",
".jpeg",
directory,
)
context.saveImage(uri, file)
val authority = context.packageName + ".fileprovider"
return getUriForFile(
context,
authority,
file,
)
} catch (e: Exception) {
throw e
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ import com.easyhz.picly.util.toTimeFormat
import com.easyhz.picly.util.toTimeFormat24
import com.easyhz.picly.view.album.upload.gallery.GalleryBottomSheetFragment
import com.easyhz.picly.view.album.upload.gallery.GalleryImageAdapter.Companion.MAX_SELECTED
import com.easyhz.picly.view.dialog.EitherDialog
import com.easyhz.picly.view.dialog.LoadingDialog
import com.easyhz.picly.view.dialog.Orientation
import com.easyhz.picly.view.navigation.NavControllerManager
import com.google.android.material.textfield.TextInputLayout
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -73,9 +71,11 @@ class UploadFragment: Fragment() {
private var isShowTimePicker: Boolean = false
private var isGranted: Boolean = false
private val args: UploadFragmentArgs by navArgs()
// private val galleryPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
// this.isGranted = isGranted
// }
private val galleryPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted ->
this.isGranted = isGranted
if (isGranted) getIncomingImages()
else showGalleryPermissionDialog()
}
private var bottomSheetFragment: GalleryBottomSheetFragment? = null
private lateinit var getResult: ActivityResultLauncher<Intent>

Expand Down Expand Up @@ -103,7 +103,7 @@ class UploadFragment: Fragment() {
}

private fun setUp() {
getIncomingImages()
checkPermission()
initCalendarView()
initTimePicker()
setTagField()
Expand All @@ -121,9 +121,27 @@ class UploadFragment: Fragment() {
setActivityResultLauncher()
}

private fun checkPermission() {
if (args.incomingImages.isNullOrEmpty()) return
if (ContextCompat.checkSelfPermission(
requireContext(),
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
) {
isGranted = true
getIncomingImages()
} else {
galleryPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
}
}

private fun getIncomingImages() {
loading.show(true)
if (!isStoragePermissionGranted()) {
showGalleryPermissionDialog()
return
}
CoroutineScope(Dispatchers.Main).launch {
loading.show(true)
try {
val incomingImages = args.incomingImages?.mapNotNull {
PiclyFileProvider.getIncomingImageUri(requireContext(), it)
Expand All @@ -132,7 +150,7 @@ class UploadFragment: Fragment() {
viewModel.addSelectedImageList(it)
}
} catch (e: Exception) {
BlueSnackBar.make(binding.root, getString(R.string.incoming_image_error))
BlueSnackBar.make(binding.root, getString(R.string.incoming_image_error)).show()
} finally {
loading.show(false)
}
Expand Down Expand Up @@ -446,31 +464,23 @@ class UploadFragment: Fragment() {
setButtonTextColor(binding.expireTimeButton, isShowTimePicker)
}
}
private fun initViews() = with(binding) {
// if (ContextCompat.checkSelfPermission(
// requireContext(),
// Manifest.permission.READ_EXTERNAL_STORAGE
// ) == PackageManager.PERMISSION_GRANTED
// ) {
// isGranted = true
// } else {
// galleryPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
// }
}

private fun showGalleryPermissionDialog() {
val dialog = EitherDialog.instance(
title = getString(R.string.dialog_gallery_permission_title),
message = getString(R.string.dialog_gallery_permission_message),
Orientation.VERTICAL
showAlertDialog(
context = requireContext(),
title= R.string.dialog_gallery_permission_title,
message = R.string.dialog_gallery_permission_message,
positiveButtonText = R.string.dialog_gallery_permission_positive,
onContinue = {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.data = Uri.parse("package:" + requireActivity().packageName)
startActivity(intent)
},
negativeButtonText = R.string.dialog_upload_negative,
onCancel = {
BlueSnackBar.make(binding.root, getString(R.string.incoming_image_error))
}
)
dialog.setPositiveButton(getString(R.string.dialog_gallery_permission_positive), ContextCompat.getColor(requireActivity(), R.color.highlightBlue)) {
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
intent.data = Uri.parse("package:" + requireActivity().packageName)
startActivity(intent)
}.setNegativeButton(getString(R.string.dialog_gallery_permission_negative), ContextCompat.getColor(requireActivity(), R.color.secondText)) {

}.show(requireActivity().supportFragmentManager)
}

private fun showUrlDialog(url: String) {
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
<string name="dialog_delete_message">정말로 삭제하시겠습니까?</string>

<string name="dialog_gallery_permission_title">권한 설정</string>
<string name="dialog_gallery_permission_message">파일 및 미디어 접근을 위해 앱 설정에서 권한을 설정해주세요.</string>
<string name="dialog_gallery_permission_message">파일 및 미디어 접근을 위해 앱 설정에서 권한을 설정해 주세요.</string>
<string name="dialog_gallery_permission_positive">앱 설정</string>
<string name="dialog_gallery_permission_negative">창 닫기</string>

Expand Down Expand Up @@ -147,4 +147,6 @@
+버튼을 눌러 앨범을 업로드하고 공유해보세요.
</string>
<string name="incoming_image_error">이미지를 불러오는데 실패하였습니다.</string>
<string name="check_permission_message">앱 설정에서 권한을 설정해 주세요.</string>

</resources>

0 comments on commit 740a253

Please sign in to comment.