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
- Request Permissions.
- 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 grantedisDenied(Context context, String... permissions)
: Check permissions all deniedgetDeniedPermissions(Context context, String... permissions)
canRequestPermission(Activity activity, String... permissions)
: Iftrue
you can request system popup,false
mean user checkedNever ask again
startSettingActivityForResult(Activity activity)
startSettingActivityForResult(Activity activity, int requestCode)
Number of Cases
-
Check permissions -> have permissions
:onPermissionGranted()
called -
Check permissions -> don't have permissions
: show request dialog
-
show request dialog -> granted permissions
:onPermissionGranted()
called -
show request dialog -> denied permissions
: show denied dialog
-
show denied dialog -> close
:onPermissionDenied()
called -
show denied dialog -> setting
:startActivityForResult()
tosetting
activity
-
setting activity ->
onActivityResult()
: check permission -
check permission -> granted permissions
:onPermissionGranted()
called -
check permission -> denied permissions
:onPermissionDenied()
called
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.