Skip to content

Commit cf47898

Browse files
chiragmiakashmi
andauthored
Feature/permission fixes (#39)
* - Provided "android.permission.MANAGE_EXTERNAL_STORAGE" permission access to the user and removed from the library * Update README.md Co-authored-by: AKASH PATEL <akash.patel@mindinventory.com>
1 parent 4e2eaf3 commit cf47898

File tree

6 files changed

+78
-45
lines changed

6 files changed

+78
-45
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Lassi is simplest way to pick media (either image, video, audio or doc)
5151
```groovy
5252
dependencies {
5353
...
54-
implementation 'com.github.Mindinventory:Lassi:0.2.1'
54+
implementation 'com.github.Mindinventory:Lassi:0.2.2'
5555
}
5656
```
5757
@@ -108,6 +108,8 @@ Lassi is simplest way to pick media (either image, video, audio or doc)
108108
}
109109
```
110110
111+
### Document access permission note
112+
If Android device SDK is >= 30 and wants to access document (only for choose the non media file) then add ```android.permission.MANAGE_EXTERNAL_STORAGE``` permission in your app otherwise library won't allow to access documents. Kindly check sample app for more detail.
111113
112114
### Guideline for contributors
113115
Contribution towards our repository is always welcome, we request contributors to create a pull request to the **develop** branch only.

app/src/main/AndroidManifest.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.lassi.app">
44

5+
<!-- This permission require only for document picker feature and if Android device SDK is >= 30-->
6+
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
7+
58
<application
69
android:allowBackup="true"
710
android:icon="@mipmap/ic_launcher"
811
android:label="@string/app_name"
12+
android:preserveLegacyExternalStorage="true"
913
android:requestLegacyExternalStorage="true"
1014
android:roundIcon="@mipmap/ic_launcher_round"
1115
android:supportsRtl="true"
12-
android:preserveLegacyExternalStorage="true"
1316
android:theme="@style/AppTheme">
1417

1518
<activity

app/src/main/java/com/lassi/app/MainActivity.kt

Lines changed: 69 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import android.app.Activity
44
import android.content.ContentResolver
55
import android.content.Intent
66
import android.net.Uri
7+
import android.os.Build
78
import android.os.Bundle
8-
import android.util.Log
9+
import android.os.Environment
10+
import android.provider.Settings
911
import android.view.View
1012
import android.webkit.MimeTypeMap
1113
import androidx.activity.result.contract.ActivityResultContracts
@@ -104,36 +106,40 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
104106
receiveData.launch(intent)
105107
}
106108
R.id.btnDocPicker -> {
107-
val intent = Lassi(this)
108-
.setMediaType(MediaType.DOC)
109-
.setMaxCount(4)
110-
.setGridSize(2)
111-
.setPlaceHolder(R.drawable.ic_document_placeholder)
112-
.setErrorDrawable(R.drawable.ic_document_placeholder)
113-
.setSelectionDrawable(R.drawable.ic_checked_media)
114-
.setStatusBarColor(R.color.colorPrimaryDark)
115-
.setToolbarColor(R.color.colorPrimary)
116-
.setToolbarResourceColor(android.R.color.white)
117-
.setSupportedFileTypes(
118-
"pdf",
119-
"odt",
120-
"doc",
121-
"docs",
122-
"docx",
123-
"txt",
124-
"ppt",
125-
"pptx",
126-
"rtf",
127-
"xlsx",
128-
"xls"
129-
)
130-
.setProgressBarColor(R.color.colorAccent)
131-
.build()
132-
receiveData.launch(intent)
109+
requestPermissionForDocument()
133110
}
134111
}
135112
}
136113

114+
private fun launchDocPicker() {
115+
val intent = Lassi(this)
116+
.setMediaType(MediaType.DOC)
117+
.setMaxCount(4)
118+
.setGridSize(2)
119+
.setPlaceHolder(R.drawable.ic_document_placeholder)
120+
.setErrorDrawable(R.drawable.ic_document_placeholder)
121+
.setSelectionDrawable(R.drawable.ic_checked_media)
122+
.setStatusBarColor(R.color.colorPrimaryDark)
123+
.setToolbarColor(R.color.colorPrimary)
124+
.setToolbarResourceColor(android.R.color.white)
125+
.setSupportedFileTypes(
126+
"pdf",
127+
"odt",
128+
"doc",
129+
"docs",
130+
"docx",
131+
"txt",
132+
"ppt",
133+
"pptx",
134+
"rtf",
135+
"xlsx",
136+
"xls"
137+
)
138+
.setProgressBarColor(R.color.colorAccent)
139+
.build()
140+
receiveData.launch(intent)
141+
}
142+
137143
private val receiveData =
138144
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
139145
if (it.resultCode == Activity.RESULT_OK) {
@@ -175,4 +181,40 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
175181
startActivity(Intent.createChooser(intent, "Open file"))
176182
}
177183
}
184+
185+
/**
186+
* If Android device SDK is >= 30 and wants to access document (only for choose the non media file)
187+
* then ask for "android.permission.MANAGE_EXTERNAL_STORAGE" permission
188+
*/
189+
private fun requestPermissionForDocument() {
190+
when {
191+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
192+
if (Environment.isExternalStorageManager()) {
193+
launchDocPicker()
194+
} else {
195+
try {
196+
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
197+
intent.addCategory("android.intent.category.DEFAULT")
198+
intent.data = Uri.parse(
199+
String.format("package:%s", applicationContext?.packageName)
200+
)
201+
mPermissionSettingResult.launch(intent)
202+
} catch (e: Exception) {
203+
val intent = Intent()
204+
intent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
205+
mPermissionSettingResult.launch(intent)
206+
}
207+
}
208+
}
209+
210+
else -> {
211+
launchDocPicker()
212+
}
213+
}
214+
}
215+
216+
private val mPermissionSettingResult =
217+
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
218+
requestPermissionForDocument()
219+
}
178220
}

lassi/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
defaultConfig {
1313
minSdkVersion 17
1414
targetSdkVersion 30
15-
versionCode 19
16-
versionName "0.2.1"
15+
versionCode 20
16+
versionName "0.2.2"
1717
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1818
vectorDrawables.useSupportLibrary = true
1919
multiDexEnabled true

lassi/src/main/AndroidManifest.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
66
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
7-
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
87
<uses-permission android:name="android.permission.CAMERA" />
98
<uses-permission android:name="android.permission.RECORD_AUDIO" />
109

lassi/src/main/java/com/lassi/presentation/docs/DocsFragment.kt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,6 @@ class DocsFragment : LassiBaseViewModelFragment<DocsViewModel>() {
8888
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R -> {
8989
if (Environment.isExternalStorageManager()) {
9090
fetchDocs()
91-
} else {
92-
try {
93-
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
94-
intent.addCategory("android.intent.category.DEFAULT")
95-
intent.data = Uri.parse(
96-
String.format("package:%s", context?.applicationContext?.packageName)
97-
)
98-
mPermissionSettingResult.launch(intent)
99-
} catch (e: Exception) {
100-
val intent = Intent()
101-
intent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION
102-
mPermissionSettingResult.launch(intent)
103-
}
10491
}
10592
}
10693
Build.VERSION.SDK_INT == Build.VERSION_CODES.Q -> {

0 commit comments

Comments
 (0)