| 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.enhancedbookmarks; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 import android.graphics.Bitmap; | |
| 9 import android.graphics.drawable.BitmapDrawable; | |
| 10 import android.support.v4.graphics.drawable.RoundedBitmapDrawable; | |
| 11 import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; | |
| 12 import android.text.format.Formatter; | |
| 13 import android.util.AttributeSet; | |
| 14 import android.view.View; | |
| 15 import android.widget.TextView; | |
| 16 | |
| 17 import org.chromium.base.ApiCompatibilityUtils; | |
| 18 import org.chromium.chrome.R; | |
| 19 import org.chromium.chrome.browser.bookmark.BookmarksBridge.BookmarkItem; | |
| 20 import org.chromium.chrome.browser.favicon.LargeIconBridge.LargeIconCallback; | |
| 21 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; | |
| 22 import org.chromium.chrome.browser.offlinepages.OfflinePageItem; | |
| 23 import org.chromium.chrome.browser.widget.RoundedIconGenerator; | |
| 24 import org.chromium.components.bookmarks.BookmarkId; | |
| 25 | |
| 26 /** | |
| 27 * A row view that shows bookmark info in the enhanced bookmarks UI. | |
| 28 */ | |
| 29 public class EnhancedBookmarkBookmarkRow extends EnhancedBookmarkRow implements
LargeIconCallback { | |
| 30 | |
| 31 private String mUrl; | |
| 32 private RoundedIconGenerator mIconGenerator; | |
| 33 private final int mMinIconSize; | |
| 34 private final int mDisplayedIconSize; | |
| 35 private final int mCornerRadius; | |
| 36 | |
| 37 /** | |
| 38 * Constructor for inflating from XML. | |
| 39 */ | |
| 40 public EnhancedBookmarkBookmarkRow(Context context, AttributeSet attrs) { | |
| 41 super(context, attrs); | |
| 42 mCornerRadius = getResources().getDimensionPixelSize( | |
| 43 R.dimen.enhanced_bookmark_item_corner_radius); | |
| 44 mMinIconSize = (int) getResources().getDimension( | |
| 45 R.dimen.enhanced_bookmark_item_min_icon_size); | |
| 46 mDisplayedIconSize = getResources().getDimensionPixelSize( | |
| 47 R.dimen.enhanced_bookmark_item_icon_size); | |
| 48 int textSize = getResources().getDimensionPixelSize( | |
| 49 R.dimen.enhanced_bookmark_item_icon_text_size); | |
| 50 int iconColor = ApiCompatibilityUtils.getColor(getResources(), | |
| 51 R.color.enhanced_bookmark_icon_background_color); | |
| 52 mIconGenerator = new RoundedIconGenerator(mDisplayedIconSize , mDisplaye
dIconSize, | |
| 53 mCornerRadius, iconColor, textSize); | |
| 54 } | |
| 55 | |
| 56 // EnhancedBookmarkRow implementation. | |
| 57 | |
| 58 @Override | |
| 59 public void onClick() { | |
| 60 int launchLocation = -1; | |
| 61 switch (mDelegate.getCurrentState()) { | |
| 62 case EnhancedBookmarkUIState.STATE_ALL_BOOKMARKS: | |
| 63 launchLocation = BookmarkLaunchLocation.ALL_ITEMS; | |
| 64 break; | |
| 65 case EnhancedBookmarkUIState.STATE_FOLDER: | |
| 66 launchLocation = BookmarkLaunchLocation.FOLDER; | |
| 67 break; | |
| 68 case EnhancedBookmarkUIState.STATE_FILTER: | |
| 69 launchLocation = BookmarkLaunchLocation.FILTER; | |
| 70 break; | |
| 71 case EnhancedBookmarkUIState.STATE_LOADING: | |
| 72 assert false : | |
| 73 "The main content shouldn't be inflated if it's still lo
ading"; | |
| 74 break; | |
| 75 default: | |
| 76 assert false : "State not valid"; | |
| 77 break; | |
| 78 } | |
| 79 mDelegate.openBookmark(mBookmarkId, launchLocation); | |
| 80 } | |
| 81 | |
| 82 @Override | |
| 83 BookmarkItem setBookmarkId(BookmarkId bookmarkId) { | |
| 84 BookmarkItem item = super.setBookmarkId(bookmarkId); | |
| 85 mUrl = item.getUrl(); | |
| 86 mIconImageView.setImageDrawable(null); | |
| 87 mTitleView.setText(item.getTitle()); | |
| 88 mDelegate.getLargeIconBridge().getLargeIconForUrl(mUrl, mMinIconSize, th
is); | |
| 89 updateOfflinePageSize(bookmarkId); | |
| 90 return item; | |
| 91 } | |
| 92 | |
| 93 private void updateOfflinePageSize(BookmarkId bookmarkId) { | |
| 94 OfflinePageItem offlinePage = null; | |
| 95 OfflinePageBridge bridge = mDelegate.getModel().getOfflinePageBridge(); | |
| 96 if (mDelegate.getCurrentState() == EnhancedBookmarkUIState.STATE_FILTER
&& bridge != null) { | |
| 97 offlinePage = bridge.getPageByBookmarkId(bookmarkId); | |
| 98 } | |
| 99 TextView textView = (TextView) findViewById(R.id.offline_page_size); | |
| 100 View bookmarkRowView = findViewById(R.id.bookmark_row); | |
| 101 if (offlinePage != null) { | |
| 102 int verticalPadding = textView.getResources().getDimensionPixelSize( | |
| 103 R.dimen.offline_page_item_vertical_spacing); | |
| 104 textView.setText(Formatter.formatFileSize(getContext(), offlinePage.
getFileSize())); | |
| 105 // Get the embedded bookmark_row layout, and add padding. This is b
ecause the entries | |
| 106 // in filter view are larger (contain more items) than normal bookma
rk view. | |
| 107 bookmarkRowView.setPadding(0, verticalPadding / 2, 0, verticalPaddin
g / 2); | |
| 108 textView.setVisibility(View.VISIBLE); | |
| 109 } else { | |
| 110 textView.setVisibility(View.GONE); | |
| 111 // Remove padding when we leave filter view. | |
| 112 bookmarkRowView.setPadding(0, 0, 0, 0); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 // LargeIconCallback implementation. | |
| 117 | |
| 118 @Override | |
| 119 public void onLargeIconAvailable(Bitmap icon, int fallbackColor) { | |
| 120 if (icon == null) { | |
| 121 mIconGenerator.setBackgroundColor(fallbackColor); | |
| 122 icon = mIconGenerator.generateIconForUrl(mUrl); | |
| 123 mIconImageView.setImageDrawable(new BitmapDrawable(getResources(), i
con)); | |
| 124 } else { | |
| 125 RoundedBitmapDrawable roundedIcon = RoundedBitmapDrawableFactory.cre
ate( | |
| 126 getResources(), | |
| 127 Bitmap.createScaledBitmap(icon, mDisplayedIconSize, mDisplay
edIconSize, false)); | |
| 128 roundedIcon.setCornerRadius(mCornerRadius); | |
| 129 mIconImageView.setImageDrawable(roundedIcon); | |
| 130 } | |
| 131 } | |
| 132 } | |
| OLD | NEW |