Skip to content

Commit

Permalink
removed deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
anggrayudi committed Jun 17, 2024
1 parent d04321b commit a4c54ee
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 210 deletions.
1 change: 1 addition & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ dependencies {
implementation deps.timber
implementation deps.material_progressbar
implementation 'androidx.preference:preference-ktx:1.2.1'
implementation 'com.afollestad.material-dialogs:files:3.3.0'

//test
testImplementation deps.junit
Expand Down
7 changes: 1 addition & 6 deletions sample/src/main/java/com/anggrayudi/storage/sample/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@ import androidx.multidex.MultiDexApplication
* @author Anggrayudi Hardiannico A. ([email protected])
* @version App, v 0.0.1 10/08/20 00.39 by Anggrayudi Hardiannico A.
*/
class App : MultiDexApplication() {

override fun onCreate() {
super.onCreate()
}
}
class App : MultiDexApplication()
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class StorageInfoAdapter(
* A storageId may contains more than one granted URIs
*/
@SuppressLint("NewApi")
@Suppress("DEPRECATION")
private fun showGrantedUris(context: Context, filterStorageId: String) {
val grantedPaths = DocumentFileCompat.getAccessibleAbsolutePaths(context)[filterStorageId]
if (grantedPaths == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,7 @@ class MainActivity : AppCompatActivity() {
isEnabled = Build.VERSION.SDK_INT in 23..28
}

binding.layoutBaseOperation.btnRequestStorageAccess.run {
isEnabled = Build.VERSION.SDK_INT >= 21
setOnClickListener { storageHelper.requestStorageAccess(REQUEST_CODE_STORAGE_ACCESS) }
}
binding.layoutBaseOperation.btnRequestStorageAccess.setOnClickListener { storageHelper.requestStorageAccess(REQUEST_CODE_STORAGE_ACCESS) }

binding.layoutBaseOperation.btnRequestFullStorageAccess.run {
isEnabled = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import com.afollestad.materialdialogs.MaterialDialog
import com.anggrayudi.storage.SimpleStorageHelper
import com.anggrayudi.storage.file.fullName
import com.anggrayudi.storage.file.getAbsolutePath
import com.anggrayudi.storage.permission.*
import com.anggrayudi.storage.permission.FragmentPermissionRequest
import com.anggrayudi.storage.permission.PermissionCallback
import com.anggrayudi.storage.permission.PermissionReport
import com.anggrayudi.storage.permission.PermissionRequest
import com.anggrayudi.storage.permission.PermissionResult
import com.anggrayudi.storage.sample.R
import com.anggrayudi.storage.sample.activity.MainActivity
import com.anggrayudi.storage.sample.databinding.InclBaseOperationBinding
Expand Down Expand Up @@ -63,10 +67,7 @@ class SampleFragment : Fragment(R.layout.incl_base_operation) {
isEnabled = Build.VERSION.SDK_INT in 23..28
}

binding.btnRequestStorageAccess.run {
isEnabled = Build.VERSION.SDK_INT >= 21
setOnClickListener { storageHelper.requestStorageAccess(MainActivity.REQUEST_CODE_STORAGE_ACCESS) }
}
binding.btnRequestStorageAccess.setOnClickListener { storageHelper.requestStorageAccess(MainActivity.REQUEST_CODE_STORAGE_ACCESS) }

binding.btnRequestFullStorageAccess.run {
isEnabled = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Expand Down
3 changes: 1 addition & 2 deletions storage/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ dependencies {
api deps.documentfile
api deps.coroutines.core
api deps.coroutines.android
api 'com.afollestad.material-dialogs:files:3.3.0'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.2"

testImplementation deps.junit
testImplementation deps.mockk
Expand Down
67 changes: 24 additions & 43 deletions storage/src/main/java/com/anggrayudi/storage/SimpleStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@ import androidx.annotation.RequiresPermission
import androidx.core.content.ContextCompat.checkSelfPermission
import androidx.documentfile.provider.DocumentFile
import androidx.fragment.app.Fragment
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.callbacks.onCancel
import com.afollestad.materialdialogs.files.folderChooser
import com.anggrayudi.storage.callback.*
import com.anggrayudi.storage.extension.*
import com.anggrayudi.storage.file.*
import com.anggrayudi.storage.callback.CreateFileCallback
import com.anggrayudi.storage.callback.FilePickerCallback
import com.anggrayudi.storage.callback.FileReceiverCallback
import com.anggrayudi.storage.callback.FolderPickerCallback
import com.anggrayudi.storage.callback.StorageAccessCallback
import com.anggrayudi.storage.extension.fromSingleUri
import com.anggrayudi.storage.extension.fromTreeUri
import com.anggrayudi.storage.extension.getStorageId
import com.anggrayudi.storage.extension.isDocumentsDocument
import com.anggrayudi.storage.extension.isDownloadsDocument
import com.anggrayudi.storage.extension.isExternalStorageDocument
import com.anggrayudi.storage.file.DocumentFileCompat
import com.anggrayudi.storage.file.FileFullPath
import com.anggrayudi.storage.file.MimeType
import com.anggrayudi.storage.file.PublicDirectory
import com.anggrayudi.storage.file.StorageId.PRIMARY
import com.anggrayudi.storage.file.StorageType
import com.anggrayudi.storage.file.canModify
import com.anggrayudi.storage.file.getAbsolutePath
import com.anggrayudi.storage.file.getBasePath
import java.io.File
import kotlin.concurrent.thread

Expand Down Expand Up @@ -106,7 +119,6 @@ class SimpleStorage private constructor(private val wrapper: ComponentWrapper) {
* volume/path of SdCard, and of course, SdCard != External Storage.
*/
private val sdCardRootAccessIntent: Intent
@Suppress("DEPRECATION")
@RequiresApi(api = Build.VERSION_CODES.N)
get() {
val sm = context.getSystemService(Context.STORAGE_SERVICE) as StorageManager
Expand Down Expand Up @@ -145,7 +157,6 @@ class SimpleStorage private constructor(private val wrapper: ComponentWrapper) {
* trigger [StorageAccessCallback.onRootPathNotSelected]. Set to [StorageType.UNKNOWN] to accept any storage type.
* @param expectedBasePath applicable for API 30+ only, because Android 11 does not allow selecting the root path.
*/
@RequiresApi(21)
@JvmOverloads
fun requestStorageAccess(
requestCode: Int = requestCodeStorageAccess,
Expand Down Expand Up @@ -229,21 +240,6 @@ class SimpleStorage private constructor(private val wrapper: ComponentWrapper) {
initialPath?.checkIfStorageIdIsAccessibleInSafSelector()
requestCodeFolderPicker = requestCode

if (Build.VERSION.SDK_INT < 21) {
MaterialDialog(context).folderChooser(
context,
initialDirectory = initialPath?.let { File(it.absolutePath) } ?: lastVisitedFolder,
allowFolderCreation = true,
selection = { _, file ->
lastVisitedFolder = file
folderPickerCallback?.onFolderSelected(requestCode, DocumentFile.fromFile(file))
}
).negativeButton(android.R.string.cancel, click = { it.cancel() })
.onCancel { folderPickerCallback?.onCanceledByUser(requestCode) }
.show()
return
}

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P || hasStoragePermission(context)) {
val intent = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
Expand All @@ -258,7 +254,6 @@ class SimpleStorage private constructor(private val wrapper: ComponentWrapper) {
}
}

@Suppress("DEPRECATION")
private var lastVisitedFolder: File = Environment.getExternalStorageDirectory()

/**
Expand All @@ -274,12 +269,8 @@ class SimpleStorage private constructor(private val wrapper: ComponentWrapper) {
initialPath?.checkIfStorageIdIsAccessibleInSafSelector()
requestCodeFilePicker = requestCode

val intent = if (Build.VERSION.SDK_INT < 21) {
Intent(Intent.ACTION_GET_CONTENT)
} else {
Intent(Intent.ACTION_OPEN_DOCUMENT)
}
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, allowMultiple)
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, allowMultiple)
if (filterMimeTypes.size > 1) {
intent.setType(MimeType.UNKNOWN)
.putExtra(Intent.EXTRA_MIME_TYPES, filterMimeTypes)
Expand Down Expand Up @@ -374,7 +365,6 @@ class SimpleStorage private constructor(private val wrapper: ComponentWrapper) {
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
val sm = context.getSystemService(Context.STORAGE_SERVICE) as StorageManager
@Suppress("DEPRECATION")
sm.storageVolumes.firstOrNull { !it.isPrimary }?.createAccessIntent(null)?.let {
if (!wrapper.startActivityForResult(it, requestCode)) {
storageAccessCallback?.onActivityHandlerNotFound(requestCode, it)
Expand Down Expand Up @@ -427,13 +417,8 @@ class SimpleStorage private constructor(private val wrapper: ComponentWrapper) {
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 context.fromSingleUri(uri)?.let { file ->
// content://com.android.externalstorage.documents/document/15FA-160C%3Aabc.txt
if (Build.VERSION.SDK_INT < 21 && file.getStorageId(context).matches(DocumentFileCompat.SD_CARD_STORAGE_ID_REGEX)) {
DocumentFile.fromFile(DocumentFileCompat.getKitkatSdCardRootFile(file.getBasePath(context)))
} else {
file
}
} else {
context.fromSingleUri(uri)
}
}.filter { it.isFile }
}
Expand Down Expand Up @@ -522,7 +507,7 @@ class SimpleStorage private constructor(private val wrapper: ComponentWrapper) {
fun onRestoreInstanceState(savedInstanceState: Bundle) {
savedInstanceState.getString(KEY_LAST_VISITED_FOLDER)?.let { lastVisitedFolder = File(it) }
expectedBasePathForAccessRequest = savedInstanceState.getString(KEY_EXPECTED_BASE_PATH_FOR_ACCESS_REQUEST)
expectedStorageTypeForAccessRequest = StorageType.values()[savedInstanceState.getInt(KEY_EXPECTED_STORAGE_TYPE_FOR_ACCESS_REQUEST)]
expectedStorageTypeForAccessRequest = StorageType.entries.toTypedArray()[savedInstanceState.getInt(KEY_EXPECTED_STORAGE_TYPE_FOR_ACCESS_REQUEST)]
requestCodeStorageAccess = savedInstanceState.getInt(KEY_REQUEST_CODE_STORAGE_ACCESS, DEFAULT_REQUEST_CODE_STORAGE_ACCESS)
requestCodeFolderPicker = savedInstanceState.getInt(KEY_REQUEST_CODE_FOLDER_PICKER, DEFAULT_REQUEST_CODE_FOLDER_PICKER)
requestCodeFilePicker = savedInstanceState.getInt(KEY_REQUEST_CODE_FILE_PICKER, DEFAULT_REQUEST_CODE_FILE_PICKER)
Expand Down Expand Up @@ -576,11 +561,7 @@ class SimpleStorage private constructor(private val wrapper: ComponentWrapper) {
private const val DEFAULT_REQUEST_CODE_FILE_PICKER: Int = 3
private const val DEFAULT_REQUEST_CODE_CREATE_FILE: Int = 4

const val KITKAT_SD_CARD_ID = "sdcard"
const val KITKAT_SD_CARD_PATH = "/storage/$KITKAT_SD_CARD_ID"

@JvmStatic
@Suppress("DEPRECATION")
val externalStoragePath: String
get() = Environment.getExternalStorageDirectory().absolutePath

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AlertDialog
import androidx.documentfile.provider.DocumentFile
import androidx.fragment.app.Fragment
import com.anggrayudi.storage.callback.*
import com.anggrayudi.storage.callback.CreateFileCallback
import com.anggrayudi.storage.callback.FilePickerCallback
import com.anggrayudi.storage.callback.FileReceiverCallback
import com.anggrayudi.storage.callback.FolderPickerCallback
import com.anggrayudi.storage.callback.StorageAccessCallback
import com.anggrayudi.storage.extension.getStorageId
import com.anggrayudi.storage.file.FileFullPath
import com.anggrayudi.storage.file.StorageType
import com.anggrayudi.storage.file.getAbsolutePath
import com.anggrayudi.storage.permission.*
import com.anggrayudi.storage.permission.ActivityPermissionRequest
import com.anggrayudi.storage.permission.FragmentPermissionRequest
import com.anggrayudi.storage.permission.PermissionCallback
import com.anggrayudi.storage.permission.PermissionReport
import com.anggrayudi.storage.permission.PermissionRequest
import com.anggrayudi.storage.permission.PermissionResult

/**
* Helper class to ease you using file & folder picker.
Expand Down Expand Up @@ -344,7 +352,6 @@ class SimpleStorageHelper {
}
}

@RequiresApi(21)
@JvmOverloads
fun requestStorageAccess(
requestCode: Int = storage.requestCodeStorageAccess,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

package com.anggrayudi.storage.extension

import android.os.Build
import androidx.annotation.RestrictTo
import com.anggrayudi.storage.SimpleStorage
import com.anggrayudi.storage.file.DocumentFileCompat
import com.anggrayudi.storage.file.DocumentFileCompat.removeForbiddenCharsFromFilename
import com.anggrayudi.storage.file.StorageId

/**
* Created on 19/08/20
Expand Down Expand Up @@ -46,10 +43,6 @@ fun String.replaceCompletely(match: String, replaceWith: String) = let {
path
}

@RestrictTo(RestrictTo.Scope.LIBRARY)
fun String.isKitkatSdCardStorageId() =
Build.VERSION.SDK_INT < 21 && (this == StorageId.KITKAT_SDCARD || this.matches(DocumentFileCompat.SD_CARD_STORAGE_ID_REGEX))

@RestrictTo(RestrictTo.Scope.LIBRARY)
fun String.hasParent(parentPath: String): Boolean {
val parentTree = parentPath.getFolderTree()
Expand All @@ -73,7 +66,6 @@ fun String.parent(): String {
val parentPath = folderTree.take(folderTree.size - 1).joinToString("/", "/")
return if (parentPath.startsWith(SimpleStorage.externalStoragePath)
|| parentPath.matches(Regex("/storage/[A-Z0-9]{4}-[A-Z0-9]{4}(.*?)"))
|| Build.VERSION.SDK_INT < 21 && parentPath.startsWith(SimpleStorage.KITKAT_SD_CARD_PATH)
) {
parentPath
} else {
Expand Down
Loading

0 comments on commit a4c54ee

Please sign in to comment.