[Android] Refactor cardview

Geoffrey Métais git at videolan.org
Fri Dec 5 15:41:16 CET 2014


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Dec  5 15:40:26 2014 +0100| [d18c2c2d7184a1d6e12943ee35e70af255165fc2] | committer: Geoffrey Métais

Refactor cardview

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=d18c2c2d7184a1d6e12943ee35e70af255165fc2
---

 .../src/org/videolan/vlc/gui/tv/CardPresenter.java |   61 ++++++++++----------
 .../src/org/videolan/vlc/gui/tv/GridFragment.java  |    2 +-
 .../org/videolan/vlc/gui/tv/MainTvActivity.java    |    6 +-
 .../org/videolan/vlc/gui/tv/SearchFragment.java    |    2 +-
 4 files changed, 34 insertions(+), 37 deletions(-)

diff --git a/vlc-android/tv/src/org/videolan/vlc/gui/tv/CardPresenter.java b/vlc-android/tv/src/org/videolan/vlc/gui/tv/CardPresenter.java
index 132d419..706513f 100644
--- a/vlc-android/tv/src/org/videolan/vlc/gui/tv/CardPresenter.java
+++ b/vlc-android/tv/src/org/videolan/vlc/gui/tv/CardPresenter.java
@@ -39,12 +39,20 @@ public class CardPresenter extends Presenter {
     private static final String TAG = "CardPresenter";
 
     private static Context sContext;
-    private static int CARD_WIDTH = 0;
+    private static Resources mRes;
+    private static int CARD_WIDTH;
     private static int CARD_HEIGHT = 0;
-    private static Resources sResources;
     private static MediaDatabase sMediaDatabase = MediaDatabase.getInstance();
     private static Drawable sDefaultCardImage;
 
+    public CardPresenter(Context context){
+        sContext = context;
+        mRes = sContext.getResources();
+        sDefaultCardImage = mRes.getDrawable(R.drawable.cone);
+        CARD_WIDTH = mRes.getDimensionPixelSize(R.dimen.tv_card_width);
+        CARD_HEIGHT = mRes.getDimensionPixelSize(R.dimen.tv_card_height);
+    }
+
     static class ViewHolder extends Presenter.ViewHolder {
         private ImageCardView mCardView;
 
@@ -64,7 +72,7 @@ public class CardPresenter extends Presenter {
             else if (media.getType() == Media.TYPE_VIDEO)
                 picture = sMediaDatabase.getPicture(sContext, media.getLocation());
             if (picture != null && picture.getByteCount() > 4)
-                mCardView.setMainImage(new BitmapDrawable(sResources, picture));
+                mCardView.setMainImage(new BitmapDrawable(mRes, picture));
             else
                 mCardView.setMainImage(sDefaultCardImage);
         }
@@ -76,20 +84,11 @@ public class CardPresenter extends Presenter {
 
     @Override
     public ViewHolder onCreateViewHolder(ViewGroup parent) {
-        sContext = parent.getContext();
-        sResources = sContext.getResources();
-        sDefaultCardImage = sContext.getResources().getDrawable(R.drawable.cone);
-        if (CARD_WIDTH == 0) {
-            CARD_WIDTH = sResources.getDimensionPixelSize(
-                    R.dimen.tv_card_width);
-            CARD_HEIGHT = sResources.getDimensionPixelSize(
-                    R.dimen.tv_card_height);
-        }
 
         ImageCardView cardView = new ImageCardView(sContext);
         cardView.setFocusable(true);
         cardView.setFocusableInTouchMode(true);
-        cardView.setBackgroundColor(sContext.getResources().getColor(R.color.lb_details_overview_bg_color));
+        cardView.setBackgroundColor(mRes.getColor(R.color.lb_details_overview_bg_color));
         return new ViewHolder(cardView);
     }
 
@@ -103,7 +102,7 @@ public class CardPresenter extends Presenter {
             holder.mCardView.setTitleText(media.getTitle());
             holder.mCardView.setContentText(media.getDescription());
             if (media.getType() == Media.TYPE_GROUP)
-                holder.updateCardViewImage(holder.view.getContext().getResources().getDrawable(
+                holder.updateCardViewImage(mRes.getDrawable(
                         R.drawable.ic_video_collection_big));
             else
                 holder.updateCardViewImage(media);
@@ -114,25 +113,8 @@ public class CardPresenter extends Presenter {
             holder.mCardView.setContentText(listItem.mSubTitle);
             holder.updateCardViewImage(media);
         } else if (item instanceof String){
-            Resources res = holder.view.getContext().getResources();
             holder.mCardView.setTitleText((String) item);
-            if (res.getString(R.string.preferences).equals(item.toString()))
-                holder.updateCardViewImage(res.getDrawable(
-                        R.drawable.ic_menu_preferences_big));
-            else if (res.getString(R.string.artists).equals(item.toString()))
-                holder.updateCardViewImage(res.getDrawable(
-                        R.drawable.ic_artist_big));
-            else if (res.getString(R.string.albums).equals(item.toString()))
-                holder.updateCardViewImage(res.getDrawable(
-                        R.drawable.ic_album_big));
-            else if (res.getString(R.string.genres).equals(item.toString()))
-                holder.updateCardViewImage(res.getDrawable(
-                        R.drawable.ic_genre_big));
-            else if (res.getString(R.string.songs).equals(item.toString()))
-                holder.updateCardViewImage(res.getDrawable(
-                        R.drawable.ic_song_big));
-            else
-                holder.updateCardViewImage(sDefaultCardImage);
+            holder.updateCardViewImage(getBitmap(mRes, item.toString()));
         }
     }
 
@@ -144,4 +126,19 @@ public class CardPresenter extends Presenter {
     public void onViewAttachedToWindow(Presenter.ViewHolder viewHolder) {
         // TODO?
     }
+
+    private Drawable getBitmap(Resources res, String item){
+        if (res.getString(R.string.preferences).equals(item.toString()))
+            return res.getDrawable( R.drawable.ic_menu_preferences_big);
+        else if (res.getString(R.string.artists).equals(item.toString()))
+            return res.getDrawable(R.drawable.ic_artist_big);
+        else if (res.getString(R.string.albums).equals(item.toString()))
+            return res.getDrawable(R.drawable.ic_album_big);
+        else if (res.getString(R.string.genres).equals(item.toString()))
+            return res.getDrawable(R.drawable.ic_genre_big);
+        else if (res.getString(R.string.songs).equals(item.toString()))
+            return res.getDrawable(R.drawable.ic_song_big);
+        else
+            return sDefaultCardImage;
+    }
 }
diff --git a/vlc-android/tv/src/org/videolan/vlc/gui/tv/GridFragment.java b/vlc-android/tv/src/org/videolan/vlc/gui/tv/GridFragment.java
index 4b89d14..cee23c4 100644
--- a/vlc-android/tv/src/org/videolan/vlc/gui/tv/GridFragment.java
+++ b/vlc-android/tv/src/org/videolan/vlc/gui/tv/GridFragment.java
@@ -91,7 +91,7 @@ public class GridFragment extends VerticalGridFragment implements VideoBrowserIn
         VerticalGridPresenter gridPresenter = new VerticalGridPresenter();
         gridPresenter.setNumberOfColumns(NUM_COLUMNS);
         setGridPresenter(gridPresenter);
-        mAdapter = new ArrayObjectAdapter(new CardPresenter());
+        mAdapter = new ArrayObjectAdapter(new CardPresenter(mContext));
         setAdapter(mAdapter);
     }
 
diff --git a/vlc-android/tv/src/org/videolan/vlc/gui/tv/MainTvActivity.java b/vlc-android/tv/src/org/videolan/vlc/gui/tv/MainTvActivity.java
index 7e87f63..8568946 100644
--- a/vlc-android/tv/src/org/videolan/vlc/gui/tv/MainTvActivity.java
+++ b/vlc-android/tv/src/org/videolan/vlc/gui/tv/MainTvActivity.java
@@ -245,7 +245,7 @@ public class MainTvActivity extends Activity implements VideoBrowserInterface {
                 size = videoList.size();
                 mVideoIndex = new HashMap<String, Integer>(size);
                 mVideoAdapter = new ArrayObjectAdapter(
-                        new CardPresenter());
+                        new CardPresenter(mContext));
                 if (NUM_ITEMS_PREVIEW < size)
                     size = NUM_ITEMS_PREVIEW;
                 for (int i = 0 ; i < size ; ++i) {
@@ -262,7 +262,7 @@ public class MainTvActivity extends Activity implements VideoBrowserInterface {
                 mRowsAdapter.add(new ListRow(header, mVideoAdapter));
             }
 
-            mCategoriesAdapter = new ArrayObjectAdapter(new CardPresenter());
+            mCategoriesAdapter = new ArrayObjectAdapter(new CardPresenter(mContext));
             mCategoriesAdapter.add(getString(R.string.artists));
             mCategoriesAdapter.add(getString(R.string.albums));
             mCategoriesAdapter.add(getString(R.string.genres));
@@ -270,7 +270,7 @@ public class MainTvActivity extends Activity implements VideoBrowserInterface {
             HeaderItem header = new HeaderItem(HEADER_CATEGORIES, getString(R.string.audio), null);
             mRowsAdapter.add(new ListRow(header, mCategoriesAdapter));
 
-            mOtherAdapter = new ArrayObjectAdapter(new CardPresenter());
+            mOtherAdapter = new ArrayObjectAdapter(new CardPresenter(mContext));
             mOtherAdapter.add(getString(R.string.preferences));
             header = new HeaderItem(HEADER_MISC, getString(R.string.other), null);
             mRowsAdapter.add(new ListRow(header, mOtherAdapter));
diff --git a/vlc-android/tv/src/org/videolan/vlc/gui/tv/SearchFragment.java b/vlc-android/tv/src/org/videolan/vlc/gui/tv/SearchFragment.java
index e0be766..a63fc36 100644
--- a/vlc-android/tv/src/org/videolan/vlc/gui/tv/SearchFragment.java
+++ b/vlc-android/tv/src/org/videolan/vlc/gui/tv/SearchFragment.java
@@ -86,7 +86,7 @@ implements android.support.v17.leanback.app.SearchFragment.SearchResultProvider
 
     private void loadRows(String query, int type) {
         ArrayList<Media> mediaList = MediaLibrary.getInstance().searchMedia(query, type);
-        final ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new CardPresenter());
+        final ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(new CardPresenter(mActivity));
         listRowAdapter.addAll(0, mediaList);
         mHandler.post(new Runnable() {
             @Override



More information about the Android mailing list