Giter Site home page Giter Site logo

florent37 / runtimepermission Goto Github PK

View Code? Open in Web Editor NEW
856.0 24.0 69.0 513 KB

Simpliest way to ask runtime permissions on Android, no need to extend class or override permissionResult method, choose your way : Kotlin / Coroutines / RxJava / Java7 / Java8

Home Page: https://florent37.github.io/RuntimePermission/

License: Apache License 2.0

Kotlin 14.52% Java 83.52% Shell 1.96%
runtime permission android rxjava kotlin coroutines lambda marshmallow manifest rx

runtimepermission's Introduction

Runtime Permission

CircleCI Language

screen

Simpliest way to ask runtime permissions on Android, choose your way :

No need to override Activity or FragmentonPermissionResult(code, permissions, result)using this library, you just have to executue RuntimePermission's methods This will not cut your code flow

General Usage (cross language)

Download

dependencies {
    implementation 'com.github.florent37:runtime-permission:(lastest version)'
}

Detect Permissions

RuntimePermission can automatically check all of your needed permissions

For example, if you add to your AndroidManifest.xml :

screen

You can use askPermission without specifying any permission

For example, in Kotlin:

askPermission(){
   //all of your permissions have been accepted by the user
}.onDeclined { e -> 
   //at least one permission have been declined by the user 
}

screen

Will automatically ask for CONTACTS and LOCALISATION permissions

Manually call permissions

You just have to call askPermission with the list of wanted permissions

In Kotlin:

askPermission(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION){
   //all of your permissions have been accepted by the user
}.onDeclined { e -> 
   //at least one permission have been declined by the user 
}

screen

Will ask for CONTACTS and LOCALISATION permissions

Kotlin-Coroutines

yourScope.launch {
    try {
        val result = askPermission(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
        //all permissions already granted or just granted
        //your action
        resultView.setText("Accepted :${result.accepted.toString()}")

    } catch (e: PermissionException) {
        if (e.hasDenied()) {
            appendText(resultView, "Denied :")
            //the list of denied permissions
            e.denied.forEach { permission ->
                appendText(resultView, permission)
            }
            //but you can ask them again, eg:

            AlertDialog.Builder(this@RuntimePermissionMainActivityKotlinCoroutine)
                    .setMessage("Please accept our permissions")
                    .setPositiveButton("yes") { dialog, which ->
                        e.askAgain()
                    }
                    .setNegativeButton("no") { dialog, which ->
                        dialog.dismiss()
                    }
                    .show();
        }

        if (e.hasForeverDenied()) {
            appendText(resultView, "ForeverDenied")
            //the list of forever denied permissions, user has check 'never ask again'
            e.foreverDenied.forEach { permission ->
                appendText(resultView, permission)
            }
            //you need to open setting manually if you really need it
            e.goToSettings();
        }
    }
}

Download

Download

implementation 'com.github.florent37:runtime-permission-kotlin:(last version)'

Kotlin

askPermission(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION){
   //all permissions already granted or just granted
  
   your action
}.onDeclined { e ->
   if (e.hasDenied()) {
       appendText(resultView, "Denied :")
       //the list of denied permissions
       e.denied.forEach {
           appendText(resultView, it)
       }

       AlertDialog.Builder(this@RuntimePermissionMainActivityKotlin)
               .setMessage("Please accept our permissions")
               .setPositiveButton("yes") { dialog, which ->
                   e.askAgain();
               } //ask again
               .setNegativeButton("no") { dialog, which ->
                   dialog.dismiss();
               }
               .show();
   }

   if(e.hasForeverDenied()) {
       appendText(resultView, "ForeverDenied :")
       //the list of forever denied permissions, user has check 'never ask again'
       e.foreverDenied.forEach {
           appendText(resultView, it)
       }
       // you need to open setting manually if you really need it
       e.goToSettings();
   }
}

Download

Download

implementation 'com.github.florent37:runtime-permission-kotlin:(last version)'

RxJava

new RxPermissions(this).request(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION))
    .subscribe(result -> {
        //all permissions already granted or just granted

        your action
    }, throwable -> {
        final PermissionResult result = ((RxPermissions.Error) throwable).getResult();

        if(result.hasDenied()) {
            appendText(resultView, "Denied :");
            //the list of denied permissions
            for (String permission : result.getDenied()) {
                appendText(resultView, permission);
            }
            //permission denied, but you can ask again, eg:


            new AlertDialog.Builder(RuntimePermissionMainActivityRx.this)
                    .setMessage("Please accept our permissions")
                    .setPositiveButton("yes", (dialog, which) -> {
                        result.askAgain();
                    }) // ask again
                    .setNegativeButton("no", (dialog, which) -> {
                        dialog.dismiss();
                    })
                    .show();
        }

        if(result.hasForeverDenied()) {
            appendText(resultView, "ForeverDenied :");
            //the list of forever denied permissions, user has check 'never ask again'
            for (String permission : result.getForeverDenied()) {
                appendText(resultView, permission);
            }
            // you need to open setting manually if you really need it
            result.goToSettings();
        }
    });

