|
| 1 | +/* |
| 2 | + * Created by Weiyi Li on 16/03/20. |
| 3 | + * https://github.com/li2 |
| 4 | + */ |
| 5 | +@file:Suppress("unused") |
| 6 | + |
| 7 | +package me.li2.android.common.framework |
| 8 | + |
| 9 | +import android.Manifest.permission.* |
| 10 | +import android.app.Activity |
| 11 | +import android.content.Context |
| 12 | +import android.content.DialogInterface |
| 13 | +import android.content.pm.PackageManager.PERMISSION_GRANTED |
| 14 | +import androidx.appcompat.app.AlertDialog |
| 15 | +import androidx.core.content.ContextCompat |
| 16 | +import com.tbruyelle.rxpermissions2.RxPermissions |
| 17 | +import hu.akarnokd.rxjava3.bridge.RxJavaBridge |
| 18 | +import io.reactivex.rxjava3.core.Observable |
| 19 | +import me.li2.android.common.framework.PermissionResult.* |
| 20 | +import me.li2.android.common.rx.buttonClicks |
| 21 | + |
| 22 | +enum class PermissionResult { |
| 23 | + GRANTED, |
| 24 | + DENIED, |
| 25 | + DENIED_NOT_ASK_AGAIN, |
| 26 | +} |
| 27 | + |
| 28 | +fun Context.isPermissionGranted(permission: String): Boolean { |
| 29 | + return ContextCompat.checkSelfPermission(this, permission) == PERMISSION_GRANTED |
| 30 | +} |
| 31 | + |
| 32 | +fun Activity.requestPermission(permission: String): Observable<PermissionResult> = |
| 33 | + requestPermissions(listOf(permission)) |
| 34 | + |
| 35 | +fun Activity.requestPermissions(permissions: List<String>): Observable<PermissionResult> = |
| 36 | + // todo remove RxJavaBridge when RxPermissions migrated to RxJava3 |
| 37 | + RxJavaBridge |
| 38 | + .toV3Observable(RxPermissions(this).requestEach(*permissions.toTypedArray())) |
| 39 | + .map { permission -> |
| 40 | + when { |
| 41 | + // permission is granted |
| 42 | + permission.granted -> GRANTED |
| 43 | + // Denied permission without ask never again |
| 44 | + permission.shouldShowRequestPermissionRationale -> DENIED |
| 45 | + // Denied permission with ask never again, need to go to the settings |
| 46 | + else -> DENIED_NOT_ASK_AGAIN |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | +fun Activity.checkAndRequestPermission( |
| 51 | + permission: String, |
| 52 | + prompt: AlertDialog? = null |
| 53 | +): Observable<PermissionResult> { |
| 54 | + return Observable.just(isPermissionGranted(permission)) |
| 55 | + .flatMap { granted -> |
| 56 | + when { |
| 57 | + !granted && prompt != null -> { |
| 58 | + prompt.also { it.show() }.buttonClicks().flatMap { which -> |
| 59 | + if (which == DialogInterface.BUTTON_POSITIVE) { |
| 60 | + requestPermission(permission) |
| 61 | + } else { |
| 62 | + Observable.just(DENIED) |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + !granted && prompt == null -> requestPermission(permission) |
| 67 | + else -> Observable.just(GRANTED) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/* |
| 73 | + * ACCESS_FINE_LOCATION for both NETWORK_PROVIDER and GPS_PROVIDER |
| 74 | + * ACCESS_COARSE_LOCATION only for NETWORK_PROVIDER. |
| 75 | + */ |
| 76 | +fun Context.isLocationPermissionGranted(): Boolean = isPermissionGranted(ACCESS_FINE_LOCATION) |
| 77 | + |
| 78 | +fun Activity.checkAndRequestLocationPermission(prompt: AlertDialog? = null): Observable<PermissionResult> = |
| 79 | + checkAndRequestPermission(ACCESS_FINE_LOCATION, prompt) |
| 80 | + |
| 81 | +fun Activity.checkAndRequestCameraPermission(prompt: AlertDialog? = null): Observable<PermissionResult> = |
| 82 | + checkAndRequestPermission(CAMERA, prompt) |
| 83 | + |
| 84 | +fun Activity.checkAndRequestBluetoothPermission(prompt: AlertDialog? = null): Observable<PermissionResult> = |
| 85 | + checkAndRequestPermission(BLUETOOTH, prompt) |
0 commit comments