Support go back while in custom root mode. by rosuH · Pull Request #158 · rosuH/AndroidFilePicker · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
5 changes: 4 additions & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
| ![](https://raw.githubusercontent.com/rosuH/AndroidFilePicker/master/images/default_theme.png) | ![](https://raw.githubusercontent.com/rosuH/AndroidFilePicker/master/images/reply_theme.png) | ![](https://raw.githubusercontent.com/rosuH/AndroidFilePicker/master/images/crane_theme.png) | ![](https://raw.githubusercontent.com/rosuH/AndroidFilePicker/master/images/shrine_theme.png) |

## 版本兼容性
支持 Android 11 及其以下所有版本。
这取决于您的 targetAPI :

- `targetAPI <= 28`,完全没有问题 ;)
- `targetAPI >= 29`,请为您的项目启用 `requestLegacyExternalStorage` 特性:D

## 下载使用

Expand Down
28 changes: 26 additions & 2 deletions filepicker/src/main/java/me/rosuh/filepicker/FilePickerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.animation.AnimationUtils
Expand All @@ -35,7 +36,8 @@ import java.io.File
import java.util.concurrent.*

@SuppressLint("ShowToast")
class FilePickerActivity : AppCompatActivity(), View.OnClickListener, RecyclerViewListener.OnItemClickListener{
class FilePickerActivity : AppCompatActivity(), View.OnClickListener,
RecyclerViewListener.OnItemClickListener {

private var rvList: RecyclerViewFilePicker? = null
private var rvNav: RecyclerView? = null
Expand Down Expand Up @@ -71,7 +73,26 @@ class FilePickerActivity : AppCompatActivity(), View.OnClickListener, RecyclerVi

private val loadFileRunnable: Runnable by lazy {
Runnable {
val customRootPathFile = pickerConfig.customRootPathFile
val rootFile = when {
customRootPathFile?.exists() == true -> {
// move to custom root dir
navDataSource.clear()
val root = FileUtils.getRootFile()
var curPath = customRootPathFile.absolutePath
while (curPath != root.parent && !curPath.isNullOrBlank()) {
Log.i("loadFileRunnable", "curPath = $curPath")
val f = File(curPath)
val fileNavBeanImpl = FileNavBeanImpl(
FileUtils.getDirAlias(f),
f.absolutePath
)
navDataSource.add(0, fileNavBeanImpl)
curPath = f.parent
}
pickerConfig.resetCustomFile()
customRootPathFile
}
navDataSource.isEmpty() && pickerConfig.isSkipDir -> {
FileUtils.getRootFile()
}
Expand All @@ -85,6 +106,7 @@ class FilePickerActivity : AppCompatActivity(), View.OnClickListener, RecyclerVi
}

val listData = FileUtils.produceListDataSource(rootFile)

// 导航栏数据集
navDataSource = FileUtils.produceNavDataSource(
navDataSource,
Expand Down Expand Up @@ -171,6 +193,8 @@ class FilePickerActivity : AppCompatActivity(), View.OnClickListener, RecyclerVi
if (!loadingThreadPool.isShutdown) {
loadingThreadPool.shutdown()
}
currOffsetMap.clear()
currPosMap.clear()
}

private fun isPermissionGrated() = ContextCompat.checkSelfPermission(
Expand Down Expand Up @@ -472,7 +496,7 @@ class FilePickerActivity : AppCompatActivity(), View.OnClickListener, RecyclerVi
position: Int
) {
if (view.id != R.id.item_list_file_picker) return
val item = (adapter as FileListAdapter).getItem(position)?: return
val item = (adapter as FileListAdapter).getItem(position) ?: return
// Check the lib users whether if intercept the click event.
val hookItemClick = FilePickerManager.config.itemClickListener?.onItemLongClick(
adapter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import me.rosuh.filepicker.FilePickerActivity
import me.rosuh.filepicker.R
import me.rosuh.filepicker.engine.ImageEngine
import me.rosuh.filepicker.filetype.FileType
import java.io.File

/**
*
Expand Down Expand Up @@ -55,10 +56,12 @@ class FilePickerConfig(private val pickerManager: FilePickerManager) {
var maxSelectable = Int.MAX_VALUE
private set

internal val defaultStorageName = contextRes.getString(R.string.file_picker_tv_sd_card)

/**
* 存储类型
*/
var mediaStorageName = contextRes.getString(R.string.file_picker_tv_sd_card)
var mediaStorageName = defaultStorageName
private set

/**
Expand All @@ -75,6 +78,9 @@ class FilePickerConfig(private val pickerManager: FilePickerManager) {
var customRootPath: String = ""
private set

internal var customRootPathFile: File? = null
private set

/**
* 自定义过滤器
*/
Expand Down Expand Up @@ -170,6 +176,15 @@ class FilePickerConfig(private val pickerManager: FilePickerManager) {

fun setCustomRootPath(path: String): FilePickerConfig {
customRootPath = path
path.takeIf {
it.isNotBlank()
}?.let {
File(it)
}?.takeIf {
it.exists()
}?.let {
customRootPathFile = it
}
return this
}

Expand Down Expand Up @@ -311,13 +326,18 @@ class FilePickerConfig(private val pickerManager: FilePickerManager) {
}
}

fun resetCustomFile() {
this.customRootPathFile = null
}

fun clear() {
this.customFileTypes.clear()
this.customImageEngine = null
this.fileItemOnClickListener = null
this.selfFilter = null
this.customDetector = null
this.defaultFileDetector.clear()
resetCustomFile()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ object FilePickerManager {
const val REQUEST_CODE = 10401

internal var contextRef: WeakReference<Activity>? = null

internal var fragmentRef: WeakReference<Fragment>? = null

internal lateinit var config: FilePickerConfig

private var dataList: MutableList<String> = ArrayList()

@JvmStatic
fun from(activity: Activity): FilePickerConfig {
reset()
Expand All @@ -40,12 +44,10 @@ object FilePickerManager {
return config
}

private var dataList: List<String> = ArrayList()

/**
* 保存数据@param list List<String>到本类中
*/
internal fun saveData(list: List<String>) {
internal fun saveData(list: MutableList<String>) {
dataList = list
}

Expand All @@ -55,16 +57,18 @@ object FilePickerManager {
*/
@JvmOverloads
@JvmStatic
fun obtainData(release: Boolean = false): List<String> {
fun obtainData(release: Boolean = false): MutableList<String> {
val result = ArrayList(dataList)
if (release) {
release()
}
return dataList
return result
}

private fun reset() {
contextRef?.clear()
fragmentRef?.clear()
dataList.clear()
ImageLoadController.reset()
}

Expand Down
49 changes: 29 additions & 20 deletions filepicker/src/main/java/me/rosuh/filepicker/utils/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package me.rosuh.filepicker.utils

import android.content.Context
import android.os.Environment
import me.rosuh.filepicker.R
import me.rosuh.filepicker.bean.FileItemBeanImpl
import me.rosuh.filepicker.bean.FileNavBeanImpl
import me.rosuh.filepicker.config.FilePickerConfig.Companion.STORAGE_CUSTOM_ROOT_PATH
Expand All @@ -25,18 +24,11 @@ class FileUtils {
* 根据配置参数获取根目录文件
* @return File
*/
fun getRootFile():File {
fun getRootFile(): File {
return when (config.mediaStorageType) {
STORAGE_EXTERNAL_STORAGE -> {
File(Environment.getExternalStorageDirectory().absoluteFile.toURI())
}
STORAGE_CUSTOM_ROOT_PATH -> {
if (config.customRootPath.isEmpty()) {
File(Environment.getExternalStorageDirectory().absoluteFile.toURI())
} else {
File(config.customRootPath)
}
}
else -> {
File(Environment.getExternalStorageDirectory().absoluteFile.toURI())
}
Expand Down Expand Up @@ -70,7 +62,7 @@ class FileUtils {
)
return config.selfFilter?.doFilter(listData) ?: listData
}
if (rootFile.listFiles().isNullOrEmpty()){
if (rootFile.listFiles().isNullOrEmpty()) {
return listData
}
for (file in rootFile.listFiles()) {
Expand Down Expand Up @@ -117,7 +109,11 @@ class FileUtils {

// 默认字典排序
// Default sort by alphabet
listData.sortWith(compareBy({ !it.isDir }, { it.fileName.uppercase(Locale.getDefault()) }))
listData.sortWith(
compareBy(
{ !it.isDir },
{ it.fileName.uppercase(Locale.getDefault()) })
)
// 将当前列表数据暴露,以供调用者自己处理数据
// expose data list to outside caller
return config.selfFilter?.doFilter(listData) ?: listData
Expand All @@ -132,18 +128,12 @@ class FileUtils {
nextPath: String,
context: Context
): ArrayList<FileNavBeanImpl> {

// 优先级:目标设备名称 --> 自定义路径 --> 默认 SD 卡
if (currentDataSource.isEmpty()) {
// 优先级:目标设备名称 --> 自定义路径 --> 默认 SD 卡
val dirName = getDirAlias(getRootFile())
currentDataSource.add(
FileNavBeanImpl(
if (!config.mediaStorageName.isNullOrEmpty()) {
config.mediaStorageName
} else if (!config.customRootPath.isEmpty()) {
config.customRootPath
} else {
context.getString(R.string.file_picker_tv_sd_card)
},
dirName,
nextPath
)
)
Expand Down Expand Up @@ -183,5 +173,24 @@ class FileUtils {
)
return currentDataSource
}

fun getDirAlias(file: File): String {
val isCustomRoot = config.mediaStorageType == STORAGE_CUSTOM_ROOT_PATH
&& file.absolutePath == config.customRootPath
val isPreSetStorageRoot = config.mediaStorageType == STORAGE_EXTERNAL_STORAGE
&& file.absolutePath == getRootFile().absolutePath
val isDefaultRoot = file.absolutePath == getRootFile().absolutePath
return when {
isCustomRoot || isPreSetStorageRoot -> {
config.mediaStorageName
}
isDefaultRoot -> {
config.defaultStorageName
}
else -> {
file.name
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
android:id="@+id/rv_nav_file_picker"
android:layout_width="match_parent"
android:layout_height="48dp"
android:overScrollMode="never"
android:elevation="1dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ll_tool_bar_file_picker" />
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name_file_picker"
android:roundIcon="@mipmap/ic_launcher_round"
android:requestLegacyExternalStorage="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SampleInJavaActivity"></activity>
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/java/me/rosuh/sample/SampleActivity.kt