Code Monkey home page Code Monkey logo

tedpermission's Introduction

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, user can on/off permissions in application setting anytime.
When you use dangerous permissons(ex. CAMERA, READ_CONTACTS, READ_PHONE_STATE, ...), you must check and request them runtime.
(http://developer.android.com/intl/ko/guide/topics/security/permissions.html#normal-dangerous)

You can make your own permission check logic like this, but it's so complex and hard to use functions Google offers: checkSelfPermission(), requestPermissions(), onRequestPermissionsResult(), onActivityResult().

TedPermission makes it easy to check and request android permissions.

(For Korean) 아래 블로그를 통해 마시멜로우 권한관련된 사항을 알아보세요
http://gun0912.tistory.com/55

Demo

Screenshot



Screenshot

  1. Request Permissions.
  2. If user denied permissions, we will show message dialog with Setting button.

Setup

Gradle

Edit root/app/build.gradle like below.

Normal

dependencies {
    compile 'gun0912.ted:tedpermission:2.1.0'
}

RxJava1

dependencies {
    compile 'gun0912.ted:tedpermission-rx1:2.1.0'
}

RxJava2

dependencies {
    compile 'gun0912.ted:tedpermission-rx2:2.1.0'
}

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



How to use

Normal

-Make PermissionListener

We will use PermissionListener for Permission Result. You will get result to onPermissionGranted(), onPermissionDenied()

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

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


    };

-Start TedPermission

TedPermission class requires setPermissionListener(), setPermissions(), and check() check() will start to check permissions.

setRationaleMessage() and setDeniedMessage() are optional methods.

    TedPermission.with(this)
    .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();



RxJava1

If you use RxJava1, You can use request() method instead check() When Permission check finish, you can receive tedPermissionResult instance. tedPermissionResult instance has isGranted(), getDeniedPermissions()

    TedRxPermission.with(this)
        .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)
        .request()
        .subscribe(tedPermissionResult -> {
          if (tedPermissionResult.isGranted()) {
            Toast.makeText(RxJava1Activity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
          } else {
            Toast.makeText(RxJava1Activity.this,
                "Permission Denied\n" + tedPermissionResult.getDeniedPermissions().toString(), Toast.LENGTH_SHORT)
                .show();
          }
        }, throwable -> {
        }, () -> {
        });



RxJava2

Also RxJava2 can use request() like RxJava1

    TedRx2Permission.with(this)
        .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 support this method

  • 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 util function.

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



Number of Cases

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

  2. Check permissions -> don't have permissions
    : show request dialog
    Screenshot

  3. show request dialog -> granted permissions
    : onPermissionGranted() called

  4. show request dialog -> denied permissions
    : show denied dialog
    Screenshot

  5. show denied dialog -> close
    : onPermissionDenied() called

  6. show denied dialog -> setting
    : startActivityForResult() to setting activity
    Screenshot

  7. setting activity -> onActivityResult()
    : check permission

  8. check permission -> granted permissions
    : onPermissionGranted() called

  9. check permission -> denied permissions
    : onPermissionDenied() called



Screenshot



License

Copyright 2017 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.

tedpermission's People

Contributors

b1uec0in avatar babosamo avatar boxresin avatar ema987 avatar gun0912 avatar jspiner avatar parksanggwon avatar yjbae-sk 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.