Commit 3e9f77a9 authored by Cathy Li's avatar Cathy Li Committed by Commit Bot

[Explore Sites]: Create explore sites tile layout and hook up to json and image loading.

Bug: 852075
Change-Id: If09bc4cac1687599bb9009c40248d678f6d16c8b
Reviewed-on: https://chromium-review.googlesource.com/1099967Reviewed-by: default avatarTheresa <twellington@chromium.org>
Reviewed-by: default avatarJustin DeWitt <dewittj@chromium.org>
Commit-Queue: Cathy Li <chili@chromium.org>
Cr-Commit-Position: refs/heads/master@{#567740}
parent 87a8a4d5
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2018 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->
<!-- An explore sites section tile. -->
<org.chromium.chrome.browser.explore_sites.ExploreSitesCategoryTileView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!-- Main icon/image -->
<ImageView
android:id="@+id/explore_sites_category_tile_icon"
android:layout_gravity="center_horizontal"
android:layout_marginTop="@dimen/tile_view_icon_margin_top_modern"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@null" />
<!-- Category title -->
<TextView
android:id="@+id/explore_sites_category_tile_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textAppearance="@style/BlackCaption" />
</org.chromium.chrome.browser.explore_sites.ExploreSitesCategoryTileView>
......@@ -32,10 +32,9 @@ public class ExploreSitesBridge {
/**
* Fetches a JSON string from URL, returning the parsed JSONobject in a callback.
*/
public void getNtpCategories(
final String url, final Callback<List<ExploreSitesCategoryTile>> callback) {
public void getNtpCategories(final Callback<List<ExploreSitesCategoryTile>> callback) {
List<ExploreSitesCategoryTile> result = new ArrayList<>();
nativeGetNtpCategories(mNativeExploreSitesBridge, url, result, callback);
nativeGetNtpCategories(mNativeExploreSitesBridge, "", result, callback);
}
/**
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.explore_sites;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import org.chromium.chrome.R;
/**
* The View representing a single explore sites category.
* Consists of a large image icon over descriptive text.
*/
public class ExploreSitesCategoryTileView extends FrameLayout {
/** The data represented by this tile. */
private ExploreSitesCategoryTile mCategoryData;
private TextView mTitleView;
private ImageView mIconView;
/** Constructor for inflating from XML. */
public ExploreSitesCategoryTileView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onFinishInflate() {
super.onFinishInflate();
mTitleView = findViewById(R.id.explore_sites_category_tile_title);
mIconView = findViewById(R.id.explore_sites_category_tile_icon);
}
public void initialize(ExploreSitesCategoryTile category) {
mCategoryData = category;
mTitleView.setText(mCategoryData.getCategoryName());
}
public void updateIcon(Drawable drawable) {
mCategoryData.setIconDrawable(drawable);
mIconView.setImageDrawable(drawable);
}
}
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.explore_sites;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.suggestions.SuggestionsNavigationDelegate;
import org.chromium.chrome.browser.util.ViewUtils;
import org.chromium.ui.mojom.WindowOpenDisposition;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Describes a portion of UI responsible for rendering a group of categories.
* It abstracts general tasks related to initializing and fetching data for the UI.
*/
public class ExploreSitesSection {
private static final int ICON_CORNER_RADIUS = 5;
private ExploreSitesBridge mBridge;
private SuggestionsNavigationDelegate mNavigationDelegate;
private View mExploreSection;
private LinearLayout mCategorySection;
private AtomicInteger mCategoryIconFetchesInFlight;
public ExploreSitesSection(
View view, Profile profile, SuggestionsNavigationDelegate navigationDelegate) {
mBridge = new ExploreSitesBridge(profile);
mExploreSection = view;
initialize();
}
private void initialize() {
mCategorySection = mExploreSection.findViewById(R.id.explore_sites_tiles);
mBridge.getNtpCategories(this ::initializeTiles);
}
private void initializeTiles(List<ExploreSitesCategoryTile> tileList) {
if (tileList.isEmpty()) {
mBridge.destroy();
return;
}
mCategoryIconFetchesInFlight = new AtomicInteger(tileList.size());
for (final ExploreSitesCategoryTile tile : tileList) {
final ExploreSitesCategoryTileView tileView =
(ExploreSitesCategoryTileView) LayoutInflater.from(mExploreSection.getContext())
.inflate(R.layout.explore_sites_category_tile_view, mCategorySection,
false);
tileView.initialize(tile);
mCategorySection.addView(tileView);
tileView.setOnClickListener(
(View v)
-> mNavigationDelegate.navigateToSuggestionUrl(
WindowOpenDisposition.CURRENT_TAB, tile.getNavigationUrl()));
mBridge.getIcon(tile.getIconUrl(), (Bitmap icon) -> onIconRetrieved(tileView, icon));
}
}
private void onIconRetrieved(ExploreSitesCategoryTileView tileView, Bitmap icon) {
tileView.updateIcon(ViewUtils.createRoundedBitmapDrawable(icon, ICON_CORNER_RADIUS));
int curValue = mCategoryIconFetchesInFlight.decrementAndGet();
if (curValue == 0) {
mBridge.destroy();
}
}
}
......@@ -77,6 +77,14 @@ public class NewTabPageLayout extends LinearLayout {
return mSiteSectionView;
}
/**
* @return the embedded explore sites section.
*/
@Nullable
public View getExploreSectionView() {
return mExploreSectionView;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
......
......@@ -32,6 +32,7 @@ import org.chromium.base.VisibleForTesting;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.compositor.layouts.content.InvalidationAwareThumbnailProvider;
import org.chromium.chrome.browser.explore_sites.ExploreSitesSection;
import org.chromium.chrome.browser.locale.LocaleManager;
import org.chromium.chrome.browser.ntp.NewTabPage.FakeboxDelegate;
import org.chromium.chrome.browser.ntp.NewTabPage.OnSearchBoxScrollListener;
......@@ -82,6 +83,9 @@ public class NewTabPageView
private View mNoSearchLogoSpacer;
private ViewGroup mShortcutsView;
@Nullable
private ExploreSitesSection mExploreSection; // Null when explore sites disabled.
private OnSearchBoxScrollListener mSearchBoxScrollListener;
private NewTabPageManager mManager;
......@@ -256,6 +260,11 @@ public class NewTabPageView
SiteSection.createViewHolder(mNewTabPageLayout.getSiteSectionView(), mUiConfig);
mSiteSectionViewHolder.bindDataSource(mTileGroup, tileRenderer);
if (ChromeFeatureList.isEnabled(ChromeFeatureList.EXPLORE_SITES)) {
mExploreSection = new ExploreSitesSection(mNewTabPageLayout.getExploreSectionView(),
profile, mManager.getNavigationDelegate());
}
mSearchProviderLogoView = mNewTabPageLayout.findViewById(R.id.search_provider_logo);
mLogoDelegate = new LogoDelegateImpl(
mManager.getNavigationDelegate(), mSearchProviderLogoView, profile);
......
......@@ -494,6 +494,8 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/engagement/SiteEngagementService.java",
"java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesBridge.java",
"java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesCategoryTile.java",
"java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesCategoryTileView.java",
"java/src/org/chromium/chrome/browser/explore_sites/ExploreSitesSection.java",
"java/src/org/chromium/chrome/browser/externalauth/ExternalAuthUtils.java",
"java/src/org/chromium/chrome/browser/externalauth/UserRecoverableErrorHandler.java",
"java/src/org/chromium/chrome/browser/externalauth/VerifiedHandler.java",
......
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