Bug 966022 - Use ItemType to define panel view items (r=margaret)

This commit is contained in:
Lucas Rocha 2014-02-19 17:37:00 +00:00
Родитель ecf495b10b
Коммит 6afca2db56
16 изменённых файлов: 248 добавлений и 206 удалений

Просмотреть файл

@ -1,60 +0,0 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.home;
import java.net.MalformedURLException;
import java.net.URL;
import org.mozilla.gecko.db.BrowserContract.HomeItems;
import org.mozilla.gecko.favicons.Favicons;
import org.mozilla.gecko.R;
import com.squareup.picasso.Picasso;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.FrameLayout;
import android.widget.TextView;
public class PanelGridItemView extends FrameLayout {
private static final String LOGTAG = "GeckoPanelGridItemView";
private final ImageView mThumbnailView;
public PanelGridItemView(Context context) {
this(context, null);
}
public PanelGridItemView(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.panelGridItemViewStyle);
}
public PanelGridItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater.from(context).inflate(R.layout.panel_grid_item_view, this);
mThumbnailView = (ImageView) findViewById(R.id.image);
}
public void updateFromCursor(Cursor cursor) {
int imageIndex = cursor.getColumnIndexOrThrow(HomeItems.IMAGE_URL);
final String imageUrl = cursor.getString(imageIndex);
Picasso.with(getContext())
.load(imageUrl)
.into(mThumbnailView);
}
}

Просмотреть файл

@ -29,13 +29,13 @@ public class PanelGridView extends GridView
private static final String LOGTAG = "GeckoPanelGridView";
private final ViewConfig mViewConfig;
private final PanelGridViewAdapter mAdapter;
private final PanelViewAdapter mAdapter;
protected OnUrlOpenListener mUrlOpenListener;
public PanelGridView(Context context, ViewConfig viewConfig) {
super(context, null, R.attr.panelGridViewStyle);
mViewConfig = viewConfig;
mAdapter = new PanelGridViewAdapter(context);
mAdapter = new PanelViewAdapter(context, viewConfig.getItemType());
setAdapter(mAdapter);
setOnItemClickListener(new PanelGridItemClickListener());
}
@ -56,24 +56,6 @@ public class PanelGridView extends GridView
mUrlOpenListener = listener;
}
private class PanelGridViewAdapter extends CursorAdapter {
public PanelGridViewAdapter(Context context) {
super(context, null, 0);
}
@Override
public void bindView(View bindView, Context context, Cursor cursor) {
final PanelGridItemView item = (PanelGridItemView) bindView;
item.updateFromCursor(cursor);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return new PanelGridItemView(context);
}
}
private class PanelGridItemClickListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Просмотреть файл

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

Просмотреть файл

@ -1,64 +0,0 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.home;
import org.mozilla.gecko.R;
import org.mozilla.gecko.db.BrowserContract.HomeItems;
import com.squareup.picasso.Picasso;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
public class PanelListRow extends TwoLineRow {
private final ImageView mIcon;
public PanelListRow(Context context) {
this(context, null);
}
public PanelListRow(Context context, AttributeSet attrs) {
super(context, attrs);
mIcon = (ImageView) findViewById(R.id.icon);
}
@Override
public void updateFromCursor(Cursor cursor) {
if (cursor == null) {
return;
}
// XXX: This will have to be updated once we come up with the
// final schema for Panel datasets (see bug 942288).
int titleIndex = cursor.getColumnIndexOrThrow(HomeItems.TITLE);
final String title = cursor.getString(titleIndex);
setTitle(title);
int descriptionIndex = cursor.getColumnIndexOrThrow(HomeItems.DESCRIPTION);
final String description = cursor.getString(descriptionIndex);
setDescription(description);
int imageIndex = cursor.getColumnIndexOrThrow(HomeItems.IMAGE_URL);
final String imageUrl = cursor.getString(imageIndex);
final boolean hasImageUrl = !TextUtils.isEmpty(imageUrl);
mIcon.setVisibility(hasImageUrl ? View.VISIBLE : View.GONE);
if (hasImageUrl) {
Picasso.with(getContext())
.load(imageUrl)
.error(R.drawable.favicon)
.into(mIcon);
}
}
}

Просмотреть файл

@ -29,13 +29,13 @@ public class PanelListView extends HomeListView
private static final String LOGTAG = "GeckoPanelListView";
private final PanelListAdapter mAdapter;
private final PanelViewAdapter mAdapter;
private final ViewConfig mViewConfig;
public PanelListView(Context context, ViewConfig viewConfig) {
super(context);
mViewConfig = viewConfig;
mAdapter = new PanelListAdapter(context);
mAdapter = new PanelViewAdapter(context, viewConfig.getItemType());
setAdapter(mAdapter);
setOnItemClickListener(new PanelListItemClickListener());
}
@ -46,23 +46,6 @@ public class PanelListView extends HomeListView
mAdapter.swapCursor(cursor);
}
private class PanelListAdapter extends CursorAdapter {
public PanelListAdapter(Context context) {
super(context, null, 0);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final PanelListRow row = (PanelListRow) view;
row.updateFromCursor(cursor);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(parent.getContext()).inflate(R.layout.panel_list_row, parent, false);
}
}
private class PanelListItemClickListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Просмотреть файл

@ -0,0 +1,37 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.home;
import org.mozilla.gecko.R;
import org.mozilla.gecko.home.HomeConfig.ItemType;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import android.view.ViewGroup;
class PanelViewAdapter extends CursorAdapter {
private final Context mContext;
private final ItemType mItemType;
public PanelViewAdapter(Context context, ItemType itemType) {
super(context, null, 0);
mContext = context;
mItemType = itemType;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final PanelItemView item = (PanelItemView) view;
item.updateFromCursor(cursor);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return PanelItemView.create(mContext, mItemType);
}
}

