Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2a28098
Add kotlin support and modify project icon
vmadalin Sep 1, 2019
30a86e1
Add launcher round icon
vmadalin Sep 2, 2019
b918b54
start migrate sample to kotlin
vmadalin Sep 2, 2019
906213e
Simplify logic on sample
vmadalin Sep 2, 2019
bf0aaf7
Add adaptive launcher icon
vmadalin Sep 2, 2019
3a3d595
Fixed travis build failure
vmadalin Sep 2, 2019
cf745e4
start migrating helpers to kotlin
vmadalin Sep 2, 2019
c80d05c
First step auto migrate library to kotlin
vmadalin Sep 2, 2019
6d620e3
simplify several logic using the request permission model as param
vmadalin Sep 3, 2019
dfd705c
several improvements in EasyPermission after automatic kotlin migrati…
vmadalin Sep 3, 2019
2f081b6
Refactored and simplify Settings Dialog after first automatic migrati…
vmadalin Sep 4, 2019
0745496
Compile again library after the last refactor on Rationale Dialog
vmadalin Sep 4, 2019
504e5d1
Some adaptions in code on settings dialog
vmadalin Sep 4, 2019
5404926
Add static analysis to travis and fix all of them except tests
vmadalin Sep 4, 2019
c1cdf7f
migrate base test to kotlin, and fix travis failure
vmadalin Sep 4, 2019
b60b8a7
Update readme with the new project logo
vmadalin Sep 5, 2019
19ebfb3
Migrated to kotlin EasyPermission Tests
vmadalin Sep 5, 2019
5b3f4d8
Finished the EasyPermissionsTest
vmadalin Sep 6, 2019
9646590
Implement test for rationale Dialog
vmadalin Sep 6, 2019
64c7a0f
Add jacoco configuration
vmadalin Sep 6, 2019
463808e
Update PR after 1 year
vmadalin Dec 20, 2020
0a4f2cd
Update readme
vmadalin Dec 20, 2020
21ce7b5
Rename packages to mvalceleanu for unique package artifactory
vmadalin Dec 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
549 changes: 549 additions & 0 deletions .detekt/config.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ android:
- tools

before_script: echo y | ${ANDROID_HOME}tools/bin/sdkmanager --channel=3 "tools" "platform-tools" "platforms;android-27"
script: ./gradlew build jacocoTestReportRelease :easypermissions:test
script: ./gradlew build spotlessCheck :easypermissions:spotlessCheck :easypermissions:ktlintCheck :easypermissions:detekt :easypermissions:test jacocoTestReportRelease

after_failure:
- cat app/build/reports/tests/testDebugUnitTest/index.html
Expand Down
1 change: 0 additions & 1 deletion CONTRIBUTING.md

This file was deleted.

15 changes: 15 additions & 0 deletions COPYRIGHT
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright $YEAR Google LLC
*
* 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
*
* https://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.
*/
150 changes: 74 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,36 @@
# EasyPermissions [![Build Status][1]][2] [![Code Coverage][3]][4] [![Android Weekly][5]][6]
# EasyPermissions-ktx [![Build Status][1]][2] [![Code Coverage][3]][4] [![Android API][5]][6] [![Apache License][7]][8]

EasyPermissions is a wrapper library to simplify basic system permissions logic when targeting
EasyPermissions-ktx is a wrapper library to simplify basic system permissions logic when targeting
Android M or higher.

![](art/logo.png)

This library lifts the burden that comes with writing a bunch of check statements whether a permission has been granted or not from you, in order to keep your code clean and safe.

## Installation

EasyPermissions is installed by adding the following dependency to your `build.gradle` file:
EasyPermissions-ktx is installed by adding the following dependency to your `build.gradle` file:

```groovy
dependencies {
// For developers using AndroidX in their applications
   implementation 'pub.devrel:easypermissions:3.0.0'

// For developers using the Android Support Library
implementation 'pub.devrel:easypermissions:2.0.1'
   implementation 'mvalceleanu:easypermissions-ktx:0.1.0'
}
```

## Usage

### Basic

To begin using EasyPermissions, have your `Activity` (or `Fragment`) override the `onRequestPermissionsResult` method:
To begin using EasyPermissions-ktx, have your `Activity` (or `Fragment`) override the `onRequestPermissionsResult` method:

```java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
```kotlin
class MainActivity : AppCompatActivity() {

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)

// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
// EasyPermissions handles the request result.
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
}
}
```
Expand All @@ -59,61 +53,56 @@ The example below shows how to request permissions for a method that requires bo
flow of needing to run the requesting method after all of its permissions have been granted.
This can also be achieved by adding logic on the `onPermissionsGranted` callback.

```java
@AfterPermissionGranted(RC_CAMERA_AND_LOCATION)
```kotlin
@AfterPermissionGranted(REQUEST_CODE_LOCATION_AND_CONTACTS_PERMISSION)
private void methodRequiresTwoPermission() {
String[] perms = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION};
if (EasyPermissions.hasPermissions(this, perms)) {
if (EasyPermissions.hasPermissions(this, ACCESS_FINE_LOCATION, READ_CONTACTS)) {
// Already have permission, do the thing
// ...
} else {
// Do not have permissions, request them now
EasyPermissions.requestPermissions(this, getString(R.string.camera_and_location_rationale),
RC_CAMERA_AND_LOCATION, perms);
EasyPermissions.requestPermissions(
host = this,
rationale = getString(R.string.permission_location_and_contacts_rationale_message),
requestCode = REQUEST_CODE_LOCATION_AND_CONTACTS_PERMISSION,
perms = ACCESS_FINE_LOCATION, READ_CONTACTS
)
}
}
```

