Skip to content

Commit

Permalink
Merge pull request #205 from qjzcj2008/SAF-Fix
Browse files Browse the repository at this point in the history
尝试为黑名单功能适配SAF
  • Loading branch information
rRemix committed Aug 4, 2023
2 parents 974f430 + c39f81d commit 0d7f0e9
Showing 1 changed file with 69 additions and 27 deletions.
96 changes: 69 additions & 27 deletions app/src/main/java/remix/myplayer/ui/activity/SettingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import android.webkit.MimeTypeMap
import android.widget.CompoundButton
import android.widget.CompoundButton.OnCheckedChangeListener
import androidx.core.content.FileProvider
import androidx.documentfile.provider.DocumentFile
import com.afollestad.materialdialogs.DialogAction
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
Expand Down Expand Up @@ -127,6 +128,26 @@ class SettingActivity : ToolbarActivity(), ColorChooserDialog.ColorCallback,

private var pendingExportPlaylist: String? = null

private var blackList: Set<String> = emptySet()

//尝试从uri获取文件夹absolutePath
//fun getFolderPath(documentFile: DocumentFile?): String? {
// if (documentFile == null) {
// return null
// }
//
// val name = documentFile.name ?: return null
// val parent = documentFile.parentFile ?: return name
//
// val parentPath = getFolderPath(parent)
// return if (parentPath != null) {
// "$parentPath/$name"
// } else {
// name
// }
//}


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand Down Expand Up @@ -1151,7 +1172,7 @@ class SettingActivity : ToolbarActivity(), ColorChooserDialog.ColorCallback,
* 设置黑名单
*/
private fun configBlackList() {
val blackList: Set<String> = SPUtil.getStringSet(this, SETTING_KEY.NAME, SETTING_KEY.BLACKLIST)
blackList = SPUtil.getStringSet(this, SETTING_KEY.NAME, SETTING_KEY.BLACKLIST)
val items = ArrayList<String>(blackList)
items.sortWith(Comparator { left, right ->
File(left).name.compareTo(
Expand Down Expand Up @@ -1212,31 +1233,11 @@ class SettingActivity : ToolbarActivity(), ColorChooserDialog.ColorCallback,
}
DialogAction.POSITIVE -> {
//add
FolderChooser(
this,
TAG_BLACKLIST,
null,
null,
null,
object : FolderChooser.FolderCallback {
override fun onFolderSelection(chooser: FolderChooser, folder: File) {
if (folder.isDirectory) {
val newBlacklist = LinkedHashSet<String>(blackList)
newBlacklist.add(folder.absolutePath)
SPUtil.putStringSet(
this@SettingActivity,
SETTING_KEY.NAME,
SETTING_KEY.BLACKLIST,
newBlacklist
)
contentResolver.notifyChange(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null
)
}
configBlackList()
}
}).show()
// 启动获取目录的Intent
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
addCategory(Intent.CATEGORY_DEFAULT)
}
startActivityForResult(intent, REQUEST_CODE_ADD_BLACKLIST)
}
}
}
Expand Down Expand Up @@ -1369,7 +1370,47 @@ class SettingActivity : ToolbarActivity(), ColorChooserDialog.ColorCallback,
pendingExportPlaylist = null
}
}
Crop.REQUEST_PICK -> {
REQUEST_CODE_ADD_BLACKLIST ->{
if (requestCode == REQUEST_CODE_ADD_BLACKLIST && resultCode == Activity.RESULT_OK) {
data?.data?.let { uri ->
val folder = DocumentFile.fromTreeUri(this, uri)
if (folder?.isDirectory == true) {
val newBlacklist = LinkedHashSet(blackList)
var folderPath:String?
if (decodedUri.lastIndexOf("document/primary:") !== -1) {
folderPath = decodedUri.split("document/primary:")[1]
} else if (decodedUri.lastIndexOf("document/home:") !== -1) {
folderPath = "Documents/" + decodedUri.split("document/home:")[1]
} else {
folderPath = null
}
//val folderPath = getFolderPath(folder) //只有文件夹
//val encodedUri = folder.uri.toString() //content uri
//var dncodedUri = Uri.decode(folder.uri.toString())
//val folderPath_test = folder?.uri?.path //getFolderPath()去掉content提供者信息后面的路径
//val stroagePath = "/storage/emulated/0/"
val rootPath = Environment.getExternalStorageDirectory()
//val fullPath = stroagePath + folderPath
val fullPath = "$rootPath/$folderPath"
if (folderPath != null) {
newBlacklist.add(fullPath)
SPUtil.putStringSet(
this@SettingActivity,
SETTING_KEY.NAME,
SETTING_KEY.BLACKLIST,
newBlacklist
)
contentResolver.notifyChange(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null
)
}
}
configBlackList()
}
}
}
Crop.REQUEST_PICK -> {
//选择图片
val cacheDir = DiskCache.getDiskCacheDir(
this, "thumbnail/player"
Expand Down Expand Up @@ -1405,6 +1446,7 @@ class SettingActivity : ToolbarActivity(), ColorChooserDialog.ColorCallback,
private const val REQUEST_THEME_COLOR = 0x10
private const val REQUEST_IMPORT_PLAYLIST = 0x102
private const val REQUEST_EXPORT_PLAYLIST = 0x103
private const val REQUEST_CODE_ADD_BLACKLIST = 0x104

private const val TAG_SCAN = "Scan"
private const val TAG_IMPORT_PLAYLIST = "ImportPlaylist"
Expand Down

0 comments on commit 0d7f0e9

Please sign in to comment.