Skip to content

Commit

Permalink
Merge pull request #131 from TeamAmaze/fix-mediascan
Browse files Browse the repository at this point in the history
Fix media scanner
  • Loading branch information
VishalNehra authored Oct 29, 2023
2 parents 62d0220 + fa4f92c commit 5081f8d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ plugins {
id("com.github.triplet.play") version "3.8.1"
id 'com.starter.easylauncher'
id 'com.mikepenz.aboutlibraries.plugin'
id 'org.jetbrains.kotlin.android'
}

android {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class FilesViewModel(val applicationContext: Application) :
applicationContext,
applicationContext.packageName, file
)
FileUtils.scanFile(uri, applicationContext)
FileUtils.scanFile(uri, data.path, applicationContext)
val usedSpace = file.totalSpace - file.usableSpace

val progress = if (file.totalSpace != 0L) {
Expand Down Expand Up @@ -1068,6 +1068,7 @@ class FilesViewModel(val applicationContext: Application) :
uri ->
FileUtils.scanFile(
uri,
mediaFileInfo.path,
applicationContext
)
}
Expand Down Expand Up @@ -1130,6 +1131,7 @@ class FilesViewModel(val applicationContext: Application) :
originalUri ->
FileUtils.scanFile(
originalUri,
it.path,
applicationContext
)
}
Expand All @@ -1141,6 +1143,7 @@ class FilesViewModel(val applicationContext: Application) :
)
FileUtils.scanFile(
uri,
it.path,
applicationContext
)
successProcessedPair = successProcessedPair.copy(
Expand Down Expand Up @@ -1233,6 +1236,7 @@ class FilesViewModel(val applicationContext: Application) :
}
FileUtils.scanFile(
contentUri,
path,
applicationContext
)
}
Expand All @@ -1243,6 +1247,7 @@ class FilesViewModel(val applicationContext: Application) :
)
FileUtils.scanFile(
uri,
path,
applicationContext
)
successProcessedPair = successProcessedPair.copy(
Expand Down Expand Up @@ -1329,6 +1334,7 @@ class FilesViewModel(val applicationContext: Application) :
)
FileUtils.scanFile(
uri,
originalFilePath,
applicationContext
)
moveToTrashLiveData.postValue(successProcessedPair)
Expand Down Expand Up @@ -1362,6 +1368,7 @@ class FilesViewModel(val applicationContext: Application) :
)
FileUtils.scanFile(
uri,
originalFilePath,
applicationContext
)
return true
Expand Down Expand Up @@ -1401,6 +1408,7 @@ class FilesViewModel(val applicationContext: Application) :
)
FileUtils.scanFile(
uri,
dest,
applicationContext
)
restoreFromTrashLiveData.postValue(successProcessedPair)
Expand Down
21 changes: 20 additions & 1 deletion app/src/main/java/com/amaze/fileutilities/utilis/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package com.amaze.fileutilities.utilis
import android.annotation.TargetApi
import android.content.Context
import android.content.Intent
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
Expand Down Expand Up @@ -344,9 +345,26 @@ class FileUtils {
* @param uri File's [Uri]
* @param c [Context]
*/
fun scanFile(uri: Uri, c: Context) {
fun scanFile(uri: Uri, path: String, c: Context) {
val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)
c.sendBroadcast(mediaScanIntent)
scanFile(path, c)
}

/**
* Triggers [Intent.ACTION_MEDIA_SCANNER_SCAN_FILE] intent to refresh the media store.
*
* @param path File's [path]
* @param c [Context]
*/
private fun scanFile(path: String, c: Context) {
MediaScannerConnection.scanFile(
c,
arrayOf(path),
null
) { path: String, _: Uri? ->
log.info("MediaConnectionUtils#scanFile finished scanning path$path")
}
}

fun deleteFileByPath(context: Context, path: String): Boolean {
Expand All @@ -361,6 +379,7 @@ class FileUtils {
uri ->
scanFile(
uri,
path,
context
)
}
Expand Down

0 comments on commit 5081f8d

Please sign in to comment.