/freedom

Freedom is an Android library to handle runtime permissions in easiest way.

Primary LanguageKotlinApache License 2.0Apache-2.0

banner

Freedom

License language Platform API

Freedom is an Android library to handle runtime permissions in easiest way. Freedom is fully written in Kotlin so it uses Kotlin features and dsl syntax extensively.

Please note that this library is still under development. Stable version of the library will be released soon. Stay tuned!

At this time, this library does not have support for multiple permission requests. We are planning to add it soon!

Gradle Dependency

  • Add the JitPack repository to your project's build.gradle file
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  • Add the dependency in your app's build.gradle file
dependencies {
    implementation 'com.github.BirjuVachhani:freedom:0.9.1'
}

Usage

Set Permissions Event Listener

Freedom.setListener(this) whenGranted {
    // permission granted
    showToast("Granted")
} whenDenied {
    // permission denied
    showToast("Denied")
} whenPermanentlyDenied {
    // permission permanently denied, show open settings dialog
    showPermissionBlockedDialog()
} whenShouldShowRationale {listener->
    // show rationale dialog
    showRationaleDialog(listener)
}

Requesting Permission

Freedom.request(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)

Handling Rationale Dialog

private fun showRationaleDialog(listener: RationaleInterface) {
    AlertDialog.Builder(this)
        .setTitle(getString(R.string.rationale_title))
        .setMessage(getString(R.string.rationale_msg)
        .setPositiveButton("Grant") { dialog, _ ->
            listener.request()
            dialog.dismiss()
        }
        .setNegativeButton("CANCEL") { dialog, _ ->
            dialog.dismiss()
        }.show()
}

Example

class MainActivity : AppCompatActivity() {

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

        Freedom.setListener(this) whenGranted {
            // permission granted
            showToast("Granted")
        } whenDenied {
            // permission denied
            showToast("Denied")
        } whenPermanentlyDenied {
            // permission permanently denied, show open settings dialog
            showPermissionBlockedDialog()
        } whenShouldShowRationale {listener->
            // show rationale dialog
            showRationaleDialog(listener)
        }
    }

    private fun showPermissionBlockedDialog() {
        AlertDialog.Builder(this)
            .setTitle("Permission Blocked")
            .setMessage("This feature requires location permission to function. Please grant location permission for settings.")
            .setPositiveButton("OPEN SETTINGS") { dialog, _ ->
                openSettings()
                dialog.dismiss()
            }
            .setNegativeButton("CANCEL") { dialog, _ ->
                dialog.dismiss()
            }.show()
    }

    private fun showRationaleDialog(listener: RationaleInterface) {
        AlertDialog.Builder(this)
            .setTitle("Permission Required")
            .setMessage("This feature requires location permission to function. Please grant location permission.")
            .setPositiveButton("Grant") { dialog, _ ->
                listener.request()
                dialog.dismiss()
            }
            .setNegativeButton("CANCEL") { dialog, _ ->
                dialog.dismiss()
            }.show()
    }

    /**
     * Opens app settings screen
     * */
    private fun openSettings() {
        val intent = Intent()
        intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
        val uri = Uri.fromParts("package", packageName, null)
        intent.data = uri
        startActivity(intent)
    }

    /**
     * method is invoked on button click which initiates permission request.
     */
    fun requestPermission(view: View) {
        Freedom.request(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
    }

    private fun showToast(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }
}

Pull Request

To generate a pull request, please consider following Pull Request Template.

Issues

To submit an issue, please check the Issue Template.

Code of Conduct

Code of Conduct

Contribution

You are most welcome to contribute to this project!

Please have a look at Contributing Guidelines, before contributing and proposing a change.

License

   Copyright © 2019 BirjuVachhani

   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.