#What is TedPermission?
After Android Marshmallow, you have to not only decalare permisions in AndroidManifest.xml
but request permissions at runtime.
Furthermore anytime user can on/off permissions at application setting.
When you use dangerous permissons(ex. CAMERA, READ_CONTACTS, READ_PHONE_STATE), you have to check and request permissions runtime.
(See dangerous permissions)
You can make check function yourself.
(How to Requesting Permissions at RunTime)
But original check function is so complex and hard..
(checkSelfPermission()
, requestPermissions()
, onRequestPermissionsResult()
, onActivityResult()
...)
TedPermission is simple permission check helper.
##Demo
- Request Permissions.
- If user denied permissions, we will show message dialog with Setting button.
##Setup
#####Gradle Get library from jitpack.io
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
compile 'com.github.ParkSangGwon:TedPermission:v1.0.12'
}
##How to use
#####1. 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();
}
};
#####2. Start TedPermission TedPermission class need `setPermissionListener()`, `setPermissions()`. and `check()` will start check permissions
setRationaleMessage()
,setDeniedMessage()
is optional method.
new TedPermission(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();
##Proguard If you use proguard, you have to add this code.
-keepattributes *Annotation*
-keepclassmembers class ** {
@com.squareup.otto.Subscribe public *;
@com.squareup.otto.Produce public *;
}
##Customize
You can customize something ...
#####Function
setGotoSettingButton(boolean) (default: true)
setRationaleMessage(R.string.xxx or String)
setRationaleConfirmText(R.string.xxx or String) (default: confirm / 확인)
setDeniedMessage(R.string.xxx or String)
setDeniedCloseButtonText(R.string.xxx or String) (default: close / 닫기)
setGotoSettingButtonText(R.string.xxx or String) (default: setting / 설정)
##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
##Thanks
- Otto - An enhanced Guava-based event bus with emphasis on Android support
##License
Copyright 2016 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.```