Skip to content

Commit

Permalink
Merge pull request #3 from chintan369/feature-permissions
Browse files Browse the repository at this point in the history
Improves permission state after permission allowed
  • Loading branch information
rushi216 authored Feb 9, 2021
2 parents 8eb5d28 + f16d098 commit 9323dc0
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 201 deletions.
18 changes: 1 addition & 17 deletions Dynamic Permission Handling.iml
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="Dynamic Permission Handling" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="false" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<module version="4">
<component name="sonarModuleSettings">
<option name="localAnalysisScripName" value="&lt;PROJECT&gt;" />
<option name="serverName" value="&lt;PROJECT&gt;" />
Expand Down
173 changes: 1 addition & 172 deletions app/app.iml

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
buildToolsVersion "29.0.0"
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.ckdroid.permissions"
minSdkVersion 19
targetSdkVersion 28
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -25,10 +25,12 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
implementation 'com.github.Promact:dynamic-permission-handling-android:1.0.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
23 changes: 21 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.ckdroid.permissions">

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" />
</manifest>
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">

<activity android:name=".PermissionTestActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
118 changes: 118 additions & 0 deletions app/src/main/java/com/ckdroid/permissions/PermissionTestActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.ckdroid.permissions

import android.Manifest
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.ckdroid.dynamicpermissions.PermissionStatus
import com.ckdroid.dynamicpermissions.PermissionUtils
import kotlinx.android.synthetic.main.activity_permission_test.*

class PermissionTestActivity : AppCompatActivity() {

private val REQUEST_PERMISSION_CODE = 12

val requestPermissionList: MutableList<String> = mutableListOf()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_permission_test)

initializePermissionList()

setClickListeners()
}

private fun initializePermissionList() {
requestPermissionList.add(Manifest.permission.CAMERA)
requestPermissionList.add(Manifest.permission.RECORD_AUDIO)
requestPermissionList.add(Manifest.permission.ACCESS_FINE_LOCATION)
requestPermissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
requestPermissionList.add(Manifest.permission.WRITE_CONTACTS)
}

private fun setClickListeners() {
btnRequestPermission.setOnClickListener {
requestMultiplePermissions()
}
}

private fun requestMultiplePermissions() {

val permissionResult =
PermissionUtils.checkAndRequestPermissions(
this,
requestPermissionList,
REQUEST_PERMISSION_CODE
)

when (permissionResult.finalStatus) {
PermissionStatus.ALLOWED -> {//DO further stuffs as all permissions are allowed by user
Toast.makeText(
this,
"Permission is already allowed by user",
Toast.LENGTH_LONG
).show()
}
PermissionStatus.DENIED_PERMANENTLY -> {
//Request user to allow permission by sending to permission list page
//You can show customized dialog and then call this function
Toast.makeText(this, "Permission is permanently denied by user", Toast.LENGTH_LONG)
.show()
PermissionUtils.askUserToRequestPermissionExplicitly(this)
}
else -> {
//Permission is requesting for first time or user denied permission before but not permanently
}
}
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_PERMISSION_CODE) {
//Check status after user allowed or denied permission using the same way while requested permission
val permissionResult =
PermissionUtils.checkAndRequestPermissions(
this,
requestPermissionList,
REQUEST_PERMISSION_CODE,
checkStatusOnly = true
)

when (permissionResult.finalStatus) {
PermissionStatus.ALLOWED -> {//DO further stuffs as all permissions are allowed by user
Toast.makeText(this, "Permission allowed by user", Toast.LENGTH_LONG).show()
}
PermissionStatus.DENIED_PERMANENTLY -> {
Toast.makeText(
this,
"Permission is permanently denied by user",
Toast.LENGTH_LONG
).show()
}
else -> {
//Permission denied by user but not permanently
}
}

val cameraPermissionStatus =
permissionResult.permissionStatus[Manifest.permission.CAMERA]

when (cameraPermissionStatus) {
PermissionStatus.ALLOWED -> {
//Allowed
}
PermissionStatus.DENIED_PERMANENTLY -> {
//Denied Permanently
}
else -> {
//Not given
}
}
}
}
}
17 changes: 17 additions & 0 deletions app/src/main/res/layout/activity_permission_test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".PermissionTestActivity">

<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnRequestPermission"
android:text="@string/requestPermission"/>

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">Dynamic Permission Handling</string>
<string name="requestPermission">Request Permissions</string>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ allprojects {
repositories {
google()
jcenter()

maven { url 'https://jitpack.io' }
}
}

Expand Down
6 changes: 3 additions & 3 deletions dynamicpermissions/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
buildToolsVersion "29.0.0"
compileSdkVersion 30
buildToolsVersion "29.0.3"


defaultConfig {
minSdkVersion 19
targetSdkVersion 28
targetSdkVersion 30
versionCode 1
versionName "1.0"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ internal class PermissionPreference(context: Context) {
editor.putBoolean(permission, true).apply()
}

fun setPermissionAllowed(permission: String) {
val editor = sharedPreference.edit()
editor.putBoolean(permission, false).apply()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class PermissionUtils {

permissions.forEach { permission ->
if (hasPermissionAllowed(activity, permission)) {
permissionPreference.setPermissionAllowed(permission)
permissionStatus[permission] = PermissionStatus.ALLOWED
} else {
val isShowRational = isNeededToShowRequestRational(activity, permission)
Expand Down Expand Up @@ -96,7 +97,7 @@ class PermissionUtils {
)
}

fun askUserToRequestPermissionExplicitly(context: Context){
fun askUserToRequestPermissionExplicitly(context: Context) {
val intent = Intent()
intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
val uri = Uri.fromParts("package", context.packageName, null)
Expand Down Expand Up @@ -125,7 +126,7 @@ class PermissionUtils {

val permissionPreference = PermissionPreference(activity)

for(permission in permissionList){
for (permission in permissionList) {
permissionPreference.setPermissionRequested(permission)
}
}
Expand Down

0 comments on commit 9323dc0

Please sign in to comment.