Commit a008c590 authored by Justin DeWitt's avatar Justin DeWitt Committed by Commit Bot

[EoS] Adds requests for category images.

* If no catalog is present, requests one from the network and displays
  default images.  When request finishes, updates the category images.
* Updates category images right away if not requesting from the network.
* Fixes the query in GetCatalogTask so that if there is no "current"
  catalog, it will use the "downloading" catalog instead.

Bug: 867488
Change-Id: Ic307f77a4b3ae2ededa711f619059b1aed562012
Reviewed-on: https://chromium-review.googlesource.com/1252908
Commit-Queue: Justin DeWitt <dewittj@chromium.org>
Reviewed-by: default avatarCathy Li <chili@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595605}
parent 5c5c5e83
......@@ -36,6 +36,14 @@ public class ExploreSitesBridge {
nativeGetIcon(profile, siteID, callback);
}
public static void getCategoryImage(
Profile profile, int categoryID, int pixelSize, Callback<Bitmap> callback) {
// TODO(dewittj): Remove this when image decoding works correctly.
Bitmap image = Bitmap.createBitmap(pixelSize, pixelSize, Bitmap.Config.ARGB_8888);
image.eraseColor(android.graphics.Color.GREEN);
callback.onResult(image);
}
/**
* Causes a network request for updating the catalog.
*/
......
......@@ -8,10 +8,13 @@ import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntDef;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.chrome.browser.UrlConstants;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.List;
......@@ -22,7 +25,41 @@ public class ExploreSitesCategory {
// The ID to use when creating the More button, that should not scroll the ESP when clicked.
public static final int MORE_BUTTON_ID = -1;
// These constants should be kept in sync with
// //chrome/browser/android/explore_sites/catalog.proto's CategoryType.
@Retention(RetentionPolicy.SOURCE)
@IntDef({CategoryType.DEFAULT, CategoryType.SOCIAL, CategoryType.ENTERTAINMENT,
CategoryType.SPORT, CategoryType.NEWS, CategoryType.SHOPPING, CategoryType.REFERENCE,
CategoryType.BANKING, CategoryType.GOVERNMENT, CategoryType.TRAVEL,
CategoryType.EDUCATION, CategoryType.JOBS, CategoryType.APPS_GAMES,
CategoryType.FAVORITE, CategoryType.GOOGLE, CategoryType.FOOD, CategoryType.HEALTH,
CategoryType.BOOKS, CategoryType.TECHNOLOGY, CategoryType.SCIENCE})
public @interface CategoryType {
int DEFAULT = 0;
int SOCIAL = 1;
int ENTERTAINMENT = 2;
int SPORT = 3;
int NEWS = 4;
int SHOPPING = 5;
int REFERENCE = 6;
int BANKING = 7;
int GOVERNMENT = 8;
int TRAVEL = 9;
int EDUCATION = 10;
int JOBS = 11;
int APPS_GAMES = 12;
int FAVORITE = 13;
int GOOGLE = 14;
int FOOD = 15;
int HEALTH = 16;
int BOOKS = 17;
int TECHNOLOGY = 18;
int SCIENCE = 19;
}
private int mCategoryId;
private @CategoryType int mCategoryType;
private String mCategoryTitle;
// Populated only in NTP.
......@@ -36,8 +73,9 @@ public class ExploreSitesCategory {
* proto, or -1 if this represents the More button.
* @param title The string to display as the caption for this tile.
*/
public ExploreSitesCategory(int categoryId, String title) {
public ExploreSitesCategory(int categoryId, @CategoryType int categoryType, String title) {
mCategoryId = categoryId;
mCategoryType = categoryType;
mCategoryTitle = title;
mSites = new ArrayList<>();
}
......@@ -45,6 +83,9 @@ public class ExploreSitesCategory {
public int getId() {
return mCategoryId;
}
public @CategoryType int getType() {
return mCategoryType;
}
public String getTitle() {
return mCategoryTitle;
......@@ -84,8 +125,8 @@ public class ExploreSitesCategory {
// easily append sites to the category.
@CalledByNative
private static ExploreSitesCategory createAndAppendToList(
int categoryId, String title, List<ExploreSitesCategory> list) {
ExploreSitesCategory category = new ExploreSitesCategory(categoryId, title);
int categoryId, int categoryType, String title, List<ExploreSitesCategory> list) {
ExploreSitesCategory category = new ExploreSitesCategory(categoryId, categoryType, title);
list.add(category);
return category;
}
......
......@@ -6,7 +6,10 @@ package org.chromium.chrome.browser.explore_sites;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.ImageView;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ntp.TitleUtil;
import org.chromium.chrome.browser.widget.tile.TileWithTextView;
......@@ -37,6 +40,19 @@ public class ExploreSitesCategoryTileView extends TileWithTextView {
super.initialize(TitleUtil.getTitleForDisplay(category.getTitle(), category.getUrl()),
SUPPORTED_OFFLINE, category.getDrawable(), TITLE_LINES);
mCategory = category;
// Correct the properties of the icon for categories, it should be the entire size of the
// icon background now.
ImageView tileViewIcon = (ImageView) findViewById(R.id.tile_view_icon);
tileViewIcon.setScaleType(ImageView.ScaleType.CENTER);
MarginLayoutParams layoutParams = (MarginLayoutParams) tileViewIcon.getLayoutParams();
int tileViewIconSize =
getContext().getResources().getDimensionPixelSize(R.dimen.tile_view_icon_size);
layoutParams.width = tileViewIconSize;
layoutParams.height = tileViewIconSize;
layoutParams.topMargin = getContext().getResources().getDimensionPixelSize(
R.dimen.tile_view_icon_background_margin_top_modern);
tileViewIcon.setLayoutParams(layoutParams);
}
/** Retrieves url associated with this view. */
......@@ -44,6 +60,10 @@ public class ExploreSitesCategoryTileView extends TileWithTextView {
return mCategory.getUrl();
}
public ExploreSitesCategory getCategory() {
return mCategory;
}
/** Renders icon based on tile data. */
public void renderIcon(ExploreSitesCategory category) {
mCategory = category;
......
......@@ -5,12 +5,14 @@
package org.chromium.chrome.browser.explore_sites;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.view.LayoutInflater;
import android.view.View;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.explore_sites.ExploreSitesCategory.CategoryType;
import org.chromium.chrome.browser.native_page.NativePageNavigationDelegate;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.suggestions.SuggestionsConfig.TileStyle;
......@@ -20,7 +22,9 @@ import org.chromium.ui.base.PageTransition;
import org.chromium.ui.mojom.WindowOpenDisposition;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Describes a portion of UI responsible for rendering a group of categories.
......@@ -29,11 +33,6 @@ import java.util.List;
public class ExploreSitesSection {
private static final int MAX_CATEGORIES = 3;
/** These should be kept in sync with //chrome/browser/android/explore_sites/catalog.proto */
private static final int SPORT = 3;
private static final int SHOPPING = 5;
private static final int FOOD = 15;
@TileStyle
private int mStyle;
private Profile mProfile;
......@@ -52,7 +51,7 @@ public class ExploreSitesSection {
}
private void initialize() {
ExploreSitesBridge.getEspCatalog(mProfile, this ::initializeCategoryTiles);
ExploreSitesBridge.getEspCatalog(mProfile, this::gotEspCatalog);
}
private Drawable getVectorDrawable(int resource) {
......@@ -72,27 +71,28 @@ public class ExploreSitesSection {
List<ExploreSitesCategory> categoryList = new ArrayList<>();
// Sport category.
ExploreSitesCategory category = new ExploreSitesCategory(
SPORT, getContext().getString(R.string.explore_sites_default_category_sports));
ExploreSitesCategory category =
new ExploreSitesCategory(-1 /* category_id */, CategoryType.SPORT,
getContext().getString(R.string.explore_sites_default_category_sports));
category.setDrawable(getVectorDrawable(R.drawable.ic_directions_run_blue_24dp));
categoryList.add(category);
// Shopping category.
category = new ExploreSitesCategory(
SHOPPING, getContext().getString(R.string.explore_sites_default_category_shopping));
category = new ExploreSitesCategory(-1 /* category_id */, CategoryType.SHOPPING,
getContext().getString(R.string.explore_sites_default_category_shopping));
category.setDrawable(getVectorDrawable(R.drawable.ic_shopping_basket_blue_24dp));
categoryList.add(category);
// Food category.
category = new ExploreSitesCategory(
FOOD, getContext().getString(R.string.explore_sites_default_category_cooking));
category = new ExploreSitesCategory(-1 /* category_id */, CategoryType.FOOD,
getContext().getString(R.string.explore_sites_default_category_cooking));
category.setDrawable(getVectorDrawable(R.drawable.ic_restaurant_menu_blue_24dp));
categoryList.add(category);
return categoryList;
}
private ExploreSitesCategory createMoreTileCategory() {
ExploreSitesCategory category = new ExploreSitesCategory(
ExploreSitesCategory category = new ExploreSitesCategory(-1 /* category_id */,
ExploreSitesCategory.MORE_BUTTON_ID, getContext().getString(R.string.more));
category.setDrawable(getVectorDrawable(R.drawable.ic_arrow_forward_blue_24dp));
return category;
......@@ -114,9 +114,55 @@ public class ExploreSitesSection {
tileView.setOnClickListener((View v) -> onClicked(category, v));
}
/**
* Checks the result, if it indicates that we don't have a valid catalog, request one from the
* network. If the network request fails, just continue but otherwise retry getting the catalog
* from the ExploreSitesBridge.
*/
private void gotEspCatalog(List<ExploreSitesCategory> categoryList) {
if (categoryList == null || categoryList.size() == 0) {
ExploreSitesBridge.updateCatalogFromNetwork(
mProfile, (Boolean success) -> { updateCategoryIcons(); });
}
// Initialize with defaults right away.
initializeCategoryTiles(categoryList);
}
private void updateCategoryIcons() {
Map<Integer, ExploreSitesCategoryTileView> viewTypes = new HashMap<>();
for (int i = 0; i < mExploreSection.getChildCount(); i++) {
ExploreSitesCategoryTileView v =
(ExploreSitesCategoryTileView) mExploreSection.getChildAt(i);
ExploreSitesCategory category = v.getCategory();
if (category == null || category.getType() == ExploreSitesCategory.MORE_BUTTON_ID)
continue;
viewTypes.put(category.getType(), v);
}
ExploreSitesBridge.getEspCatalog(mProfile, (List<ExploreSitesCategory> categoryList) -> {
for (ExploreSitesCategory category : categoryList) {
ExploreSitesCategoryTileView v = viewTypes.get(category.getType());
if (v == null) {
continue;
}
ExploreSitesBridge.getCategoryImage(mProfile, category.getId(),
v.getContext().getResources().getDimensionPixelSize(
R.dimen.tile_view_icon_size),
(Bitmap image) -> {
if (image != null) {
category.setIcon(mExploreSection.getContext(), image);
v.renderIcon(category);
}
});
}
});
}
private void initializeCategoryTiles(List<ExploreSitesCategory> categoryList) {
boolean needIcons = true;
if (categoryList == null || categoryList.size() == 0) {
categoryList = createDefaultCategoryTiles();
needIcons = false; // Icons are already prepared in the default tiles.
}
int tileCount = 0;
......@@ -126,6 +172,9 @@ public class ExploreSitesSection {
createTileView(category);
}
createTileView(createMoreTileCategory());
if (needIcons) {
updateCategoryIcons();
}
}
private void onClicked(ExploreSitesCategory category, View v) {
......
......@@ -19,15 +19,18 @@ public class ExploreSitesCategoryUnitTest {
@Test
public void testAddSite() {
final int id = 1;
@ExploreSitesCategory.CategoryType
final int type = ExploreSitesCategory.CategoryType.SCIENCE;
final int siteId = 100;
final String title = "test";
final String url = "http://www.google.com";
final String categoryTitle = "Movies";
ExploreSitesCategory category = new ExploreSitesCategory(id, categoryTitle);
ExploreSitesCategory category = new ExploreSitesCategory(id, type, categoryTitle);
category.addSite(new ExploreSitesSite(siteId, title, url));
assertEquals(id, category.getId());
assertEquals(type, category.getType());
assertEquals(1, category.getSites().size());
assertEquals(siteId, category.getSites().get(0).getId());
assertEquals(title, category.getSites().get(0).getTitle());
......
......@@ -50,7 +50,7 @@ void CatalogReady(ScopedJavaGlobalRef<jobject>(j_result_obj),
for (auto& category : *result) {
ScopedJavaLocalRef<jobject> j_category =
Java_ExploreSitesCategory_createAndAppendToList(
env, category.category_id,
env, category.category_id, category.category_type,
ConvertUTF8ToJavaString(env, category.label), j_result_obj);
for (auto& site : category.sites) {
Java_ExploreSitesSite_createSiteInCategory(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment