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

Side by Side Diff: chrome/android/java_staging/src/org/chromium/chrome/browser/ntp/BookmarksPageView.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 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 unified diff | Download patch
OLDNEW
(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.content.Context;
8 import android.graphics.Bitmap;
9 import android.graphics.BitmapFactory;
10 import android.text.TextUtils;
11 import android.util.AttributeSet;
12 import android.util.LruCache;
13 import android.view.View;
14 import android.view.ViewGroup;
15 import android.view.inputmethod.EditorInfo;
16 import android.view.inputmethod.InputConnection;
17 import android.widget.BaseAdapter;
18 import android.widget.HorizontalScrollView;
19 import android.widget.ImageView;
20 import android.widget.LinearLayout;
21 import android.widget.TextView;
22
23 import com.google.android.apps.chrome.R;
24
25 import org.chromium.chrome.browser.BookmarksBridge.BookmarkItem;
26 import org.chromium.chrome.browser.BookmarksBridge.BookmarksCallback;
27 import org.chromium.chrome.browser.favicon.FaviconHelper.FaviconImageCallback;
28 import org.chromium.components.bookmarks.BookmarkId;
29 import org.chromium.ui.base.LocalizationUtils;
30
31 import java.util.Collections;
32 import java.util.List;
33
34 /**
35 * The native bookmarks page, represented by some basic data such as folder cont ents,
36 * folder hierarchy, and an Android View that displays the page.
37 */
38 public class BookmarksPageView extends LinearLayout implements BookmarksCallback {
39
40 private static final int MAX_NUM_FAVICONS_TO_CACHE = 256;
41
42 private BookmarksPageManager mManager;
43 private HorizontalScrollView mHierarchyContainer;
44 private LinearLayout mHierarchyLayout;
45 private Bitmap mDefaultFavicon;
46 private BookmarkItemView.DrawingData mDrawingData;
47 private final int mDesiredFaviconSize;
48 private final LruCache<String, Bitmap> mFaviconCache;
49
50 private final BookmarkListAdapter mAdapter;
51 private BookmarksListView mBookmarksList;
52 private TextView mEmptyView;
53 private int mSavedListPosition = 0;
54 private int mSavedListTop = 0;
55
56 private boolean mSnapshotBookmarksChanged;
57 private int mSnapshotWidth;
58 private int mSnapshotHeight;
59 private int mSnapshotBookmarksListPosition;
60 private int mSnapshotBookmarksListTop;
61 private int mSnapshotBookmarksHierarchyScrollX;
62
63 /**
64 * Manages the view interaction with the rest of the system.
65 */
66 public interface BookmarksPageManager {
67 /**
68 * @return True, if destroy() has been called.
69 */
70 boolean isDestroyed();
71
72 /**
73 * @return Whether this bookmarks page should use incognito mode.
74 */
75 boolean isIncognito();
76
77 /**
78 * @return Whether "Open in new tab" should be shown in the context menu
79 * when long pressing a bookmark.
80 */
81 boolean shouldShowOpenInNewTab();
82
83 /**
84 * @return Whether "Open in new incognito tab" should be shown in the co ntext menu
85 * when long pressing a bookmark.
86 */
87 boolean shouldShowOpenInNewIncognitoTab();
88
89 /**
90 * @return Whether to show a context menu on long press.
91 */
92 boolean isContextMenuEnabled();
93
94 /**
95 * Opens the bookmark item. If item is a bookmark then it opens the page else,
96 * if folder, it displays the folder contents.
97 * @param item BookmarkItem to be opened.
98 */
99 void open(BookmarkItemView item);
100
101 /**
102 * Opens a bookmark item in a new tab.
103 * @param item The bookmark item to open.
104 */
105 void openInNewTab(BookmarkItemView item);
106
107 /**
108 * Opens a bookmark item in a new incognito tab.
109 * @param item The bookmark item to open.
110 */
111 void openInNewIncognitoTab(BookmarkItemView item);
112
113 /**
114 * Opens the bookmark folder to display the folder contents.
115 * @param item BookmarkFolderHierarchyItem to be opened.
116 */
117 void openFolder(BookmarkFolderHierarchyItem item);
118
119 /**
120 * Deletes a bookmark entry.
121 * @param item The bookmark item to remove.
122 */
123 void delete(BookmarkItemView item);
124
125 /**
126 * Edits a bookmark entry.
127 * @param item The bookmark item to be edited.
128 */
129 void edit(BookmarkItemView item);
130
131 /**
132 * Gets the favicon image for a given URL.
133 * @param url The URL of the site whose favicon is being requested.
134 * @param size The desired size of the favicon in pixels.
135 * @param faviconCallback The callback to be notified when the favicon i s available.
136 */
137 void getFaviconImageForUrl(String url, int size, FaviconImageCallback fa viconCallback);
138 }
139
140 /**
141 * Constructor for inflating from XML.
142 */
143 public BookmarksPageView(Context context, AttributeSet attrs) {
144 super(context, attrs);
145
146 mDesiredFaviconSize = getResources().getDimensionPixelSize(
147 R.dimen.ntp_list_item_favicon_size);
148 mFaviconCache = new LruCache<String, Bitmap>(MAX_NUM_FAVICONS_TO_CACHE);
149 mAdapter = new BookmarkListAdapter();
150 }
151
152 @Override
153 protected void onFinishInflate() {
154 super.onFinishInflate();
155
156 mHierarchyContainer =
157 (HorizontalScrollView) findViewById(R.id.folder_structure_scroll _view);
158 mHierarchyContainer.setSmoothScrollingEnabled(true);
159 mHierarchyLayout = (LinearLayout) findViewById(R.id.bookmark_folder_stru cture);
160
161 mBookmarksList = (BookmarksListView) findViewById(R.id.bookmarks_list_vi ew);
162 mBookmarksList.setAdapter(mAdapter);
163
164 mEmptyView = (TextView) findViewById(R.id.bookmarks_empty_view);
165 mBookmarksList.setEmptyView(mEmptyView);
166 }
167
168 @Override
169 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
170 // Fixes lanscape transitions when unfocusing the URL bar: crbug.com/288 546
171 outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN;
172 return super.onCreateInputConnection(outAttrs);
173 }
174
175 /**
176 * Handles initializing the view.
177 * @param manager The manager handling the external dependencies of this vie w.
178 */
179 void initialize(BookmarksPageManager manager) {
180 mManager = manager;
181 }
182
183 @Override
184 protected void onDetachedFromWindow() {
185 mSavedListPosition = mBookmarksList.getFirstVisiblePosition();
186 View v = mBookmarksList.getChildAt(0);
187 mSavedListTop = (v == null) ? 0 : v.getTop();
188 super.onDetachedFromWindow();
189 }
190
191 @Override
192 protected void onAttachedToWindow() {
193 super.onAttachedToWindow();
194 mBookmarksList.setSelectionFromTop(mSavedListPosition, mSavedListTop);
195 }
196
197 /**
198 * @see org.chromium.chrome.browser.compositor.layouts.content
199 * .InvalidationAwareThumbnailProvider#shouldCaptureThumbnail()
200 */
201 boolean shouldCaptureThumbnail() {
202 if (getWidth() == 0 || getHeight() == 0) return false;
203
204 View topItem = mBookmarksList.getChildAt(0);
205 return mSnapshotBookmarksChanged
206 || getWidth() != mSnapshotWidth
207 || getHeight() != mSnapshotHeight
208 || mSnapshotBookmarksListPosition != mBookmarksList.getFirstVisi blePosition()
209 || mSnapshotBookmarksListTop != (topItem == null ? 0 : topItem.g etTop())
210 || mHierarchyContainer.getScrollX() != mSnapshotBookmarksHierarc hyScrollX;
211 }
212
213 /**
214 * Triggered after a thumbnail has been captured to update the thumbnail vis ual state used to
215 * determine dirtiness.
216 */
217 void updateThumbnailState() {
218 mSnapshotWidth = getWidth();
219 mSnapshotHeight = getHeight();
220 mSnapshotBookmarksListPosition = mBookmarksList.getFirstVisiblePosition( );
221 View topItem = mBookmarksList.getChildAt(0);
222 mSnapshotBookmarksListTop = topItem == null ? 0 : topItem.getTop();
223 mSnapshotBookmarksHierarchyScrollX = mHierarchyContainer.getScrollX();
224 mSnapshotBookmarksChanged = false;
225 }
226
227 // BookmarksCallback overrides
228
229 @Override
230 public void onBookmarksAvailable(BookmarkId folderId, List<BookmarkItem> boo kmarksList) {
231 if (mEmptyView.length() == 0) {
232 // Set the empty view's text now that the first bookmarks callback h as happened. If we
233 // set the text earlier, the user will see the "No bookmarks here" m essage while we're
234 // waiting for the callback.
235 mEmptyView.setText(R.string.bookmarks_folder_empty);
236 }
237
238 mAdapter.setBookmarksList(bookmarksList);
239 mAdapter.notifyDataSetChanged();
240
241 // In theory, the adapter should trigger a re-layout when the bookmarks list changes. In
242 // practice, if the bookmarks page is in the background and hence not at tached to the view
243 // hierarchy, this doesn't happen. So, trigger the re-layout explicitly.
244 mBookmarksList.requestLayout();
245 mBookmarksList.invalidate();
246
247 // Cause the scroll bar to appear then fade out again if this folder is scrollable.
248 mBookmarksList.awakenScrollBars();
249
250 mSnapshotBookmarksChanged = true;
251 }
252
253 @Override
254 public void onBookmarksFolderHierarchyAvailable(BookmarkId folderId,
255 List<BookmarkItem> bookmarksList) {
256 if (mManager.isDestroyed()) return;
257 mHierarchyLayout.removeAllViews();
258 for (int i = bookmarksList.size() - 1; i >= 0; i--) {
259 BookmarkItem bookmark = bookmarksList.get(i);
260 addItemToHierarchyView(bookmark.getTitle(), bookmark.getId(), (i == 0) ? true : false);
261 }
262 mHierarchyLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListen er() {
263 @Override
264 public void onLayoutChange(View v, int left, int top, int right, int bottom,
265 int oldLeft, int oldTop, int oldRight, int oldBottom) {
266 mHierarchyLayout.removeOnLayoutChangeListener(this);
267 mHierarchyContainer.fullScroll(LocalizationUtils.isLayoutRtl()
268 ? View.FOCUS_LEFT : View.FOCUS_RIGHT);
269 }
270 });
271
272 mSnapshotBookmarksChanged = true;
273 }
274
275 /**
276 * Add a BookmarkFolderHierarchyItem item to the hierarchy layout.
277 * @param title Title of the folder
278 * @param id Id of the folder
279 * @param isCurrentFolder Whether the folder is the current folder.
280 */
281 private void addItemToHierarchyView(String title, final BookmarkId id,
282 boolean isCurrentFolder) {
283 if (TextUtils.isEmpty(title)) {
284 // TODO(cramya): Need to check why we cannot get "Bookmarks" folder information.
285 title = getResources().getString(R.string.ntp_bookmarks);
286 } else {
287 ImageView separator = new ImageView(getContext());
288 separator.setImageResource(R.drawable.breadcrumb_arrow);
289 mHierarchyLayout.addView(separator);
290 }
291 final BookmarkFolderHierarchyItem item = new BookmarkFolderHierarchyItem (
292 getContext(), mManager, id, title, isCurrentFolder);
293 mHierarchyLayout.addView(item);
294 }
295
296 /**
297 * List Adapter for Bookmarks List View.
298 */
299 private class BookmarkListAdapter extends BaseAdapter {
300
301 public List<BookmarkItem> mBookmarks = Collections.emptyList();
302
303 /**
304 * Sets the bookmarks list for adapter.
305 * @param bookmarks BookmarkItem list.
306 */
307 public void setBookmarksList(List<BookmarkItem> bookmarks) {
308 mBookmarks = bookmarks;
309 }
310
311 @Override
312 public View getView(int position, View convertView, ViewGroup parent) {
313 if (mDrawingData == null) mDrawingData = new BookmarkItemView.Drawin gData(getContext());
314 final BookmarkItem bookmark = getItem(position);
315 final BookmarkItemView item;
316 if (convertView instanceof BookmarkItemView) {
317 item = (BookmarkItemView) convertView;
318 if (!item.reset(bookmark.getId(), bookmark.getTitle(), bookmark. getUrl(),
319 bookmark.isEditable(), bookmark.isManaged())) {
320 return item;
321 }
322 } else {
323 item = new BookmarkItemView(getContext(), mManager,
324 bookmark.getId(), bookmark.getTitle(), bookmark.getUrl() ,
325 bookmark.isEditable(), bookmark.isManaged(), mDrawingDat a);
326 }
327 if (!bookmark.isFolder() && !TextUtils.isEmpty(bookmark.getUrl())) {
328 Bitmap favicon = mFaviconCache.get(bookmark.getUrl());
329 if (favicon != null) {
330 item.setFavicon(favicon);
331 } else if (!mManager.isDestroyed()) {
332 FaviconImageCallback faviconCallback = new FaviconImageCallb ack() {
333 @Override
334 public void onFaviconAvailable(Bitmap image, String icon Url) {
335 if (image == null) {
336 if (mDefaultFavicon == null) {
337 mDefaultFavicon = BitmapFactory.decodeResour ce(
338 getResources(), R.drawable.default_f avicon);
339 }
340 image = mDefaultFavicon;
341 }
342 mFaviconCache.put(bookmark.getUrl(), image);
343 // It's possible the BookmarkItemView has been recyc led and is now
344 // displaying a different bookmark. Don't update the favicon in this
345 // case.
346 if (bookmark.getUrl().equals(item.getUrl())) {
347 item.setFavicon(image);
348 mSnapshotBookmarksChanged = true;
349 }
350 }
351 };
352 mManager.getFaviconImageForUrl(bookmark.getUrl(), mDesiredFa viconSize,
353 faviconCallback);
354 }
355 }
356 return item;
357 }
358
359 @Override
360 public long getItemId(int position) {
361 return position;
362 }
363
364 @Override
365 public int getCount() {
366 return mBookmarks.size();
367 }
368
369 @Override
370 public BookmarkItem getItem(int position) {
371 return mBookmarks.get(position);
372 }
373
374 @Override
375 public boolean isEmpty() {
376 return mBookmarks.isEmpty();
377 }
378
379 @Override
380 public boolean hasStableIds() {
381 return false;
382 }
383 }
384 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698