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.annotation.SuppressLint; |
| 8 import android.content.Context; |
| 9 import android.graphics.Bitmap; |
| 10 import android.graphics.PorterDuff; |
| 11 import android.graphics.drawable.Drawable; |
| 12 import android.net.Uri; |
| 13 import android.util.AttributeSet; |
| 14 import android.view.View; |
| 15 import android.view.ViewGroup; |
| 16 import android.widget.AdapterView; |
| 17 import android.widget.AdapterView.OnItemClickListener; |
| 18 import android.widget.ArrayAdapter; |
| 19 import android.widget.FrameLayout; |
| 20 import android.widget.ListPopupWindow; |
| 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.enhanced_bookmarks.LaunchLocation; |
| 27 import org.chromium.chrome.browser.enhancedbookmarks.EnhancedBookmarkItemsAdapte
r.BookmarkGrid; |
| 28 import org.chromium.chrome.browser.enhancedbookmarks.EnhancedBookmarkSalientImag
eView.SalientImageDrawableFactory; |
| 29 import org.chromium.chrome.browser.widget.CustomShapeDrawable.CircularDrawable; |
| 30 import org.chromium.chrome.browser.widget.CustomShapeDrawable.TopRoundedCornerDr
awable; |
| 31 import org.chromium.chrome.browser.widget.TintedImageButton; |
| 32 import org.chromium.components.bookmarks.BookmarkId; |
| 33 import org.chromium.components.bookmarks.BookmarkType; |
| 34 |
| 35 import java.util.List; |
| 36 |
| 37 /** |
| 38 * A view that shows a bookmark's title, screenshot, URL, etc, shown in the enha
nced bookmarks UI. |
| 39 */ |
| 40 abstract class EnhancedBookmarkItem extends FrameLayout implements EnhancedBookm
arkUIObserver, |
| 41 SalientImageDrawableFactory, BookmarkGrid { |
| 42 /** |
| 43 * The item to show in list view mode. |
| 44 */ |
| 45 @SuppressLint("Instantiatable") |
| 46 static class EnhancedBookmarkListItem extends EnhancedBookmarkItem { |
| 47 public EnhancedBookmarkListItem(Context context, AttributeSet attrs) { |
| 48 super(context, attrs); |
| 49 } |
| 50 |
| 51 @Override |
| 52 public Drawable getSalientDrawable(int color) { |
| 53 return new CircularDrawable(color); |
| 54 } |
| 55 |
| 56 @Override |
| 57 public Drawable getSalientDrawable(Bitmap bitmap) { |
| 58 return new CircularDrawable(bitmap); |
| 59 } |
| 60 } |
| 61 |
| 62 /** |
| 63 * The item to show in grid mode. |
| 64 */ |
| 65 @SuppressLint("Instantiatable") |
| 66 static class EnhancedBookmarkGridItem extends EnhancedBookmarkItem { |
| 67 public EnhancedBookmarkGridItem(Context context, AttributeSet attrs) { |
| 68 super(context, attrs); |
| 69 } |
| 70 |
| 71 @Override |
| 72 public Drawable getSalientDrawable(int color) { |
| 73 return new TopRoundedCornerDrawable(color, getResources().getDimensi
onPixelSize( |
| 74 R.dimen.enhanced_bookmark_item_corner_radius)); |
| 75 } |
| 76 |
| 77 @Override |
| 78 public Drawable getSalientDrawable(Bitmap bitmap) { |
| 79 return new TopRoundedCornerDrawable(bitmap, getResources().getDimens
ionPixelSize( |
| 80 R.dimen.enhanced_bookmark_item_corner_radius)); |
| 81 } |
| 82 } |
| 83 |
| 84 private EnhancedBookmarkSalientImageView mSalientImageView; |
| 85 private TintedImageButton mMoreIcon; |
| 86 private TextView mFolderTitleView; |
| 87 private TextView mTitleView; |
| 88 private TextView mUrlView; |
| 89 private EnhancedBookmarkItemHighlightView mHighlightView; |
| 90 |
| 91 private BookmarkId mBookmarkId; |
| 92 private EnhancedBookmarkDelegate mDelegate; |
| 93 private ListPopupWindow mPopupMenu; |
| 94 private boolean mIsAttachedToWindow = false; |
| 95 |
| 96 /** |
| 97 * Constructor for inflating from XML. |
| 98 */ |
| 99 public EnhancedBookmarkItem(Context context, AttributeSet attrs) { |
| 100 super(context, attrs); |
| 101 } |
| 102 |
| 103 @Override |
| 104 protected void onFinishInflate() { |
| 105 super.onFinishInflate(); |
| 106 mSalientImageView = (EnhancedBookmarkSalientImageView) findViewById(R.id
.bookamrk_image); |
| 107 mMoreIcon = (TintedImageButton) findViewById(R.id.more); |
| 108 mMoreIcon.setOnClickListener(this); |
| 109 mMoreIcon.setColorFilterMode(PorterDuff.Mode.SRC.MULTIPLY); |
| 110 mFolderTitleView = (TextView) findViewById(R.id.folder_title); |
| 111 mTitleView = (TextView) findViewById(R.id.title); |
| 112 mUrlView = (TextView) findViewById(R.id.url); |
| 113 mHighlightView = (EnhancedBookmarkItemHighlightView) findViewById(R.id.h
ighlight); |
| 114 } |
| 115 |
| 116 @Override |
| 117 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 118 // Ensure all items are of the same width. |
| 119 int updatedWidthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 120 MeasureSpec.getSize(widthMeasureSpec), |
| 121 MeasureSpec.EXACTLY); |
| 122 super.onMeasure(updatedWidthMeasureSpec, heightMeasureSpec); |
| 123 } |
| 124 |
| 125 @Override |
| 126 public void setBookmarkId(BookmarkId bookmarkId) { |
| 127 mBookmarkId = bookmarkId; |
| 128 updateBookmarkInfo(); |
| 129 } |
| 130 |
| 131 @Override |
| 132 public BookmarkId getBookmarkId() { |
| 133 return mBookmarkId; |
| 134 } |
| 135 |
| 136 /** |
| 137 * Show drop-down menu after user click on more-info icon |
| 138 * @param view The anchor view for the menu |
| 139 */ |
| 140 private void showMenu(View view) { |
| 141 if (mPopupMenu == null) { |
| 142 mPopupMenu = new ListPopupWindow(getContext(), null, 0, |
| 143 R.style.EnhancedBookmarkMenuStyle); |
| 144 mPopupMenu.setAdapter(new ArrayAdapter<String>( |
| 145 getContext(), R.layout.eb_popup_item, new String[] { |
| 146 getContext().getString(R.string.enhanced_bookmark_it
em_select), |
| 147 getContext().getString(R.string.enhanced_bookmark_it
em_edit), |
| 148 getContext().getString(R.string.enhanced_bookmark_it
em_move), |
| 149 getContext().getString(R.string.enhanced_bookmark_it
em_delete)}) { |
| 150 private static final int MOVE_POSITION = 1; |
| 151 |
| 152 @Override |
| 153 public boolean areAllItemsEnabled() { |
| 154 return false; |
| 155 } |
| 156 |
| 157 @Override |
| 158 public boolean isEnabled(int position) { |
| 159 // Partner bookmarks can't move, so disable that. |
| 160 return mBookmarkId.getType() != BookmarkType.PARTNER |
| 161 || position != MOVE_POSITION; |
| 162 } |
| 163 |
| 164 @Override |
| 165 public View getView(int position, View convertView, ViewGroup pa
rent) { |
| 166 View view = super.getView(position, convertView, parent); |
| 167 view.setEnabled(isEnabled(position)); |
| 168 return view; |
| 169 } |
| 170 }); |
| 171 mPopupMenu.setAnchorView(view); |
| 172 mPopupMenu.setWidth(getResources().getDimensionPixelSize( |
| 173 R.dimen.enhanced_bookmark_item_popup_width)); |
| 174 mPopupMenu.setVerticalOffset(-view.getHeight()); |
| 175 mPopupMenu.setModal(true); |
| 176 mPopupMenu.setOnItemClickListener(new OnItemClickListener() { |
| 177 @Override |
| 178 public void onItemClick(AdapterView<?> parent, View view, int po
sition, |
| 179 long id) { |
| 180 if (position == 0) { |
| 181 setChecked(mDelegate.toggleSelectionForBookmark(mBookmar
kId)); |
| 182 } else if (position == 1) { |
| 183 mDelegate.startDetailActivity(mBookmarkId, mSalientImage
View); |
| 184 } else if (position == 2) { |
| 185 EnhancedBookmarkFolderSelectActivity.startFolderSelectAc
tivity(getContext(), |
| 186 mBookmarkId); |
| 187 } else if (position == 3) { |
| 188 mDelegate.getModel().deleteBookmarks(mBookmarkId); |
| 189 } |
| 190 mPopupMenu.dismiss(); |
| 191 } |
| 192 }); |
| 193 } |
| 194 mPopupMenu.show(); |
| 195 mPopupMenu.getListView().setDivider(null); |
| 196 } |
| 197 |
| 198 private void initialize() { |
| 199 mDelegate.addUIObserver(this); |
| 200 updateSelectionState(); |
| 201 } |
| 202 |
| 203 private void cleanup() { |
| 204 if (mPopupMenu != null) mPopupMenu.dismiss(); |
| 205 if (mDelegate != null) mDelegate.removeUIObserver(this); |
| 206 } |
| 207 |
| 208 private void updateSelectionState() { |
| 209 mMoreIcon.setClickable(!mDelegate.isSelectionEnabled()); |
| 210 } |
| 211 |
| 212 /** |
| 213 * Update the UI to reflect the current bookmark information. |
| 214 */ |
| 215 void updateBookmarkInfo() { |
| 216 if (mBookmarkId == null) return; |
| 217 |
| 218 BookmarkItem bookmarkItem = mDelegate.getModel().getBookmarkById(mBookma
rkId); |
| 219 mMoreIcon.setVisibility(bookmarkItem.isEditable() ? VISIBLE : GONE); |
| 220 |
| 221 mTitleView.setText(bookmarkItem.getTitle()); |
| 222 mUrlView.setText(Uri.parse(bookmarkItem.getUrl()).getHost()); |
| 223 |
| 224 mFolderTitleView.setVisibility(View.INVISIBLE); |
| 225 BookmarkId parentId = bookmarkItem.getParentId(); |
| 226 // On folder mode, folder name is shown at top so no need to show it aga
in. |
| 227 if (mDelegate.getCurrentState() != EnhancedBookmarkDelegate.STATE_FOLDER |
| 228 && parentId != null) { |
| 229 BookmarkItem parentItem = mDelegate.getModel().getBookmarkById(paren
tId); |
| 230 if (parentItem != null) { |
| 231 mFolderTitleView.setVisibility(View.VISIBLE); |
| 232 mFolderTitleView.setText(parentItem.getTitle()); |
| 233 } |
| 234 } |
| 235 |
| 236 mSalientImageView.load(mDelegate.getModel(), bookmarkItem.getUrl(), |
| 237 EnhancedBookmarkUtils.generateBackgroundColor(bookmarkItem), thi
s); |
| 238 } |
| 239 |
| 240 @Override |
| 241 public void onClick(View view) { |
| 242 if (view == mMoreIcon) { |
| 243 showMenu(view); |
| 244 } else { |
| 245 int launchLocation = -1; |
| 246 switch (mDelegate.getCurrentState()) { |
| 247 case EnhancedBookmarkDelegate.STATE_ALL_BOOKMARKS: |
| 248 launchLocation = LaunchLocation.ALL_ITEMS; |
| 249 break; |
| 250 case EnhancedBookmarkDelegate.STATE_FOLDER: |
| 251 launchLocation = LaunchLocation.FOLDER; |
| 252 break; |
| 253 case EnhancedBookmarkDelegate.STATE_FILTER: |
| 254 launchLocation = LaunchLocation.FILTER; |
| 255 break; |
| 256 case EnhancedBookmarkDelegate.STATE_LOADING: |
| 257 assert false : |
| 258 "The main content shouldn't be inflated if it's stil
l loading"; |
| 259 break; |
| 260 default: |
| 261 assert false : "State not valid"; |
| 262 break; |
| 263 } |
| 264 mDelegate.openBookmark(mBookmarkId, launchLocation); |
| 265 } |
| 266 } |
| 267 |
| 268 @Override |
| 269 protected void onAttachedToWindow() { |
| 270 super.onAttachedToWindow(); |
| 271 mIsAttachedToWindow = true; |
| 272 if (mDelegate != null) initialize(); |
| 273 } |
| 274 |
| 275 @Override |
| 276 protected void onDetachedFromWindow() { |
| 277 super.onDetachedFromWindow(); |
| 278 mIsAttachedToWindow = false; |
| 279 cleanup(); |
| 280 } |
| 281 |
| 282 // Checkable implementations. |
| 283 |
| 284 @Override |
| 285 public boolean isChecked() { |
| 286 return mHighlightView.isChecked(); |
| 287 } |
| 288 |
| 289 @Override |
| 290 public void toggle() { |
| 291 setChecked(!isChecked()); |
| 292 } |
| 293 |
| 294 @Override |
| 295 public void setChecked(boolean checked) { |
| 296 mHighlightView.setChecked(checked); |
| 297 } |
| 298 |
| 299 // EnhancedBookmarkUIObserver implementations. |
| 300 |
| 301 @Override |
| 302 public void onEnhancedBookmarkDelegateInitialized(EnhancedBookmarkDelegate d
elegate) { |
| 303 assert mDelegate == null; |
| 304 mDelegate = delegate; |
| 305 if (mIsAttachedToWindow) initialize(); |
| 306 } |
| 307 |
| 308 @Override |
| 309 public void onDestroy() { |
| 310 cleanup(); |
| 311 } |
| 312 |
| 313 @Override |
| 314 public void onAllBookmarksStateSet() { |
| 315 } |
| 316 |
| 317 @Override |
| 318 public void onFolderStateSet(BookmarkId folder) { |
| 319 } |
| 320 |
| 321 @Override |
| 322 public void onFilterStateSet(String filter) { |
| 323 } |
| 324 |
| 325 @Override |
| 326 public void onSelectionStateChange(List<BookmarkId> selectedBookmarks) { |
| 327 updateSelectionState(); |
| 328 } |
| 329 |
| 330 @Override |
| 331 public void onListModeChange(boolean isListModeEnabled) {} |
| 332 } |
OLD | NEW |