Download

implementation 'com.github.florent37:runtime-permission-rx:(last version)'

Java8

askPermission(this)
     .request(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)

     .onAccepted((result) -> {
         //all permissions already granted or just granted

         your action
     })
     .onDenied((result) -> {
         appendText(resultView, "Denied :");
         //the list of denied permissions
         for (String permission : result.getDenied()) {
             appendText(resultView, permission);
         }
         //permission denied, but you can ask again, eg:

         new AlertDialog.Builder(RuntimePermissionMainActivityJava8.this)
                 .setMessage("Please accept our permissions")
                 .setPositiveButton("yes", (dialog, which) -> {
                     result.askAgain();
                 }) // ask again
                 .setNegativeButton("no", (dialog, which) -> {
                     dialog.dismiss();
                 })
                 .show();

     })
     .onForeverDenied((result) -> {
         appendText(resultView, "ForeverDenied :");
         //the list of forever denied permissions, user has check 'never ask again'
         for (String permission : result.getForeverDenied()) {
             appendText(resultView, permission);
         }
         // you need to open setting manually if you really need it
         result.goToSettings();
     })
     .ask();

Download

Download

implementation 'com.github.florent37:runtime-permission:(last version)'

Java7

askPermission(this, Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
    .ask(new PermissionListener() {
        @Override
        public void onAccepted(RuntimePermission runtimePermission, List<String> accepted) {
            //all permissions already granted or just granted

            your action
        }

        @Override
        public void onDenied(RuntimePermission runtimePermission, List<String> denied, List<String> foreverDenied) {
            if(permissionResult.hasDenied()) {
                appendText(resultView, "Denied :");
                //the list of denied permissions
                for (String permission : denied) {
                    appendText(resultView, permission);
                }

                //permission denied, but you can ask again, eg:

                new AlertDialog.Builder(RuntimePermissionMainActivityJava7.this)
                        .setMessage("Please accept our permissions")
                        .setPositiveButton("yes", (dialog, which) -> {
                            permissionResult.askAgain();
                        }) // ask again
                        .setNegativeButton("no", (dialog, which) -> {
                            dialog.dismiss();
                        })
                        .show();
            }


            if(permissionResult.hasForeverDenied()) {
                appendText(resultView, "ForeverDenied :");
                //the list of forever denied permissions, user has check 'never ask again'
                for (String permission : foreverDenied) {
                    appendText(resultView, permission);
                }
                // you need to open setting manually if you really need it
                permissionResult.goToSettings();
            }
        }
    });

How to Contribute

We welcome your contributions to this project.

The best way to submit a patch is to send us a pull request.

To report a specific problem or feature request, open a new issue on Github.

Credits

Manifest permission detection has been forked from https://github.com/sensorberg-dev/permission-bitte, thanks Sensorberg GmbH

Author: Florent Champigny

Blog : http://www.tutos-android-france.com/

Fiches Plateau Moto : https://www.fiches-plateau-moto.fr/

Android app on Google Play Follow me on Google+ Follow me on Twitter Follow me on LinkedIn

License

Copyright 2018 florent37, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

runtimepermission's People

Contributors

armchilingarov avatar carstenhag avatar florent37 avatar kylych1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

runtimepermission's Issues

Permissions List Parameters

askPermission(this, List<Manifest.permission> perms).ask(new PermissionListener(){...}

Hi,Is it possible to use this structure ?

How to set callback for accepted permission

Hi,

According to the readme, success callback passed to askPermission:

askPermission(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION) {
   //all of your permissions have been accepted by the user
}.onDeclined { e -> 
   //at least one permission have been declined by the user 
}

But source shows that we are passing ResponseCallback, not AcceptedCallback. And there is not way to pass AcceptedCallback out of the box:

askPermission().onAccepted

The only way is to create new fragment extension:

fun Fragment.askPermission(vararg permissions: String, accepted: (PermissionResult) -> Unit): KotlinRuntimePermission {
    return KotlinRuntimePermission(
        RuntimePermission.askPermission(activity)
            .request(permissions.toList())
            .onAccepted(accepted))
}

Clear up Licensing

The README says it is Apache 2, but the LICENSE file is for MIT from the forked project. Which is it?

Please clear this up and provide one license definition.
Thank you

Application getting unresponsive while asking for multiple permissions.

I have written the below code to ask for the camera, read external storage and write external storage permissions, but after I allow the permissions the App gets hanged.

askPermission(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
        )

The library version that I am using is 1.1.2.

Kindly help.

Filed resolve dependencies

When i try add this line: implementation 'com.github.florent37:runtime-permission-kotlin:1.0.7'
in my graddle configuration file i get this error: Failed to resolve: org.jetbrains.kotlin:kotlin-stdlib-common:1.3.0-rc-131. For version 1.0.0 everything works right.

RxPermission : Get Permissions Result only if already granted

    @CallSuper
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        int orientation = getResources().getBoolean(R.bool.material_responsive_is_tablet) ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        setRequestedOrientation(orientation);
        super.onCreate(savedInstanceState);
        presenter = initPresenter();
        presenter.onAttachView((V) this);
        requestPermissions();
    }

    private void requestPermissions() {
        new RxPermissions(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION).subscribe(results -> {
            Log.i(TAG, "Permissions Granted");
            onPermissionsAccepted(results);
        }, throwable -> {
            Log.i(TAG, "Some Permissions have been denied");
            if (throwable instanceof RxPermissions.Error) {
                final PermissionResult result = ((RxPermissions.Error) throwable).getResult();
                //permission denied, but you can ask again, eg:
                new AlertDialog.Builder(this)
                        .setMessage(getString(R.string.permission_reason))
                        .setPositiveButton("yes", (dialog, which) -> result.askAgain())
                        .setNegativeButton("no", (dialog, which) -> dialog.dismiss())
                        .show();

                //the list of forever denied permissions, user has check 'never ask again'
                List<String> foreverDenied = result.getForeverDenied();
                if (foreverDenied.size() > 0)
                    new AlertDialog.Builder(this)
                            .setMessage(getString(R.string.ask_permission_setting))
                            .setPositiveButton("yes", (dialog, which) -> result.goToSettings())
                            .setNegativeButton("no", (dialog, which) -> dialog.dismiss())
                            .show();
            }
        }).dispose();
    }
    protected abstract void onPermissionsAccepted(PermissionResult result);

With the following code, my logs are only called if the permissions have already been granted and not at all when they are granted.

Need a better wiki- didn't understand a lot of methods

What i am looking for is,
1)ask permission
2) on denied -->ask again
3) on denied forever(don't ask) --->Go to settings

 askPermission(Manifest.permission.ACCESS_FINE_LOCATION){
            if(mBound){
                mService!!?.requestLocationUpdates()}
        }
                .onDeclined {e ->

            e.denied.forEach {
                Log.d("PermissionTest","denied")
                       // e.askAgain()  **----->what if i write this code here??**
            }
                    e.askAgain()  **------> What is the use of this statement??**

            e.foreverDenied.forEach {
                Log.d("PermissionTest","foreverDenied")
                e.goToSettings()
             }
                    e.runtimePermission.ask()   **------>What is the use of this statement??**
                    e.hasDenied()  **------>What is the use of this statement??**
                    e.accepted.forEach {  }  **------>What is the use of this statement??**
                    e.run {  }  **------>What is the use of this statement??**
                    e.hasForeverDenied()  **------>What is the use of this statement??**
        }

Permission fragment error with kotlin androidx alpha03

When i updated from android.material alpha02 to alpha03 i have this strange comportement when i start to ask for permission :

  • First permission popup that i decline
  • My dialog that tell the user to accept permission, and the positiveButon click launch permissionResult.askAgain()
  • Nothing (no popup or anything comming at the screen), and when i click on the multi task button the app crashed

Stacktrace :
02-20 17:32:40.541 18236-18236/com.sncf.android.internal.mobileocarvente.dev E/AndroidRuntime: FATAL EXCEPTION: main Process: com.sncf.android.internal.mobileocarvente.dev, PID: 18236 java.lang.IllegalStateException: Failure saving state: active PermissionFragment{46f00ad (e39d268c-17ad-47b7-8172-7cf87735488d) PERMISSION_FRAGMENT_WEEEEE} was removed from the FragmentManager at androidx.fragment.app.FragmentManagerImpl.saveAllState(FragmentManagerImpl.java:2315) at androidx.fragment.app.FragmentController.saveAllState(FragmentController.java:150) at androidx.fragment.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:496) at androidx.appcompat.app.AppCompatActivity.onSaveInstanceState(AppCompatActivity.java:511) at android.app.Activity.performSaveInstanceState(Activity.java:1487) at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1318) at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:5483) at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:4773) at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4847) at android.app.ActivityThread.access$1400(ActivityThread.java:229) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1850) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7325) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Running on galaxy A5 with SDK 23.

