From 93b8281cfb51ff907c56c8c21cd64e2af7b80c49 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 12 Nov 2016 15:24:40 +0100 Subject: [PATCH] some mediascan adjustments --- .../com/simplemobiletools/gallery/Utils.kt | 5 +- .../gallery/activities/MainActivity.java | 15 +++-- .../gallery/activities/MediaActivity.java | 24 ++------ .../gallery/activities/ViewPagerActivity.java | 5 +- .../gallery/activities/EditActivity.kt | 6 +- .../gallery/asynctasks/CopyTask.kt | 5 +- .../asynctasks/GetDirectoriesAsynctask.kt | 58 ++++++++++--------- .../gallery/dialogs/RenameFileDialog.kt | 4 +- 8 files changed, 55 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/Utils.kt b/app/src/main/java/com/simplemobiletools/gallery/Utils.kt index 453797c7f..30bb0b592 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/Utils.kt +++ b/app/src/main/java/com/simplemobiletools/gallery/Utils.kt @@ -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) = context.rescanFiles(paths) + fun scanPath(context: Context, path: String) = context.scanPath(path) {} + + fun scanFiles(context: Context, files: ArrayList) = context.scanFiles(files) {} } } diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java index 5127877a0..ce6d7257f 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java @@ -230,25 +230,24 @@ public class MainActivity extends SimpleActivity mIsSnackbarShown = false; - final List updatedFiles = new ArrayList<>(); + final ArrayList 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(); } diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java index d8e8f9135..37572f7cb 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java @@ -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 getMedia() { final List media = new ArrayList<>(); - final List invalidFiles = new ArrayList<>(); + final ArrayList 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); diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java index 0730e81d4..5f23b971b 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -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 getMedia() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt index ac650f13e..ac372db06 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/EditActivity.kt @@ -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 { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/CopyTask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/CopyTask.kt index 8c74a555c..cd2fccc82 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/CopyTask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/CopyTask.kt @@ -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?) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt index 345bfef0f..24f96112a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -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 { val directories = LinkedHashMap() - val invalidFiles = ArrayList() + val invalidFiles = ArrayList() 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(dirs) - val invalids = invalidFiles.toTypedArray() - context.rescanFiles(invalids) + context.scanFiles(invalidFiles) {} return dirs } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt index 424359521..fe2e0ca69 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameFileDialog.kt @@ -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) }