Skip to content

Commit

Permalink
permissions updated
Browse files Browse the repository at this point in the history
  • Loading branch information
umair committed Jul 28, 2024
1 parent 7baefd2 commit 2b71431
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 60 deletions.
5 changes: 2 additions & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
android:maxSdkVersion="32"
tools:ignore="ScopedStorage" />

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

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
Expand All @@ -13,6 +14,7 @@ import android.view.ViewGroup
import android.view.Window
import android.view.inputmethod.InputMethodManager
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
Expand All @@ -31,6 +33,7 @@ class Home : Fragment() {
private val viewModel: TiktokViewModel by activityViewModels()
private var videoId: String? = null

@RequiresApi(Build.VERSION_CODES.TIRAMISU)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand Down Expand Up @@ -108,16 +111,26 @@ class Home : Fragment() {
}


@RequiresApi(Build.VERSION_CODES.TIRAMISU)
private fun startReadExternalStoragePermission() {
this.requestReadExternalPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
if(Build.VERSION.SDK_INT>Build.VERSION_CODES.R){
requestReadExternalPermissionLauncher.launch(Manifest.permission.READ_MEDIA_VIDEO)
}else{
requestReadExternalPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
}
}


private val requestReadExternalPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
startWriteExternalStoragePermission()
if(Build.VERSION.SDK_INT<=Build.VERSION_CODES.Q){
startWriteExternalStoragePermission()
}else{
performDownload()
}

} else {
viewModel.showAlertDialog(
getString(R.string.media_permission),
Expand All @@ -128,84 +141,87 @@ class Home : Fragment() {
}

private fun startWriteExternalStoragePermission() {
this.requestWriteExternalPermissionLauncher.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
requestWriteExternalPermissionLauncher.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}

private val requestWriteExternalPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
if (binding.edUrl.text.toString().isNotEmpty()) {
lifecycleScope.launch {
if (Utils.isValidUrl(binding.edUrl.text.toString())) {
performDownload()
} else {

viewModel.showAlertDialog(
getString(R.string.media_permission),
getString(R.string.please_allow_media_permission_to_continue),
requireContext()

val dialogBuilder = AlertDialog.Builder(context)
val inflater = requireActivity().layoutInflater
val dialogView: View =
inflater.inflate(R.layout.fetching_dialog, null, false)
dialogBuilder.setView(dialogView)
val alertDialog = dialogBuilder.create()
alertDialog.setCancelable(false)
)
}
}


private fun hideKeyboard(view: View) {
val inputMethodManager = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}

private fun performDownload(){
if (binding.edUrl.text.toString().isNotEmpty()) {
lifecycleScope.launch {
if (Utils.isValidUrl(binding.edUrl.text.toString())) {

val dialogBuilder = AlertDialog.Builder(context)
val inflater = requireActivity().layoutInflater
val dialogView: View =
inflater.inflate(R.layout.fetching_dialog, null, false)
dialogBuilder.setView(dialogView)
val alertDialog = dialogBuilder.create()
alertDialog.setCancelable(false)

alertDialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
alertDialog.window!!.attributes.windowAnimations = R.style.DialogAnimation


alertDialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
alertDialog.window!!.attributes.windowAnimations = R.style.DialogAnimation

alertDialog.show()
val window: Window? = alertDialog.window
window!!.setLayout(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
)


alertDialog.show()
val window: Window? = alertDialog.window
window!!.setLayout(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
)

viewModel.expandShortenedUrl(binding.edUrl.text.toString())
viewModel.getExpandedUrlLiveData()
.observe(viewLifecycleOwner) { expandedUrl ->
alertDialog.dismiss()
if (expandedUrl == "Error:" || expandedUrl == "Timeout occurred:") {
showToast(expandedUrl.toString())
return@observe
} else {
videoId = viewModel.extractVideoIdFromUrl(expandedUrl)
if (!TIKTOK_DOWNLOAD_PATH.exists()) {
TIKTOK_DOWNLOAD_PATH.mkdir()
}
Glide.with(requireContext())
.load("${Utils.SOURCE_IMAGEVIEW}$videoId.webp")
.into(binding.srcImage)

binding.edUrl.setText("")
binding.cdDownloadOptions.visibility = View.VISIBLE

viewModel.expandShortenedUrl(binding.edUrl.text.toString())
viewModel.getExpandedUrlLiveData()
.observe(viewLifecycleOwner) { expandedUrl ->
alertDialog.dismiss()
if (expandedUrl == "Error:" || expandedUrl == "Timeout occurred:") {
showToast(expandedUrl.toString())
return@observe
} else {
videoId = viewModel.extractVideoIdFromUrl(expandedUrl)
if (!TIKTOK_DOWNLOAD_PATH.exists()) {
TIKTOK_DOWNLOAD_PATH.mkdir()
}
Glide.with(requireContext())
.load("${Utils.SOURCE_IMAGEVIEW}$videoId.webp")
.into(binding.srcImage)

binding.edUrl.setText("")
binding.cdDownloadOptions.visibility = View.VISIBLE
}
}

} else {
showToast("Invalid url, please check again that pasted url is of tiktok video.")
}
} else {
showToast("Invalid url, please check again that pasted url is of tiktok video.")
}
} else {
showToast("url is not valid!")
}
} else {

viewModel.showAlertDialog(
getString(R.string.media_permission),
getString(R.string.please_allow_media_permission_to_continue),
requireContext()

)
showToast("url is not valid!")
}
}


private fun hideKeyboard(view: View) {
val inputMethodManager = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}

}

0 comments on commit 2b71431

Please sign in to comment.