runtime-permission-kotlin : kotlin-stdlib-jre7 is deprecated

Just tried to compile and got :

C:\Users*.gradle\caches\modules-2\files-2.1\org.jetbrains.kotlin\kotlin-stdlib-jre7\1.2.30\b38fc19e670ebef931fef3ccdcb94eac2b65bb7d\kotlin-stdlib-jre7-1.2.30.jar: kotlin-stdlib-jre7 is deprecated. Please use kotlin-stdlib-jdk7 instead

For ones facing the problem just do :

implementation("com.github.florent37:runtime-permission-kotlin:$runtime_permission_version") {
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jre7'
}

btw thank you for the library ;)
Cheers!

Kotlin implementation - both blocks called

I used the Kotlin code sample as a basis and came up with the following:

askPermission(Manifest.permission.CALL_PHONE) { res ->
	i { "all of your permissions have been accepted by the user: ${res.accepted}" }
	i { "... and denied: ${res.denied}" }
	val i = Intent(Intent.ACTION_CALL)
	i.data = Uri.parse("tel:01234 56789")
	startActivity(i)
}.onDeclined { e ->
	i { "Permissions declined: ${e.denied}" }
	i { "...and accepted: ${e.accepted}" }
}

I then called the function and chose to deny the permission. What I expected to happen was the bottom block to be called and the top to be untouched, what I actually saw is that both blocks get called - first the second, then the first. Here's the output:

