[Android] [PATCH 03/11] Code factorization

Geoffrey Métais geoffrey.metais at gmail.com
Mon Nov 17 17:53:40 CET 2014


interface instead of hardcoded classes
GridFragment can handle all media, not only videos
start/stop scan methods in Utils
---
 vlc-android/src/org/videolan/vlc/MediaLibrary.java |  5 +-
 .../src/org/videolan/vlc/gui/tv/CardPresenter.java |  7 ++-
 .../vlc/gui/tv/DetailsDescriptionPresenter.java    |  2 +-
 .../src/org/videolan/vlc/gui/tv/GridFragment.java  | 71 ++++++++--------------
 .../org/videolan/vlc/gui/tv/MainTvActivity.java    | 57 +++++------------
 .../org/videolan/vlc/gui/tv/MediaItemDetails.java  | 46 +++++++++++++-
 .../vlc/gui/tv/MediaItemDetailsFragment.java       |  7 +--
 .../vlc/gui/video/VideoBrowserInterface.java       |  7 ++-
 .../videolan/vlc/gui/video/VideoGridFragment.java  | 60 ++++--------------
 .../videolan/vlc/gui/video/VideoListHandler.java   | 29 +++++++++
 vlc-android/src/org/videolan/vlc/util/Util.java    | 15 +++++
 11 files changed, 153 insertions(+), 153 deletions(-)
 create mode 100644 vlc-android/src/org/videolan/vlc/gui/video/VideoListHandler.java

diff --git a/vlc-android/src/org/videolan/vlc/MediaLibrary.java b/vlc-android/src/org/videolan/vlc/MediaLibrary.java
index 29a1a0b..f883cbc 100644
--- a/vlc-android/src/org/videolan/vlc/MediaLibrary.java
+++ b/vlc-android/src/org/videolan/vlc/MediaLibrary.java
@@ -40,6 +40,7 @@ import org.videolan.vlc.gui.MainActivity;
 import org.videolan.vlc.gui.audio.AudioBrowserFragment;
 import org.videolan.vlc.gui.video.VideoGridFragment;
 import org.videolan.vlc.util.AndroidDevices;
+import org.videolan.vlc.util.Util;
 import org.videolan.vlc.util.VLCInstance;
 import org.videolan.vlc.util.WeakHandler;
 
