OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.ntp; | |
6 | |
7 import android.accounts.Account; | |
8 import android.accounts.AccountManager; | |
9 import android.app.Activity; | |
10 import android.app.Dialog; | |
11 import android.content.Context; | |
12 import android.view.LayoutInflater; | |
13 import android.view.View; | |
14 | |
15 import org.chromium.base.Log; | |
16 import org.chromium.chrome.R; | |
17 import org.chromium.chrome.browser.NativePage; | |
18 import org.chromium.chrome.browser.ntp.InterestsService.GetInterestsCallback; | |
19 import org.chromium.chrome.browser.ntp.InterestsService.Interest; | |
20 import org.chromium.chrome.browser.profiles.Profile; | |
21 import org.chromium.chrome.browser.signin.OAuth2TokenService; | |
22 import org.chromium.chrome.browser.tab.Tab; | |
23 import org.chromium.content_public.browser.LoadUrlParams; | |
24 import org.chromium.sync.signin.AccountManagerHelper; | |
25 import org.chromium.sync.signin.AccountManagerHelper.GetAuthTokenCallback; | |
26 | |
27 import java.util.Arrays; | |
28 import java.util.List; | |
29 | |
30 /** | |
31 * List the interests of the user. When an interest is clicked the user is redir ect to a google | |
32 * search with news about that subject. | |
33 */ | |
34 public class InterestsPage implements NativePage { | |
35 private final Profile mProfile; | |
36 private final InterestsPageView mPageView; | |
37 private final String mTitle; | |
38 private final int mBackgroundColor; | |
39 | |
40 // Whether destroy() has been called. | |
41 private boolean mIsDestroyed; | |
42 | |
43 private static final String TAG = "cr.browser.ntp"; | |
44 | |
45 /** | |
46 * Creates a InterestsPage to be shown in document mode. | |
47 * | |
48 * @param context The view context for showing the page. | |
49 * @param tab The tab from which interests page is loaded. | |
50 * @param profile The profile from which to load interests. | |
51 * @param listener The InterestsSelectedListener to notify when the user cli cks an interest. | |
52 * @return The new InterestPage object. | |
53 */ | |
54 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.
| |
55 Profile profile, InterestsListener listener, | |
56 Activity activity) { | |
57 return new InterestsPage( | |
58 context, profile, tab, listener, activity); | |
59 } | |
60 | |
61 private InterestsPage(Context context, final Profile profile, Tab tab, | |
62 InterestsListener listener, Activity activity) { | |
63 mProfile = profile; | |
64 mTitle = context.getResources().getString(R.string.ntp_interests); | |
65 mBackgroundColor = context.getResources().getColor(R.color.ntp_bg); | |
66 | |
67 LayoutInflater inflater = LayoutInflater.from(context); | |
68 | |
69 mPageView = (InterestsPageView) inflater.inflate(R.layout.interests_page , null); | |
70 mPageView.setListener(listener); | |
71 | |
72 GetAuthTokenCallback callback = new AccountManagerHelper.GetAuthTokenCal lback() { | |
73 @Override | |
74 public void tokenAvailable(String accessToken, boolean isTransientEr ror) { | |
75 if (accessToken == null) { | |
76 return; | |
77 } | |
78 new InterestsService(profile).getInterests(accessToken, | |
79 new GetInterestsCallback() { | |
80 @Override | |
81 public void onInterestsAvailableCallback(Interest[] interests) { | |
82 List<Interest> interestList = Arrays.asList(inte rests); | |
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.
| |
83 | |
84 mPageView.setInterests(interestList); | |
85 } | |
86 }); | |
87 } | |
88 }; | |
89 | |
90 Account account = getCurrentAccount(context); | |
91 | |
92 OAuth2TokenService.getOAuth2AccessToken(context, | |
93 activity, | |
94 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
| |
95 InterestsService.AUTH_SCOPE, | |
96 callback); | |
97 } | |
98 | |
99 private Account getCurrentAccount(Context context) { | |
100 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()?
| |
101 | |
102 if (accounts.length > 1) { | |
103 Log.w(TAG, "Multiple accounts found. Using the first one."); | |
104 return accounts[0]; | |
105 } else if (accounts.length == 0) { | |
106 Log.w(TAG, "No account found."); | |
107 return null; | |
108 } | |
109 | |
110 return accounts[0]; | |
111 } | |
112 | |
113 /** | |
114 * Interface to be notified when the user clicks on a interest. | |
115 **/ | |
116 public interface InterestsListener { | |
117 /** | |
118 * Called when a interest is selected. | |
119 * | |
120 * @param name The name of the selected interest. | |
121 */ | |
122 void onInterestClicked(String name); | |
123 } | |
124 | |
125 /** | |
126 * Default action to take when an interest is clicked. This redirects the cu rrent tab to a news | |
127 * search about the subject. | |
128 */ | |
129 public static class DefaultInterestListener implements InterestsListener { | |
130 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.
| |
131 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.
| |
132 private final Tab mTab; | |
133 | |
134 public DefaultInterestListener(Tab tab) { | |
135 mTab = tab; | |
136 } | |
137 | |
138 @Override | |
139 public void onInterestClicked(String name) { | |
140 if (mDialog == null) { | |
141 return; | |
142 } | |
143 | |
144 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.
| |
145 | |
146 mTab.loadUrl(new LoadUrlParams(url)); | |
147 mDialog.dismiss(); | |
148 } | |
149 | |
150 public void setDialog(Dialog dialog) { | |
151 mDialog = dialog; | |
152 } | |
153 } | |
154 | |
155 @Override | |
156 public String getTitle() { | |
157 return mTitle; | |
158 } | |
159 | |
160 @Override | |
161 public int getBackgroundColor() { | |
162 return mBackgroundColor; | |
163 } | |
164 | |
165 @Override | |
166 public View getView() { | |
167 return mPageView; | |
168 } | |
169 | |
170 /** | |
171 * Releases internal resources. This must be called eventually, and the obje ct should not used | |
172 * after calling this. | |
173 */ | |
174 @Override | |
175 public void destroy() { | |
176 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
| |
177 } | |
178 | |
179 @Override | |
180 public String getHost() { | |
181 return "interests"; | |
182 } | |
183 | |
184 @Override | |
185 public String getUrl() { | |
186 return "chrome-native://interests"; | |
187 } | |
188 | |
189 @Override | |
190 public void updateForUrl(String url) { | |
191 } | |
192 } | |
OLD | NEW |