I/Permissions declined: [android.permission.CALL_PHONE]
I/...and accepted: []
I/all of your permissions have been accepted by the user: []
I/... and denied: [android.permission.CALL_PHONE]

I solved the issue by modifying it somewhat:

askPermission(Manifest.permission.CALL_PHONE) { res ->
	res.accepted.filter { it == CALL_PHONE }.forEach {
		i { "the ${res.accepted} permission has been accepted by the user" }
		val i = Intent(Intent.ACTION_CALL)
		i.data = Uri.parse("tel:01234 56789")
		@Suppress("ConvertToStringTemplate")
		startActivity(i)
	}
}.onDeclined { e ->
	i { "Permissions declined: ${e.denied}" }
}

Which works, but is not quite as elegant as the docs suggest it should be.

Also note the call to @Suppress("ConvertToStringTemplate") - this is to suppress the usual permissions lint error that we should try/catch a security exception. If there is any way this library can suppress that warning that would be ideal.

Java 7: No matter the selection, askPermission falls in the onDenied block

I have a pretty basic Java 7 implementation. No matter what the user selects, I am always falling in the onDenied block:

        Toast.makeText(this, "photo button pressed", Toast.LENGTH_SHORT).show();
        askPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE)
                .ask(new PermissionListener() {
                    @Override
                    public void onAccepted(RuntimePermission runtimePermission, List<String> accepted) {
                        Toast.makeText(DetailActivity.this, "permission accepted", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onDenied(RuntimePermission runtimePermission, List<String> denied, List<String> foreverDenied) {
                        // permission denied
                        Timber.d("permissions denied");
                        Toast.makeText(DetailActivity.this, "permission denied", Toast.LENGTH_SHORT).show();
                    }
                });
    }

JCenter shutting down

With JCenter being deprecated (and shutting down by next February), would it be possible to move this library to MavenCentral?

Calling accepted when all permission denied with Never Ask Again

Hi,
I am using Runtime permission for Kotlin but When it asks for permissions and I check Never Ask Again for all permissions then deny permissions, so it goes into the accepted function.

Actual - It goes to accepted function when I check Never Ask Permission and deny them

Expected - It should go into the denied function when anyone denies all permissions with never ask again.

Deny doesn't work

Here is my try:

askPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) { _ ->
            Timber.d("accept permission WRITE_EXTERNAL_STORAGE")
        }.onDeclined { e ->
            if (e.hasDenied()) {
                Toast.makeText(context, R.string.msg_permission_file, Toast.LENGTH_LONG).show()
                e.askAgain()
                return@onDeclined
            }

            if (e.hasForeverDenied()) {
                Toast.makeText(context, R.string.msg_permission_file, Toast.LENGTH_LONG).show()
                e.goToSettings()
            }
        }

