• Stars
    star
    1,694
  • Rank 26,417 (Top 0.6 %)
  • Language
    Java
  • Created about 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easy check permission library for Android Marshmallow

Android Arsenal

What is TedPermission?

After the update to Android 6.0 Marshmallow, we have to not only declare permissions in AndroidManifest.xml, but also request them at runtime. Furthermore, the user can turn permissions on/off anytime in application settings.
When you use dangerous permissons(ex. CAMERA, READ_CONTACTS, READ_PHONE_STATE, ...), you must check and request them at runtime.
(https://developer.android.com/guide/topics/permissions/overview?hl=en#normal-dangerous)

You can make your own permission check logic like this, but it's very complex, mainly because functions Google offer are very hard to use: checkSelfPermission(), requestPermissions(), onRequestPermissionsResult(), onActivityResult().

TedPermission makes it easy to check and request android permissions.

(For Korean) μ•„λž˜ λΈ”λ‘œκ·Έλ₯Ό 톡해 λ§ˆμ‹œλ©œλ‘œμš° κΆŒν•œ κ΄€λ ¨λœ 사항을 μ•Œμ•„λ³΄μ„Έμš”
http://gun0912.tistory.com/55

Demo



Screenshot

  1. Request permissions.
  2. If user denied permissions, a message dialog with a button to go to Settings will appear.

Setup

  • Edit root/app/build.gradle like below.
  • You can choose only one library depend on your code style normal/coroutine/rxJava2/rxJava3
  • Replace x.y.z with the version shown in the 'Maven Central' button below, or the specific version you want (e.g. replace x.y.z with 3.3.0 if you want v3.3.0).

Maven Central

repositories {
  google()
  mavenCentral()
}

dependencies {
    // Normal
    implementation 'io.github.ParkSangGwon:tedpermission-normal:x.y.z'
    
    // Coroutine
    implementation 'io.github.ParkSangGwon:tedpermission-coroutine:x.y.z'

    // RxJava2
    implementation 'io.github.ParkSangGwon:tedpermission-rx2:x.y.z'
    // RxJava3
    implementation 'io.github.ParkSangGwon:tedpermission-rx3:x.y.z'
}

If you think this library is useful, please press the star button at the top.



How to use

Normal

-Make PermissionListener

We will use PermissionListener for handling permission check result. You will get the result to onPermissionGranted() or onPermissionDenied() depending on approved permissions.

    PermissionListener permissionlistener = new PermissionListener() {
        @Override
        public void onPermissionGranted() {
            Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPermissionDenied(List<String> deniedPermissions) {
            Toast.makeText(MainActivity.this, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
        }


    };

-Start TedPermission

TedPermission class requires setPermissionListener(), setPermissions(), and check() methods. Call check() to start checking for permissions.

setRationaleMessage() and setDeniedMessage() are optional methods for displaying messages.

    TedPermission.create()
        .setPermissionListener(permissionlistener)
        .setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
        .setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
        .check();



Coroutine

If you use kotlin and coroutine, You can use check()function. TedPermissionResult instance has isGranted(), getDeniedPermissions() methods for checking permission check result.

val permissionResult =
    TedPermission.create()
        .setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION)
        .check()

Also if you want know only granted result, you can use checkGranted(): boolean

RxJava

If you use RxJava, You can use request() method instead check(). When permission check has finished, you will receive TedPermissionResult instance. TedPermissionResult instance has isGranted(), getDeniedPermissions() methods for checking permission check result.

    TedPermission.create()
        .setRationaleTitle(R.string.rationale_title)
        .setRationaleMessage(R.string.rationale_message) // "we need permission for read contact and find your location"
        .setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
        .request()
        .subscribe(tedPermissionResult -> {
          if (tedPermissionResult.isGranted()) {
            Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
          } else {
            Toast.makeText(this,
                "Permission Denied\n" + tedPermissionResult.getDeniedPermissions().toString(), Toast.LENGTH_SHORT)
                .show();
          }
        }, throwable -> {
        });

Customize

TedPermission supports the following methods.

  • setGotoSettingButton(boolean) (default: true)
  • setRationaleTitle(R.string.xxx or String)
  • setRationaleMessage(R.string.xxx or String)
  • setRationaleConfirmText(R.string.xxx or String) (default: confirm / 확인)
  • setDeniedTitle(R.string.xxx or String)
  • setDeniedMessage(R.string.xxx or String)
  • setDeniedCloseButtonText(R.string.xxx or String) (default: close / λ‹«κΈ°)
  • setGotoSettingButtonText(R.string.xxx or String) (default: setting / μ„€μ •)

Also you can use the following utility functions.

  • isGranted(String... permissions): Check if all permissions are granted
  • isDenied(String... permissions): Check if all permissions are denied
  • getDeniedPermissions(String... permissions)
  • canRequestPermission(Activity activity, String... permissions): If true you can request a system popup, false means user checked Never ask again.
  • startSettingActivityForResult()
  • startSettingActivityForResult(int requestCode)



Number of Cases

  1. Check permissions -> Already have permissions
    : onPermissionGranted() is called.

  2. Check permissions -> Don't have permissions
    : Request dialog is shown.
    Screenshot

  3. Show request dialog -> User granted permissions
    : onPermissionGranted() is called.

  4. Show request dialog -> User denied one or more permissions
    : Denied dialog is shown.
    Screenshot

  5. Show denied dialog -> Close the dialog
    : onPermissionDenied() called

  6. Show denied dialog -> Setting button clicked
    : startActivityForResult() to application Setting Activity.
    Screenshot

  7. Setting Activity -> onActivityResult()
    : Check permissions again

  8. Check permission -> Permissions are granted
    : onPermissionGranted() is called.

  9. Check permission -> There are denied permissions
    : onPermissionDenied() is called.



Screenshot



License

Copyright 2021 Ted Park

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.

More Repositories

1

TedBottomPicker

TedBottomPicker is simple image picker using bottom sheet
Java
1,113
star
2

TedImagePicker

TedImagePicker is simple/beautiful/smart image picker
Kotlin
522
star
3

TedPicker

Multiple image select library for Android. Take a picture or Select from gallary
Java
278
star
4

TedNaverMapClustering

λ„€μ΄λ²„μ§€λ„μš© ν΄λŸ¬μŠ€ν„° μœ ν‹Έλ¦¬ν‹° 라이브러리
Kotlin
165
star
5

TedKeyboardObserver

TedKeyboardObserver is keyboard's visibility observer
Kotlin
99
star
6

TedDataBindingSample

Java
90
star
7

TedAdHelper

[Android]Mediation advertise helper for ADMOB,FACEBOOK - Support Native, Banner, Backpress Dialog, Front AD
Java
83
star
8

GifProgressSample

Java
69
star
9

ClearEditText

Java
58
star
10

ProductFlavorSample

Java
55
star
11

IncomingCallMarketBroadcastReceiver

Java
52
star
12

DLog

Java
48
star
13

Introduce

λ°•μƒκΆŒ μ†Œκ°œ
43
star
14

GoogleMapCustomMarker

Java
41
star
15

UpdateCheck

Java
40
star
16

TedAdmobDialog

[Android]BackPress Ad Dialog for Admob
Java
37
star
17

TedOnActivityResult

StartActivityForResult() / OnActivityResult() using Coroutine/RxJava
Java
33
star
18

TedPaletteSample

Java
22
star
19

BuildTimeSpeedUpSample

Java
21
star
20

KakaoLogin

Java
19
star
21

TedUtil

Useful util class for android
Java
17
star
22

TedDynamicLinkSample

Java
17
star
23

ObjectUtils

Java
15
star
24

NoMoreApplicationForLibraryModule

Kotlin
14
star
25

TedDeepLinkSample

Kotlin
13
star
26

RangeBarChart

A powerful πŸš€ Android range bar chart library as well as scaling, panning and animations.
Kotlin
12
star
27

EnumRetrofitSample

Kotlin
7
star
28

StartActivitySample

Kotlin
3
star
29

IntervalTimePicker

IntervalTimePicker can control interval of minutes using TimePicker.
Kotlin
3
star
30

OSSRH-69704

1
star