Bug 1220928 - Make static viewType to ItemType converter. r=sebastian
authorChenxia Liu <liuche@mozilla.com>
Wed, 24 Feb 2016 16:13:18 -0800
changeset 291319 82af1bb19859da55d3ea73012d02a70d5b428c80
parent 291318 da52ef729a4ba7457bd456f0352f9117ea4cf2fa
child 291320 df8b8021d41939f362f013a70239d827d505dffd
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian
bugs1220928
milestone48.0a1
Bug 1220928 - Make static viewType to ItemType converter. r=sebastian MozReview-Commit-ID: CIOxLFaQIIo
mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryAdapter.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryAdapter.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryAdapter.java
@@ -3,29 +3,41 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 package org.mozilla.gecko.home;
 
 import android.support.v7.widget.RecyclerView;
 
 import android.content.Context;
 import android.database.Cursor;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.db.RemoteClient;
 
 import java.util.Collections;
 import java.util.List;
 
 public class CombinedHistoryAdapter extends RecyclerView.Adapter<CombinedHistoryItem> {
-    // These ordinal positions are used in CombinedHistoryAdapter as viewType.
+    private static final String LOGTAG = "GeckoCombinedHistAdapt";
     private enum ItemType {
-        CLIENT, HISTORY
+        CLIENT, HISTORY;
+
+        public static ItemType viewTypeToItemType(int viewType) {
+            if (viewType >= ItemType.values().length) {
+                Log.e(LOGTAG, "No corresponding ItemType!");
+            }
+            return ItemType.values()[viewType];
+        }
+
+        public static int itemTypeToViewType(ItemType itemType) {
+            return itemType.ordinal();
+        }
     }
 
     private List<RemoteClient> remoteClients = Collections.emptyList();
     private Cursor historyCursor;
     private final Context context;
 
     public CombinedHistoryAdapter(Context context) {
         super();
@@ -41,50 +53,55 @@ public class CombinedHistoryAdapter exte
         historyCursor = history;
         notifyDataSetChanged();
     }
 
     private int transformPosition(ItemType type, int position) {
         if (type == ItemType.CLIENT) {
             return position;
         } else {
-            return position - (remoteClients == null ? 0 : remoteClients.size());
+            return position - remoteClients.size();
         }
     }
 
     @Override
     public CombinedHistoryItem onCreateViewHolder(ViewGroup viewGroup, int viewType) {
         final LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
         final View view;
 
-        if (viewType == ItemType.CLIENT.ordinal()) {
-            view = inflater.inflate(R.layout.home_remote_tabs_group, viewGroup, false);
-            return new CombinedHistoryItem.ClientItem(view);
-        } else {
-            view = inflater.inflate(R.layout.home_item_row, viewGroup, false);
-            return new CombinedHistoryItem.HistoryItem(view);
+        final ItemType itemType = ItemType.viewTypeToItemType(viewType);
+
+        switch (itemType) {
+            case CLIENT:
+                view = inflater.inflate(R.layout.home_remote_tabs_group, viewGroup, false);
+                return new CombinedHistoryItem.ClientItem(view);
+            case HISTORY:
+                view = inflater.inflate(R.layout.home_item_row, viewGroup, false);
+                return new CombinedHistoryItem.HistoryItem(view);
+            default:
+                throw new IllegalArgumentException("Unexpected Home Panel item type");
         }
     }
 
     @Override
     public int getItemViewType(int position) {
         final int numClients = remoteClients.size();
-        return (position < numClients) ? ItemType.CLIENT.ordinal() : ItemType.HISTORY.ordinal();
+        return (position < numClients) ? ItemType.itemTypeToViewType(ItemType.CLIENT) : ItemType.itemTypeToViewType(ItemType.HISTORY);
     }
 
     @Override
     public int getItemCount() {
         final int remoteSize = remoteClients.size();
         final int historySize = historyCursor == null ? 0 : historyCursor.getCount();
         return remoteSize + historySize;
     }
 
     @Override
     public void onBindViewHolder(CombinedHistoryItem viewHolder, int position) {
-        final ItemType itemType = ItemType.values()[getItemViewType(position)];
+        final ItemType itemType = ItemType.viewTypeToItemType(getItemViewType(position));
         final int localPosition = transformPosition(itemType, position);
 
         switch (itemType) {
             case CLIENT:
                 final CombinedHistoryItem.ClientItem clientItem = (CombinedHistoryItem.ClientItem) viewHolder;
                 final RemoteClient client = remoteClients.get(localPosition);
                 clientItem.bind(client, context);
                 break;