Commit 2c289b6b authored by Sébastien Toque's avatar Sébastien Toque
Browse files

optimize medias loading from database

parent 83ec3040
......@@ -25,6 +25,8 @@ import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import android.content.ContentValues;
......@@ -273,9 +275,9 @@ public class DatabaseManager {
* Get all paths from the items in the database
* @return list of File
*/
public synchronized List<File> getMediaFiles() {
public synchronized HashSet<File> getMediaFiles() {
List<File> files = new ArrayList<File>();
HashSet<File> files = new HashSet<File>();
Cursor cursor;
cursor = mDb.query(
......@@ -294,6 +296,48 @@ public class DatabaseManager {
return files;
}
public synchronized HashMap<String, Media> getMedias() {
Cursor cursor;
HashMap<String, Media> medias = new HashMap<String, Media>();
Bitmap picture = null;
byte[] blob;
cursor = mDb.query(
MEDIA_TABLE_NAME,
new String[] {
MEDIA_TIME, //0 long
MEDIA_LENGTH, //1 long
MEDIA_TYPE, //2 int
MEDIA_PICTURE, //3 Bitmap
MEDIA_TITLE, //4 string
MEDIA_ARTIST, //5 string
MEDIA_GENRE, //6 string
MEDIA_ALBUM, //7 string
MEDIA_PATH //8 string
},
null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
blob = cursor.getBlob(3);
if (blob != null) {
picture = BitmapFactory.decodeByteArray(blob, 0, blob.length);
}
Media media = new Media(mContext, new File(cursor.getString(8)), cursor.getLong(0),
cursor.getLong(1), cursor.getInt(2),
picture, cursor.getString(4),
cursor.getString(5), cursor.getString(6),
cursor.getString(7));
medias.put(media.getPath(), media);
} while (cursor.moveToNext());
}
cursor.close();
return medias;
}
public synchronized Media getMedia(String path) {
Cursor cursor;
......
......@@ -25,6 +25,8 @@ import java.io.FileFilter;
import java.lang.Thread.State;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Stack;
......@@ -174,11 +176,11 @@ public class MediaLibrary {
if (directorys.isEmpty())
directorys.add(new File(root));
// get all paths of the existing media items
List<File> existingFiles = mDBManager.getMediaFiles();
// get all existing media items
HashMap<String, Media> existingMedias = mDBManager.getMedias();
// list of all added files
List<File> addedFiles = new ArrayList<File>();
HashSet<File> addedFiles = new HashSet<File>();
// clear all old item
mItemList.clear();
......@@ -194,10 +196,11 @@ public class MediaLibrary {
File[] f = null;
if ((f = dir.listFiles(mediaFileFilter)) != null) {
for (int i = 0; i < f.length; i++) {
if (f[i].isFile()) {
File file = f[i];
if (file.isFile()) {
total++;
} else if (f[i].isDirectory()) {
directorys.push(f[i]);
} else if (file.isDirectory()) {
directorys.push(file);
}
}
}
......@@ -212,32 +215,32 @@ public class MediaLibrary {
File[] f = null;
if ((f = dir.listFiles(mediaFileFilter)) != null) {
for (int i = 0; i < f.length; i++) {
if (f[i].isFile()) {
File file = f[i];
MainActivity.sendTextInfo(mainHandler, f[i].getName(), count, total);
if (file.isFile()) {
MainActivity.sendTextInfo(mainHandler, file.getName(), count, total);
count++;
if (existingFiles.contains(f[i])) {
if (existingMedias.containsKey(file.getPath())) {
/** only add file if it is not already in the
* list. eg. if user select an subfolder as well
*/
if (!addedFiles.contains(f[i])) {
if (!addedFiles.contains(file)) {
// get existing media item from database
mItemList.add(mDBManager.getMedia(
f[i].getPath()));
addedFiles.add(f[i]);
mItemList.add(existingMedias.get(file.getPath()));
addedFiles.add(file);
}
} else {
// create new media item
mItemList.add(new Media(mContext, f[i]));
mItemList.add(new Media(mContext, file));
}
} else if (f[i].isDirectory()) {
directorys.push(f[i]);
} else if (file.isDirectory()) {
directorys.push(file);
}
}
}
}
MainActivity.clearTextInfo(mainHandler);
// update the video and audio activities
for (int i = 0; i < mUpdateHandler.size(); i++) {
......@@ -246,12 +249,15 @@ public class MediaLibrary {
}
// remove file from database
for (int i = 0; i < existingFiles.size(); i++) {
if (!addedFiles.contains(existingFiles.get(i))) {
mDBManager.removeMedia(existingFiles.get(i).getPath());
}
for (File file : addedFiles) {
existingMedias.remove(file.getPath());
}
for (String path : existingMedias.keySet()) {
mDBManager.removeMedia(path);
}
// hide progressbar in header
MainActivity.clearTextInfo(mainHandler);
mainHandler.sendEmptyMessage(MainActivity.HIDE_PROGRESSBAR);
}
};
......
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