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.filepicker.extensions.*
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import java.io.File import java.io.File
import java.util.*
class Utils { class Utils {
companion object { companion object {
@ -135,6 +136,8 @@ class Utils {
fun getFileDocument(context: Context, path: String, treeUri: String) = context.getFileDocument(path, treeUri) 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; mIsSnackbarShown = false;
final List<String> updatedFiles = new ArrayList<>(); final ArrayList<File> updatedFiles = new ArrayList<>();
for (String delPath : mToBeDeleted) { for (String delPath : mToBeDeleted) {
final File dir = new File(delPath); final File dir = new File(delPath);
if (dir.exists()) { if (dir.exists()) {
final File[] files = dir.listFiles(); final File[] files = dir.listFiles();
for (File f : files) { for (File file : files) {
if (f.isFile()) { if (file.isFile()) {
updatedFiles.add(f.getAbsolutePath()); updatedFiles.add(file);
deleteItem(f); deleteItem(file);
} }
} }
updatedFiles.add(dir.getAbsolutePath()); updatedFiles.add(dir);
if (dir.listFiles().length == 0) if (dir.listFiles().length == 0)
deleteItem(dir); deleteItem(dir);
} }
} }
final String[] deletedPaths = updatedFiles.toArray(new String[updatedFiles.size()]); Utils.Companion.scanFiles(getApplicationContext(), updatedFiles);
MediaScannerConnection.scanFile(getApplicationContext(), deletedPaths, null, null);
mToBeDeleted.clear(); mToBeDeleted.clear();
} }

View file

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

View file

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

View file

@ -2,7 +2,6 @@ package com.simplemobiletools.gallery.activities
import android.app.Activity import android.app.Activity
import android.graphics.Bitmap import android.graphics.Bitmap
import android.media.MediaScannerConnection
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
@ -10,6 +9,7 @@ import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import com.simplemobiletools.filepicker.extensions.getFileDocument import com.simplemobiletools.filepicker.extensions.getFileDocument
import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions import com.simplemobiletools.filepicker.extensions.needsStupidWritePermissions
import com.simplemobiletools.filepicker.extensions.scanPath
import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.filepicker.extensions.toast
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils 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) setResult(Activity.RESULT_OK, intent)
runOnUiThread { runOnUiThread {
toast(R.string.file_saved) toast(R.string.file_saved)
} }
finish() finish()
}) }
} }
private fun getCompressionFormat(file: File): Bitmap.CompressFormat { private fun getCompressionFormat(file: File): Bitmap.CompressFormat {

View file

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

View file

@ -3,7 +3,7 @@ package com.simplemobiletools.gallery.asynctasks
import android.content.Context import android.content.Context
import android.os.AsyncTask import android.os.AsyncTask
import android.provider.MediaStore 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.Config
import com.simplemobiletools.gallery.Constants import com.simplemobiletools.gallery.Constants
import com.simplemobiletools.gallery.R 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> { override fun doInBackground(vararg params: Void): ArrayList<Directory> {
val directories = LinkedHashMap<String, Directory>() val directories = LinkedHashMap<String, Directory>()
val invalidFiles = ArrayList<String>() val invalidFiles = ArrayList<File>()
for (i in 0..1) { for (i in 0..1) {
if ((isPickVideo) && i == 0) if ((isPickVideo) && i == 0)
continue continue
@ -42,34 +42,37 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
val order = getSortOrder() val order = getSortOrder()
val cursor = context.contentResolver.query(uri, columns, null, null, order) val cursor = context.contentResolver.query(uri, columns, null, null, order)
if (cursor != null && cursor.moveToFirst()) { if (cursor != null) {
val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA) if (cursor.moveToFirst()) {
do {
val fullPath: String = cursor.getString(pathIndex) ?: continue
val file = File(fullPath)
val parentDir = file.parent
if (!file.exists()) { val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA)
invalidFiles.add(file.absolutePath) do {
continue val fullPath: String = cursor.getString(pathIndex) ?: continue
} val file = File(fullPath)
val parentDir = file.parent
val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED) if (!file.exists()) {
val timestamp = cursor.getLong(dateIndex) invalidFiles.add(file)
if (directories.containsKey(parentDir)) { continue
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())) val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)
} val timestamp = cursor.getLong(dateIndex)
} while (cursor.moveToNext()) 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() cursor.close()
} }
} }
@ -79,8 +82,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
Directory.mSorting = mConfig.directorySorting Directory.mSorting = mConfig.directorySorting
Collections.sort<Directory>(dirs) Collections.sort<Directory>(dirs)
val invalids = invalidFiles.toTypedArray() context.scanFiles(invalidFiles) {}
context.rescanFiles(invalids)
return dirs 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) { private fun sendSuccess(currFile: File, newFile: File) {
val changedFiles = arrayOf(currFile.absolutePath, newFile.absolutePath) val changedFiles = arrayListOf(currFile, newFile)
activity.rescanFiles(changedFiles) activity.scanFiles(changedFiles) {}
listener.onRenameFileSuccess(newFile) listener.onRenameFileSuccess(newFile)
} }