diff --git a/LetsCheckPermission/src/main/java/com/akardas/letscheckpermission/LetsCheckPermission.kt b/LetsCheckPermission/src/main/java/com/akardas/letscheckpermission/LetsCheckPermission.kt index 94e5426..f580551 100644 --- a/LetsCheckPermission/src/main/java/com/akardas/letscheckpermission/LetsCheckPermission.kt +++ b/LetsCheckPermission/src/main/java/com/akardas/letscheckpermission/LetsCheckPermission.kt @@ -3,6 +3,8 @@ package com.akardas.letscheckpermission import android.content.Context import android.content.pm.PackageManager import android.os.Build +import androidx.activity.result.contract.ActivityResultContracts +import androidx.annotation.ChecksSdkIntAtLeast import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import java.security.Permissions @@ -10,6 +12,7 @@ import java.security.Permissions class LetsCheckPermission(private val context: Context) { + @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.M) private fun shouldAskPermission(): Boolean { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M } @@ -41,9 +44,6 @@ class LetsCheckPermission(private val context: Context) { } - - - } enum class Status{ diff --git a/LetsCheckPermission/src/main/java/com/akardas/letscheckpermission/RequestPermission.kt b/LetsCheckPermission/src/main/java/com/akardas/letscheckpermission/RequestPermission.kt deleted file mode 100644 index 750df90..0000000 --- a/LetsCheckPermission/src/main/java/com/akardas/letscheckpermission/RequestPermission.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.akardas.letscheckpermission - -import android.app.Activity -import android.widget.Toast -import androidx.activity.result.contract.ActivityResultContracts -import androidx.appcompat.app.AppCompatActivity - -object RequestPermission { - - fun withResult(activity: AppCompatActivity, permission:String, result:(isGranted:Boolean) -> Unit){ - val requestPermissionLauncher = activity.registerForActivityResult( - ActivityResultContracts.RequestPermission() - ) { isGranted -> - result.invoke(isGranted) - } - requestPermissionLauncher.launch(permission) - } - -} \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 015a6bb..ebf9155 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -78,8 +78,8 @@ dependencies { implementation 'io.github.hariprasanths:bounceview-android:0.2.0' - //implementation project(path: ':LetsCheckPermission') - implementation 'com.github.akardas16:LetPermission:1.0.0' + implementation project(path: ':LetsCheckPermission') + // implementation 'com.github.akardas16:LetPermission:1.0.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.4' diff --git a/app/src/main/java/com/akardas/letpermission/MainActivity.kt b/app/src/main/java/com/akardas/letpermission/MainActivity.kt index 170a4e6..8b2c663 100644 --- a/app/src/main/java/com/akardas/letpermission/MainActivity.kt +++ b/app/src/main/java/com/akardas/letpermission/MainActivity.kt @@ -3,11 +3,11 @@ package com.akardas.letpermission import android.Manifest import android.os.Bundle import android.widget.Toast +import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import com.akardas.letpermission.databinding.ActivityMainBinding import com.akardas.letscheckpermission.LetPermissionPreferences import com.akardas.letscheckpermission.LetsCheckPermission -import com.akardas.letscheckpermission.RequestPermission import com.akardas.letscheckpermission.Status @@ -32,20 +32,18 @@ class MainActivity : AppCompatActivity() { //Permission denied but you still have chance to show permission pop up again Toast.makeText(this,"Permission denied!",Toast.LENGTH_LONG).show() LetPermissionPreferences(this).firstTimeAsking(Manifest.permission.READ_CONTACTS,false) - RequestPermission.withResult(this,Manifest.permission.READ_CONTACTS){ isGranted -> - //You can listen the result from here when user granted o denied permission - Toast.makeText(this,"is granted $isGranted",Toast.LENGTH_LONG).show() - } + + //Request READ_CONTACTS Permission + requestPermissionLauncher.launch(Manifest.permission.READ_CONTACTS) } Status.NOT_ASKED -> {//Showing Pop up is possible //you have not yet requested permission Toast.makeText(this,"no Permission!",Toast.LENGTH_LONG).show() if (LetPermissionPreferences(this).isFirstTimeAsking(Manifest.permission.READ_CONTACTS)){ LetPermissionPreferences(this).firstTimeAsking(Manifest.permission.READ_CONTACTS,false) - RequestPermission.withResult(this,Manifest.permission.READ_CONTACTS){ isGranted -> - //You can listen the result from here when user granted o denied permission - Toast.makeText(this,"is granted $isGranted",Toast.LENGTH_LONG).show() - } + + //Request READ_CONTACTS Permission + requestPermissionLauncher.launch(Manifest.permission.READ_CONTACTS) } } Status.DENIED_WITH_NEVER_ASK -> { @@ -60,5 +58,11 @@ class MainActivity : AppCompatActivity() { } + private val requestPermissionLauncher = registerForActivityResult( + ActivityResultContracts.RequestPermission()) { isGranted -> + //You can listen the result from here when user granted o denied permission + Toast.makeText(this,"is granted: $isGranted",Toast.LENGTH_LONG).show() + } + } \ No newline at end of file