Or for finer control over the rationale dialog, use a `PermissionRequest`:

```java
EasyPermissions.requestPermissions(
new PermissionRequest.Builder(this, RC_CAMERA_AND_LOCATION, perms)
.setRationale(R.string.camera_and_location_rationale)
.setPositiveButtonText(R.string.rationale_ask_ok)
.setNegativeButtonText(R.string.rationale_ask_cancel)
.setTheme(R.style.my_fancy_style)
.build());
```
```kotlin
val request = PermissionRequest.Builder(spyActivity)
.code(REQUEST_CODE)
.perms(REQUEST_CODE_LOCATION_AND_CONTACTS_PERMISSION)
.theme(R.style.my_fancy_style)
.rationale(R.string.camera_and_location_rationale)
.positiveButtonText(R.string.rationale_ask_ok)
.negativeButtonText(R.string.rationale_ask_cancel)
.build()
EasyPermissions.requestPermissions(spyActivity, request)

Optionally, for a finer control, you can have your `Activity` / `Fragment` implement
the `PermissionCallbacks` interface.

```java
public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
```kotlin
class MainActivity : AppCompatActivity(), EasyPermissions.PermissionCallbacks {

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)

// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
// EasyPermissions handles the request result.
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
}

@Override
public void onPermissionsGranted(int requestCode, List<String> list) {
override fun onPermissionsGranted(requestCode: Int, perms: List<String>) {
// Some permissions have been granted
// ...
}

@Override
public void onPermissionsDenied(int requestCode, List<String> list) {
override fun onPermissionsDenied(requestCode: Int, perms: List<String>) {
// Some permissions have been denied
// ...
}
Expand All @@ -134,26 +123,35 @@ works after the permission has been denied and your app has received
the `onPermissionsDenied` callback. Otherwise the library cannot distinguish
permanent denial from the "not yet denied" case.

```java
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());
```kotlin
override fun onPermissionsDenied(requestCode: Int, perms: List<String>) {
Log.d(TAG, "onPermissionsDenied: $requestCode :${perms.size()}")

// (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."
// This will display a dialog directing them to enable the permission in app settings.
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
new AppSettingsDialog.Builder(this).build().show();
if (EasyPermissions.somePermissionPermanentlyDenied(this, perms.toString())) {
SettingsDialog.Builder(this).build().show()
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == DEFAULT_SETTINGS_REQ_CODE) {
val yes = getString(R.string.yes)
val no = getString(R.string.no)

if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
// Do something after user returned from app settings screen, like showing a Toast.
Toast.makeText(this, R.string.returned_from_app_settings_to_activity, Toast.LENGTH_SHORT)
.show();
Toast.makeText(
this,
getString(
R.string.returned_from_app_settings_to_activity,
if (hasCameraPermission()) yes else no,
if (hasLocationAndContactsPermissions()) yes else no,
if (hasSmsPermission()) yes else no,
if (hasStoragePermission()) yes else no
),
LENGTH_LONG
).show()
}
}
```
Expand All @@ -162,15 +160,13 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {

Implement the `EasyPermissions.RationaleCallbacks` if you want to interact with the rationale dialog.

```java
@Override
public void onRationaleAccepted(int requestCode) {
```kotlin
override fun onRationaleAccepted(requestCode: Int) {
// Rationale accepted to request some permissions
// ...
}

@Override
public void onRationaleDenied(int requestCode) {
override fun onRationaleDenied(requestCode: Int) {
// Rationale denied to request some permissions
// ...
}
Expand All @@ -197,9 +193,11 @@ Rationale callbacks don't necessarily imply permission changes. To check for tho

```

[1]: https://travis-ci.org/googlesamples/easypermissions.svg?branch=master
[2]: https://travis-ci.org/googlesamples/easypermissions
[3]: https://codecov.io/gh/googlesamples/easypermissions/branch/master/graph/badge.svg
[4]: https://codecov.io/gh/googlesamples/easypermissions
[5]: https://img.shields.io/badge/Android%20Weekly-%23185-2CB3E5.svg?style=flat
[6]: http://androidweekly.net/issues/issue-185
[1]: https://travis-ci.com/vmadalin/easypermissions-ktx.svg?branch=master
[2]: https://travis-ci.com/vmadalin/easypermissions-ktx
[3]: https://codecov.io/gh/vmadalin/easypermissions-ktx/branch/master/graph/badge.svg
[4]: https://codecov.io/gh/vmadalin/easypermissions-ktx
[5]: https://img.shields.io/badge/API-14%2B-blue.svg?style=flat
[6]: https://android-arsenal.com/api?level=14
[7]: https://img.shields.io/badge/License-Apache%202.0-lightgrey.svg
[8]: http://www.apache.org/licenses/LICENSE-2.0
12 changes: 8 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion compileSdk
compileSdkVersion project.compileSdk

defaultConfig {
applicationId "pub.devrel.easypermissions.sample"
applicationId "mvalceleanu.easypermissions.sample"
minSdkVersion 14
targetSdkVersion targetSdk
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

buildTypes {
Expand All @@ -20,6 +23,7 @@ android {
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation project(':easypermissions')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation project(':easypermissions-ktx')
}
12 changes: 5 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="pub.devrel.easypermissions.sample">
package="mvalceleanu.easypermissions.sample">

<uses-permission android:name="android.permission.CAMERA" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:label="@string/app_name"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
tools:ignore="AllowBackup,GoogleAppIndexingWarning,RtlEnabled">

<activity android:name=".MainActivity">
<intent-filter>
Expand Down
Binary file added app/src/main/ic_launcher-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading