permissions-dispatcher / PermissionsDispatcher

A declarative API to handle Android runtime permissions.
https://github.com/permissions-dispatcher/PermissionsDispatcher
Apache License 2.0
11.22k stars 1.44k forks source link

Internet permission dialog is not shown #722

Closed kaushalyap closed 3 years ago

kaushalyap commented 3 years ago

FAQs


Overview

Expected

Actual

Environment

Reproducible steps

Declare following in AndroidManifest.xml <uses-permission android:name="android.permission.INTERNET" />

Add following code to MainFragment.kt

class MainFragment : Fragment() {

    private lateinit var internetPermissionsRequester: PermissionsRequester
    private var _binding: FragmentMainBinding? = null
    private val binding get() = _binding!!
    private lateinit var mainActivity: MainActivity

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentMainBinding.inflate(inflater, container, false)
        mainActivity = activity as MainActivity
        internetPermissionsRequester.launch()
        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun downloadModel() {
        val conditions = CustomModelDownloadConditions.Builder()
            .requireWifi()
            .build()
        FirebaseModelDownloader.getInstance()
            .getModel("demo", DownloadType.LOCAL_MODEL_UPDATE_IN_BACKGROUND, conditions)
            .addOnCompleteListener { customModel ->

                customModel.addOnSuccessListener { model: CustomModel? ->
                    Toast.makeText(context, "Model Name : ${model?.name}", Toast.LENGTH_SHORT).show()
                }
            }
            .addOnFailureListener {
                Toast.makeText(context, "Internet is necessary to download the model!", Toast.LENGTH_SHORT).show()
            }
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        internetPermissionsRequester = constructPermissionsRequest(
            Manifest.permission.INTERNET,
            onShowRationale = ::onInternetShowRationale,
            onPermissionDenied = ::onInternetDenied,
            onNeverAskAgain = ::onInternetNeverAskAgain,
            requiresPermission = ::downloadModel
        )
    }

    private fun onInternetDenied() {
        Toast.makeText(context, R.string.permission_internet_denied, Toast.LENGTH_SHORT).show()
    }

    private fun onInternetNeverAskAgain() {
        Toast.makeText(
            context,
            R.string.permission_internet_never_ask_again,
            Toast.LENGTH_SHORT
        ).show()
    }

    private fun onInternetShowRationale(request: PermissionRequest) {
        mainActivity.showRationaleDialog(R.string.permission_internet_rationale, request)
    }
}

Run the app and navigate to MainFragment

jczerski commented 3 years ago

You shouldn't ask for INTERNET permission at runtime doc: https://developer.android.com/guide/topics/permissions/overview#types

Normal permissions do not directly risk the user's privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically.