Commit b0987460 authored by wanglei's avatar wanglei

...

parent 8fce5607
......@@ -12,22 +12,8 @@ import com.base.datarecovery.bean.ConstObject.SCAN_PHOTOS
import com.base.datarecovery.bean.ConstObject.SCAN_VIDEOS
import com.base.datarecovery.databinding.ActivityFileScanBinding
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.help.FileHelp.loadFileByFilter
import com.base.datarecovery.help.PermissionHelp.checkStorePermission
import com.base.datarecovery.help.PermissionHelp.requestStorePermission
import com.base.datarecovery.utils.BarUtils
import com.base.datarecovery.utils.FileHexEx.isDocument
import com.base.datarecovery.utils.FileHexEx.isImage
import com.base.datarecovery.utils.FileHexEx.isVideo
import com.base.datarecovery.utils.LogEx
import com.base.datarecovery.view.DialogViews.showExitFunctionDialog
import com.base.datarecovery.view.DialogViews.showGerPermission
import com.base.datarecovery.view.FileScanDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.launch
/**
* 文件扫描,遍历文件夹的方式进行
......@@ -64,11 +50,6 @@ class FileScanActivity : BaseActivity<ActivityFileScanBinding>() {
binding.ivIcon.setImageResource(R.mipmap.tu_videos_scan)
}
}
if (checkStorePermission()) {
beginScan()
} else {
requestPermission()
}
}
override fun initListener() {
......@@ -84,87 +65,10 @@ class FileScanActivity : BaseActivity<ActivityFileScanBinding>() {
}
}
@SuppressLint("SetTextI18n")
private fun beginScan() {
if (scanOnce)
return
scanOnce = true
val mPathFlow = MutableSharedFlow<String>(
replay = 5,//当新的订阅者Collect时,发送几个已经发送过的数据给它
extraBufferCapacity = 5,//减去replay,MutableSharedFlow还缓存多少数据,缓冲池容量 = replay + extraBufferCapacity
onBufferOverflow = BufferOverflow.SUSPEND//缓存策略,三种 丢掉最新值、丢掉最旧值和挂起
)
val pathFlow: SharedFlow<String> = mPathFlow
val mFoundFlow = MutableSharedFlow<Int>(
replay = 5,//当新的订阅者Collect时,发送几个已经发送过的数据给它
extraBufferCapacity = 5,//减去replay,MutableSharedFlow还缓存多少数据,缓冲池容量 = replay + extraBufferCapacity
onBufferOverflow = BufferOverflow.SUSPEND//缓存策略,三种 丢掉最新值、丢掉最旧值和挂起
)
val foundFlow: SharedFlow<Int> = mFoundFlow
val dialogClass = FileScanDialog(this)
val scanDialog = dialogClass.showFileScanDialog(pathFlow, foundFlow)
val filter = when (scanType) {
SCAN_PHOTOS -> ::isImage
SCAN_DOCUMENTS -> ::isDocument
SCAN_VIDEOS -> ::isVideo
else -> ::isImage
}
var oneShowAd = false
val root = Environment.getExternalStorageDirectory()
val pathList = arrayListOf<String>()
lifecycleScope.loadFileByFilter(
mPathFlow,
mFoundFlow,
root, filter = filter,
onDo = { file ->
LogEx.logDebug(TAG, "file =${file.absolutePath}")
pathList.add(file.absolutePath)
if (!oneShowAd) {
oneShowAd = true
lifecycleScope.launch(Dispatchers.Main) {
dialogClass.scanShowUI()
AdmobInterstitialUtils.showInterstitialAd(this@FileScanActivity, true, false) { }
}
}
},
onFinish = {
if (pathList.isEmpty()) {
scanDialog.dismiss()
binding.ivIcon.setImageResource(R.mipmap.queshengye)
binding.tvBtn.text = "Finished"
binding.tvBtn.setOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
} else {
dialogClass.stopScan(scanType, pathList)
}
}
)
}
private fun requestPermission() {
showGerPermission(tittle = "Storage Permission Required",
desc = "This feature requires access to your storage to scan your photos, videos, and documents and recover any accidentally deleted data. We will not transmit your data to any third-party service. Please grant permission so that we can provide you with better service.",
deny = { finishToMain() },
allow = {
requestStorePermission(launcher, jumpAction = {}, result = { flag ->
if (flag) beginScan()
})
})
}
}
\ No newline at end of file
......@@ -18,6 +18,8 @@ import com.base.datarecovery.bean.RecoveryBean.Companion.setType
import com.base.datarecovery.databinding.ActivityFileScanResultBinding
import com.base.datarecovery.help.BaseActivity
import com.base.datarecovery.help.FileHelp.loadFileByFilter
import com.base.datarecovery.help.PermissionHelp.checkStorePermission
import com.base.datarecovery.help.PermissionHelp.requestStorePermission
import com.base.datarecovery.utils.BarUtils
import com.base.datarecovery.utils.FileHexEx
import com.base.datarecovery.utils.LogEx
......@@ -28,6 +30,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import java.io.File
import java.util.HashMap
......@@ -43,10 +46,6 @@ class FileScanResultActivity : BaseActivity<ActivityFileScanResultBinding>() {
private lateinit var fileFolderAdapter: FileFolderAdapter
private val beanList by lazy {
intent.getStringArrayExtra("PathList") ?: arrayOf()
}
@SuppressLint("SetTextI18n")
override fun initView() {
......@@ -72,8 +71,6 @@ class FileScanResultActivity : BaseActivity<ActivityFileScanResultBinding>() {
binding.tvFileType.text = "videos"
}
}
binding.tvFileNumber.text = beanList.size.toString()
fileFolderAdapter = FileFolderAdapter(scanType) { folderBean ->
startActivity(Intent(this, FileRecoveryActivity::class.java).apply {
putExtra("ScanType", scanType)
......@@ -81,7 +78,14 @@ class FileScanResultActivity : BaseActivity<ActivityFileScanResultBinding>() {
})
}
binding.rv.adapter = fileFolderAdapter
initData()
if (checkStorePermission()) {
beginScan()
} else {
requestStorePermission(launcher, result = {
if (it) beginScan() else finishToMain()
})
}
}
override fun initListener() {
......@@ -97,9 +101,9 @@ class FileScanResultActivity : BaseActivity<ActivityFileScanResultBinding>() {
}
}
private fun initData() {
private fun setAdapterData(pathList: ArrayList<String>) {
val hashMap = HashMap<String, ArrayList<RecoveryBean>>()
beanList.forEach { path ->
pathList.forEach { path ->
val folder = File(path).parent ?: ""
if (hashMap[folder] == null) {
hashMap[folder] = arrayListOf()
......@@ -118,11 +122,13 @@ class FileScanResultActivity : BaseActivity<ActivityFileScanResultBinding>() {
binding.tvFolderNumber.text = list.size.toString()
fileFolderAdapter.setData(list)
}
private fun beginScan() {
if (scanOnce)
return
scanOnce = true
//变量路径
val mPathFlow = MutableSharedFlow<String>(
replay = 5,//当新的订阅者Collect时,发送几个已经发送过的数据给它
extraBufferCapacity = 5,//减去replay,MutableSharedFlow还缓存多少数据,缓冲池容量 = replay + extraBufferCapacity
......@@ -130,12 +136,14 @@ class FileScanResultActivity : BaseActivity<ActivityFileScanResultBinding>() {
)
val pathFlow: SharedFlow<String> = mPathFlow
val mFoundFlow = MutableSharedFlow<Int>(
replay = 5,//当新的订阅者Collect时,发送几个已经发送过的数据给它
extraBufferCapacity = 5,//减去replay,MutableSharedFlow还缓存多少数据,缓冲池容量 = replay + extraBufferCapacity
onBufferOverflow = BufferOverflow.SUSPEND//缓存策略,三种 丢掉最新值、丢掉最旧值和挂起
//符合条件Flow
val mFoundFlow = MutableSharedFlow<Pair<Int, String>>(
replay = 5,
extraBufferCapacity = 5,
onBufferOverflow = BufferOverflow.SUSPEND
)
val foundFlow: SharedFlow<Int> = mFoundFlow
val foundFlow: SharedFlow<Pair<Int, String>> = mFoundFlow
val dialogClass = FileScanDialog(this)
val scanDialog = dialogClass.showFileScanDialog(pathFlow, foundFlow)
......@@ -147,16 +155,23 @@ class FileScanResultActivity : BaseActivity<ActivityFileScanResultBinding>() {
else -> FileHexEx::isImage
}
val pathList = ArrayList<String>()
lifecycleScope.launch(Dispatchers.Main) {
mFoundFlow.collectLatest {
pathList.add(it.second)
binding.tvFileNumber.text = pathList.size.toString()
setAdapterData(pathList)
}
}
var oneShowAd = false
val root = Environment.getExternalStorageDirectory()
val pathList = arrayListOf<String>()
lifecycleScope.loadFileByFilter(
mPathFlow,
mFoundFlow,
root, filter = filter,
onDo = { file ->
LogEx.logDebug(TAG, "file =${file.absolutePath}")
pathList.add(file.absolutePath)
if (!oneShowAd) {
oneShowAd = true
lifecycleScope.launch(Dispatchers.Main) {
......@@ -167,7 +182,6 @@ class FileScanResultActivity : BaseActivity<ActivityFileScanResultBinding>() {
},
onFinish = {
scanDialog.dismiss()
dialogClass.stopScan(scanType, pathList)
}
)
......
......@@ -101,5 +101,4 @@ class FileFolderAdapter(
beanList.addAll(list)
notifyDataSetChanged()
}
}
\ No newline at end of file
......@@ -11,6 +11,7 @@ import com.base.datarecovery.activity.SettingActivity
import com.base.datarecovery.activity.junkclean.ScanJunkActivity
import com.base.datarecovery.activity.privacyspace.PrivacyPinOneActivity
import com.base.datarecovery.activity.privacyspace.PrivacySpaceActivity
import com.base.datarecovery.activity.recovery.FileScanResultActivity
import com.base.datarecovery.activity.repeat.RepeatAnimationActivity
import com.base.datarecovery.activity.screenshot.ScreenShotAnimationActivity
import com.base.datarecovery.ads.AdmobNativeUtils
......@@ -38,17 +39,17 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>() {
startActivity(Intent(requireContext(), ScanJunkActivity::class.java))
}
binding.flRyPhoto.setOnClickListener {
startActivity(Intent(requireContext(), FileScanActivity::class.java).apply {
startActivity(Intent(requireContext(), FileScanResultActivity::class.java).apply {
putExtra("Type", SCAN_PHOTOS)
})
}
binding.flRyVideo.setOnClickListener {
startActivity(Intent(requireContext(), FileScanActivity::class.java).apply {
startActivity(Intent(requireContext(), FileScanResultActivity::class.java).apply {
putExtra("Type", SCAN_VIDEOS)
})
}
binding.cardRyDocument.setOnClickListener {
startActivity(Intent(requireContext(), FileScanActivity::class.java).apply {
startActivity(Intent(requireContext(), FileScanResultActivity::class.java).apply {
putExtra("Type", SCAN_DOCUMENTS)
})
}
......
......@@ -20,7 +20,7 @@ object FileHelp {
fun CoroutineScope.loadFileByFilter(
flow1: MutableSharedFlow<String>? = null,
flow2: MutableSharedFlow<Int>? = null,
flow2: MutableSharedFlow<Pair<Int, String>>? = null,
folder: File,
filter: (file: File) -> Boolean,
onDo: ((file: File) -> Unit)? = null,
......@@ -41,7 +41,7 @@ object FileHelp {
if (flag) {
onDo?.invoke(it)
size++
flow2?.emit(size)
flow2?.emit(Pair(size, it.absolutePath))
}
flow1?.emit(it.absolutePath)
}
......@@ -61,7 +61,7 @@ object FileHelp {
if (flag) {
onDo?.invoke(it)
size++
flow2?.emit(size)
flow2?.emit(Pair(size, it.absolutePath))
}
flow1?.emit(it.absolutePath)
}
......
......@@ -29,7 +29,7 @@ class FileScanDialog(
@SuppressLint("SetTextI18n")
fun showFileScanDialog(
sharedFlow: SharedFlow<String>,
foundFlow: SharedFlow<Int>
foundFlow: SharedFlow<Pair<Int, String>>
): AlertDialog {
dialog?.setView(binding.root)
dialog?.setCanceledOnTouchOutside(false)
......@@ -65,7 +65,7 @@ class FileScanDialog(
}
activity.lifecycleScope.launch {
foundFlow.collectLatest {
binding.tvFoundNumber.text = "Found $it files"
binding.tvFoundNumber.text = "Found ${it.first} files"
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment