some mediascan adjustments

This commit is contained in:
tibbi 2016-11-12 15:24:40 +01:00
parent df5b90319f
commit 93b8281cfb
8 changed files with 55 additions and 67 deletions

View file

@ -16,6 +16,7 @@ import android.webkit.MimeTypeMap
import com.simplemobiletools.filepicker.extensions.*
import com.simplemobiletools.gallery.models.Medium
import java.io.File
import java.util.*
class Utils {
companion object {
@ -135,6 +136,8 @@ class Utils {
fun getFileDocument(context: Context, path: String, treeUri: String) = context.getFileDocument(path, treeUri)
fun scanFiles(context: Context, paths: Array<String>) = context.rescanFiles(paths)
fun scanPath(context: Context, path: String) = context.scanPath(path) {}
fun scanFiles(context: Context, files: ArrayList<File>) = context.scanFiles(files) {}
}
}

View file

@ -230,25 +230,24 @@ public class MainActivity extends SimpleActivity
mIsSnackbarShown = false;
final List<String> updatedFiles = new ArrayList<>();
final ArrayList<File> updatedFiles = new ArrayList<>();
for (String delPath : mToBeDeleted) {
final File dir = new File(delPath);
if (dir.exists()) {
final File[] files = dir.listFiles();
for (File f : files) {
if (f.isFile()) {
updatedFiles.add(f.getAbsolutePath());
deleteItem(f);
for (File file : files) {
if (file.isFile()) {
updatedFiles.add(file);
deleteItem(file);
}
}
updatedFiles.add(dir.getAbsolutePath());
updatedFiles.add(dir);
if (dir.listFiles().length == 0)
deleteItem(dir);
}
}
final String[] deletedPaths = updatedFiles.toArray(new String[updatedFiles.size()]);
MediaScannerConnection.scanFile(getApplicationContext(), deletedPaths, null, null);
Utils.Companion.scanFiles(getApplicationContext(), updatedFiles);
mToBeDeleted.clear();
}

View file

@ -169,20 +169,6 @@ public class MediaActivity extends SimpleActivity
((MediaAdapter)mGridView.getAdapter()).updateDisplayFilenames(mConfig.getDisplayFileNames());
}
private void rescanDirectory(File dir) {
final File[] files = dir.listFiles();
final String[] paths = new String[files.length];
final int cnt = dir.listFiles().length;
for (int i = 0; i < cnt; i++) {
paths[i] = files[i].getPath();
if (files[i].isDirectory()) {
rescanDirectory(files[i]);
}
}
Utils.Companion.scanFiles(getApplicationContext(), paths);
}
private void showSortingDialog() {
new ChangeSorting(this, false);
}
@ -210,7 +196,7 @@ public class MediaActivity extends SimpleActivity
private List<Medium> getMedia() {
final List<Medium> media = new ArrayList<>();
final List<String> invalidFiles = new ArrayList<>();
final ArrayList<File> invalidFiles = new ArrayList<>();
for (int i = 0; i < 2; i++) {
if (mIsGetVideoIntent && i == 0)
continue;
@ -242,7 +228,7 @@ public class MediaActivity extends SimpleActivity
final long timestamp = cursor.getLong(dateIndex);
media.add(new Medium(file.getName(), curPath, (i == 1), timestamp, file.length()));
} else {
invalidFiles.add(file.getAbsolutePath());
invalidFiles.add(file);
}
}
} while (cursor.moveToNext());
@ -252,9 +238,7 @@ public class MediaActivity extends SimpleActivity
Medium.mSorting = mConfig.getSorting();
Collections.sort(media);
final String[] invalids = invalidFiles.toArray(new String[invalidFiles.size()]);
MediaScannerConnection.scanFile(getApplicationContext(), invalids, null, null);
Utils.Companion.scanFiles(getApplicationContext(), invalidFiles);
return media;
}
@ -544,7 +528,7 @@ public class MediaActivity extends SimpleActivity
public void onRefresh() {
final File dir = new File(mPath);
if (dir.isDirectory()) {
rescanDirectory(dir);
Utils.Companion.scanPath(getApplicationContext(), mPath);
}
initializeGallery();
mSwipeRefreshLayout.setRefreshing(false);

View file

@ -104,7 +104,7 @@ public class ViewPagerActivity extends SimpleActivity
mBeingDeleted = "";
hideSystemUI();
MediaScannerConnection.scanFile(getApplicationContext(), new String[]{mPath}, null, null);
Utils.Companion.scanPath(getApplicationContext(), mPath);
addUndoMargin();
mDirectory = new File(mPath).getParent();
mMedia = getMedia();
@ -350,8 +350,7 @@ public class ViewPagerActivity extends SimpleActivity
file.delete();
}
final String[] toBeDeleted = new String[]{mDirectory};
MediaScannerConnection.scanFile(getApplicationContext(), toBeDeleted, null, null);
Utils.Companion.scanPath(getApplicationContext(), mDirectory);
}
private List<Medium> getMedia() {

View file

@ -2,7 +2,6 @@ package com.simplemobiletools.gallery.activities
import android.app.Activity
import android.graphics.Bitmap
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Bundle
import android.util.Log
@ -10,6 +9,7 @@ import android.view.Menu
import android.view.MenuItem
import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.scanPath
import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
@ -134,14 +134,14 @@ class EditActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener
}
}
MediaScannerConnection.scanFile(applicationContext, arrayOf(path), null, { path: String, uri: Uri ->
scanPath(path) {
setResult(Activity.RESULT_OK, intent)
runOnUiThread {
toast(R.string.file_saved)
}
finish()
})
}
}
private fun getCompressionFormat(file: File): Bitmap.CompressFormat {

View file

@ -6,7 +6,7 @@ import android.support.v4.util.Pair
import android.util.Log
import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.rescanFile
import com.simplemobiletools.filepicker.extensions.scanFile
import com.simplemobiletools.gallery.Config
import java.io.*
import java.lang.ref.WeakReference
@ -69,6 +69,7 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn
val inputStream = FileInputStream(newFile)
val out = context.contentResolver.openOutputStream(document.uri)
copyStream(inputStream, out)
context.scanFile(destination) {}
}
} else {
copy(newFile, File(destination, child))
@ -92,8 +93,8 @@ class CopyTask(listener: CopyTask.CopyDoneListener, val context: Context) : Asyn
out = FileOutputStream(destination)
}
context.rescanFile(destination)
copyStream(inputStream, out)
context.scanFile(destination) {}
}
private fun copyStream(inputStream: InputStream, out: OutputStream?) {

View file

@ -3,7 +3,7 @@ package com.simplemobiletools.gallery.asynctasks
import android.content.Context
import android.os.AsyncTask
import android.provider.MediaStore
import com.simplemobiletools.filepicker.extensions.rescanFiles
import com.simplemobiletools.filepicker.extensions.scanFiles
import com.simplemobiletools.gallery.Config
import com.simplemobiletools.gallery.Constants
import com.simplemobiletools.gallery.R
@ -26,7 +26,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
override fun doInBackground(vararg params: Void): ArrayList<Directory> {
val directories = LinkedHashMap<String, Directory>()
val invalidFiles = ArrayList<String>()
val invalidFiles = ArrayList<File>()
for (i in 0..1) {
if ((isPickVideo) && i == 0)
continue
@ -42,34 +42,37 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
val order = getSortOrder()
val cursor = context.contentResolver.query(uri, columns, null, null, order)
if (cursor != null && cursor.moveToFirst()) {
val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA)
do {
val fullPath: String = cursor.getString(pathIndex) ?: continue
val file = File(fullPath)
val parentDir = file.parent
if (cursor != null) {
if (cursor.moveToFirst()) {
if (!file.exists()) {
invalidFiles.add(file.absolutePath)
continue
}
val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA)
do {
val fullPath: String = cursor.getString(pathIndex) ?: continue
val file = File(fullPath)
val parentDir = file.parent
val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)
val timestamp = cursor.getLong(dateIndex)
if (directories.containsKey(parentDir)) {
val directory: Directory = directories[parentDir]!!
val newImageCnt = directory.mediaCnt + 1
directory.mediaCnt = newImageCnt
directory.addSize(file.length())
} else if (!mToBeDeleted.contains(parentDir)) {
var dirName = Utils.getFilename(parentDir)
if (mConfig.getIsFolderHidden(parentDir)) {
dirName += " ${context.resources.getString(R.string.hidden)}"
if (!file.exists()) {
invalidFiles.add(file)
continue
}
directories.put(parentDir, Directory(parentDir, fullPath, dirName, 1, timestamp, file.length()))
}
} while (cursor.moveToNext())
val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)
val timestamp = cursor.getLong(dateIndex)
if (directories.containsKey(parentDir)) {
val directory: Directory = directories[parentDir]!!
val newImageCnt = directory.mediaCnt + 1
directory.mediaCnt = newImageCnt
directory.addSize(file.length())
} else if (!mToBeDeleted.contains(parentDir)) {
var dirName = Utils.getFilename(parentDir)
if (mConfig.getIsFolderHidden(parentDir)) {
dirName += " ${context.resources.getString(R.string.hidden)}"
}
directories.put(parentDir, Directory(parentDir, fullPath, dirName, 1, timestamp, file.length()))
}
} while (cursor.moveToNext())
}
cursor.close()
}
}
@ -79,8 +82,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
Directory.mSorting = mConfig.directorySorting
Collections.sort<Directory>(dirs)
val invalids = invalidFiles.toTypedArray()
context.rescanFiles(invalids)
context.scanFiles(invalidFiles) {}
return dirs
}

View file

@ -67,8 +67,8 @@ class RenameFileDialog(val activity: SimpleActivity, val file: File, val listene
}
private fun sendSuccess(currFile: File, newFile: File) {
val changedFiles = arrayOf(currFile.absolutePath, newFile.absolutePath)
activity.rescanFiles(changedFiles)
val changedFiles = arrayListOf(currFile, newFile)
activity.scanFiles(changedFiles) {}
listener.onRenameFileSuccess(newFile)
}