Commit 98e2c70e authored by wanglei's avatar wanglei

...

parent 2ca447af
......@@ -14,6 +14,7 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.base.filerecoveryrecyclebin.activity.repeat.RepeatActivity
import com.base.filerecoveryrecyclebin.ads.admob.AdmobBannerUtils
import com.base.filerecoveryrecyclebin.bean.MediaBean
import com.base.filerecoveryrecyclebin.databinding.ActivityMainBinding
......@@ -33,6 +34,7 @@ import com.base.filerecoveryrecyclebin.view.PermissionDialog.showPermissionBotto
import com.base.filerecoveryrecyclebin.view.RateStarPop.showRateStarPopDialog
import com.facebook.appevents.AppEventsLogger
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
......@@ -177,7 +179,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
if (checkStorePermission()) {
initStayServiceObserver()
initPhotoSize()
if (photoSizejob == null) {
photoSizejob = initPhotoSize()
}
}
lifecycleScope.launch(Dispatchers.Main) {
......@@ -186,6 +190,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
}
}
var photoSizejob: Job? = null
private fun initPhotoSize() = lifecycleScope.launch(Dispatchers.IO) {
LogEx.logDebug(TAG, "initPhotoSize")
......@@ -195,13 +200,17 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
val screenSize =
list.filter { it.path.lowercase(Locale.ENGLISH).contains("screenshot") }.map { File(it.path) }.sumOf { it.length() }
val similar = calculateSimilar(list).flatMap { it.value }.map { File(it.path) }.sumOf { it.length() }
val similarPhoto = calculateSimilar(list)
RepeatActivity.readySimilarList = similarPhoto
val similar = similarPhoto.flatMap { it.value }.map { File(it.path) }.sumOf { it.length() }
LogEx.logDebug(TAG, "screenSize=$screenSize")
delay(1500)
photoSizeFlow.emit(Pair(screenSize, similar))
photoSizejob = null
}
}
\ No newline at end of file
......@@ -27,6 +27,7 @@ import com.base.filerecoveryrecyclebin.view.DialogViews.showGerPermission
import com.base.filerecoveryrecyclebin.view.DialogViews.showTittleDescExitDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeout
import java.io.File
class RepeatActivity : BaseActivity<ActivityRepeatBinding>() {
......@@ -74,9 +75,6 @@ class RepeatActivity : BaseActivity<ActivityRepeatBinding>() {
})
}
// val s = opencvCompareSimilar("/storage/emulated/0/DCIM/1.jpg","/storage/emulated/0/DCIM/22.jpg")
// LogEx.logDebug(TAG,"s=$s")
// Toast.makeText(this, s.toString(), Toast.LENGTH_SHORT).show()
}
override fun initListener() {
......@@ -147,20 +145,24 @@ class RepeatActivity : BaseActivity<ActivityRepeatBinding>() {
val list = arrayListOf<MediaBean>()
getImageMedia(list)
LogEx.logDebug(TAG, "list=${list.size}")
val similarList = calculateSimilar(list)
// similarList.forEach { t, u ->
// LogEx.logDebug(TAG, "t=$t u=$u")
// }
// val hashMap = HashMap<String, ArrayList<MediaBean>>()
// list.forEach {
// val time = it.time.toFormatTime()
// if (hashMap[time] == null) {
// hashMap[time] = arrayListOf()
// }
// hashMap[time]?.add(it)
// }
val beanList = similarList.map { map ->
list.reversed()
val result = if (readySimilarList == null) {
val similarList = HashMap<String, ArrayList<MediaBean>>()
val startTime = System.currentTimeMillis()
val endTime = withTimeout(15 * 1000) {
calculateSimilar(list, similarList)
System.currentTimeMillis()
}
val spendTime = endTime - startTime
LogEx.logDebug(TAG, "spendTime=$spendTime startTime=$startTime endTime=$endTime")
similarList
} else {
readySimilarList ?: hashMapOf()
}
val beanList = result.map { map ->
val time = File(map.key).lastModified().toFormatTime()
MediaTimeBean(time = time, beans = map.value)
}
......@@ -175,7 +177,10 @@ class RepeatActivity : BaseActivity<ActivityRepeatBinding>() {
binding.llEmpty.isVisible = beanList.isEmpty()
}
}
}
companion object {
var readySimilarList: HashMap<String, ArrayList<MediaBean>>? = null
}
}
\ No newline at end of file
......@@ -22,9 +22,12 @@ import com.base.filerecoveryrecyclebin.utils.BarUtils
import com.base.filerecoveryrecyclebin.view.DialogViews.showDeletePermanentlyDialog
import com.base.filerecoveryrecyclebin.view.DialogViews.showExitFunctionDialog
import com.base.filerecoveryrecyclebin.view.DialogViews.showGerPermission
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeout
import java.io.File
import java.text.SimpleDateFormat
import java.util.Locale
......@@ -32,6 +35,7 @@ import kotlin.collections.HashSet
class ScreenShotActivity : BaseActivity<ActivityScreenShotBinding>() {
private val TAG = "ScreenShotActivity"
private lateinit var screenShotAdapter: ScreenShotAdapter
override val binding: ActivityScreenShotBinding by lazy {
......@@ -79,7 +83,9 @@ class ScreenShotActivity : BaseActivity<ActivityScreenShotBinding>() {
}
private fun initData() = lifecycleScope.launch(Dispatchers.IO) {
val list = getScreenshotBean()
launch(Dispatchers.Main) {
screenShotAdapter.setData(list)
val size = list.sumOf { it.screenPhotoBean.sumOf { ss -> ss.file.length() } }
......@@ -156,28 +162,37 @@ class ScreenShotActivity : BaseActivity<ActivityScreenShotBinding>() {
}
}
private fun getScreenshotBean(): ArrayList<ScreenshotBean> {
private fun CoroutineScope.getScreenshotBean(myBeans: ArrayList<ScreenshotBean>? = null): ArrayList<ScreenshotBean> {
val beans = myBeans ?: arrayListOf<ScreenshotBean>()
val dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
val screenFiles = FileHelp.getDirFiles(dcim)
val hash = HashMap<String, HashSet<File>>()
val formatter = SimpleDateFormat("yyyy/MM", Locale.getDefault())
runCatching {
screenFiles.forEach { file ->
val time = formatter.format(file.lastModified())
if (hash[time] == null) {
hash[time] = HashSet()
}
val set = hash[time]
set?.add(file)
if (!isActive) throw Exception("out Time")
}
}
val beans = arrayListOf<ScreenshotBean>()
runCatching {
hash.forEach { (t, u) ->
val arrayList = arrayListOf<ScreenPhotoBean>()
val mapList = u.map { file -> ScreenPhotoBean(file) }
arrayList.addAll(mapList)
beans.add(ScreenshotBean(t, arrayList))
if (!isActive) throw Exception("out Time")
}
}
return beans
}
}
\ No newline at end of file
......@@ -10,7 +10,7 @@ object LogEx {
// "FileHexEx",
"FileScanResultActivity",
"FileGridAdapter",
"TestSimilar", "SimilarHelper"
// "TestSimilar", "SimilarHelper"
)
fun logDebug(tag: String, content: String, isMust: Boolean = false) {
......
......@@ -4,8 +4,11 @@ import android.graphics.BitmapFactory
import android.provider.MediaStore.Audio.Radio
import com.base.filerecoveryrecyclebin.bean.MediaBean
import com.base.filerecoveryrecyclebin.utils.TestSimilar.testSimilar
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.isActive
//import com.base.filerecoveryrecyclebin.utils.OpencvImageHelp.opencvCompareSimilar
import java.io.File
import kotlin.math.abs
import kotlin.random.Random
object SimilarHelper {
......@@ -13,9 +16,14 @@ object SimilarHelper {
private val TAG = "SimilarHelper"
fun calculateSimilar(list: List<MediaBean>): HashMap<String, ArrayList<MediaBean>> {
fun CoroutineScope.calculateSimilar(
list: List<MediaBean>,
myResult: HashMap<String, ArrayList<MediaBean>>? = null
): HashMap<String, ArrayList<MediaBean>> {
val result = myResult ?: HashMap<String, ArrayList<MediaBean>>()
try {
val result = HashMap<String, ArrayList<MediaBean>>()
val eachArrayList = arrayListOf<MediaBean>().apply {
addAll(list)
}
......@@ -24,7 +32,7 @@ object SimilarHelper {
val iterator = list.iterator()
while (iterator.hasNext()) {
while (iterator.hasNext() && isActive) {
val item = iterator.next()
LogEx.logDebug(TAG, "next item=$item")
......@@ -32,13 +40,17 @@ object SimilarHelper {
if (!haveComperedList.contains(item.path)) {
val compareIterator = eachArrayList.iterator()
while (compareIterator.hasNext()) {
while (compareIterator.hasNext() && isActive) {
val compareItem = compareIterator.next()
// LogEx.logDebug(TAG, "compareItem=$compareItem")
if (item.path != compareItem.path) {
// val percent = opencvCompareSimilar(item.path, compareItem.path)
// val percent = similarPercent(File(item.path), File(compareItem.path))
val file = File(item.path)
val compareFile = File(compareItem.path)
val timeFlag = abs(file.lastModified() - compareFile.lastModified()) < 7 * 24 * 60 * 60 * 1000L
if (!timeFlag) continue
val isSimilar = testSimilar(item.path, compareItem.path)
LogEx.logDebug(TAG, "isSimilar=$isSimilar")
if (isSimilar) {
......@@ -60,6 +72,10 @@ object SimilarHelper {
}
}
} catch (e: Exception) {
e.printStackTrace()
}
return result
}
......
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