Commit 2ccbed59 authored by Ludovic Fauvet's avatar Ludovic Fauvet
Browse files

MediaLibrary: allow an ongoing scan to be restarted

parent 2dfeb3b8
......@@ -38,6 +38,7 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
......@@ -50,6 +51,8 @@ public class MediaLibrary {
private final ArrayList<Media> mItemList;
private final ArrayList<Handler> mUpdateHandler;
private boolean isStopping = false;
private boolean mRestart = false;
private Context mRestartContext;
protected Thread mLoadingThread;
private MediaLibrary(Context context) {
......@@ -58,6 +61,17 @@ public class MediaLibrary {
mUpdateHandler = new ArrayList<Handler>();
}
public void loadMediaItems(Context context, boolean restart) {
if (restart && isWorking()) {
/* do a clean restart if a scan is ongoing */
mRestart = true;
isStopping = true;
mRestartContext = context;
} else {
loadMediaItems(context);
}
}
public void loadMediaItems(Context context) {
if (mLoadingThread == null || mLoadingThread.getState() == State.TERMINATED) {
isStopping = false;
......@@ -246,7 +260,7 @@ public class MediaLibrary {
}
// Filter the extensions and the folders
try {
try {
if ((f = dir.listFiles(mediaFileFilter)) != null) {
for (File file : f) {
if (file.isFile()) {
......@@ -318,11 +332,33 @@ public class MediaLibrary {
VideoListFragment.actionScanStop(mContext);
mContext = null;
if (mRestart) {
Log.d(TAG, "Restarting scan");
mRestart = false;
restartHandler.sendEmptyMessage(1);
} else {
mRestartContext = null;
mContext = null;
}
}
}
};
private Handler restartHandler = new Handler() {
@Override
public void handleMessage(final Message msgs) {
restartHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (mRestartContext != null)
loadMediaItems(mRestartContext);
else
Log.e(TAG, "Context lost in a black hole");
}
}, 200);
}
};
/**
* Filters all irrelevant files
*/
......
......@@ -309,7 +309,7 @@ public class MainActivity extends SherlockFragmentActivity {
/* Load media items from database and storage */
if (mScanNeeded)
MediaLibrary.getInstance(this).loadMediaItems(this);
MediaLibrary.getInstance(this).loadMediaItems(this, true);
super.onResume();
}
......@@ -443,7 +443,7 @@ public class MainActivity extends SherlockFragmentActivity {
else if(mCurrentFragment.equals("history"))
((HistoryFragment) getFragment(mCurrentFragment)).refresh();
else
MediaLibrary.getInstance(this).loadMediaItems(this);
MediaLibrary.getInstance(this).loadMediaItems(this, true);
break;
// Open MRL
case R.id.ml_menu_open_mrl:
......
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