Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1975)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ntp/InterestsPage.java

Issue 1351303003: Add the UI for the Interests Prototype. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ntp-interests-jni
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/InterestsPage.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/InterestsPage.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/InterestsPage.java
new file mode 100644
index 0000000000000000000000000000000000000000..a119807246c491bf6e14491583bbb16e9dcfb732
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/InterestsPage.java
@@ -0,0 +1,192 @@
+// Copyright 2015 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.ntp;
+
+import android.accounts.Account;
+import android.accounts.AccountManager;
+import android.app.Activity;
+import android.app.Dialog;
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+
+import org.chromium.base.Log;
+import org.chromium.chrome.R;
+import org.chromium.chrome.browser.NativePage;
+import org.chromium.chrome.browser.ntp.InterestsService.GetInterestsCallback;
+import org.chromium.chrome.browser.ntp.InterestsService.Interest;
+import org.chromium.chrome.browser.profiles.Profile;
+import org.chromium.chrome.browser.signin.OAuth2TokenService;
+import org.chromium.chrome.browser.tab.Tab;
+import org.chromium.content_public.browser.LoadUrlParams;
+import org.chromium.sync.signin.AccountManagerHelper;
+import org.chromium.sync.signin.AccountManagerHelper.GetAuthTokenCallback;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * List the interests of the user. When an interest is clicked the user is redirect to a google
+ * search with news about that subject.
+ */
+public class InterestsPage implements NativePage {
+ private final Profile mProfile;
+ private final InterestsPageView mPageView;
+ private final String mTitle;
+ private final int mBackgroundColor;
+
+ // Whether destroy() has been called.
+ private boolean mIsDestroyed;
+
+ private static final String TAG = "cr.browser.ntp";
+
+ /**
+ * Creates a InterestsPage to be shown in document mode.
+ *
+ * @param context The view context for showing the page.
+ * @param tab The tab from which interests page is loaded.
+ * @param profile The profile from which to load interests.
+ * @param listener The InterestsSelectedListener to notify when the user clicks an interest.
+ * @return The new InterestPage object.
+ */
+ public static InterestsPage buildPage(Context context, Tab tab,
Marc Treib 2015/09/18 13:26:03 Any reason for not just calling the constructor di
tache 2015/09/21 15:35:55 Done.
+ Profile profile, InterestsListener listener,
+ Activity activity) {
+ return new InterestsPage(
+ context, profile, tab, listener, activity);
+ }
+
+ private InterestsPage(Context context, final Profile profile, Tab tab,
+ InterestsListener listener, Activity activity) {
+ mProfile = profile;
+ mTitle = context.getResources().getString(R.string.ntp_interests);
+ mBackgroundColor = context.getResources().getColor(R.color.ntp_bg);
+
+ LayoutInflater inflater = LayoutInflater.from(context);
+
+ mPageView = (InterestsPageView) inflater.inflate(R.layout.interests_page, null);
+ mPageView.setListener(listener);
+
+ GetAuthTokenCallback callback = new AccountManagerHelper.GetAuthTokenCallback() {
+ @Override
+ public void tokenAvailable(String accessToken, boolean isTransientError) {
+ if (accessToken == null) {
+ return;
+ }
+ new InterestsService(profile).getInterests(accessToken,
+ new GetInterestsCallback() {
+ @Override
+ public void onInterestsAvailableCallback(Interest[] interests) {
+ List<Interest> interestList = Arrays.asList(interests);
Marc Treib 2015/09/18 13:26:03 Any particular reason for switching from array to
tache 2015/09/21 15:35:55 The native call returns the callback as an array.
Marc Treib 2015/09/21 16:20:08 Acknowledged, fair enough.
+
+ mPageView.setInterests(interestList);
+ }
+ });
+ }
+ };
+
+ Account account = getCurrentAccount(context);
+
+ OAuth2TokenService.getOAuth2AccessToken(context,
+ activity,
+ account,
Marc Treib 2015/09/18 13:26:03 Does this handle a null account?
tache 2015/09/21 15:35:55 It should. I wrapped it in an if statement just to
Marc Treib 2015/09/21 16:20:08 Wouldn't it be great if OAuth2TokenService documen
+ InterestsService.AUTH_SCOPE,
+ callback);
+ }
+
+ private Account getCurrentAccount(Context context) {
+ Account[] accounts = AccountManager.get(context).getAccounts();
Marc Treib 2015/09/18 13:26:03 This seems wrong - AccountManager is an Android th
tache 2015/09/21 15:35:55 I couldn't find anything that returns me the prima
Marc Treib 2015/09/21 16:20:07 ChromeSigninController.getSignedInUser()?
+
+ if (accounts.length > 1) {
+ Log.w(TAG, "Multiple accounts found. Using the first one.");
+ return accounts[0];
+ } else if (accounts.length == 0) {
+ Log.w(TAG, "No account found.");
+ return null;
+ }
+
+ return accounts[0];
+ }
+
+ /**
+ * Interface to be notified when the user clicks on a interest.
+ **/
+ public interface InterestsListener {
+ /**
+ * Called when a interest is selected.
+ *
+ * @param name The name of the selected interest.
+ */
+ void onInterestClicked(String name);
+ }
+
+ /**
+ * Default action to take when an interest is clicked. This redirects the current tab to a news
+ * search about the subject.
+ */
+ public static class DefaultInterestListener implements InterestsListener {
+ private static final String GOOGLE_SEARCH_URL = "https://www.google.com/search?tbm=nws&q=%s";
Marc Treib 2015/09/18 13:26:03 nit: too-long line also, .._URL_PATTERN?
tache 2015/09/21 15:35:55 Done.
+ private Dialog mDialog;
Marc Treib 2015/09/18 13:26:03 The dialog thing is weird. Why would a DefaultList
tache 2015/09/21 15:35:55 Moved this to the NewTabPage class similarly.
+ private final Tab mTab;
+
+ public DefaultInterestListener(Tab tab) {
+ mTab = tab;
+ }
+
+ @Override
+ public void onInterestClicked(String name) {
+ if (mDialog == null) {
+ return;
+ }
+
+ String url = String.format(GOOGLE_SEARCH_URL, name);
Marc Treib 2015/09/18 13:26:03 You need to URL encode the query string.
tache 2015/09/21 15:35:55 Done.
+
+ mTab.loadUrl(new LoadUrlParams(url));
+ mDialog.dismiss();
+ }
+
+ public void setDialog(Dialog dialog) {
+ mDialog = dialog;
+ }
+ }
+
+ @Override
+ public String getTitle() {
+ return mTitle;
+ }
+
+ @Override
+ public int getBackgroundColor() {
+ return mBackgroundColor;
+ }
+
+ @Override
+ public View getView() {
+ return mPageView;
+ }
+
+ /**
+ * Releases internal resources. This must be called eventually, and the object should not used
+ * after calling this.
+ */
+ @Override
+ public void destroy() {
+ mIsDestroyed = true;
Marc Treib 2015/09/18 13:26:03 Should this method do anything? Also, mIsDestroyed
tache 2015/09/21 15:35:55 The InterestPage doesn't have any members that nee
+ }
+
+ @Override
+ public String getHost() {
+ return "interests";
+ }
+
+ @Override
+ public String getUrl() {
+ return "chrome-native://interests";
+ }
+
+ @Override
+ public void updateForUrl(String url) {
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698