Giter Site home page Giter Site logo

birjuvachhani / freedom Goto Github PK

View Code? Open in Web Editor NEW
15.0 3.0 4.0 179 KB

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

License: Apache License 2.0

Kotlin 100.00%
android-library kotlin-android androidpermission android permission-android runtime-permissions permission-requests

freedom's Introduction

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.

freedom's People

Contributors

birjuvachhani avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

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.