mobile/android/base/home/PanelItemView.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 package org.mozilla.gecko.home;
     8 import org.mozilla.gecko.R;
     9 import org.mozilla.gecko.db.BrowserContract.HomeItems;
    10 import org.mozilla.gecko.home.HomeConfig.ItemType;
    12 import android.content.Context;
    13 import android.database.Cursor;
    14 import android.text.TextUtils;
    15 import android.view.LayoutInflater;
    16 import android.view.View;
    17 import android.widget.ImageView;
    18 import android.widget.LinearLayout;
    19 import android.widget.TextView;
    21 import com.squareup.picasso.Picasso;
    23 class PanelItemView extends LinearLayout {
    24     private final TextView title;
    25     private final TextView description;
    26     private final ImageView image;
    27     private final LinearLayout titleDescContainer;
    29     private PanelItemView(Context context, int layoutId) {
    30         super(context);
    32         LayoutInflater.from(context).inflate(layoutId, this);
    33         title = (TextView) findViewById(R.id.title);
    34         description = (TextView) findViewById(R.id.description);
    35         image = (ImageView) findViewById(R.id.image);
    36         titleDescContainer = (LinearLayout) findViewById(R.id.title_desc_container);
    37     }
    39     public void updateFromCursor(Cursor cursor) {
    40         int titleIndex = cursor.getColumnIndexOrThrow(HomeItems.TITLE);
    41         final String titleText = cursor.getString(titleIndex);
    43         // Only show title if the item has one
    44         final boolean hasTitle = !TextUtils.isEmpty(titleText);
    45         title.setVisibility(hasTitle ? View.VISIBLE : View.GONE);
    46         titleDescContainer.setVisibility(hasTitle ? View.VISIBLE : View.GONE);
    47         if (hasTitle) {
    48             title.setText(titleText);
    49         }
    51         int descriptionIndex = cursor.getColumnIndexOrThrow(HomeItems.DESCRIPTION);
    52         final String descriptionText = cursor.getString(descriptionIndex);
    54         // Only show description if the item has one
    55         final boolean hasDescription = !TextUtils.isEmpty(descriptionText);
    56         description.setVisibility(hasDescription ? View.VISIBLE : View.GONE);
    57         if (hasDescription) {
    58             description.setText(descriptionText);
    59         }
    61         titleDescContainer.setVisibility(hasTitle || hasDescription ? View.VISIBLE : View.GONE);
    63         int imageIndex = cursor.getColumnIndexOrThrow(HomeItems.IMAGE_URL);
    64         final String imageUrl = cursor.getString(imageIndex);
    66         // Only try to load the image if the item has define image URL
    67         final boolean hasImageUrl = !TextUtils.isEmpty(imageUrl);
    68         image.setVisibility(hasImageUrl ? View.VISIBLE : View.GONE);
    70         if (hasImageUrl) {
    71             Picasso.with(getContext())
    72                    .load(imageUrl)
    73                    .into(image);
    74         }
    75     }
    77     private static class ArticleItemView extends PanelItemView {
    78         private ArticleItemView(Context context) {
    79             super(context, R.layout.panel_article_item);
    80             setOrientation(LinearLayout.HORIZONTAL);
    81         }
    82     }
    84     private static class ImageItemView extends PanelItemView {
    85         private ImageItemView(Context context) {
    86             super(context, R.layout.panel_image_item);
    87             setOrientation(LinearLayout.VERTICAL);
    88         }
    89     }
    91     public static PanelItemView create(Context context, ItemType itemType) {
    92         switch(itemType) {
    93             case ARTICLE:
    94                 return new ArticleItemView(context);
    96             case IMAGE:
    97                 return new ImageItemView(context);
    99             default:
   100                 throw new IllegalArgumentException("Could not create panel item view from " + itemType);
   101         }
   102     }
   103 }

mercurial