Просмотреть файл

@ -233,12 +233,12 @@ gbjar.sources += [
'home/LastTabsPanel.java',
'home/MostRecentPanel.java',
'home/MultiTypeCursorAdapter.java',
'home/PanelGridItemView.java',
'home/PanelGridView.java',
'home/PanelItemView.java',
'home/PanelLayout.java',
'home/PanelListRow.java',
'home/PanelListView.java',
'home/PanelManager.java',
'home/PanelViewAdapter.java',
'home/PinSiteDialog.java',
'home/ReadingListPanel.java',
'home/SearchEngine.java',

Просмотреть файл

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/image"
android:layout_width="54dp"
android:layout_height="44dp"
android:layout_marginTop="10dip"
android:layout_marginLeft="10dip"
android:scaleType="centerCrop"/>
<LinearLayout android:id="@+id/title_desc_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:paddingBottom="5dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:minHeight="@dimen/page_row_height"
android:orientation="vertical">
<TextView android:id="@+id/title"
style="@style/Widget.PanelItemView.Title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"/>
<TextView android:id="@+id/description"
style="@style/Widget.PanelItemView.Description"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:maxLength="1024"/>
</LinearLayout>
</merge>

Просмотреть файл

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:gecko="http://schemas.android.com/apk/res-auto">
<org.mozilla.gecko.widget.SquaredImageView android:id="@+id/image"
style="@style/Widget.PanelGridItemImageView" />
</merge>

Просмотреть файл

@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<org.mozilla.gecko.widget.SquaredImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:background="@color/panel_image_item_background"/>
<LinearLayout android:id="@+id/title_desc_container"
android:layout_width="fill_parent"
android:layout_height="@dimen/page_row_height"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:orientation="vertical">
<TextView android:id="@+id/title"
style="@style/Widget.PanelItemView.Title"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:singleLine="true"/>
<TextView android:id="@+id/description"
style="@style/Widget.PanelItemView.Description"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="3dp"
android:singleLine="true"
android:maxLength="1024"/>
</LinearLayout>
</merge>

Просмотреть файл

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<org.mozilla.gecko.home.PanelListRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="@dimen/page_row_height"
android:paddingLeft="10dip"
android:minHeight="@dimen/page_row_height"/>

Просмотреть файл

@ -48,7 +48,6 @@
<item name="topSitesGridItemViewStyle">@style/Widget.TopSitesGridItemView</item>
<item name="topSitesGridViewStyle">@style/Widget.TopSitesGridView</item>
<item name="panelGridViewStyle">@style/Widget.PanelGridView</item>
<item name="panelGridItemViewStyle">@style/Widget.PanelGridItemView</item>
<item name="topSitesThumbnailViewStyle">@style/Widget.TopSitesThumbnailView</item>
<item name="homeListViewStyle">@style/Widget.HomeListView</item>
<item name="geckoMenuListViewStyle">@style/Widget.GeckoMenuListView</item>

Просмотреть файл

@ -41,9 +41,6 @@
<!-- Style for the PanelGridView -->
<attr name="panelGridViewStyle" format="reference" />
<!-- Default style for the PanelGridItemView -->
<attr name="panelGridItemViewStyle" format="reference" />
<!-- Default style for the TopSitesGridView -->
<attr name="topSitesGridViewStyle" format="reference" />

Просмотреть файл

@ -90,6 +90,6 @@
<color name="home_last_tab_bar_bg">#FFF5F7F9</color>
<color name="panel_grid_item_image_background">#D1D9E1</color>
<color name="panel_image_item_background">#D1D9E1</color>
</resources>

Просмотреть файл

@ -130,6 +130,21 @@
<item name="android:drawableLeft">@drawable/bookmark_folder</item>
</style>
<style name="Widget.PanelItemView" />
<style name="Widget.PanelItemView.Title">
<item name="android:textAppearance">@style/TextAppearance.Widget.Home.ItemTitle</item>
<item name="android:maxLines">2</item>
<item name="android:ellipsize">end</item>
</style>
<style name="Widget.PanelItemView.Description">
<item name="android:textAppearance">@style/TextAppearance.Widget.Home.ItemDescription</item>
<item name="android:includeFontPadding">false</item>
<item name="android:maxLines">2</item>
<item name="android:ellipsize">end</item>
</style>
<style name="Widget.HomeGridView" parent="Widget.GridView">
<item name="android:padding">7dp</item>
<item name="android:horizontalSpacing">0dp</item>
@ -156,19 +171,6 @@
<item name="android:verticalSpacing">2dp</item>
</style>
<style name="Widget.PanelGridItemView">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="Widget.PanelGridItemImageView">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:scaleType">centerCrop</item>
<item name="android:adjustViewBounds">true</item>
<item name="android:background">@color/panel_grid_item_image_background</item>
</style>
<style name="Widget.BookmarkItemView" parent="Widget.TwoLineRow"/>
<style name="Widget.BookmarksListView" parent="Widget.HomeListView"/>

Просмотреть файл

@ -81,7 +81,6 @@
<item name="topSitesGridItemViewStyle">@style/Widget.TopSitesGridItemView</item>
<item name="topSitesGridViewStyle">@style/Widget.TopSitesGridView</item>
<item name="panelGridViewStyle">@style/Widget.PanelGridView</item>
<item name="panelGridItemViewStyle">@style/Widget.PanelGridItemView</item>
<item name="topSitesThumbnailViewStyle">@style/Widget.TopSitesThumbnailView</item>
<item name="homeListViewStyle">@style/Widget.HomeListView</item>
<item name="geckoMenuListViewStyle">@style/Widget.GeckoMenuListView</item>