Skip to content

Commit

Permalink
fix: force stop when rejected permission
Browse files Browse the repository at this point in the history
  • Loading branch information
adityastic committed Jun 28, 2019
1 parent c7368e3 commit e011fc4
Showing 1 changed file with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.DialogInterface
import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
Expand All @@ -23,6 +24,7 @@ import android.widget.EditText
import android.widget.TextView
import android.widget.LinearLayout
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.google.android.material.tabs.TabLayout
Expand Down Expand Up @@ -63,6 +65,7 @@ import java.util.TimerTask
class TextArtFragment : BaseFragment() {
companion object {
private const val SCAN_TIMEOUT_MS = 9500L
private const val REQUEST_PERMISSION_CODE = 10
@JvmStatic
fun newInstance() =
TextArtFragment()
Expand Down Expand Up @@ -108,8 +111,9 @@ class TextArtFragment : BaseFragment() {

private fun setupButton() {
save_button.setOnClickListener {
textViewMainText.hideKeyboard()
showSaveFileDialog()
if (checkStoragePermission()) {
startSaveFile()
}
}

transfer_button.setOnClickListener {
Expand Down Expand Up @@ -139,6 +143,31 @@ class TextArtFragment : BaseFragment() {
}
}

fun startSaveFile() {
textViewMainText.hideKeyboard()
showSaveFileDialog()
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
REQUEST_PERMISSION_CODE -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startSaveFile()
}
}
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}

private fun checkStoragePermission(): Boolean {
return if (ActivityCompat.checkSelfPermission(requireContext(),
android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_PERMISSION_CODE)
false
} else
true
}

private fun showAlertDialog() {
val dialogMessage = getString(R.string.enable_bluetooth)
val builder = AlertDialog.Builder(requireContext())
Expand Down

0 comments on commit e011fc4

Please # to comment.