There are 2 issues:

  1. When user deny permission, it can go to onDeclined callback, but the askAgain() doesn't work at all. In the RuntimePermission source code, the PermissionFragment oldFragment is not null, but it doesn't visible.
  2. When user select deny with do not ask again, the onDeclined callback isn't callable forever. There is no way to handle permission after that.

I though deny is basic feature, mean it should be tested carefully?

Failed to resolve: support-fragment

I want to useRuntimePermission in my app, but after updating android studio to

Android Studio 3.1.2
Build #AI-173.4720617, built on April 14, 2018
JRE: 1.8.0_152-release-1024-b02 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o

Windows 10 10.0 when I add implementation 'com.github.florent37:runtime-permission:1.0.1' dependency into app.gradle file, it will show Failed to resolve: support-fragment
My gradle version is gradle:3.2.0-alpha01

Crash in fragment when screen rotates

Hello! I found an issue with using your library in fragments. Try to run the code below and rotate your screen, and you'll see an exception like this:

2018-11-24 04:05:54.640 20891-20891/com.test.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.test.myapplication, PID: 20891
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=65559, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.test.myapplication/com.test.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4089)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
        at android.app.ActivityThread.-wrap20(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at android.widget.Toast.<init>(Toast.java:103)
        at android.widget.Toast.makeText(Toast.java:256)
        at com.test.myapplication.TestFragment.showToast(TestFragment.kt:36)
        at com.tes.myapplication.TestFragment$doIt$2.invoke(TestFragment.kt:29)
        at com.test.myapplication.TestFragment$doIt$2.invoke(TestFragment.kt:14)
        at com.github.florent37.runtimepermission.kotlin.KotlinRuntimePermission$onDeclined$1.onResponse(kotlin-runtimepermissions.kt:29)
        at com.github.florent37.runtimepermission.RuntimePermission.onReceivedPermissionResult(RuntimePermission.java:123)
        at com.github.florent37.runtimepermission.RuntimePermission.access$000(RuntimePermission.java:27)
        at com.github.florent37.runtimepermission.RuntimePermission$1.onRequestPermissionsResult(RuntimePermission.java:47)
        at com.github.florent37.runtimepermission.PermissionFragment.onRequestPermissionsResult(PermissionFragment.java:89)
        at androidx.fragment.app.FragmentActivity.onRequestPermissionsResult(FragmentActivity.java:860)
        at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:7084)
        at android.app.Activity.dispatchActivityResult(Activity.java:6936)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132) 
        at android.app.ActivityThread.-wrap20(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6119) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

My activity:

package com.test.myapplication

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.container)

        if (savedInstanceState == null) {
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.container, TestFragment())
                .commit()
        }
    }
}

My fragment:

package com.test.myapplication

import android.Manifest
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.github.florent37.runtimepermission.kotlin.askPermission
import kotlinx.android.synthetic.main.testfragment.*
import kotlinx.android.synthetic.main.testfragment.view.*

class TestFragment : Fragment() {

    private var toast: Toast? = null

    lateinit var button: Button

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val view = inflater.inflate(R.layout.testfragment, container, false)
        button = view.button
        button.setOnClickListener { doIt() }
        return view
    }

    fun doIt() {
        askPermission(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION) {
            showToast("ACCEPTED")
            button.text = "ACCEPTED"
        }.onDeclined {
            showToast("DECLINED")
            button.text = "DECLINED"
        }
    }

    fun showToast(text: String) {
        toast?.cancel()
        toast = Toast.makeText(context, text, Toast.LENGTH_SHORT).apply { show() }
    }
}

Fix readme

implementation 'com.github.florent37:runtime-permission:1.0.0'

Add the dash in readme :)

Crashlytics show a Fatal Exception on com.github.florent37.runtimepermission.kotlin.PermissionException

Crashlytics show a fatal Exception, Issue happens with some devices so, I can't reproduce the issue.

Library version :

implementation "com.github.florent37:runtime-permission-kotlin:1.1.2"

