Skip to content

Commit ad51b68

Browse files
authored
Merge pull request #569 from permissions-dispatcher/fix_434
OnShowRationale API change with keeping backward compatibilty
2 parents cd4b232 + 36da33c commit ad51b68

32 files changed

+1241
-1178
lines changed

doc/java_usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ PermissionsDispatcher introduces only a few annotations, keeping its general API
1818
|---|---|---|
1919
|`@RuntimePermissions`|****|Register an `Activity` or `Fragment`(we support both) to handle permissions|
2020
|`@NeedsPermission`|****|Annotate a method which performs the action that requires one or more permissions|
21-
|`@OnShowRationale`||Annotate a method which explains why the permission/s is/are needed. It passes in a `PermissionRequest` object which can be used to continue or abort the current permission request upon user input|
21+
|`@OnShowRationale`||Annotate a method which explains why the permissions are needed. It passes in a `PermissionRequest` object which can be used to continue or abort the current permission request upon user input. If you don't specify any argument for the method compiler will generate `process${NeedsPermissionMethodName}ProcessRequest` and `cancel${NeedsPermissionMethodName}ProcessRequest`. You can use those methods in place of `PermissionRequest`(ex: with `DialogFragment`)|
2222
|`@OnPermissionDenied`||Annotate a method which is invoked if the user doesn't grant the permissions|
2323
|`@OnNeverAskAgain`||Annotate a method which is invoked if the user chose to have the device "never ask again" about a permission|
2424

doc/kotlin_usage.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@
22

33
Here's a minimum example, in which you register a `MainActivity` which requires `Manifest.permission.CAMERA`.
44

5-
### 0. Preparation
5+
### 0. Prepare AndroidManifest
66

77
Add the following line to `AndroidManifest.xml`:
88

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

1111
### 1. Attach annotations
1212

