Commit 9c51cfde authored by Ludovic Fauvet's avatar Ludovic Fauvet
Browse files

Stop all background processing when the MainActivity is not active

This includes thumbnails and media scanner.
parent f60f29c7
......@@ -39,6 +39,7 @@ import android.content.SharedPreferences;
import android.os.Environment;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
public class MediaLibrary {
public final static String TAG = "VLC/MediaLibrary";
......@@ -49,6 +50,7 @@ public class MediaLibrary {
private final DatabaseManager mDBManager;
private final ArrayList<Media> mItemList;
private final ArrayList<Handler> mUpdateHandler;
private boolean isStopping = false;
protected Thread mLoadingThread;
private MediaLibrary(Context context) {
......@@ -60,12 +62,17 @@ public class MediaLibrary {
public void loadMediaItems(Context context) {
if (mLoadingThread == null || mLoadingThread.getState() == State.TERMINATED) {
isStopping = false;
VideoListFragment.actionScanStart(context.getApplicationContext());
mLoadingThread = new Thread(new GetMediaItemsRunnable(context.getApplicationContext()));
mLoadingThread.start();
}
}
public void stop() {
isStopping = true;
}
public static MediaLibrary getInstance(Context context) {
if (mInstance == null)
mInstance = new MediaLibrary(context);
......@@ -204,98 +211,108 @@ public class MediaLibrary {
int count = 0;
ArrayList<File> mediaToScan = new ArrayList<File>();
try {
// Count total files, and stack them
while (!directories.isEmpty()) {
File dir = directories.pop();
String dirPath = dir.getAbsolutePath();
File[] f = null;
// Skip some system folders
if (dirPath.startsWith("/proc/") || dirPath.startsWith("/sys/") || dirPath.startsWith("/dev/"))
continue;
// Do not scan again if same canonical path
try {
dirPath = dir.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
if (directoriesScanned.contains(dirPath))
continue;
else
directoriesScanned.add(dirPath);
// Do no scan media in .nomedia folders
if (new File(dirPath + "/.nomedia").exists()) {
continue;
}
// Count total files, and stack them
while (!directories.isEmpty()) {
File dir = directories.pop();
String dirPath = dir.getAbsolutePath();
File[] f = null;
// Skip some system folders
if (dirPath.startsWith("/proc/") || dirPath.startsWith("/sys/") || dirPath.startsWith("/dev/"))
continue;
// Do not scan again if same canonical path
try {
dirPath = dir.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
if (directoriesScanned.contains(dirPath))
continue;
else
directoriesScanned.add(dirPath);
// Do no scan media in .nomedia folders
if (new File(dirPath + "/.nomedia").exists()) {
continue;
}
// Filter the extensions and the folders
try {
if ((f = dir.listFiles(mediaFileFilter)) != null) {
for (File file : f) {
if (file.isFile()) {
mediaToScan.add(file);
} else if (file.isDirectory()) {
directories.push(file);
// Filter the extensions and the folders
try {
if ((f = dir.listFiles(mediaFileFilter)) != null) {
for (File file : f) {
if (file.isFile()) {
mediaToScan.add(file);
} else if (file.isDirectory()) {
directories.push(file);
}
}
}
} catch (Exception e)
{
// listFiles can fail in OutOfMemoryError, go to the next folder
continue;
}
if (isStopping) {
Log.d(TAG, "Stopping scan");
return;
}
} catch (Exception e)
{
// listFiles can fail in OutOfMemoryError, go to the next folder
continue;
}
}
// Process the stacked items
for (File file : mediaToScan) {
String fileURI = Util.PathToURI(file.getPath());
MainActivity.sendTextInfo(mContext, file.getName(), count,
mediaToScan.size());
count++;
if (existingMedias.containsKey(fileURI)) {
/**
* only add file if it is not already in the list. eg. if
* user select an subfolder as well
*/
if (!addedLocations.contains(fileURI)) {
// get existing media item from database
mItemList.add(existingMedias.get(fileURI));
addedLocations.add(fileURI);
// Process the stacked items
for (File file : mediaToScan) {
String fileURI = Util.PathToURI(file.getPath());
MainActivity.sendTextInfo(mContext, file.getName(), count,
mediaToScan.size());
count++;
if (existingMedias.containsKey(fileURI)) {
/**
* only add file if it is not already in the list. eg. if
* user select an subfolder as well
*/
if (!addedLocations.contains(fileURI)) {
// get existing media item from database
mItemList.add(existingMedias.get(fileURI));
addedLocations.add(fileURI);
}
} else {
// create new media item
mItemList.add(new Media(fileURI, true));
}
if (isStopping) {
Log.d(TAG, "Stopping scan");
return;
}
} else {
// create new media item
mItemList.add(new Media(fileURI, true));
}
}
} finally {
// update the video and audio activities
for (int i = 0; i < mUpdateHandler.size(); i++) {
Handler h = mUpdateHandler.get(i);
h.sendEmptyMessage(MEDIA_ITEMS_UPDATED);
}
// update the video and audio activities
for (int i = 0; i < mUpdateHandler.size(); i++) {
Handler h = mUpdateHandler.get(i);
h.sendEmptyMessage(MEDIA_ITEMS_UPDATED);
}
// remove old files & folders from database if storage is mounted
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
for (String fileURI : addedLocations) {
existingMedias.remove(fileURI);
}
mDBManager.removeMedias(existingMedias.keySet());
// remove old files & folders from database if storage is mounted
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
for (String fileURI : addedLocations) {
existingMedias.remove(fileURI);
for (File file : mDBManager.getMediaDirs())
if (!file.isDirectory())
mDBManager.removeDir(file.getAbsolutePath());
}
mDBManager.removeMedias(existingMedias.keySet());
for (File file : mDBManager.getMediaDirs())
if (!file.isDirectory())
mDBManager.removeDir(file.getAbsolutePath());
}
// hide progressbar in footer
MainActivity.clearTextInfo(mContext);
MainActivity.hideProgressBar(mContext);
// hide progressbar in footer
MainActivity.clearTextInfo(mContext);
MainActivity.hideProgressBar(mContext);
VideoListFragment.actionScanStop(mContext);
VideoListFragment.actionScanStop(mContext);
mContext = null;
mContext = null;
}
}
};
......
......@@ -20,6 +20,7 @@
package org.videolan.vlc;
import java.lang.Thread.State;
import java.nio.ByteBuffer;
import java.util.LinkedList;
import java.util.Queue;
......@@ -36,14 +37,16 @@ import android.graphics.Bitmap.Config;
import android.util.DisplayMetrics;
import android.util.Log;
public class ThumbnailerManager extends Thread {
public class ThumbnailerManager implements Runnable {
public final static String TAG = "VLC/ThumbnailerManager";
private final Queue<Media> mItems = new LinkedList<Media>();
private boolean isStopping = false;
private final Lock lock = new ReentrantLock();
private final Condition notEmpty = lock.newCondition();
protected Thread mThread;
private LibVLC mLibVlc;
private final VideoListFragment mVideoListActivity;
private int totalCount;
......@@ -60,7 +63,18 @@ public class ThumbnailerManager extends Thread {
DisplayMetrics metrics = new DisplayMetrics();
mVideoListActivity.getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
mDensity = metrics.density;
start();
}
public void start() {
if (mThread == null || mThread.getState() == State.TERMINATED) {
isStopping = false;
mThread = new Thread(this);
mThread.start();
}
}
public void stop() {
isStopping = true;
}
/**
......@@ -94,9 +108,11 @@ public class ThumbnailerManager extends Thread {
int count = 0;
int total = 0;
Log.d(TAG, "Thumbnailer started");
String prefix = mVideoListActivity.getResources().getString(R.string.thumbnail);
while (!isInterrupted()) {
while (!isStopping) {
lock.lock();
// Get the id of the file browser item to create its thumbnail.
boolean killed = false;
......@@ -105,14 +121,17 @@ public class ThumbnailerManager extends Thread {
Log.i(TAG, "hide ProgressBar!");
MainActivity.hideProgressBar(mVideoListActivity.getActivity());
MainActivity.clearTextInfo(mVideoListActivity.getActivity());
totalCount = 0;
notEmpty.await();
} catch (InterruptedException e) {
killed = true;
break;
}
}
if (killed)
if (killed) {
lock.unlock();
break;
}
total = totalCount;
Media item = mItems.poll();
lock.unlock();
......@@ -131,10 +150,6 @@ public class ThumbnailerManager extends Thread {
//Log.i(TAG, "create new bitmap for: " + item.getName());
byte[] b = mLibVlc.getThumbnail(item.getLocation(), width, height);
// Activity stopped & destroyed, abort everything
if (isInterrupted())
break;
if (b == null) {// We were not able to create a thumbnail for this item.
item.setPicture(mVideoListActivity.getActivity(), null);
continue;
......@@ -158,5 +173,6 @@ public class ThumbnailerManager extends Thread {
break;
}
}
Log.d(TAG, "Thumbnailer stopped");
}
}
......@@ -308,6 +308,9 @@ public class MainActivity extends SherlockFragmentActivity {
*/
@Override
protected void onPause() {
/* Stop scanning for files */
MediaLibrary.getInstance(this).stop();
SharedPreferences.Editor editor = getSharedPreferences("MainActivity", MODE_PRIVATE).edit();
editor.putString("fragment", mCurrentFragment);
editor.commit();
......
......@@ -124,9 +124,16 @@ public class VideoListFragment extends SherlockListFragment implements ISortable
updateList();
}
@Override
public void onStart() {
mThumbnailerManager.start();
super.onStart();
}
@Override
public void onPause() {
super.onPause();
mThumbnailerManager.stop();
mMediaLibrary.removeUpdateHandler(mHandler);
}
......@@ -145,7 +152,7 @@ public class VideoListFragment extends SherlockListFragment implements ISortable
@Override
public void onDestroy() {
mThumbnailerManager.clearJobs();
mThumbnailerManager.interrupt();
mThumbnailerManager.stop();
mBarrier.reset();
mVideoAdapter.clear();
getActivity().unregisterReceiver(messageReceiverVideoListFragment);
......
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