-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from chintan369/feature-permissions
Improves permission state after permission allowed
- Loading branch information
Showing
11 changed files
with
177 additions
and
201 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -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
118
app/src/main/java/com/ckdroid/permissions/PermissionTestActivity.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,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 | ||
} | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
<?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> |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
<resources> | ||
<string name="app_name">Dynamic Permission Handling</string> | ||
<string name="requestPermission">Request Permissions</string> | ||
</resources> |
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 |
---|---|---|
|
@@ -19,7 +19,7 @@ allprojects { | |
repositories { | ||
google() | ||
jcenter() | ||
|
||
maven { url 'https://jitpack.io' } | ||
} | ||
} | ||
|
||
|
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