@@ -82,7 +83,7 @@ public class MediaLibrary {
     public void loadMediaItems() {
         if (mLoadingThread == null || mLoadingThread.getState() == State.TERMINATED) {
             isStopping = false;
-            VideoGridFragment.actionScanStart();
+            Util.actionScanStart();
             mLoadingThread = new Thread(new GetMediaItemsRunnable());
             mLoadingThread.start();
         }
@@ -355,7 +356,7 @@ public class MediaLibrary {
                 MainActivity.clearTextInfo();
                 MainActivity.hideProgressBar();
 
-                VideoGridFragment.actionScanStop();
+                Util.actionScanStop();
 
                 if (mRestart) {
                     Log.d(TAG, "Restarting scan");
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/CardPresenter.java b/vlc-android/src/org/videolan/vlc/gui/tv/CardPresenter.java
index 5da55d5..b1338ee 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/CardPresenter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/CardPresenter.java
@@ -6,6 +6,7 @@ import org.videolan.vlc.R;
 
 import android.content.Context;
 import android.content.res.Resources;
+import android.graphics.Bitmap;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.support.v17.leanback.widget.ImageCardView;
@@ -46,7 +47,11 @@ public class CardPresenter extends Presenter {
         }
 
         protected void updateCardViewImage(String mediaLocation) {
-        	mCardView.setMainImage(new BitmapDrawable(sResources, sMediaDatabase.getPicture(sContext, mediaLocation)));
+			Bitmap picture = sMediaDatabase.getPicture(sContext, mediaLocation);
+			if (picture.getByteCount() > 4)
+				mCardView.setMainImage(new BitmapDrawable(sResources, picture));
+			else
+				updateCardViewImage(sDefaultCardImage);
         }
 
         protected void updateCardViewImage(Drawable image) {
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/DetailsDescriptionPresenter.java b/vlc-android/src/org/videolan/vlc/gui/tv/DetailsDescriptionPresenter.java
index 4300f77..0832b9e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/DetailsDescriptionPresenter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/DetailsDescriptionPresenter.java
@@ -36,7 +36,7 @@ public class DetailsDescriptionPresenter extends
 		// Here we provide static data for testing purposes:
 		viewHolder.getTitle().setText(details.getTitle());
 		viewHolder.getSubtitle().setText(details.getSubTitle());
-//		viewHolder.getBody().setText(details.getBody());
+		viewHolder.getBody().setText(details.getBody());
 	}
 
 	
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/GridFragment.java b/vlc-android/src/org/videolan/vlc/gui/tv/GridFragment.java
index f02fa05..64a64ae 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/GridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/GridFragment.java
@@ -31,22 +31,20 @@ import org.videolan.vlc.R;
 import org.videolan.vlc.Thumbnailer;
 import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.gui.video.VideoBrowserInterface;
+import org.videolan.vlc.gui.video.VideoListHandler;
 import org.videolan.vlc.gui.video.VideoPlayerActivity;
-import org.videolan.vlc.util.WeakHandler;
+import org.videolan.vlc.util.Util;
 
 import android.content.Context;
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.os.Bundle;
 import android.os.Handler;
-import android.os.Message;
 import android.support.v17.leanback.app.VerticalGridFragment;
 import android.support.v17.leanback.widget.ArrayObjectAdapter;
 import android.support.v17.leanback.widget.OnItemClickedListener;
-import android.support.v17.leanback.widget.OnItemSelectedListener;
 import android.support.v17.leanback.widget.Row;
 import android.support.v17.leanback.widget.VerticalGridPresenter;
-import android.util.Log;
 
 public class GridFragment extends VerticalGridFragment implements VideoBrowserInterface {
 	private static final String TAG = "VerticalGridFragment";
@@ -58,26 +56,28 @@ public class GridFragment extends VerticalGridFragment implements VideoBrowserIn
 	private ArrayObjectAdapter mAdapter;
 	private MediaLibrary mMediaLibrary;
 	private Thumbnailer mThumbnailer;
-	HashMap<String, Integer> mVideoIndex;
+	HashMap<String, Integer> mMediaIndex;
 	Context mContext;
+	long mType;
 
 	@Override
 	public void onCreate(Bundle savedInstanceState) {
-		Log.i(TAG, "onCreate");
 		super.onCreate(savedInstanceState);
 		mContext = getActivity();
+		mType = getActivity().getIntent().getLongExtra("id", 0);
 
 		setTitle(getString(R.string.app_name_full));
 
 		mMediaLibrary = MediaLibrary.getInstance();
-		mThumbnailer = new Thumbnailer(mContext, getActivity().getWindowManager().getDefaultDisplay());
+		if (mType == HEADER_VIDEO)
+			mThumbnailer = new Thumbnailer(mContext, getActivity().getWindowManager().getDefaultDisplay());
 		setupFragment();
 	}
 
 	public void onResume() {
 		super.onResume();
 		if (mMediaLibrary.isWorking()) {
-			actionScanStart();
+			Util.actionScanStart();
 		}
 
 		/* Start the thumbnailer */
@@ -115,16 +115,20 @@ public class GridFragment extends VerticalGridFragment implements VideoBrowserIn
 		setGridPresenter(gridPresenter);
 
 
-		ArrayList<Media> videoList = mMediaLibrary.getVideoItems();
-		size = videoList.size();
-		mVideoIndex = new HashMap<String, Integer>(size);
+		ArrayList<Media> mediaList = null;
+		if (mType == HEADER_VIDEO)
+			mediaList = mMediaLibrary.getVideoItems();
+		else if (mType == HEADER_MUSIC)
+			mediaList = mMediaLibrary.getAudioItems();
+		size = mediaList == null ? 0 : mediaList.size();
+		mMediaIndex = new HashMap<String, Integer>(size);
 		
 		for (int i = 0 ; i < size ; ++i){
-			media = videoList.get(i);
-			picture = mediaDatabase.getPicture(mContext, media.getLocation());
+			media = mediaList.get(i);
 			mAdapter.add(media);
-			mVideoIndex.put(media.getLocation(), i);
+			mMediaIndex.put(media.getLocation(), i);
 			if (mThumbnailer != null){
+				picture = mediaDatabase.getPicture(mContext, media.getLocation());
 				if (picture== null) {
 					mThumbnailer.addJob(media);
 				} else {
@@ -163,46 +167,21 @@ public class GridFragment extends VerticalGridFragment implements VideoBrowserIn
 	@Override
 	public void setItemToUpdate(Media item) {
 		mItemToUpdate = item;
-		mHandler.sendEmptyMessage(UPDATE_ITEM);
+		mHandler.sendEmptyMessage(VideoListHandler.UPDATE_ITEM);
 	}
 
-	private void updateItem() {
-		mAdapter.notifyArrayItemRangeChanged(mVideoIndex.get(mItemToUpdate.getLocation()), 1);
+	public void updateItem() {
+		mAdapter.notifyArrayItemRangeChanged(mMediaIndex.get(mItemToUpdate.getLocation()), 1);
 		try {
 			mBarrier.await();
 		} catch (InterruptedException e) {
 		} catch (BrokenBarrierException e) {}
 	}
 
-	public static void actionScanStart() {
-		Intent intent = new Intent();
-		intent.setAction(ACTION_SCAN_START);
-		VLCApplication.getAppContext().sendBroadcast(intent);
-	}
-
-	public static void actionScanStop() {
-		Intent intent = new Intent();
-		intent.setAction(ACTION_SCAN_STOP);
-		VLCApplication.getAppContext().sendBroadcast(intent);
-	}
+	@Override
+	public void updateList() {
+		// TODO Auto-generated method stub
+	};
 
 	private Handler mHandler = new VideoListHandler(this);
-
-	private static class VideoListHandler extends WeakHandler<GridFragment> {
-		public VideoListHandler(GridFragment owner) {
-			super(owner);
-		}
-
-		@Override
-		public void handleMessage(Message msg) {
-			GridFragment owner = getOwner();
-			if(owner == null) return;
-
-			switch (msg.what) {
-			case UPDATE_ITEM:
-				owner.updateItem();
-				break;
-			}
-		}
-	};
 }
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java b/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java
index cce4186..363743c 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/MainTvActivity.java
@@ -31,8 +31,9 @@ import org.videolan.vlc.R;
 import org.videolan.vlc.Thumbnailer;
 import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.gui.video.VideoBrowserInterface;
+import org.videolan.vlc.gui.video.VideoListHandler;
 import org.videolan.vlc.gui.video.VideoPlayerActivity;
-import org.videolan.vlc.util.WeakHandler;
+import org.videolan.vlc.util.Util;
 
 import android.app.Activity;
 import android.app.FragmentManager;
@@ -41,7 +42,6 @@ import android.graphics.Bitmap;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.Handler;
-import android.os.Message;
 import android.os.Parcelable;
 import android.support.v17.leanback.app.BackgroundManager;
 import android.support.v17.leanback.app.BrowseFragment;
@@ -51,6 +51,7 @@ import android.support.v17.leanback.widget.ListRow;
 import android.support.v17.leanback.widget.ListRowPresenter;
 import android.support.v17.leanback.widget.OnItemClickedListener;
 import android.support.v17.leanback.widget.Row;
+import android.util.Log;
 
 public class MainTvActivity extends Activity implements VideoBrowserInterface {
 
@@ -81,11 +82,11 @@ public class MainTvActivity extends Activity implements VideoBrowserInterface {
 				Intent intent = new Intent(MainTvActivity.this,
 						DetailsActivity.class);
 				// pass the item information
-				intent.putExtra("id", row.getId());
-				intent.putExtra("item", (Parcelable)new TvMedia(0, media.getTitle(), media.getDescription(), media.getArtworkURL(), media.getArtworkURL(), media.getLocation()));
+				intent.putExtra("item", (Parcelable)new MediaItemDetails(media.getTitle(), media.getArtist(), media.getAlbum()+"\n"+media.getLocation(), media.getLocation()));
 				startActivity(intent);
 			} else if (media.getType() == Media.TYPE_GROUP){
 				Intent intent = new Intent(mContext, VerticalGridActivity.class);
+				intent.putExtra("id", row.getId());
 				startActivity(intent);
 			}
 		}
@@ -119,7 +120,7 @@ public class MainTvActivity extends Activity implements VideoBrowserInterface {
 		super.onResume();
 		mMediaLibrary.addUpdateHandler(mHandler);
 		if (mMediaLibrary.isWorking()) {
-			actionScanStart();
+			Util.actionScanStart();
 		}
 
 		/* Start the thumbnailer */
@@ -152,19 +153,6 @@ public class MainTvActivity extends Activity implements VideoBrowserInterface {
         BackgroundManager.getInstance(this).setDrawable(mDefaultBackground);
     }
 
-
-	public static void actionScanStart() {
-		Intent intent = new Intent();
-		intent.setAction(ACTION_SCAN_START);
-		VLCApplication.getAppContext().sendBroadcast(intent);
-	}
-
-	public static void actionScanStop() {
-		Intent intent = new Intent();
-		intent.setAction(ACTION_SCAN_STOP);
-		VLCApplication.getAppContext().sendBroadcast(intent);
-	}
-
 	public void await() throws InterruptedException, BrokenBarrierException {
 		mBarrier.await();
 	}
@@ -173,7 +161,7 @@ public class MainTvActivity extends Activity implements VideoBrowserInterface {
 		mBarrier.reset();
 	}
 
-	private void updateList() {
+	public void updateList() {
 		MediaDatabase mediaDatabase = MediaDatabase.getInstance();
 		ArrayList<Media> videoList = mMediaLibrary.getVideoItems();
 		ArrayList<Media> audioList = mMediaLibrary.getAudioItems();
@@ -206,7 +194,7 @@ public class MainTvActivity extends Activity implements VideoBrowserInterface {
 			// Empty item to launch grid activity
 			videoAdapter.add(new Media(null, 0, 0, Media.TYPE_GROUP, null, "Browse more", null, null, null, 0, 0, null, 0, 0));
 
-			HeaderItem header = new HeaderItem(0, "Videos", null);
+			HeaderItem header = new HeaderItem(HEADER_VIDEO, "Videos", null);
 			mRowsAdapter.add(new ListRow(header, videoAdapter));
 		}
 		
@@ -216,7 +204,10 @@ public class MainTvActivity extends Activity implements VideoBrowserInterface {
 			for (Media music : audioList) {
 				audioAdapter.add(music);
 			}
-			HeaderItem header = new HeaderItem(1, "Music", null);
+			// Empty item to launch grid activity
+			audioAdapter.add(new Media(null, 0, 0, Media.TYPE_GROUP, null, "Browse more", null, null, null, 0, 0, null, 0, 0));
+
+			HeaderItem header = new HeaderItem(HEADER_MUSIC, "Music", null);
 			mRowsAdapter.add(new ListRow(header, audioAdapter));
 		}
 		mBrowseFragment.setAdapter(mRowsAdapter);
@@ -225,10 +216,10 @@ public class MainTvActivity extends Activity implements VideoBrowserInterface {
 	@Override
 	public void setItemToUpdate(Media item) {
 		mItemToUpdate = item;
-		mHandler.sendEmptyMessage(UPDATE_ITEM);
+		mHandler.sendEmptyMessage(VideoListHandler.UPDATE_ITEM);
 	}
 
-	private void updateItem() {
+	public void updateItem() {
 		videoAdapter.notifyArrayItemRangeChanged(mVideoIndex.get(mItemToUpdate.getLocation()), 1);
 		try {
 			mBarrier.await();
@@ -237,24 +228,4 @@ public class MainTvActivity extends Activity implements VideoBrowserInterface {
 	}
 
 	private Handler mHandler = new VideoListHandler(this);
-
-	private static class VideoListHandler extends WeakHandler<MainTvActivity> {
-		public VideoListHandler(MainTvActivity owner) {
-			super(owner);
-		}
-
-		@Override
-		public void handleMessage(Message msg) {
-			MainTvActivity owner = getOwner();
-			if(owner == null) return;
-
-			switch (msg.what) {
-			case UPDATE_ITEM:
-				owner.updateItem();
-				break;
-			case MediaLibrary.MEDIA_ITEMS_UPDATED:
-				owner.updateList();
-			}
-		}
-	};
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/MediaItemDetails.java b/vlc-android/src/org/videolan/vlc/gui/tv/MediaItemDetails.java
index 343b31e..d4df1e0 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/MediaItemDetails.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/MediaItemDetails.java
@@ -19,14 +19,18 @@
  *****************************************************************************/
 package org.videolan.vlc.gui.tv;
 
-public class MediaItemDetails {
+import android.os.Parcel;
+import android.os.Parcelable;
 
-	private String title, subTitle, body;
+public class MediaItemDetails implements Parcelable {
 
-	public MediaItemDetails(String title, String subTitle, String body) {
+	private String title, subTitle, body, location;
+
+	public MediaItemDetails(String title, String subTitle, String body, String location) {
 		this.title = title;
 		this.subTitle = subTitle;
 		this.body = body;
+		this.location = location;
 	}
 
 	public String getTitle() {
@@ -40,4 +44,40 @@ public class MediaItemDetails {
 	public String getBody() {
 		return body;
 	}
+
+	public String getLocation(){
+		return location;
+	}
+
+	@Override
+	public int describeContents() {
+		// TODO Auto-generated method stub
+		return 0;
+	}
+
+	@Override
+	public void writeToParcel(Parcel dest, int flags) {
+		dest.writeString(title);
+		dest.writeString(subTitle);
+		dest.writeString(body);
+		dest.writeString(location);
+	}
+
+	public static final Parcelable.Creator<MediaItemDetails> CREATOR
+	= new Parcelable.Creator<MediaItemDetails>() {
+		public MediaItemDetails createFromParcel(Parcel in) {
+			return new MediaItemDetails(in);
+		}
+
+		public MediaItemDetails[] newArray(int size) {
+			return new MediaItemDetails[size];
+		}
+	};
+
+	private MediaItemDetails(Parcel in) {
+		title = in.readString();
+		subTitle = in.readString();
+		body = in.readString();
+		location = in.readString();
+	}
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/MediaItemDetailsFragment.java b/vlc-android/src/org/videolan/vlc/gui/tv/MediaItemDetailsFragment.java
index 1ccbc5e..96f6302 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/MediaItemDetailsFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/MediaItemDetailsFragment.java
@@ -32,7 +32,6 @@ import android.support.v17.leanback.widget.DetailsOverviewRowPresenter;
 import android.support.v17.leanback.widget.HeaderItem;
 import android.support.v17.leanback.widget.ListRow;
 import android.support.v17.leanback.widget.ListRowPresenter;
-import android.util.Log;
 
 public class MediaItemDetailsFragment extends DetailsFragment {
 	private static final String TAG = "MediaItemDetailsFragment";
@@ -40,7 +39,6 @@ public class MediaItemDetailsFragment extends DetailsFragment {
 
 	@Override
 	public void onCreate(Bundle savedInstanceState) {
-		Log.i(TAG, "onCreate");
 		super.onCreate(savedInstanceState);
 
 		buildDetails();
@@ -48,8 +46,7 @@ public class MediaItemDetailsFragment extends DetailsFragment {
 
 	private void buildDetails() {
 		Bundle extras = getActivity().getIntent().getExtras();
-		Log.d(TAG, "id "+extras.getLong("id"));
-		TvMedia media = extras.getParcelable("item");
+		MediaItemDetails media = extras.getParcelable("item");
 		ClassPresenterSelector selector = new ClassPresenterSelector();
 		// Attach your media item details presenter to the row presenter:
 		DetailsOverviewRowPresenter rowPresenter =
@@ -61,7 +58,7 @@ public class MediaItemDetailsFragment extends DetailsFragment {
 		mRowsAdapter = new ArrayObjectAdapter(selector);
 
 		Resources res = getActivity().getResources();
-		DetailsOverviewRow detailsOverview = new DetailsOverviewRow(new MediaItemDetails(media.getTitle(), media.getDescription(), "Big body"));
+		DetailsOverviewRow detailsOverview = new DetailsOverviewRow(media);
 
 		// Add images and action buttons to the details view
 		detailsOverview.setImageDrawable(res.getDrawable(R.drawable.cone));
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoBrowserInterface.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoBrowserInterface.java
index 12e5f1d..84efc01 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoBrowserInterface.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoBrowserInterface.java
@@ -5,11 +5,12 @@ import java.util.concurrent.BrokenBarrierException;
 import org.videolan.libvlc.Media;
 
 public interface VideoBrowserInterface {
-    static final String ACTION_SCAN_START = "org.videolan.vlc.gui.ScanStart";
-    static final String ACTION_SCAN_STOP = "org.videolan.vlc.gui.ScanStop";
-    static final int UPDATE_ITEM = 0;
+    public static final int HEADER_VIDEO = 0;
+	public static final int HEADER_MUSIC = 1;
 
 	public void resetBarrier();
 	public void setItemToUpdate(Media item);
 	public void await() throws InterruptedException, BrokenBarrierException;
+	public void updateItem();
+	public void updateList();
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
index ffa12d1..5b450dc 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -148,12 +148,12 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
 
         // init the information for the scan (2/2)
         IntentFilter filter = new IntentFilter();
-        filter.addAction(ACTION_SCAN_START);
-        filter.addAction(ACTION_SCAN_STOP);
+        filter.addAction(Util.ACTION_SCAN_START);
+        filter.addAction(Util.ACTION_SCAN_STOP);
         getActivity().registerReceiver(messageReceiverVideoListFragment, filter);
         Log.i(TAG,"mMediaLibrary.isWorking() " + Boolean.toString(mMediaLibrary.isWorking()));
         if (mMediaLibrary.isWorking()) {
-            actionScanStart();
+        	Util.actionScanStart();
         }
 
         mAnimator = new VideoGridAnimator(getGridView());
@@ -358,32 +358,7 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
      */
     private Handler mHandler = new VideoListHandler(this);
 
-    private static class VideoListHandler extends WeakHandler<VideoGridFragment> {
-        public VideoListHandler(VideoGridFragment owner) {
-            super(owner);
-        }
-
-        @Override
-        public void handleMessage(Message msg) {
-            VideoGridFragment fragment = getOwner();
-            if(fragment == null) return;
-
-            switch (msg.what) {
-            case UPDATE_ITEM:
-                fragment.updateItem();
-                break;
-            case MediaLibrary.MEDIA_ITEMS_UPDATED:
-                // Don't update the adapter while the layout animation is running
-                if (fragment.mAnimator.isAnimationDone())
-                    fragment.updateList();
-                else
-                    sendEmptyMessageDelayed(msg.what, 500);
-                break;
-            }
-        }
-    };
-
-    private void updateItem() {
+    public void updateItem() {
         mVideoAdapter.update(mItemToUpdate);
         try {
             mBarrier.await();
@@ -391,16 +366,15 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
         } catch (BrokenBarrierException e) {
         }
     }
-
     private void focusHelper(boolean idIsEmpty) {
         View parent = getView();
-		MainActivity main = (MainActivity)getActivity();
+        MainActivity main = (MainActivity)getActivity();
         main.setMenuFocusDown(idIsEmpty, android.R.id.list);
         main.setSearchAsFocusDown(idIsEmpty, parent,
-            android.R.id.list);
-    }
+                    android.R.id.list);
+        }
 
-    private void updateList() {
+    public void updateList() {
         List<Media> itemList = mMediaLibrary.getVideoItems();
 
         if (mThumbnailer != null)
@@ -444,7 +418,7 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
 
     public void setItemToUpdate(Media item) {
         mItemToUpdate = item;
-        mHandler.sendEmptyMessage(UPDATE_ITEM);
+        mHandler.sendEmptyMessage(VideoListHandler.UPDATE_ITEM);
     }
 
     public void setGroup(String prefix) {
@@ -464,25 +438,13 @@ public class VideoGridFragment extends SherlockGridFragment implements ISortable
         public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
 
-            if (action.equalsIgnoreCase(ACTION_SCAN_START)) {
+            if (action.equalsIgnoreCase(Util.ACTION_SCAN_START)) {
                 mLayoutFlipperLoading.setVisibility(View.VISIBLE);
                 mTextViewNomedia.setVisibility(View.INVISIBLE);
-            } else if (action.equalsIgnoreCase(ACTION_SCAN_STOP)) {
+            } else if (action.equalsIgnoreCase(Util.ACTION_SCAN_STOP)) {
                 mLayoutFlipperLoading.setVisibility(View.INVISIBLE);
                 mTextViewNomedia.setVisibility(View.VISIBLE);
             }
         }
     };
-
-    public static void actionScanStart() {
-        Intent intent = new Intent();
-        intent.setAction(ACTION_SCAN_START);
-        VLCApplication.getAppContext().sendBroadcast(intent);
-    }
-
-    public static void actionScanStop() {
-        Intent intent = new Intent();
-        intent.setAction(ACTION_SCAN_STOP);
-        VLCApplication.getAppContext().sendBroadcast(intent);
-    }
 }
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListHandler.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListHandler.java
new file mode 100644
index 0000000..74a6461
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListHandler.java
@@ -0,0 +1,29 @@
+package org.videolan.vlc.gui.video;
+
+import org.videolan.vlc.MediaLibrary;
+import org.videolan.vlc.util.WeakHandler;
+
+import android.os.Message;
+
+public class VideoListHandler extends WeakHandler<VideoBrowserInterface> {
+	public static final int UPDATE_ITEM = 0;
+    public static final int MEDIA_ITEMS_UPDATED = 100;
+
+	public VideoListHandler(VideoBrowserInterface owner) {
+		super(owner);
+	}
+
+	@Override
+	public void handleMessage(Message msg) {
+		VideoBrowserInterface owner = getOwner();
+		if(owner == null) return;
+
+		switch (msg.what) {
+		case UPDATE_ITEM:
+			owner.updateItem();
+			break;
+		case MediaLibrary.MEDIA_ITEMS_UPDATED:
+			owner.updateList();
+		}
+	}
+};
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/util/Util.java b/vlc-android/src/org/videolan/vlc/util/Util.java
index 879cfa1..0b5665f 100644
--- a/vlc-android/src/org/videolan/vlc/util/Util.java
+++ b/vlc-android/src/org/videolan/vlc/util/Util.java
@@ -32,6 +32,7 @@ import org.videolan.vlc.MediaLibrary;
 import org.videolan.vlc.VLCApplication;
 
 import android.content.Context;
+import android.content.Intent;
 import android.content.res.TypedArray;
 import android.text.TextUtils.TruncateAt;
 import android.util.DisplayMetrics;
@@ -41,6 +42,8 @@ import android.widget.Toast;
 
 public class Util {
     private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
+    public static final String ACTION_SCAN_START = "org.videolan.vlc.gui.ScanStart";
+    public static final String ACTION_SCAN_STOP = "org.videolan.vlc.gui.ScanStop";
 
     /** Print an on-screen message to alert the user */
     public static void toaster(Context context, int stringId) {
@@ -148,4 +151,16 @@ public class Util {
             }
         }
     }
+
+    public static void actionScanStart() {
+        Intent intent = new Intent();
+        intent.setAction(ACTION_SCAN_START);
+        VLCApplication.getAppContext().sendBroadcast(intent);
+    }
+
+    public static void actionScanStop() {
+        Intent intent = new Intent();
+        intent.setAction(ACTION_SCAN_STOP);
+        VLCApplication.getAppContext().sendBroadcast(intent);
+    }
 }
-- 
1.9.1



More information about the Android mailing list