Stacktrace from crashlytics :

Fatal Exception: com.github.florent37.runtimepermission.kotlin.PermissionException
       at com.github.florent37.runtimepermission.kotlin.coroutines.experimental.Kotlin_runtimepermissions_coroutinesKt$askPermission$2$1.onResponse(kotlin-runtimepermissions-coroutines.kt:22)
       at com.github.florent37.runtimepermission.RuntimePermission.onReceivedPermissionResult(RuntimePermission.java:125)
       at com.github.florent37.runtimepermission.RuntimePermission.access$000(RuntimePermission.java:29)
       at com.github.florent37.runtimepermission.RuntimePermission$1.onRequestPermissionsResult(RuntimePermission.java:49)
       at com.github.florent37.runtimepermission.PermissionFragment.onRequestPermissionsResult(PermissionFragment.java:89)
       at androidx.fragment.app.FragmentActivity.onRequestPermissionsResult(FragmentActivity.java:768)
       at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6664)
       at android.app.Activity.dispatchActivityResult(Activity.java:6543)
       at android.app.ActivityThread.deliverResults(ActivityThread.java:3936)
       at android.app.ActivityThread.handleSendResult(ActivityThread.java:3983)
       at android.app.ActivityThread.-wrap16(ActivityThread.java)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1548)
       at android.os.Handler.dispatchMessage(Handler.java:111)
       at android.os.Looper.loop(Looper.java:207)
       at android.app.ActivityThread.main(ActivityThread.java:5765)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

Affected devices :

sc1
s2
s3
s4

Dealing With System Permission

System App permission like WRITE_SETTINGS is not handled properly. Whenever I ask this kind of permission it automatically reaches to neveraskagain block.

(java 7) It seems that onAccepted or onDenied are not called

Hi, i hope you could help me...
Here is my code (basically the same as in readme)

`askPermission(this)
            .ask(new PermissionListener() {

                @Override
                public void onAccepted(RuntimePermission runtimePermission, List<String> accepted) {
                    //all permissions already granted or just grantedyour action
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d("PERMISSIONS", "accepted");
                        }
                    });
                }

                @Override
                public void onDenied(RuntimePermission runtimePermission, List<String> denied, List<String> foreverDenied) {
                    //the list of denied permissions
                    for (String permission : denied) {
                        Log.d("PERMISSIONS", permission + " denied");
                    }
                    //permission denied, but you can ask again, eg:

                    //the list of forever denied permissions, user has check 'never ask again'
                    for (String permission : foreverDenied) {
                        Log.d("PERMISSIONS", permission + " forever denied");
                    }
                    // you need to open setting manually if you really need it
                    //runtimePermission.goToSettings();
                }
            });`

The permission is asked but there is no log "accepted" or "denied" (when I try to call a pop up dialog it is not displayed) . Why that?

IllegalStateException when using with Navigation Architecture Component

When using this library in combination with the Navigation Architecture Component, this exception is thrown when starting new FragmentActivity.

This only happens when the Permission Dialog has been presented.

Fatal Exception: java.lang.IllegalStateException: Failure saving state: active PermissionFragment{39141c6 (a18f9993-a83d-4c2c-97c0-454b2e268527) PERMISSION_FRAGMENT_WEEEEE} was removed from the FragmentManager
       at androidx.fragment.app.FragmentManagerImpl.saveAllState(FragmentManagerImpl.java:2315)
       at androidx.fragment.app.FragmentController.saveAllState(FragmentController.java:150)
       at androidx.fragment.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:496)
       at androidx.appcompat.app.AppCompatActivity.onSaveInstanceState(AppCompatActivity.java:511)
       at android.app.Activity.performSaveInstanceState(Activity.java:1549)
       at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1443)
       at android.app.ActivityThread.callActivityOnSaveInstanceState(ActivityThread.java:4809)
       at android.app.ActivityThread.callActivityOnStop(ActivityThread.java:4157)
       at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:4121)
       at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4196)
       at android.app.servertransaction.StopActivityItem.execute(StopActivityItem.java:41)
       at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
       at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
       at android.os.Handler.dispatchMessage(Handler.java:106)
       at android.os.Looper.loop(Looper.java:193)
       at android.app.ActivityThread.main(ActivityThread.java:6669)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)```

never getting callback of Accept or deny blocks

First time, I did deny Two Permission then I got callback, after second time launch app, I did deny first permission, and then accept second permission, at this moment I haven't get any callback methods.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.