|
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/. */ |
|
5 |
|
6 package org.mozilla.gecko.home; |
|
7 |
|
8 import org.mozilla.gecko.R; |
|
9 import org.mozilla.gecko.db.BrowserContract.HomeItems; |
|
10 import org.mozilla.gecko.home.HomeConfig.ItemType; |
|
11 |
|
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; |
|
20 |
|
21 import com.squareup.picasso.Picasso; |
|
22 |
|
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; |
|
28 |
|
29 private PanelItemView(Context context, int layoutId) { |
|
30 super(context); |
|
31 |
|
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 } |
|
38 |
|
39 public void updateFromCursor(Cursor cursor) { |
|
40 int titleIndex = cursor.getColumnIndexOrThrow(HomeItems.TITLE); |
|
41 final String titleText = cursor.getString(titleIndex); |
|
42 |
|
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 } |
|
50 |
|
51 int descriptionIndex = cursor.getColumnIndexOrThrow(HomeItems.DESCRIPTION); |
|
52 final String descriptionText = cursor.getString(descriptionIndex); |
|
53 |
|
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 } |
|
60 |
|
61 titleDescContainer.setVisibility(hasTitle || hasDescription ? View.VISIBLE : View.GONE); |
|
62 |
|
63 int imageIndex = cursor.getColumnIndexOrThrow(HomeItems.IMAGE_URL); |
|
64 final String imageUrl = cursor.getString(imageIndex); |
|
65 |
|
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); |
|
69 |
|
70 if (hasImageUrl) { |
|
71 Picasso.with(getContext()) |
|
72 .load(imageUrl) |
|
73 .into(image); |
|
74 } |
|
75 } |
|
76 |
|
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 } |
|
83 |
|
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 } |
|
90 |
|
91 public static PanelItemView create(Context context, ItemType itemType) { |
|
92 switch(itemType) { |
|
93 case ARTICLE: |
|
94 return new ArticleItemView(context); |
|
95 |
|
96 case IMAGE: |
|
97 return new ImageItemView(context); |
|
98 |
|
99 default: |
|
100 throw new IllegalArgumentException("Could not create panel item view from " + itemType); |
|
101 } |
|
102 } |
|
103 } |