michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.home; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: import org.mozilla.gecko.db.BrowserContract.HomeItems; michael@0: import org.mozilla.gecko.home.HomeConfig.ItemType; michael@0: michael@0: import android.content.Context; michael@0: import android.database.Cursor; michael@0: import android.text.TextUtils; michael@0: import android.view.LayoutInflater; michael@0: import android.view.View; michael@0: import android.widget.ImageView; michael@0: import android.widget.LinearLayout; michael@0: import android.widget.TextView; michael@0: michael@0: import com.squareup.picasso.Picasso; michael@0: michael@0: class PanelItemView extends LinearLayout { michael@0: private final TextView title; michael@0: private final TextView description; michael@0: private final ImageView image; michael@0: private final LinearLayout titleDescContainer; michael@0: michael@0: private PanelItemView(Context context, int layoutId) { michael@0: super(context); michael@0: michael@0: LayoutInflater.from(context).inflate(layoutId, this); michael@0: title = (TextView) findViewById(R.id.title); michael@0: description = (TextView) findViewById(R.id.description); michael@0: image = (ImageView) findViewById(R.id.image); michael@0: titleDescContainer = (LinearLayout) findViewById(R.id.title_desc_container); michael@0: } michael@0: michael@0: public void updateFromCursor(Cursor cursor) { michael@0: int titleIndex = cursor.getColumnIndexOrThrow(HomeItems.TITLE); michael@0: final String titleText = cursor.getString(titleIndex); michael@0: michael@0: // Only show title if the item has one michael@0: final boolean hasTitle = !TextUtils.isEmpty(titleText); michael@0: title.setVisibility(hasTitle ? View.VISIBLE : View.GONE); michael@0: titleDescContainer.setVisibility(hasTitle ? View.VISIBLE : View.GONE); michael@0: if (hasTitle) { michael@0: title.setText(titleText); michael@0: } michael@0: michael@0: int descriptionIndex = cursor.getColumnIndexOrThrow(HomeItems.DESCRIPTION); michael@0: final String descriptionText = cursor.getString(descriptionIndex); michael@0: michael@0: // Only show description if the item has one michael@0: final boolean hasDescription = !TextUtils.isEmpty(descriptionText); michael@0: description.setVisibility(hasDescription ? View.VISIBLE : View.GONE); michael@0: if (hasDescription) { michael@0: description.setText(descriptionText); michael@0: } michael@0: michael@0: titleDescContainer.setVisibility(hasTitle || hasDescription ? View.VISIBLE : View.GONE); michael@0: michael@0: int imageIndex = cursor.getColumnIndexOrThrow(HomeItems.IMAGE_URL); michael@0: final String imageUrl = cursor.getString(imageIndex); michael@0: michael@0: // Only try to load the image if the item has define image URL michael@0: final boolean hasImageUrl = !TextUtils.isEmpty(imageUrl); michael@0: image.setVisibility(hasImageUrl ? View.VISIBLE : View.GONE); michael@0: michael@0: if (hasImageUrl) { michael@0: Picasso.with(getContext()) michael@0: .load(imageUrl) michael@0: .into(image); michael@0: } michael@0: } michael@0: michael@0: private static class ArticleItemView extends PanelItemView { michael@0: private ArticleItemView(Context context) { michael@0: super(context, R.layout.panel_article_item); michael@0: setOrientation(LinearLayout.HORIZONTAL); michael@0: } michael@0: } michael@0: michael@0: private static class ImageItemView extends PanelItemView { michael@0: private ImageItemView(Context context) { michael@0: super(context, R.layout.panel_image_item); michael@0: setOrientation(LinearLayout.VERTICAL); michael@0: } michael@0: } michael@0: michael@0: public static PanelItemView create(Context context, ItemType itemType) { michael@0: switch(itemType) { michael@0: case ARTICLE: michael@0: return new ArticleItemView(context); michael@0: michael@0: case IMAGE: michael@0: return new ImageItemView(context); michael@0: michael@0: default: michael@0: throw new IllegalArgumentException("Could not create panel item view from " + itemType); michael@0: } michael@0: } michael@0: }