aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca/allanwang/kau/permissions
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-12-24 01:17:31 -0500
committerGitHub <noreply@github.com>2018-12-24 01:17:31 -0500
commitfb90c4a4ab23f2fd246c29c1dde4d6e155a2e38b (patch)
treecc6d67461b6caa256043b9482f7646831d528a08 /core/src/main/kotlin/ca/allanwang/kau/permissions
parenta380adea1052d39f23c9c4d432a9380ce347d6c4 (diff)
downloadkau-fb90c4a4ab23f2fd246c29c1dde4d6e155a2e38b.tar.gz
kau-fb90c4a4ab23f2fd246c29c1dde4d6e155a2e38b.tar.bz2
kau-fb90c4a4ab23f2fd246c29c1dde4d6e155a2e38b.zip
Enhancement/ktlint (#179)
* Add spotless dependency * Apply ktlint * Add license headers
Diffstat (limited to 'core/src/main/kotlin/ca/allanwang/kau/permissions')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt29
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionResult.kt17
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/permissions/Permissions.kt26
3 files changed, 61 insertions, 11 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt
index 922dc09..57a6f42 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt
@@ -1,3 +1,18 @@
+/*
+ * Copyright 2018 Allan Wang
+ *
+ * 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.
+ */
package ca.allanwang.kau.permissions
import android.app.Activity
@@ -12,7 +27,6 @@ import ca.allanwang.kau.utils.hasPermission
import ca.allanwang.kau.utils.toast
import java.lang.ref.WeakReference
-
/**
* Created by Allan Wang on 2017-07-03.
*
@@ -30,13 +44,17 @@ internal object PermissionManager {
private val manifestPermission = lazyContext<Array<String>> {
try {
it.packageManager.getPackageInfo(it.packageName, PackageManager.GET_PERMISSIONS)?.requestedPermissions
- ?: emptyArray()
+ ?: emptyArray()
} catch (e: Exception) {
emptyArray()
}
}
- operator fun invoke(context: Context, permissions: Array<out String>, callback: (granted: Boolean, deniedPerm: String?) -> Unit) {
+ operator fun invoke(
+ context: Context,
+ permissions: Array<out String>,
+ callback: (granted: Boolean, deniedPerm: String?) -> Unit
+ ) {
KL.d { "Permission manager for: ${permissions.contentToString()}" }
if (!buildIsMarshmallowAndUp) return callback(true, null)
val missingPermissions = permissions.filter { !context.hasPermission(it) }
@@ -58,7 +76,7 @@ internal object PermissionManager {
}
}
val activity = (context as? Activity)
- ?: throw KauException("Context is not an instance of an activity; cannot request permissions")
+ ?: throw KauException("Context is not an instance of an activity; cannot request permissions")
KL.i { "Requesting permissions ${permissions.contentToString()}" }
ActivityCompat.requestPermissions(activity, permissions, 1)
}
@@ -89,5 +107,4 @@ internal object PermissionManager {
}
KL.i { "Post on permission result: pending ${pendingResults.size}" }
}
-
-} \ No newline at end of file
+}
diff --git a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionResult.kt b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionResult.kt
index ba3e6dd..8888c91 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionResult.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionResult.kt
@@ -1,3 +1,18 @@
+/*
+ * Copyright 2018 Allan Wang
+ *
+ * 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.
+ */
package ca.allanwang.kau.permissions
import android.content.pm.PackageManager
@@ -25,4 +40,4 @@ class PermissionResult(permissions: Array<out String>, val callback: (granted: B
callback(true, null)
return true
}
-} \ No newline at end of file
+}
diff --git a/core/src/main/kotlin/ca/allanwang/kau/permissions/Permissions.kt b/core/src/main/kotlin/ca/allanwang/kau/permissions/Permissions.kt
index 83cd2ed..3c90b05 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/permissions/Permissions.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/Permissions.kt
@@ -1,3 +1,18 @@
+/*
+ * Copyright 2018 Allan Wang
+ *
+ * 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.
+ */
package ca.allanwang.kau.permissions
import android.Manifest
@@ -6,7 +21,6 @@ import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
-
/**
* Created by Allan Wang on 2017-07-02.
*
@@ -22,7 +36,8 @@ import androidx.annotation.RequiresApi
/**
* Hook that should be added inside all [Activity.onRequestPermissionsResult] so that the Permission manager can handle the responses
*/
-fun Activity.kauOnRequestPermissionsResult(permissions: Array<out String>, grantResults: IntArray) = PermissionManager.onRequestPermissionsResult(this, permissions, grantResults)
+fun Activity.kauOnRequestPermissionsResult(permissions: Array<out String>, grantResults: IntArray) =
+ PermissionManager.onRequestPermissionsResult(this, permissions, grantResults)
/**
* Request a permission with a callback
@@ -31,7 +46,10 @@ fun Activity.kauOnRequestPermissionsResult(permissions: Array<out String>, grant
* The [callback] returns [granted], which is true if all permissions are granted
* [deniedPerm] is the first denied permission, if granted is false
*/
-fun Context.kauRequestPermissions(vararg permissions: String, callback: (granted: Boolean, deniedPerm: String?) -> Unit) = PermissionManager(this, permissions, callback)
+fun Context.kauRequestPermissions(
+ vararg permissions: String,
+ callback: (granted: Boolean, deniedPerm: String?) -> Unit
+) = PermissionManager(this, permissions, callback)
/**
* See http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous for a
@@ -72,4 +90,4 @@ const val PERMISSION_RECEIVE_MMS = Manifest.permission.RECEIVE_MMS
const val PERMISSION_READ_EXTERNAL_STORAGE = Manifest.permission.READ_EXTERNAL_STORAGE
const val PERMISSION_WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE
-const val PERMISSION_SYSTEM_ALERT_WINDOW = Manifest.permission.SYSTEM_ALERT_WINDOW \ No newline at end of file
+const val PERMISSION_SYSTEM_ALERT_WINDOW = Manifest.permission.SYSTEM_ALERT_WINDOW