Bug 966022 - Introduce ItemType into HomeConfig/Home.jsm (r=margaret)
authorLucas Rocha <lucasr@mozilla.com>
Wed, 19 Feb 2014 17:36:59 +0000
changeset 169934 49ce1474b9afd3abe2412a6a87e8eb59ff486dfa
parent 169933 393c539741f2fc982824f566094ef85857359ca7
child 169935 90be3775d43de4eab51967a0874affd8638bf6f6
push id270
push userpvanderbeken@mozilla.com
push dateThu, 06 Mar 2014 09:24:21 +0000
reviewersmargaret
bugs966022
milestone30.0a1
Bug 966022 - Introduce ItemType into HomeConfig/Home.jsm (r=margaret)
mobile/android/base/home/HomeConfig.java
mobile/android/modules/Home.jsm
--- a/mobile/android/base/home/HomeConfig.java
+++ b/mobile/android/base/home/HomeConfig.java
@@ -464,16 +464,68 @@ public final class HomeConfig {
 
             @Override
             public ViewType[] newArray(final int size) {
                 return new ViewType[size];
             }
         };
     }
 
+    public static enum ItemType implements Parcelable {
+        ARTICLE("article"),
+        IMAGE("image");
+
+        private final String mId;
+
+        ItemType(String id) {
+            mId = id;
+        }
+
+        public static ItemType fromId(String id) {
+            if (id == null) {
+                throw new IllegalArgumentException("Could not convert null String to ItemType");
+            }
+
+            for (ItemType itemType : ItemType.values()) {
+                if (TextUtils.equals(itemType.mId, id.toLowerCase())) {
+                    return itemType;
+                }
+            }
+
+            throw new IllegalArgumentException("Could not convert String id to ItemType");
+        }
+
+        @Override
+        public String toString() {
+            return mId;
+        }
+
+        @Override
+        public int describeContents() {
+            return 0;
+        }
+
+        @Override
+        public void writeToParcel(Parcel dest, int flags) {
+            dest.writeInt(ordinal());
+        }
+
+        public static final Creator<ItemType> CREATOR = new Creator<ItemType>() {
+            @Override
+            public ItemType createFromParcel(final Parcel source) {
+                return ItemType.values()[source.readInt()];
+            }
+
+            @Override
+            public ItemType[] newArray(final int size) {
+                return new ItemType[size];
+            }
+        };
+    }
+
     public static enum ItemHandler implements Parcelable {
         BROWSER("browser"),
         INTENT("intent");
 
         private final String mId;
 
         ItemHandler(String id) {
             mId = id;
@@ -519,100 +571,116 @@ public final class HomeConfig {
                 return new ItemHandler[size];
             }
         };
     }
 
     public static class ViewConfig implements Parcelable {
         private final ViewType mType;
         private final String mDatasetId;
+        private final ItemType mItemType;
         private final ItemHandler mItemHandler;
 
         private static final String JSON_KEY_TYPE = "type";
         private static final String JSON_KEY_DATASET = "dataset";
+        private static final String JSON_KEY_ITEM_TYPE = "itemType";
         private static final String JSON_KEY_ITEM_HANDLER = "itemHandler";
 
         public ViewConfig(JSONObject json) throws JSONException, IllegalArgumentException {
             mType = ViewType.fromId(json.getString(JSON_KEY_TYPE));
             mDatasetId = json.getString(JSON_KEY_DATASET);
+            mItemType = ItemType.fromId(json.getString(JSON_KEY_ITEM_TYPE));
             mItemHandler = ItemHandler.fromId(json.getString(JSON_KEY_ITEM_HANDLER));
 
             validate();
         }
 
         @SuppressWarnings("unchecked")
         public ViewConfig(Parcel in) {
             mType = (ViewType) in.readParcelable(getClass().getClassLoader());
             mDatasetId = in.readString();
+            mItemType = (ItemType) in.readParcelable(getClass().getClassLoader());
             mItemHandler = (ItemHandler) in.readParcelable(getClass().getClassLoader());
 
             validate();
         }
 
         public ViewConfig(ViewConfig viewConfig) {
             mType = viewConfig.mType;
             mDatasetId = viewConfig.mDatasetId;
+            mItemType = viewConfig.mItemType;
             mItemHandler = viewConfig.mItemHandler;
 
             validate();
         }
 
-        public ViewConfig(ViewType type, String datasetId, ItemHandler itemHandler) {
+        public ViewConfig(ViewType type, String datasetId, ItemType itemType, ItemHandler itemHandler) {
             mType = type;
             mDatasetId = datasetId;
+            mItemType = itemType;
             mItemHandler = itemHandler;
 
             validate();
         }
 
         private void validate() {
             if (mType == null) {
                 throw new IllegalArgumentException("Can't create ViewConfig with null type");
             }
 
             if (TextUtils.isEmpty(mDatasetId)) {
                 throw new IllegalArgumentException("Can't create ViewConfig with empty dataset ID");
             }
 
+            if (mItemType == null) {
+                throw new IllegalArgumentException("Can't create ViewConfig with null item type");
+            }
+
             if (mItemHandler == null) {
                 throw new IllegalArgumentException("Can't create ViewConfig with null item handler");
             }
         }
 
         public ViewType getType() {
             return mType;
         }
 
         public String getDatasetId() {
             return mDatasetId;
         }
 
+        public ItemType getItemType() {
+            return mItemType;
+        }
+
         public ItemHandler getItemHandler() {
             return mItemHandler;
         }
 
         public JSONObject toJSON() throws JSONException {
             final JSONObject json = new JSONObject();
 
             json.put(JSON_KEY_TYPE, mType.toString());
             json.put(JSON_KEY_DATASET, mDatasetId);
+            json.put(JSON_KEY_ITEM_TYPE, mItemType.toString());
             json.put(JSON_KEY_ITEM_HANDLER, mItemHandler.toString());
 
             return json;
         }
 
         @Override
         public int describeContents() {
             return 0;
         }
 
         @Override
         public void writeToParcel(Parcel dest, int flags) {
             dest.writeParcelable(mType, 0);
             dest.writeString(mDatasetId);
+            dest.writeParcelable(mItemType, 0);
             dest.writeParcelable(mItemHandler, 0);
         }
 
         public static final Creator<ViewConfig> CREATOR = new Creator<ViewConfig>() {
             @Override
             public ViewConfig createFromParcel(final Parcel in) {
                 return new ViewConfig(in);
             }
--- a/mobile/android/modules/Home.jsm
+++ b/mobile/android/modules/Home.jsm
@@ -223,16 +223,22 @@ let HomePanels = (function () {
     }),
 
     // Valid actions for a panel.
     Action: Object.freeze({
       INSTALL: "install",
       REFRESH: "refresh"
     }),
 
+    // Valid item types for a panel view.
+    Item: Object.freeze({
+      ARTICLE: "article",
+      IMAGE: "image"
+    }),
+
     // Valid item handlers for a panel view.
     ItemHandler: Object.freeze({
       BROWSER: "browser",
       INTENT: "intent"
     }),
 
     add: function(options) {
       let panel = new Panel(options);
@@ -252,16 +258,28 @@ let HomePanels = (function () {
         throw "Home.panels: Invalid layout for panel: panel.id = " + panel.id + ", panel.layout =" + panel.layout;
       }
 
       for (let view of panel.views) {
         if (!_valueExists(this.View, view.type)) {
           throw "Home.panels: Invalid view type: panel.id = " + panel.id + ", view.type = " + view.type;
         }
 
+        if (!view.itemType) {
+          if (view.type == this.View.LIST) {
+            // Use ARTICLE item type by default in LIST views
+            view.itemType = this.Item.ARTICLE;
+          } else if (view.type == this.View.GRID) {
+            // Use IMAGE item type by default in GRID views
+            view.itemType = this.Item.IMAGE;
+          }
+        } else if (!_valueExists(this.Item, view.itemType)) {
+          throw "Home.panels: Invalid item type: panel.id = " + panel.id + ", view.itemType = " + view.itemType;
+        }
+
         if (!view.itemHandler) {
           // Use BROWSER item handler by default
           view.itemHandler = this.ItemHandler.BROWSER;
         } else if (!_valueExists(this.ItemHandler, view.itemHandler)) {
           throw "Home.panels: Invalid item handler: panel.id = " + panel.id + ", view.itemHandler = " + view.itemHandler;
         }
 
         if (!view.dataset) {