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.annotation.SuppressLint; |
| 8 import android.content.Context; |
| 9 import android.content.res.Resources; |
| 10 import android.content.res.TypedArray; |
| 11 import android.graphics.Bitmap; |
| 12 import android.graphics.drawable.BitmapDrawable; |
| 13 import android.graphics.drawable.Drawable; |
| 14 import android.support.v7.widget.AppCompatTextView; |
| 15 import android.text.TextUtils; |
| 16 import android.util.TypedValue; |
| 17 import android.view.ContextMenu; |
| 18 import android.view.ContextMenu.ContextMenuInfo; |
| 19 import android.view.Gravity; |
| 20 import android.view.Menu; |
| 21 import android.view.MenuItem; |
| 22 import android.view.View; |
| 23 import android.view.View.OnClickListener; |
| 24 import android.view.View.OnCreateContextMenuListener; |
| 25 |
| 26 import com.google.android.apps.chrome.R; |
| 27 |
| 28 import org.chromium.base.ApiCompatibilityUtils; |
| 29 import org.chromium.chrome.browser.ntp.BookmarksPageView.BookmarksPageManager; |
| 30 import org.chromium.chrome.browser.widget.TintedDrawable; |
| 31 import org.chromium.components.bookmarks.BookmarkId; |
| 32 |
| 33 /** |
| 34 * Displays the bookmark item along with the favicon. This item can be clicked t
o be taken to that |
| 35 * page. |
| 36 */ |
| 37 class BookmarkItemView extends AppCompatTextView implements OnCreateContextMenuL
istener, |
| 38 MenuItem.OnMenuItemClickListener, OnClickListener { |
| 39 |
| 40 // Context menu item IDs. |
| 41 static final int ID_OPEN_IN_NEW_TAB = 0; |
| 42 static final int ID_OPEN_IN_INCOGNITO_TAB = 1; |
| 43 static final int ID_DELETE = 2; |
| 44 static final int ID_EDIT = 3; |
| 45 |
| 46 /** |
| 47 * Drawing-related values that can be shared between instances of BookmarkIt
em. |
| 48 */ |
| 49 static final class DrawingData { |
| 50 |
| 51 private final int mPadding; |
| 52 private final int mMinHeight; |
| 53 private final int mFaviconSize; |
| 54 private final int mFaviconContainerSize; |
| 55 private final int mTextSize; |
| 56 private final int mTextColor; |
| 57 |
| 58 /** |
| 59 * Initialize shared values used for drawing the favicon, borders and sh
adows. |
| 60 * @param context The view context in which the BookmarkItem will be dra
wn. |
| 61 */ |
| 62 DrawingData(Context context) { |
| 63 Resources res = context.getResources(); |
| 64 mPadding = res.getDimensionPixelOffset(R.dimen.ntp_list_item_padding
); |
| 65 mMinHeight = res.getDimensionPixelSize(R.dimen.ntp_list_item_min_hei
ght); |
| 66 mFaviconSize = res.getDimensionPixelSize(R.dimen.ntp_list_item_favic
on_size); |
| 67 mFaviconContainerSize = res.getDimensionPixelSize( |
| 68 R.dimen.ntp_list_item_favicon_container_size); |
| 69 mTextSize = res.getDimensionPixelSize(R.dimen.ntp_list_item_text_siz
e); |
| 70 mTextColor = res.getColor(R.color.ntp_list_item_text); |
| 71 } |
| 72 } |
| 73 |
| 74 private final BookmarksPageManager mManager; |
| 75 private final DrawingData mDrawingData; |
| 76 private String mTitle; |
| 77 private String mUrl; |
| 78 private BookmarkId mId; |
| 79 private boolean mIsFolder; |
| 80 private boolean mIsEditable; |
| 81 private boolean mIsManaged; |
| 82 private Bitmap mFavicon; |
| 83 |
| 84 /** |
| 85 * @param context The view context in which this item will be shown. |
| 86 * @param manager The BookmarksPageManager used to handle clicks. |
| 87 * @param id The id of the bookmark item. |
| 88 * @param title The title of the page. |
| 89 * @param url The URL of the page. |
| 90 * @param isEditable Whether this bookmark item can be edited. |
| 91 * @param isManaged Whether this is a managed bookmark. |
| 92 */ |
| 93 @SuppressLint("InlinedApi") |
| 94 BookmarkItemView(Context context, BookmarksPageManager manager, BookmarkId i
d, String title, |
| 95 String url, boolean isEditable, boolean isManaged, DrawingData drawi
ngData) { |
| 96 super(context); |
| 97 mManager = manager; |
| 98 mDrawingData = drawingData; |
| 99 |
| 100 setTextColor(mDrawingData.mTextColor); |
| 101 setTextSize(TypedValue.COMPLEX_UNIT_PX, mDrawingData.mTextSize); |
| 102 setMinimumHeight(mDrawingData.mMinHeight); |
| 103 setGravity(Gravity.CENTER_VERTICAL); |
| 104 setSingleLine(); |
| 105 setEllipsize(TextUtils.TruncateAt.END); |
| 106 ApiCompatibilityUtils.setTextAlignment(this, View.TEXT_ALIGNMENT_VIEW_ST
ART); |
| 107 |
| 108 TypedArray a = context.getTheme().obtainStyledAttributes( |
| 109 new int[] { R.attr.listChoiceBackgroundIndicator }); |
| 110 Drawable background = a.getDrawable(0); |
| 111 a.recycle(); |
| 112 setBackground(background); |
| 113 |
| 114 setOnClickListener(this); |
| 115 setOnCreateContextMenuListener(this); |
| 116 |
| 117 reset(id, title, url, isEditable, isManaged); |
| 118 } |
| 119 |
| 120 /** |
| 121 * Resets the view contents so that it can be reused in the listview. |
| 122 * @param id The id of the bookmark item. |
| 123 * @param title The title of the page. |
| 124 * @param url The URL of the page. |
| 125 * @param isEditable Whether this bookmark item can be edited. |
| 126 * @param isManaged Whether this is a managed bookmark. |
| 127 * @return boolean Whether the values were reset needing a favicon refetch. |
| 128 */ |
| 129 public boolean reset(BookmarkId id, String title, String url, boolean isEdit
able, |
| 130 boolean isManaged) { |
| 131 // Reset drawable state so ripples don't continue when the view is reuse
d. |
| 132 jumpDrawablesToCurrentState(); |
| 133 |
| 134 if (mId != null && mId.equals(id) && TextUtils.equals(title, mTitle) |
| 135 && TextUtils.equals(url, mUrl) && isEditable == mIsEditable |
| 136 && isManaged == mIsManaged) { |
| 137 return false; |
| 138 } |
| 139 mTitle = title; |
| 140 mUrl = url; |
| 141 mIsFolder = TextUtils.isEmpty(mUrl); |
| 142 mIsEditable = isEditable; |
| 143 mIsManaged = isManaged; |
| 144 mId = id; |
| 145 setText(mTitle); |
| 146 setFavicon(null); |
| 147 if (mIsFolder) { |
| 148 setContentDescription(getResources().getString( |
| 149 R.string.accessibility_bookmark_folder, mTitle)); |
| 150 } |
| 151 return true; |
| 152 } |
| 153 |
| 154 /** @return The URL of this bookmark item. */ |
| 155 public String getUrl() { |
| 156 return mUrl; |
| 157 } |
| 158 |
| 159 /** @return The title of this bookmark item. */ |
| 160 public String getTitle() { |
| 161 return mTitle; |
| 162 } |
| 163 |
| 164 /** @return Whether the BookmarkItem is a folder. */ |
| 165 public boolean isFolder() { |
| 166 return mIsFolder; |
| 167 } |
| 168 |
| 169 /** @return The bookmark/folder id. */ |
| 170 public BookmarkId getBookmarkId() { |
| 171 return mId; |
| 172 } |
| 173 |
| 174 /** @return The favicon of this bookmark item. */ |
| 175 public Bitmap getFavicon() { |
| 176 return mFavicon; |
| 177 } |
| 178 |
| 179 /** |
| 180 * Updates the favicon and triggers a redraw with the new favicon |
| 181 * @param favicon The new favicon to display. May be null. |
| 182 */ |
| 183 void setFavicon(Bitmap favicon) { |
| 184 int padding = mDrawingData.mPadding; |
| 185 int startPadding = padding; |
| 186 int drawablePadding = mDrawingData.mPadding; |
| 187 Drawable faviconDrawable = null; |
| 188 mFavicon = favicon; |
| 189 if (favicon != null || mIsFolder) { |
| 190 int iconSize; |
| 191 if (mIsFolder) { |
| 192 faviconDrawable = TintedDrawable.constructTintedDrawable(getReso
urces(), |
| 193 mIsManaged ? R.drawable.eb_managed : R.drawable.eb_other
s); |
| 194 iconSize = mDrawingData.mFaviconContainerSize; |
| 195 } else { |
| 196 faviconDrawable = new BitmapDrawable(getResources(), favicon); |
| 197 iconSize = mDrawingData.mFaviconSize; |
| 198 startPadding += (mDrawingData.mFaviconContainerSize - iconSize)
/ 2; |
| 199 drawablePadding += (mDrawingData.mFaviconContainerSize - iconSiz
e + 1) / 2; |
| 200 } |
| 201 faviconDrawable.setBounds(0, 0, iconSize, iconSize); |
| 202 setCompoundDrawablePadding(drawablePadding); |
| 203 } else { |
| 204 startPadding = 2 * padding + mDrawingData.mFaviconContainerSize; |
| 205 } |
| 206 ApiCompatibilityUtils.setPaddingRelative(this, startPadding, 0, padding,
0); |
| 207 ApiCompatibilityUtils.setCompoundDrawablesRelative(this, faviconDrawable
, null, null, null); |
| 208 } |
| 209 |
| 210 @Override |
| 211 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo me
nuInfo) { |
| 212 if (mManager.isDestroyed()) return; |
| 213 if (!mManager.isContextMenuEnabled()) return; |
| 214 if (!mIsFolder && mManager.shouldShowOpenInNewTab()) { |
| 215 menu.add(Menu.NONE, ID_OPEN_IN_NEW_TAB, Menu.NONE, |
| 216 R.string.contextmenu_open_in_new_tab).setOnMenuItemClickList
ener(this); |
| 217 } |
| 218 if (!mIsFolder && mManager.shouldShowOpenInNewIncognitoTab()) { |
| 219 menu.add(Menu.NONE, ID_OPEN_IN_INCOGNITO_TAB, Menu.NONE, |
| 220 R.string.contextmenu_open_in_incognito_tab).setOnMenuItemCl
ickListener(this); |
| 221 } |
| 222 if (mIsEditable && !mManager.isIncognito()) { |
| 223 menu.add(Menu.NONE, ID_EDIT, Menu.NONE, mIsFolder |
| 224 ? R.string.contextmenu_edit_folder : R.string.contextmenu_ed
it_bookmark) |
| 225 .setOnMenuItemClickListener(this); |
| 226 menu.add(Menu.NONE, ID_DELETE, Menu.NONE, mIsFolder |
| 227 ? R.string.contextmenu_delete_folder : R.string.contextmenu_
delete_bookmark) |
| 228 .setOnMenuItemClickListener(this); |
| 229 } |
| 230 } |
| 231 |
| 232 @Override |
| 233 public boolean onMenuItemClick(MenuItem item) { |
| 234 if (mManager.isDestroyed()) return true; |
| 235 switch (item.getItemId()) { |
| 236 case ID_OPEN_IN_NEW_TAB: |
| 237 mManager.openInNewTab(this); |
| 238 return true; |
| 239 case ID_OPEN_IN_INCOGNITO_TAB: |
| 240 mManager.openInNewIncognitoTab(this); |
| 241 return true; |
| 242 case ID_DELETE: |
| 243 mManager.delete(this); |
| 244 return true; |
| 245 case ID_EDIT: |
| 246 mManager.edit(this); |
| 247 return true; |
| 248 default: |
| 249 return false; |
| 250 } |
| 251 } |
| 252 |
| 253 @Override |
| 254 public void onClick(View v) { |
| 255 if (mManager.isDestroyed()) return; |
| 256 mManager.open(this); |
| 257 } |
| 258 } |
OLD | NEW |