googlesamples / easypermissions

Simplify Android M system permissions
https://firebaseopensource.com/projects/googlesamples/easypermissions/
Apache License 2.0
9.87k stars 1.46k forks source link

[Workaround] Invoke an action when permission prompts are allowed #305

Closed handhikadj closed 4 years ago

handhikadj commented 4 years ago

Basic Information

Device type: Samsung A20 OS version: 29 EasyPermissions version: 3.0.0

Describe the problem

I'm unable to do an action when permission prompts are allowed I need a workaround to do it

Code

package org.rainjay.lexattend.views.homefragment

import android.Manifest
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.NavigationUI
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import org.rainjay.lexattend.R
import org.rainjay.lexattend.databinding.FragmentHomeBinding
import org.rainjay.lexattend.service.ConnectivityLiveData
import pub.devrel.easypermissions.AfterPermissionGranted
import pub.devrel.easypermissions.EasyPermissions

class HomeFragment : Fragment(), EasyPermissions.PermissionCallbacks {
    private lateinit var binding: FragmentHomeBinding
    private val PERMISSION_REQUEST_CODE: Int = 2020
    private lateinit var homeFragmentViewModel: HomeFragmentViewModel

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)

        (activity as AppCompatActivity).supportActionBar?.hide()

        homeFragmentViewModel = ViewModelProvider(
            this,
            HomeFragmentViewModelFactory(requireActivity().application)
        ).get(HomeFragmentViewModel::class.java)

        binding.lifecycleOwner = this

        loadPermissions()

        return binding.root
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return NavigationUI.onNavDestinationSelected(
            item,
            requireView().findNavController()
        ) || super.onOptionsItemSelected(item)
    }

    @AfterPermissionGranted(2020)
    private fun loadPermissions() {
        val perms: Array<String> = arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION)

        if (EasyPermissions.hasPermissions(requireContext(), *perms)) {
            Toast.makeText(context, "granted", Toast.LENGTH_SHORT).show() // <-- this won't run. the app should be restarted first to make this run
        } else {
            EasyPermissions.requestPermissions(
                this,
                "Please allow the following permissions",
                PERMISSION_REQUEST_CODE,
                *perms
            )
        }
    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>) {
        Toast.makeText(context, "granted", Toast.LENGTH_SHORT).show() // <-- this won't run. the app should be restarted first to make this run
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>) {
        MaterialAlertDialogBuilder(context, R.style.Body_ThemeOverlay_MaterialComponents_MaterialAlertDialog)
            .setTitle("Permissions are Denied")
            .setMessage("Please allow all permissions to continue to use this app. You can close this app and fix the permission on your phone")
            .setCancelable(false)
            .show()
    }
}
handhikadj commented 4 years ago

sorry my bad. problem's solved

RutaleIvanPaul commented 4 years ago

@handhikadj How did you solve this? I am having a similar issue

handhikadj commented 4 years ago

@RutaleIvanPaul if I can recall, i fixed it to:

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this) // <-- the 'this' fixes
    }