Commit 67847a16 authored by Ludovic Fauvet's avatar Ludovic Fauvet
Browse files

Move the ThumbnailerManager into the MainActivity

Generating the thumbnails being a background job of the MainActivity
(the same way that the file scanning of the media library) it makes
sense to be managed by the latter and not by one of its fragment.

Furthermore the ThumbnailerManager is now a singleton so it can be
accessed indifferently from the MainActivity or the VideoListFragment.
parent a0e0b533
......@@ -32,6 +32,7 @@ import java.util.concurrent.locks.ReentrantLock;
import org.videolan.vlc.gui.MainActivity;
import org.videolan.vlc.gui.video.VideoListFragment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.util.DisplayMetrics;
......@@ -40,6 +41,9 @@ import android.util.Log;
public class ThumbnailerManager implements Runnable {
public final static String TAG = "VLC/ThumbnailerManager";
private static ThumbnailerManager mInstance;
private VideoListFragment mVideoListFragment;
private final Queue<Media> mItems = new LinkedList<Media>();
private boolean isStopping = false;
......@@ -48,12 +52,12 @@ public class ThumbnailerManager implements Runnable {
protected Thread mThread;
private LibVLC mLibVlc;
private final VideoListFragment mVideoListActivity;
private final Activity mActivity;
private int totalCount;
private final float mDensity;
public ThumbnailerManager(VideoListFragment videoListActivity) {
mVideoListActivity = videoListActivity;
private ThumbnailerManager(Activity parentActivity) {
mActivity = parentActivity;
try {
mLibVlc = LibVLC.getInstance();
} catch (LibVlcException e) {
......@@ -61,12 +65,19 @@ public class ThumbnailerManager implements Runnable {
}
DisplayMetrics metrics = new DisplayMetrics();
mVideoListActivity.getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
mDensity = metrics.density;
}
public void start() {
public static ThumbnailerManager getInstance(Activity activity) {
if (mInstance == null)
mInstance = new ThumbnailerManager(activity);
return mInstance;
}
public void start(VideoListFragment videoListFragment) {
if (mThread == null || mThread.getState() == State.TERMINATED) {
mVideoListFragment = videoListFragment;
isStopping = false;
mThread = new Thread(this);
mThread.start();
......@@ -110,7 +121,7 @@ public class ThumbnailerManager implements Runnable {
Log.d(TAG, "Thumbnailer started");
String prefix = mVideoListActivity.getResources().getString(R.string.thumbnail);
String prefix = mActivity.getResources().getString(R.string.thumbnail);
while (!isStopping) {
lock.lock();
......@@ -118,8 +129,8 @@ public class ThumbnailerManager implements Runnable {
boolean killed = false;
while (mItems.size() == 0) {
try {
MainActivity.hideProgressBar(mVideoListActivity.getActivity());
MainActivity.clearTextInfo(mVideoListActivity.getActivity());
MainActivity.hideProgressBar(mActivity);
MainActivity.clearTextInfo(mActivity);
totalCount = 0;
notEmpty.await();
} catch (InterruptedException e) {
......@@ -135,9 +146,9 @@ public class ThumbnailerManager implements Runnable {
Media item = mItems.poll();
lock.unlock();
MainActivity.showProgressBar(mVideoListActivity.getActivity());
MainActivity.showProgressBar(mActivity);
MainActivity.sendTextInfo(mVideoListActivity.getActivity(), String.format("%s %s", prefix, item.getFileName()), count, total);
MainActivity.sendTextInfo(mActivity, String.format("%s %s", prefix, item.getFileName()), count, total);
count++;
int width = (int) (120 * mDensity);
......@@ -149,7 +160,7 @@ public class ThumbnailerManager implements Runnable {
byte[] b = mLibVlc.getThumbnail(item.getLocation(), width, height);
if (b == null) {// We were not able to create a thumbnail for this item.
item.setPicture(mVideoListActivity.getActivity(), null);
item.setPicture(mActivity, null);
continue;
}
......@@ -158,13 +169,13 @@ public class ThumbnailerManager implements Runnable {
Log.i(TAG, "Thumbnail created for " + item.getFileName());
item.setPicture(mVideoListActivity.getActivity(), thumbnail);
item.setPicture(mActivity, thumbnail);
// Post to the file browser the new item.
mVideoListActivity.setItemToUpdate(item);
mVideoListFragment.setItemToUpdate(item);
// Wait for the file browser to process the change.
try {
mVideoListActivity.await();
mVideoListFragment.await();
} catch (InterruptedException e) {
break;
} catch (BrokenBarrierException e) {
......
......@@ -28,10 +28,12 @@ import org.videolan.vlc.LibVLC;
import org.videolan.vlc.LibVlcException;
import org.videolan.vlc.MediaLibrary;
import org.videolan.vlc.R;
import org.videolan.vlc.ThumbnailerManager;
import org.videolan.vlc.Util;
import org.videolan.vlc.VLCCallbackTask;
import org.videolan.vlc.gui.SidebarAdapter.SidebarEntry;
import org.videolan.vlc.gui.video.VideoListAdapter;
import org.videolan.vlc.gui.video.VideoListFragment;
import org.videolan.vlc.interfaces.ISortable;
import org.videolan.vlc.widget.AudioMiniPlayer;
......@@ -270,6 +272,14 @@ public class MainActivity extends SherlockFragmentActivity {
mMenu.setBehindOffset(Util.convertDpToPx(behindOffset_dp));
}
@Override
protected void onStart() {
/* Start the thumbnailer */
VideoListFragment f = (VideoListFragment)mSidebarAdapter.getFragment("video");
ThumbnailerManager.getInstance(this).start(f);
super.onStart();
}
@Override
protected void onResume() {
mAudioController.addAudioPlayer(mAudioPlayer);
......@@ -312,6 +322,8 @@ public class MainActivity extends SherlockFragmentActivity {
protected void onPause() {
/* Stop scanning for files */
MediaLibrary.getInstance(this).stop();
/* Stop the thumbnailer */
ThumbnailerManager.getInstance(this).stop();
SharedPreferences.Editor editor = getSharedPreferences("MainActivity", MODE_PRIVATE).edit();
editor.putString("fragment", mCurrentFragment);
......@@ -325,6 +337,7 @@ public class MainActivity extends SherlockFragmentActivity {
try {
unregisterReceiver(messageReceiver);
} catch (IllegalArgumentException e) {}
ThumbnailerManager.getInstance(this).clearJobs();
super.onDestroy();
}
......
......@@ -75,7 +75,6 @@ public class VideoListFragment extends SherlockListFragment implements ISortable
protected Media mItemToUpdate;
protected final CyclicBarrier mBarrier = new CyclicBarrier(2);
protected ThumbnailerManager mThumbnailerManager;
protected static final int UPDATE_ITEM = 0;
......@@ -88,7 +87,6 @@ public class VideoListFragment extends SherlockListFragment implements ISortable
mVideoAdapter = new VideoListAdapter(getActivity());
mMediaLibrary = MediaLibrary.getInstance(getActivity());
mThumbnailerManager = new ThumbnailerManager(this);
setListAdapter(mVideoAdapter);
}
......@@ -126,14 +124,12 @@ public class VideoListFragment extends SherlockListFragment implements ISortable
@Override
public void onStart() {
mThumbnailerManager.start();
super.onStart();
}
@Override
public void onPause() {
super.onPause();
mThumbnailerManager.stop();
mMediaLibrary.removeUpdateHandler(mHandler);
}
......@@ -151,8 +147,6 @@ public class VideoListFragment extends SherlockListFragment implements ISortable
@Override
public void onDestroy() {
mThumbnailerManager.clearJobs();
mThumbnailerManager.stop();
mBarrier.reset();
mVideoAdapter.clear();
getActivity().unregisterReceiver(messageReceiverVideoListFragment);
......@@ -253,7 +247,8 @@ public class VideoListFragment extends SherlockListFragment implements ISortable
List<Media> itemList = mMediaLibrary.getVideoItems();
mThumbnailerManager.clearJobs();
ThumbnailerManager t = ThumbnailerManager.getInstance(getActivity());
t.clearJobs();
mVideoAdapter.clear();
if (itemList.size() > 0) {
......@@ -261,7 +256,7 @@ public class VideoListFragment extends SherlockListFragment implements ISortable
if (item.getType() == Media.TYPE_VIDEO) {
mVideoAdapter.add(item);
if (item.getPicture() == null && !item.isPictureParsed())
mThumbnailerManager.addJob(item);
t.addJob(item);
}
}
mVideoAdapter.sort();
......
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