13+
PermissionsDispatcher introduces only a few annotations, keeping its general API concise:
14+
1315
> NOTE: Annotated methods must not be `private`.
1416
17+
|Annotation|Required|Description|
18+
|---|---|---|
19+
|`@RuntimePermissions`|****|Register an `Activity` or `Fragment`(we support both) to handle permissions|
20+
|`@NeedsPermission`|****|Annotate a method which performs the action that requires one or more permissions|
21+
|`@OnShowRationale`||Annotate a method which explains why the permissions are needed. It passes in a `PermissionRequest` object which can be used to continue or abort the current permission request upon user input. If you don't specify any argument for the method compiler will generate `process${NeedsPermissionMethodName}ProcessRequest` and `cancel${NeedsPermissionMethodName}ProcessRequest`. You can use those methods in place of `PermissionRequest`(ex: with `DialogFragment`)|
22+
|`@OnPermissionDenied`||Annotate a method which is invoked if the user doesn't grant the permissions|
23+
|`@OnNeverAskAgain`||Annotate a method which is invoked if the user chose to have the device "never ask again" about a permission|
24+
1525
```kotlin
1626
@RuntimePermissions
1727
class MainActivity : AppCompatActivity(), View.OnClickListener {

processor/src/main/kotlin/permissions/dispatcher/processor/PermissionsProcessor.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import permissions.dispatcher.processor.util.kotlinMetadataClass
88
import java.io.File
99
import javax.annotation.processing.AbstractProcessor
1010
import javax.annotation.processing.Filer
11-
import javax.annotation.processing.Messager
1211
import javax.annotation.processing.ProcessingEnvironment
1312
import javax.annotation.processing.RoundEnvironment
1413
import javax.lang.model.SourceVersion
@@ -33,12 +32,10 @@ class PermissionsProcessor : AbstractProcessor() {
3332

3433
/* Processing Environment helpers */
3534
private var filer: Filer by Delegates.notNull()
36-
var messager: Messager by Delegates.notNull()
3735

3836
override fun init(processingEnv: ProcessingEnvironment) {
3937
super.init(processingEnv)
4038
filer = processingEnv.filer
41-
messager = processingEnv.messager
4239
ELEMENT_UTILS = processingEnv.elementUtils
4340
TYPE_UTILS = processingEnv.typeUtils
4441
}
@@ -86,13 +83,13 @@ class PermissionsProcessor : AbstractProcessor() {
8683
kaptGeneratedDir.parentFile.mkdirs()
8784
}
8885

89-
val processorUnit = findAndValidateProcessorUnit(kotlinProcessorUnits(), element)
86+
val processorUnit = findAndValidateProcessorUnit(kotlinProcessorUnits, element)
9087
val kotlinFile = processorUnit.createFile(rpe, requestCodeProvider)
9188
kotlinFile.writeTo(kaptGeneratedDir)
9289
}
9390

9491
private fun processJava(element: Element, rpe: RuntimePermissionsElement, requestCodeProvider: RequestCodeProvider) {
95-
val processorUnit = findAndValidateProcessorUnit(javaProcessorUnits(), element)
92+
val processorUnit = findAndValidateProcessorUnit(javaProcessorUnits, element)
9693
val javaFile = processorUnit.createFile(rpe, requestCodeProvider)
9794
javaFile.writeTo(filer)
9895
}

processor/src/main/kotlin/permissions/dispatcher/processor/ProcessorUnit.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import javax.lang.model.type.TypeMirror
66

77
interface ProcessorUnit<out K> {
88
fun getTargetType(): TypeMirror
9+
/**
10+
* Creates the File for the provided @RuntimePermissions element.
11+
* <p>
12+
* This will delegate to other methods that compose generated code.
13+
*/
914
fun createFile(rpe: RuntimePermissionsElement, requestCodeProvider: RequestCodeProvider): K
1015
}
1116

processor/src/main/kotlin/permissions/dispatcher/processor/RuntimePermissionsElement.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ class RuntimePermissionsElement(val e: TypeElement) {
3232
validateNeverAskMethods()
3333
}
3434

35-
/* Begin private */
36-
3735
private fun validateNeedsMethods() {
3836
checkNotEmpty(needsElements, this, NeedsPermission::class.java)
3937
checkPrivateMethods(needsElements, NeedsPermission::class.java)
@@ -64,8 +62,6 @@ class RuntimePermissionsElement(val e: TypeElement) {
6462
checkSpecialPermissionsWithNeverAskAgain(onNeverAskElements)
6563
}
6664

67-
/* Begin public */
68-
6965
fun findOnRationaleForNeeds(needsElement: ExecutableElement): ExecutableElement? {
7066
return findMatchingMethodForNeeds(needsElement, onRationaleElements, OnShowRationale::class.java)
7167
}

processor/src/main/kotlin/permissions/dispatcher/processor/exception/WrongParametersException.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ import permissions.dispatcher.processor.util.simpleString
44
import javax.lang.model.element.ExecutableElement
55
import javax.lang.model.type.TypeMirror
66

7-
class WrongParametersException(e: ExecutableElement, requiredType: TypeMirror?) : RuntimeException("Method '${e.simpleString()}()' must declare parameters of type '${requiredType?.simpleString()}'")
7+
class WrongParametersException(e: ExecutableElement, numParams: Int, requiredType: TypeMirror) : RuntimeException("Method '${e.simpleString()}()' must has less than or equal to $numParams size parameter and type of it is supposed to be '${requiredType.simpleString()}'")

processor/src/main/kotlin/permissions/dispatcher/processor/impl/ProcessorUnits.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,5 @@ import permissions.dispatcher.processor.impl.java.JavaFragmentProcessorUnit
55
import permissions.dispatcher.processor.impl.kotlin.KotlinActivityProcessorUnit
66
import permissions.dispatcher.processor.impl.kotlin.KotlinFragmentProcessorUnit
77

8-
fun javaProcessorUnits() = listOf(
9-
JavaActivityProcessorUnit(),
10-
JavaFragmentProcessorUnit())
11-
12-
fun kotlinProcessorUnits() = listOf(
13-
KotlinActivityProcessorUnit(),
14-
KotlinFragmentProcessorUnit())
8+
val javaProcessorUnits = listOf(JavaActivityProcessorUnit(), JavaFragmentProcessorUnit())
9+
val kotlinProcessorUnits = listOf(KotlinActivityProcessorUnit(), KotlinFragmentProcessorUnit())

processor/src/main/kotlin/permissions/dispatcher/processor/impl/java/JavaActivityProcessorUnit.kt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,19 @@ import permissions.dispatcher.processor.util.typeMirrorOf
66
import javax.lang.model.type.TypeMirror
77

88
/**
9-
* ProcessorUnit implementation for Activity classes.
9+
* ProcessorUnit implementation for Activity.
1010
*/
1111
class JavaActivityProcessorUnit : JavaBaseProcessorUnit() {
1212

13-
private val ACTIVITY_COMPAT = ClassName.get("androidx.core.app", "ActivityCompat")
13+
override fun getTargetType(): TypeMirror = typeMirrorOf("android.app.Activity")
1414

15-
override fun isDeprecated(): Boolean = false
16-
17-
override fun getTargetType(): TypeMirror {
18-
return typeMirrorOf("android.app.Activity")
19-
}
20-
21-
override fun getActivityName(targetParam: String): String {
22-
return targetParam
23-
}
15+
override fun getActivityName(targetParam: String): String = targetParam
2416

2517
override fun addShouldShowRequestPermissionRationaleCondition(builder: MethodSpec.Builder, targetParam: String, permissionField: String, isPositiveCondition: Boolean) {
2618
builder.beginControlFlow("if (\$N\$T.shouldShowRequestPermissionRationale(\$N, \$N))", if (isPositiveCondition) "" else "!", PERMISSION_UTILS, targetParam, permissionField)
2719
}
2820

2921
override fun addRequestPermissionsStatement(builder: MethodSpec.Builder, targetParam: String, permissionField: String, requestCodeField: String) {
30-
builder.addStatement("\$T.requestPermissions(\$N, \$N, \$N)", ACTIVITY_COMPAT, targetParam, permissionField, requestCodeField)
22+
builder.addStatement("\$T.requestPermissions(\$N, \$N, \$N)", ClassName.get("androidx.core.app", "ActivityCompat"), targetParam, permissionField, requestCodeField)
3123
}
3224
}

0 commit comments

Comments
 (0)