Skip to content

Commit 07d3b5b

Browse files
authored
Support go back while in custom root mode. (#158)
* 📝 [Update] - README.md * 📝 [Update] - Support Android 10. - README.md * 🐛 [Fix] - Data list not clean. #157 * ✨ [Update] - Supported go back while in custom root mode. #41
1 parent 2954868 commit 07d3b5b

File tree

6 files changed

+86
-30
lines changed

6 files changed

+86
-30
lines changed

filepicker/src/main/java/me/rosuh/filepicker/FilePickerActivity.kt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import android.support.v4.widget.SwipeRefreshLayout
1414
import android.support.v7.app.AppCompatActivity
1515
import android.support.v7.widget.LinearLayoutManager
1616
import android.support.v7.widget.RecyclerView
17+
import android.util.Log
1718
import android.view.LayoutInflater
1819
import android.view.View
1920
import android.view.animation.AnimationUtils
@@ -35,7 +36,8 @@ import java.io.File
3536
import java.util.concurrent.*
3637

3738
@SuppressLint("ShowToast")
38-
class FilePickerActivity : AppCompatActivity(), View.OnClickListener, RecyclerViewListener.OnItemClickListener{
39+
class FilePickerActivity : AppCompatActivity(), View.OnClickListener,
40+
RecyclerViewListener.OnItemClickListener {
3941

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

7274
private val loadFileRunnable: Runnable by lazy {
7375
Runnable {
76+
val customRootPathFile = pickerConfig.customRootPathFile
7477
val rootFile = when {
78+
customRootPathFile?.exists() == true -> {
79+
// move to custom root dir
80+
navDataSource.clear()
81+
val root = FileUtils.getRootFile()
82+
var curPath = customRootPathFile.absolutePath
83+
while (curPath != root.parent && !curPath.isNullOrBlank()) {
84+
Log.i("loadFileRunnable", "curPath = $curPath")
85+
val f = File(curPath)
86+
val fileNavBeanImpl = FileNavBeanImpl(
87+
FileUtils.getDirAlias(f),
88+
f.absolutePath
89+
)
90+
navDataSource.add(0, fileNavBeanImpl)
91+
curPath = f.parent
92+
}
93+
pickerConfig.resetCustomFile()
94+
customRootPathFile
95+
}
7596
navDataSource.isEmpty() && pickerConfig.isSkipDir -> {
7697
FileUtils.getRootFile()
7798
}
@@ -85,6 +106,7 @@ class FilePickerActivity : AppCompatActivity(), View.OnClickListener, RecyclerVi
85106
}
86107

87108
val listData = FileUtils.produceListDataSource(rootFile)
109+
88110
// 导航栏数据集
89111
navDataSource = FileUtils.produceNavDataSource(
90112
navDataSource,
@@ -171,6 +193,8 @@ class FilePickerActivity : AppCompatActivity(), View.OnClickListener, RecyclerVi
171193
if (!loadingThreadPool.isShutdown) {
172194
loadingThreadPool.shutdown()
173195
}
196+
currOffsetMap.clear()
197+
currPosMap.clear()
174198
}
175199

176200
private fun isPermissionGrated() = ContextCompat.checkSelfPermission(
@@ -472,7 +496,7 @@ class FilePickerActivity : AppCompatActivity(), View.OnClickListener, RecyclerVi
472496
position: Int
473497
) {
474498
if (view.id != R.id.item_list_file_picker) return
475-
val item = (adapter as FileListAdapter).getItem(position)?: return
499+
val item = (adapter as FileListAdapter).getItem(position) ?: return
476500
// Check the lib users whether if intercept the click event.
477501
val hookItemClick = FilePickerManager.config.itemClickListener?.onItemLongClick(
478502
adapter,

filepicker/src/main/java/me/rosuh/filepicker/config/FilePickerConfig.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import me.rosuh.filepicker.FilePickerActivity
77
import me.rosuh.filepicker.R
88
import me.rosuh.filepicker.engine.ImageEngine
99
import me.rosuh.filepicker.filetype.FileType
10+
import java.io.File
1011

1112
/**
1213
*
@@ -55,10 +56,12 @@ class FilePickerConfig(private val pickerManager: FilePickerManager) {
5556
var maxSelectable = Int.MAX_VALUE
5657
private set
5758

59+
internal val defaultStorageName = contextRes.getString(R.string.file_picker_tv_sd_card)
60+
5861
/**
5962
* 存储类型
6063
*/
61-
var mediaStorageName = contextRes.getString(R.string.file_picker_tv_sd_card)
64+
var mediaStorageName = defaultStorageName
6265
private set
6366

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

81+
internal var customRootPathFile: File? = null
82+
private set
83+
7884
/**
7985
* 自定义过滤器
8086
*/
@@ -170,6 +176,15 @@ class FilePickerConfig(private val pickerManager: FilePickerManager) {
170176

171177
fun setCustomRootPath(path: String): FilePickerConfig {
172178
customRootPath = path
179+
path.takeIf {
180+
it.isNotBlank()
181+
}?.let {
182+
File(it)
183+
}?.takeIf {
184+
it.exists()
185+
}?.let {
186+
customRootPathFile = it
187+
}
173188
return this
174189
}
175190

@@ -311,13 +326,18 @@ class FilePickerConfig(private val pickerManager: FilePickerManager) {
311326
}
312327
}
313328

329+
fun resetCustomFile() {
330+
this.customRootPathFile = null
331+
}
332+
314333
fun clear() {
315334
this.customFileTypes.clear()
316335
this.customImageEngine = null
317336
this.fileItemOnClickListener = null
318337
this.selfFilter = null
319338
this.customDetector = null
320339
this.defaultFileDetector.clear()
340+
resetCustomFile()
321341
}
322342

323343
companion object {

filepicker/src/main/java/me/rosuh/filepicker/config/FilePickerManager.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ object FilePickerManager {
1717
const val REQUEST_CODE = 10401
1818

1919
internal var contextRef: WeakReference<Activity>? = null
20+
2021
internal var fragmentRef: WeakReference<Fragment>? = null
22+
2123
internal lateinit var config: FilePickerConfig
2224

25+
private var dataList: MutableList<String> = ArrayList()
26+
2327
@JvmStatic
2428
fun from(activity: Activity): FilePickerConfig {
2529
reset()
@@ -40,12 +44,10 @@ object FilePickerManager {
4044
return config
4145
}
4246

43-
private var dataList: List<String> = ArrayList()
44-
4547
/**
4648
* 保存数据@param list List<String>到本类中
4749
*/
48-
internal fun saveData(list: List<String>) {
50+
internal fun saveData(list: MutableList<String>) {
4951
dataList = list
5052
}
5153

@@ -55,16 +57,18 @@ object FilePickerManager {
5557
*/
5658
@JvmOverloads
5759
@JvmStatic
58-
fun obtainData(release: Boolean = false): List<String> {
60+
fun obtainData(release: Boolean = false): MutableList<String> {
61+
val result = ArrayList(dataList)
5962
if (release) {
6063
release()
6164
}
62-
return dataList
65+
return result
6366
}
6467

6568
private fun reset() {
6669
contextRef?.clear()
6770
fragmentRef?.clear()
71+
dataList.clear()
6872
ImageLoadController.reset()
6973
}
7074

filepicker/src/main/java/me/rosuh/filepicker/utils/FileUtils.kt

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package me.rosuh.filepicker.utils
22

33
import android.content.Context
44
import android.os.Environment
5-
import me.rosuh.filepicker.R
65
import me.rosuh.filepicker.bean.FileItemBeanImpl
76
import me.rosuh.filepicker.bean.FileNavBeanImpl
87
import me.rosuh.filepicker.config.FilePickerConfig.Companion.STORAGE_CUSTOM_ROOT_PATH
@@ -25,18 +24,11 @@ class FileUtils {
2524
* 根据配置参数获取根目录文件
2625
* @return File
2726
*/
28-
fun getRootFile():File {
27+
fun getRootFile(): File {
2928
return when (config.mediaStorageType) {
3029
STORAGE_EXTERNAL_STORAGE -> {
3130
File(Environment.getExternalStorageDirectory().absoluteFile.toURI())
3231
}
33-
STORAGE_CUSTOM_ROOT_PATH -> {
34-
if (config.customRootPath.isEmpty()) {
35-
File(Environment.getExternalStorageDirectory().absoluteFile.toURI())
36-
} else {
37-
File(config.customRootPath)
38-
}
39-
}
4032
else -> {
4133
File(Environment.getExternalStorageDirectory().absoluteFile.toURI())
4234
}
@@ -70,7 +62,7 @@ class FileUtils {
7062
)
7163
return config.selfFilter?.doFilter(listData) ?: listData
7264
}
73-
if (rootFile.listFiles().isNullOrEmpty()){
65+
if (rootFile.listFiles().isNullOrEmpty()) {
7466
return listData
7567
}
7668
for (file in rootFile.listFiles()) {
@@ -117,7 +109,11 @@ class FileUtils {
117109

118110
// 默认字典排序
119111
// Default sort by alphabet
120-
listData.sortWith(compareBy({ !it.isDir }, { it.fileName.uppercase(Locale.getDefault()) }))
112+
listData.sortWith(
113+
compareBy(
114+
{ !it.isDir },
115+
{ it.fileName.uppercase(Locale.getDefault()) })
116+
)
121117
// 将当前列表数据暴露,以供调用者自己处理数据
122118
// expose data list to outside caller
123119
return config.selfFilter?.doFilter(listData) ?: listData
@@ -132,18 +128,12 @@ class FileUtils {
132128
nextPath: String,
133129
context: Context
134130
): ArrayList<FileNavBeanImpl> {
135-
131+
// 优先级:目标设备名称 --> 自定义路径 --> 默认 SD 卡
136132
if (currentDataSource.isEmpty()) {
137-
// 优先级:目标设备名称 --> 自定义路径 --> 默认 SD 卡
133+
val dirName = getDirAlias(getRootFile())
138134
currentDataSource.add(
139135
FileNavBeanImpl(
140-
if (!config.mediaStorageName.isNullOrEmpty()) {
141-
config.mediaStorageName
142-
} else if (!config.customRootPath.isEmpty()) {
143-
config.customRootPath
144-
} else {
145-
context.getString(R.string.file_picker_tv_sd_card)
146-
},
136+
dirName,
147137
nextPath
148138
)
149139
)
@@ -183,5 +173,24 @@ class FileUtils {
183173
)
184174
return currentDataSource
185175
}
176+
177+
fun getDirAlias(file: File): String {
178+
val isCustomRoot = config.mediaStorageType == STORAGE_CUSTOM_ROOT_PATH
179+
&& file.absolutePath == config.customRootPath
180+
val isPreSetStorageRoot = config.mediaStorageType == STORAGE_EXTERNAL_STORAGE
181+
&& file.absolutePath == getRootFile().absolutePath
182+
val isDefaultRoot = file.absolutePath == getRootFile().absolutePath
183+
return when {
184+
isCustomRoot || isPreSetStorageRoot -> {
185+
config.mediaStorageName
186+
}
187+
isDefaultRoot -> {
188+
config.defaultStorageName
189+
}
190+
else -> {
191+
file.name
192+
}
193+
}
194+
}
186195
}
187196
}

filepicker/src/main/res/layout/main_activity_for_file_picker.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797
android:id="@+id/rv_nav_file_picker"
9898
android:layout_width="match_parent"
9999
android:layout_height="48dp"
100-
android:overScrollMode="never"
101100
android:elevation="1dp"
102101
app:layout_constraintStart_toStartOf="parent"
103102
app:layout_constraintTop_toBottomOf="@+id/ll_tool_bar_file_picker" />

sample/src/main/java/me/rosuh/sample/SampleActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ class SampleActivity : AppCompatActivity() {
210210
// 自定义根目录
211211
btn_custom_root_path.setOnClickListener {
212212
FilePickerManager.from(this@SampleActivity)
213-
.storageType("下载", FilePickerConfig.STORAGE_CUSTOM_ROOT_PATH)
213+
.storageType("⬇️", FilePickerConfig.STORAGE_CUSTOM_ROOT_PATH)
214214
.setTheme(getRandomTheme())
215215
// 不指定名称则为导航栏将显示绝对路径
216216
// .storageType(FilePickerConfig.STORAGE_CUSTOM_ROOT_PATH)

0 commit comments

Comments
 (0)