├── .gitignore ├── .idea ├── .gitignore ├── .name ├── compiler.xml ├── gradle.xml ├── kotlinc.xml └── misc.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── dalemncy │ │ └── demo │ │ └── permissions │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── dalemncy │ │ │ └── demo │ │ │ └── permissions │ │ │ ├── MainActivity.kt │ │ │ └── constants │ │ │ └── Constants.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── values-night │ │ └── themes.xml │ │ ├── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── themes.xml │ │ └── xml │ │ ├── backup_rules.xml │ │ └── data_extraction_rules.xml │ └── test │ └── java │ └── com │ └── dalemncy │ └── demo │ └── permissions │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | local.properties 16 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | Permissions Demo -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/kotlinc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android Kotlin Permissions 2 | 3 | This repository serves as a comprehensive guide to handling camera and location permissions in Android using Kotlin. 4 | 5 | ## Features 6 | 7 | - Easy integration of permission requests 8 | - Simple and clear code snippets 9 | - Smooth user experience 10 | 11 | ## Installation 12 | 13 | Clone the repository and open it in Android Studio to get started. 14 | 15 | ## Usage 16 | 17 | Refer to the provided demos and code snippets for an easy implementation of camera and location permissions. 18 | 19 | ## Contributions 20 | 21 | Contributions are welcome! Please feel free to raise issues and submit pull requests. 22 | 23 | ## License 24 | 25 | This project is licensed under the [MIT License](https://opensource.org/licenses/MIT). 26 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'org.jetbrains.kotlin.android' 4 | } 5 | 6 | android { 7 | namespace 'com.dalemncy.demo.permissions' 8 | compileSdk 34 9 | 10 | defaultConfig { 11 | applicationId "com.dalemncy.demo.permissions" 12 | minSdk 24 13 | targetSdk 34 14 | versionCode 1 15 | versionName "1.0" 16 | 17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 18 | } 19 | 20 | buildTypes { 21 | release { 22 | minifyEnabled false 23 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 24 | } 25 | } 26 | buildFeatures { 27 | viewBinding true 28 | } 29 | compileOptions { 30 | sourceCompatibility JavaVersion.VERSION_1_8 31 | targetCompatibility JavaVersion.VERSION_1_8 32 | } 33 | kotlinOptions { 34 | jvmTarget = '1.8' 35 | } 36 | } 37 | 38 | dependencies { 39 | 40 | implementation 'androidx.core:core-ktx:1.8.0' 41 | implementation 'androidx.appcompat:appcompat:1.6.1' 42 | implementation 'com.google.android.material:material:1.10.0' 43 | implementation 'androidx.constraintlayout:constraintlayout:2.1.4' 44 | testImplementation 'junit:junit:4.13.2' 45 | androidTestImplementation 'androidx.test.ext:junit:1.1.5' 46 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' 47 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/com/dalemncy/demo/permissions/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.dalemncy.demo.permissions 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.dalemncy.demo.permissions", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 21 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/dalemncy/demo/permissions/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.dalemncy.demo.permissions 2 | 3 | import android.Manifest 4 | import android.content.Intent 5 | import android.content.pm.PackageManager 6 | import androidx.appcompat.app.AppCompatActivity 7 | import android.os.Bundle 8 | import android.provider.MediaStore 9 | import android.widget.Toast 10 | import androidx.activity.result.contract.ActivityResultContracts 11 | import androidx.core.app.ActivityCompat 12 | import androidx.core.content.ContextCompat 13 | import com.dalemncy.demo.permissions.constants.Constants 14 | import com.dalemncy.demo.permissions.databinding.ActivityMainBinding 15 | 16 | class MainActivity : AppCompatActivity() { 17 | private lateinit var binding: ActivityMainBinding 18 | 19 | private val takePicture = registerForActivityResult( 20 | ActivityResultContracts.StartActivityForResult()) { result -> 21 | if (result.resultCode == RESULT_OK) { 22 | // Add content here for processing captured photo. 23 | Toast.makeText( 24 | this, 25 | "Picture taken successfully", 26 | Toast.LENGTH_SHORT 27 | ).show() 28 | } else { 29 | Toast.makeText( 30 | this, 31 | "Picture wasn't taken", 32 | Toast.LENGTH_SHORT 33 | ).show() 34 | } 35 | } 36 | 37 | override fun onCreate(savedInstanceState: Bundle?) { 38 | super.onCreate(savedInstanceState) 39 | binding = ActivityMainBinding.inflate(layoutInflater) 40 | setContentView(binding.root) 41 | 42 | binding.camera.setOnClickListener { camera() } 43 | binding.location.setOnClickListener { location() } 44 | } 45 | 46 | override fun onRequestPermissionsResult( 47 | requestCode: Int, 48 | permissions: Array, 49 | grantResults: IntArray 50 | ) { 51 | super.onRequestPermissionsResult(requestCode, permissions, grantResults) 52 | if (requestCode == Constants.LOCATION_PERMISSION_CODE) { 53 | if (grantResults.isNotEmpty() && grantResults[0] 54 | == PackageManager.PERMISSION_GRANTED) { 55 | openLocation() 56 | } else { 57 | Toast.makeText( 58 | this, 59 | "Location permission denied", 60 | Toast.LENGTH_SHORT 61 | ).show() 62 | } 63 | } else if (requestCode == Constants.CAMERA_PERMISSION_CODE) { 64 | if (grantResults.isNotEmpty() && grantResults[0] 65 | == PackageManager.PERMISSION_GRANTED) { 66 | openCamera() 67 | } else { 68 | Toast.makeText( 69 | this, 70 | "Camera permission denied", 71 | Toast.LENGTH_SHORT 72 | ).show() 73 | } 74 | } 75 | } 76 | 77 | private fun camera() { 78 | if (ContextCompat.checkSelfPermission( 79 | this, Manifest.permission.CAMERA) 80 | == PackageManager.PERMISSION_DENIED 81 | ) { 82 | ActivityCompat.requestPermissions( 83 | this, 84 | arrayOf(Manifest.permission.CAMERA), 85 | Constants.CAMERA_PERMISSION_CODE 86 | ) 87 | } else { 88 | openCamera() 89 | } 90 | } 91 | 92 | private fun openCamera() { 93 | val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) 94 | try { 95 | takePicture.launch(takePictureIntent) 96 | } catch (e: Exception) { 97 | Toast.makeText(this, "Error: ${e.message}", Toast.LENGTH_SHORT).show() 98 | finish() 99 | } 100 | } 101 | 102 | 103 | private fun location() { 104 | if (ContextCompat.checkSelfPermission( 105 | this, 106 | Manifest.permission.ACCESS_FINE_LOCATION 107 | ) == PackageManager.PERMISSION_DENIED 108 | ) { 109 | ActivityCompat.requestPermissions( 110 | this, 111 | arrayOf( 112 | Manifest.permission.ACCESS_FINE_LOCATION, 113 | Manifest.permission.ACCESS_COARSE_LOCATION 114 | ), 115 | Constants.LOCATION_PERMISSION_CODE 116 | ) 117 | } else { 118 | openLocation() 119 | } 120 | } 121 | 122 | private fun openLocation() { 123 | // Permission is already granted 124 | // You can perform your location-related task here. 125 | Toast.makeText( 126 | this, 127 | "Location permission already granted", 128 | Toast.LENGTH_SHORT 129 | ).show() 130 | } 131 | } -------------------------------------------------------------------------------- /app/src/main/java/com/dalemncy/demo/permissions/constants/Constants.kt: -------------------------------------------------------------------------------- 1 | package com.dalemncy.demo.permissions.constants 2 | 3 | class Constants { 4 | companion object { 5 | const val LOCATION_PERMISSION_CODE = 100 6 | const val CAMERA_PERMISSION_CODE = 200 7 | } 8 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 14 |