Skip to content

Commit

Permalink
Fix issue #27
Browse files Browse the repository at this point in the history
  • Loading branch information
anggrayudi committed May 20, 2021
1 parent cafa69f commit 128106b
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 99 deletions.
2 changes: 1 addition & 1 deletion JAVA_COMPATIBILITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ You can learn it [here](https://www.raywenderlich.com/10986797-extension-functio
Common extension functions are stored in package `com.anggrayudi.storage.extension`. The others are in `com.anggrayudi.storage.file`.
You'll find that the most useful extension functions come from `DocumentFileExtKt` and `FileExtKt`. They are:
* `DocumentFile.storageId` and `File.storageId` → Get storage ID. Returns `primary` for external storage and something like `AAAA-BBBB` for SD card.
* `DocumentFile.absolutePath` → Get file's absolute path. Returns something like `/storage/AAAA-BBBB/Music/My Love.mp3`.
* `DocumentFile.getAbsolutePath()` → Get file's absolute path. Returns something like `/storage/AAAA-BBBB/Music/My Love.mp3`.
* `DocumentFile.copyFileTo()` and `File.copyFileTo()`
* `DocumentFile.search()` and `File.search()`, etc.

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Since `java.io.File` has been deprecated in Android 10, thus you have to use `Do
Simple Storage adds Kotlin extension functions to `DocumentFile`, so you can manage files like this:
* `DocumentFile.storageId`
* `DocumentFile.storageType`
* `DocumentFile.basePath`
* `DocumentFile.getBasePath()`
* `DocumentFile.copyFileTo()`
* `DocumentFile.moveFolderTo()`
* `DocumentFile.search()`
Expand Down Expand Up @@ -231,7 +231,7 @@ class MainActivity : AppCompatActivity() {
}

override fun onFolderSelected(requestCode: Int, folder: DocumentFile) {
Toast.makeText(baseContext, folder.absolutePath, Toast.LENGTH_SHORT).show()
Toast.makeText(baseContext, folder.getAbsolutePath(baseContext), Toast.LENGTH_SHORT).show()
}

override fun onCanceledByUser(requestCode: Int) {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
ext.kotlin_version = '1.5.0'

dependencies {
classpath 'com.android.tools.build:gradle:4.2.0'
classpath 'com.android.tools.build:gradle:4.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.15.1'
classpath "org.jetbrains.dokka:dokka-gradle-plugin:1.4.20"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ android.useAndroidX=true
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
STORAGE_VERSION=0.6.0-SNAPSHOT
STORAGE_VERSION=0.6.0
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ class MainActivity : AppCompatActivity() {
REQUEST_CODE_PICK_TARGET_FOLDER_FOR_FOLDER_COPY -> layoutCopyFolderToFolder.updateFolderSelectionView(folder)
REQUEST_CODE_PICK_SOURCE_FOLDER_FOR_MOVE -> layoutMoveFolderFromFolder.updateFolderSelectionView(folder)
REQUEST_CODE_PICK_TARGET_FOLDER_FOR_FOLDER_MOVE -> layoutMoveFolderToFolder.updateFolderSelectionView(folder)
else -> Toast.makeText(baseContext, folder.absolutePath, Toast.LENGTH_SHORT).show()
else -> Toast.makeText(baseContext, folder.getAbsolutePath(this), Toast.LENGTH_SHORT).show()
}
}
}

private fun View.updateFolderSelectionView(folder: DocumentFile) {
tag = folder
tvFilePath.text = folder.absolutePath
tvFilePath.text = folder.getAbsolutePath(context)
}

private fun View.updateFileSelectionView(file: DocumentFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import android.widget.Toast
import androidx.fragment.app.Fragment
import com.afollestad.materialdialogs.MaterialDialog
import com.anggrayudi.storage.SimpleStorageHelper
import com.anggrayudi.storage.file.absolutePath
import com.anggrayudi.storage.file.fullName
import com.anggrayudi.storage.file.getAbsolutePath
import com.anggrayudi.storage.permission.*
import com.anggrayudi.storage.sample.R
import com.anggrayudi.storage.sample.activity.MainActivity
Expand Down Expand Up @@ -91,7 +91,7 @@ class SampleFragment : Fragment() {
Toast.makeText(requireContext(), "File selected: ${file.fullName}", Toast.LENGTH_SHORT).show()
}
storageHelper.onFolderSelected = { requestCode, folder ->
Toast.makeText(requireContext(), folder.absolutePath, Toast.LENGTH_SHORT).show()
Toast.makeText(requireContext(), folder.getAbsolutePath(requireContext()), Toast.LENGTH_SHORT).show()
}
}
}
12 changes: 10 additions & 2 deletions storage/src/main/java/com/anggrayudi/storage/SimpleStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import android.provider.Settings
import androidx.annotation.RequiresApi
import androidx.annotation.RequiresPermission
import androidx.core.content.ContextCompat.checkSelfPermission
import androidx.documentfile.provider.DocumentFile
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.anggrayudi.storage.callback.FilePickerCallback
Expand All @@ -27,6 +28,7 @@ import com.anggrayudi.storage.file.DocumentFileCompat
import com.anggrayudi.storage.file.StorageType
import com.anggrayudi.storage.file.canModify
import timber.log.Timber
import java.io.File
import kotlin.concurrent.thread

/**
Expand Down Expand Up @@ -234,7 +236,7 @@ class SimpleStorage private constructor(private val wrapper: ComponentWrapper) {
else -> StorageType.SD_CARD
}
if (folder == null || !folder.canModify) {
folderPickerCallback?.onStorageAccessDenied(requestCode, null, storageType)
folderPickerCallback?.onStorageAccessDenied(requestCode, folder, storageType)
return
}
if (uri.toString() == DocumentFileCompat.DOWNLOADS_TREE_URI
Expand Down Expand Up @@ -262,7 +264,13 @@ class SimpleStorage private constructor(private val wrapper: ComponentWrapper) {
filePickerCallback?.onCanceledByUser(requestCode)
return
}
val file = wrapper.context.fromSingleUri(data?.data ?: return)
val uri = data?.data ?: return
val file = if (uri.isDownloadsDocument && Build.VERSION.SDK_INT < 28 && uri.path?.startsWith("/document/raw:") == true) {
val fullPath = uri.path.orEmpty().substringAfterLast("/document/raw:")
DocumentFile.fromFile(File(fullPath))
} else {
wrapper.context.fromSingleUri(uri)
}
if (file == null || !file.canRead()) {
filePickerCallback?.onStoragePermissionDenied(requestCode, file)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.anggrayudi.storage.callback.FolderPickerCallback
import com.anggrayudi.storage.callback.StorageAccessCallback
import com.anggrayudi.storage.file.DocumentFileCompat
import com.anggrayudi.storage.file.StorageType
import com.anggrayudi.storage.file.absolutePath
import com.anggrayudi.storage.file.getAbsolutePath
import com.karumi.dexter.Dexter
import com.karumi.dexter.MultiplePermissionsReport
import com.karumi.dexter.listener.multi.BaseMultiplePermissionsListener
Expand Down Expand Up @@ -63,17 +63,18 @@ class SimpleStorageHelper {
}

override fun onRootPathPermissionGranted(requestCode: Int, root: DocumentFile) {
val context = storage.context
if (openFolderPickerOnceGranted) {
storage.openFolderPicker(requestCodeFolderPicker)
Toast.makeText(
storage.context,
storage.context.getString(R.string.ss_selecting_root_path_success_with_open_folder_picker, root.absolutePath),
context,
context.getString(R.string.ss_selecting_root_path_success_with_open_folder_picker, root.getAbsolutePath(context)),
Toast.LENGTH_LONG
).show()
} else {
Toast.makeText(
storage.context,
storage.context.getString(R.string.ss_selecting_root_path_success_without_open_folder_picker, root.absolutePath),
context,
context.getString(R.string.ss_selecting_root_path_success_without_open_folder_picker, root.getAbsolutePath(context)),
Toast.LENGTH_SHORT
).show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
package com.anggrayudi.storage.extension

import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import androidx.fragment.app.Fragment

/**
* Created on 17/08/20
Expand All @@ -30,9 +30,11 @@ fun Activity.startActivityForResultSafely(requestCode: Int, intent: Intent) {
}
}

fun Fragment.startActivityForResultSafely(requestCode: Int, intent: Intent) {
if (intent.hasActivityHandler(requireContext())) {
startActivityForResult(intent, requestCode)
fun Context.unregisterReceiverSafely(receiver: BroadcastReceiver) {
try {
unregisterReceiver(receiver)
} catch (e: IllegalArgumentException) {
// ignore
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ object DocumentFileCompat {

const val EXTERNAL_STORAGE_AUTHORITY = "com.android.externalstorage.documents"

/*
* File picker for each API version gives the following URIs:
* * API 26 - 27 => `content://com.android.providers.downloads.documents/document/22`
* * API 28 - 29 => `content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2Fscreenshot.jpeg`
* * API 30+ => `content://com.android.providers.downloads.documents/document/msf%3A42`
*/
const val DOWNLOADS_FOLDER_AUTHORITY = "com.android.providers.downloads.documents"

const val MEDIA_FOLDER_AUTHORITY = "com.android.providers.media.documents"
Expand Down Expand Up @@ -137,7 +143,7 @@ object DocumentFileCompat {
* * For file in external storage => `/storage/emulated/0/Downloads/MyMovie.mp4`.
* * For file in SD card => `/storage/9016-4EF8/Downloads/MyMovie.mp4` or you can input simple path like this `9016-4EF8:Downloads/MyMovie.mp4`.
* You can input `9016-4EF8:` or `/storage/9016-4EF8` for SD card's root path.
* @see DocumentFile.absolutePath
* @see DocumentFile.getAbsolutePath
*/
@JvmOverloads
@JvmStatic
Expand Down Expand Up @@ -544,15 +550,15 @@ object DocumentFileCompat {
val directory = currentDirectory.findFile(it)
if (directory == null) {
currentDirectory = currentDirectory.createDirectory(it) ?: return@forEach
val fullPath = currentDirectory.absolutePath
val fullPath = currentDirectory.getAbsolutePath(context)
cleanedFullPaths.forEachIndexed { index, s ->
if (fullPath == s) {
results[index] = currentDirectory
}
}
} else if (directory.isDirectory && directory.canRead()) {
currentDirectory = directory
val fullPath = directory.absolutePath
val fullPath = directory.getAbsolutePath(context)
cleanedFullPaths.forEachIndexed { index, s ->
if (fullPath == s) {
results[index] = directory
Expand Down
Loading

0 comments on commit 128106b

Please sign in to comment.