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