-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
CraZyLegenD
committed
Jun 17, 2019
1 parent
7dfc4ef
commit 4d5a096
Showing
11 changed files
with
525 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/crazylegend/setofusefulkotlinextensions/MainAbstractActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,4 +142,5 @@ class MainApplication : Application() { | |
//setting the locale | ||
//LocaleHelper.setLocale(context, "en") | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
...rc/main/java/com/crazylegend/kotlinextensions/permissionHandlers/BasePermissionManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.crazylegend.kotlinextensions.permissionHandlers | ||
|
||
import android.content.pm.PackageManager | ||
import android.os.Bundle | ||
import androidx.core.content.ContextCompat | ||
import androidx.fragment.app.Fragment | ||
|
||
/** | ||
* Created by hristijan on 6/17/19 to long live and prosper ! | ||
*/ | ||
|
||
abstract class BasePermissionManager : Fragment() { | ||
|
||
private val rationalRequest = mutableMapOf<Int, Boolean>() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
retainInstance = true | ||
} | ||
|
||
override fun onRequestPermissionsResult( | ||
requestCode: Int, | ||
permissions: Array<out String>, | ||
grantResults: IntArray | ||
) { | ||
if (grantResults.isNotEmpty() && | ||
grantResults.all { it == PackageManager.PERMISSION_GRANTED } | ||
) { | ||
onPermissionResult(PermissionResult.PermissionGranted(requestCode)) | ||
} else if (permissions.any { shouldShowRequestPermissionRationale(it) }) { | ||
onPermissionResult( | ||
PermissionResult.PermissionDenied(requestCode, | ||
permissions.filterIndexed { index, _ -> | ||
grantResults[index] == PackageManager.PERMISSION_DENIED | ||
} | ||
) | ||
) | ||
} else { | ||
onPermissionResult( | ||
PermissionResult.PermissionDeniedPermanently(requestCode, | ||
permissions.filterIndexed { index, _ -> | ||
grantResults[index] == PackageManager.PERMISSION_DENIED | ||
} | ||
)) | ||
} | ||
} | ||
|
||
protected fun requestPermissions(requestId: Int, vararg permissions: String) { | ||
|
||
rationalRequest[requestId]?.let { | ||
requestPermissions(permissions, requestId) | ||
rationalRequest.remove(requestId) | ||
return | ||
} | ||
|
||
val notGranted = permissions.filter { | ||
ContextCompat.checkSelfPermission( | ||
requireActivity(), | ||
it | ||
) != PackageManager.PERMISSION_GRANTED | ||
}.toTypedArray() | ||
|
||
when { | ||
notGranted.isEmpty() -> | ||
onPermissionResult(PermissionResult.PermissionGranted(requestId)) | ||
notGranted.any { shouldShowRequestPermissionRationale(it) } -> { | ||
rationalRequest[requestId] = true | ||
onPermissionResult(PermissionResult.ShowRationale(requestId)) | ||
} | ||
else -> { | ||
requestPermissions(notGranted, requestId) | ||
} | ||
} | ||
} | ||
|
||
protected abstract fun onPermissionResult(permissionResult: PermissionResult) | ||
} |
168 changes: 168 additions & 0 deletions
168
...ns/src/main/java/com/crazylegend/kotlinextensions/permissionHandlers/PermissionManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package com.crazylegend.kotlinextensions.permissionHandlers | ||
|
||
import android.arch.lifecycle.LiveData | ||
import android.content.Context | ||
import android.support.annotation.MainThread | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.fragment.app.Fragment | ||
|
||
|
||
/** | ||
* Interface definition for a callback to get [LiveData] of [PermissionResult] | ||
* | ||
interface PermissionObserver { | ||
fun setupObserver(permissionResultLiveData: LiveData<PermissionResult>) | ||
} | ||
* Implement this interface to get [LiveData] for observing permission request result. | ||
*/ | ||
|
||
/** | ||
override fun setupObserver(permissionResultLiveData: LiveData<PermissionResult>) { | ||
permissionResultLiveData.observe(this, Observer<PermissionResult> { | ||
when (it) { | ||
is PermissionResult.PermissionGranted -> { | ||
if (it.requestId == REQUEST_ID) { | ||
//Add your logic here after user grants permission(s) | ||
} | ||
} | ||
is PermissionResult.PermissionDenied -> { | ||
if (it.requestId == REQUEST_ID) { | ||
//Add your logic to handle permission denial | ||
} | ||
} | ||
is PermissionResult.PermissionDeniedPermanently -> { | ||
if (it.requestId == REQUEST_ID) { | ||
//Add your logic here if user denied permission(s) permanently. | ||
//Ideally you should ask user to manually go to settings and enable permission(s) | ||
} | ||
} | ||
is PermissionResult.ShowRational -> { | ||
if (it.requestId == REQUEST_ID) { | ||
//If user denied permission frequently then she/he is not clear about why you are asking this permission. | ||
//This is your chance to explain them why you need permission. | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
* | ||
*/ | ||
|
||
|
||
|
||
/** | ||
* Created by hristijan on 6/17/19 to long live and prosper ! | ||
*/ | ||
|
||
class PermissionManager : BasePermissionManager() { | ||
|
||
private val permissionResultLiveEvent: SingleLiveEvent<PermissionResult> by lazy { | ||
SingleLiveEvent<PermissionResult>() | ||
} | ||
|
||
override fun onPermissionResult(permissionResult: PermissionResult) { | ||
permissionResultLiveEvent.postValue(permissionResult) | ||
} | ||
|
||
override fun onAttach(context: Context) { | ||
super.onAttach(context) | ||
if (parentFragment != null) { | ||
(parentFragment as PermissionObserver).setupObserver(permissionResultLiveEvent) | ||
} else { | ||
(context as PermissionObserver).setupObserver(permissionResultLiveEvent) | ||
} | ||
} | ||
|
||
|
||
companion object { | ||
|
||
private const val TAG = "PermissionManager" | ||
|
||
/** | ||
* A static factory method to request permission from activity. | ||
* Your activity must implement [PermissionObserver] | ||
* | ||
* @param activity an instance of [AppCompatActivity] which is also [PermissionObserver] | ||
* @param requestId Request ID for permission request | ||
* @param permissions Permission(s) to request | ||
* | ||
* @throws [IllegalArgumentException] if your activity doesn't implement [PermissionObserver] | ||
*/ | ||
@JvmStatic | ||
@MainThread | ||
fun requestPermissions( | ||
activity: AppCompatActivity, | ||
requestId: Int, | ||
vararg permissions: String | ||
) { | ||
_requestPermissions( | ||
activity, | ||
requestId, | ||
*permissions | ||
) | ||
} | ||
|
||
/** | ||
* A static factory method to request permission from fragment. | ||
* Your fragment must implement [PermissionObserver] | ||
* | ||
* @param fragment an instance of [Fragment] which is also [PermissionObserver] | ||
* @param requestId Request ID for permission request | ||
* @param permissions Permission(s) to request | ||
* | ||
* @throws [IllegalArgumentException] if your fragment doesn't implement [PermissionObserver] | ||
*/ | ||
@JvmStatic | ||
@MainThread | ||
fun requestPermissions( | ||
fragment: Fragment, | ||
requestId: Int, | ||
vararg permissions: String | ||
) { | ||
_requestPermissions( | ||
fragment, | ||
requestId, | ||
*permissions | ||
) | ||
} | ||
|
||
private fun _requestPermissions( | ||
activityOrFragment: Any, | ||
requestId: Int, | ||
vararg permissions: String | ||
) { | ||
|
||
val fragmentManager = if (activityOrFragment is AppCompatActivity) { | ||
activityOrFragment.supportFragmentManager | ||
} else { | ||
(activityOrFragment as Fragment).childFragmentManager | ||
} | ||
|
||
if (fragmentManager.findFragmentByTag(TAG) != null) { | ||
(fragmentManager.findFragmentByTag(TAG) as PermissionManager).requestPermissions( | ||
requestId, | ||
*permissions | ||
) | ||
} else { | ||
if (activityOrFragment !is PermissionObserver) { | ||
throw IllegalArgumentException( | ||
"Activity/Fragment must implement PermissionObserver" | ||
) | ||
} else { | ||
val permissionManager = PermissionManager() | ||
fragmentManager.beginTransaction().add( | ||
permissionManager, | ||
TAG | ||
).commitNow() | ||
permissionManager.requestPermissions(requestId, *permissions) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...s/src/main/java/com/crazylegend/kotlinextensions/permissionHandlers/PermissionObserver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.crazylegend.kotlinextensions.permissionHandlers | ||
|
||
import android.arch.lifecycle.LiveData | ||
|
||
|
||
/** | ||
* Created by hristijan on 6/17/19 to long live and prosper ! | ||
*/ | ||
|
||
/** | ||
* Interface definition for a callback to get [LiveData] of [PermissionResult] | ||
* | ||
* Implement this interface to get [LiveData] for observing permission request result. | ||
*/ | ||
interface PermissionObserver { | ||
fun setupObserver(permissionResultLiveData: LiveData<PermissionResult>) | ||
} |
20 changes: 20 additions & 0 deletions
20
...ons/src/main/java/com/crazylegend/kotlinextensions/permissionHandlers/PermissionResult.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.crazylegend.kotlinextensions.permissionHandlers | ||
|
||
|
||
/** | ||
* Created by hristijan on 6/17/19 to long live and prosper ! | ||
*/ | ||
|
||
sealed class PermissionResult { | ||
class PermissionGranted(val requestId: Int) : PermissionResult() | ||
class PermissionDenied( | ||
val requestId: Int, | ||
val deniedPermissions: List<String> | ||
) : PermissionResult() | ||
|
||
class ShowRationale(val requestId: Int) : PermissionResult() | ||
class PermissionDeniedPermanently( | ||
val requestId: Int, | ||
val permanentlyDeniedPermissions: List<String> | ||
) : PermissionResult() | ||
} |
Oops, something went wrong.