| 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.util.AttributeSet; | |
| 9 import android.view.View; | |
| 10 import android.view.View.OnClickListener; | |
| 11 import android.view.View.OnLongClickListener; | |
| 12 import android.view.ViewGroup; | |
| 13 import android.widget.AdapterView; | |
| 14 import android.widget.AdapterView.OnItemClickListener; | |
| 15 import android.widget.ArrayAdapter; | |
| 16 import android.widget.Checkable; | |
| 17 import android.widget.FrameLayout; | |
| 18 import android.widget.ImageView; | |
| 19 import android.widget.ListPopupWindow; | |
| 20 import android.widget.TextView; | |
| 21 | |
| 22 import org.chromium.chrome.R; | |
| 23 import org.chromium.chrome.browser.bookmark.BookmarksBridge.BookmarkItem; | |
| 24 import org.chromium.chrome.browser.widget.TintedImageButton; | |
| 25 import org.chromium.components.bookmarks.BookmarkId; | |
| 26 | |
| 27 import java.util.List; | |
| 28 | |
| 29 /** | |
| 30 * Common logic for bookmark and folder rows. | |
| 31 */ | |
| 32 abstract class EnhancedBookmarkRow extends FrameLayout implements EnhancedBookma
rkUIObserver, | |
| 33 Checkable, OnClickListener, OnLongClickListener { | |
| 34 | |
| 35 protected ImageView mIconImageView; | |
| 36 protected TextView mTitleView; | |
| 37 protected TintedImageButton mMoreIcon; | |
| 38 private EnhancedBookmarkItemHighlightView mHighlightView; | |
| 39 | |
| 40 protected EnhancedBookmarkDelegate mDelegate; | |
| 41 protected BookmarkId mBookmarkId; | |
| 42 private ListPopupWindow mPopupMenu; | |
| 43 private boolean mIsAttachedToWindow = false; | |
| 44 | |
| 45 /** | |
| 46 * Constructor for inflating from XML. | |
| 47 */ | |
| 48 public EnhancedBookmarkRow(Context context, AttributeSet attrs) { | |
| 49 super(context, attrs); | |
| 50 } | |
| 51 | |
| 52 /** | |
| 53 * Updates this row for the given {@link BookmarkId}. | |
| 54 * @return The {@link BookmarkItem} corresponding the given {@link BookmarkI
d}. | |
| 55 */ | |
| 56 BookmarkItem setBookmarkId(BookmarkId bookmarkId) { | |
| 57 mBookmarkId = bookmarkId; | |
| 58 BookmarkItem bookmarkItem = mDelegate.getModel().getBookmarkById(bookmar
kId); | |
| 59 clearPopup(); | |
| 60 if (isSelectable()) { | |
| 61 mMoreIcon.setVisibility(bookmarkItem.isEditable() ? VISIBLE : GONE); | |
| 62 setChecked(mDelegate.isBookmarkSelected(bookmarkId)); | |
| 63 } | |
| 64 return bookmarkItem; | |
| 65 } | |
| 66 | |
| 67 /** | |
| 68 * Same as {@link OnClickListener#onClick(View)} on this. | |
| 69 * Subclasses should override this instead of setting their own OnClickListe
ner because this | |
| 70 * class handles onClick events in selection mode, and won't forward events
to subclasses in | |
| 71 * that case. | |
| 72 */ | |
| 73 protected abstract void onClick(); | |
| 74 | |
| 75 private void initialize() { | |
| 76 mDelegate.addUIObserver(this); | |
| 77 updateSelectionState(); | |
| 78 } | |
| 79 | |
| 80 private void clearPopup() { | |
| 81 if (mPopupMenu != null) { | |
| 82 if (mPopupMenu.isShowing()) mPopupMenu.dismiss(); | |
| 83 mPopupMenu = null; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 private void cleanup() { | |
| 88 clearPopup(); | |
| 89 if (mDelegate != null) mDelegate.removeUIObserver(this); | |
| 90 } | |
| 91 | |
| 92 private void updateSelectionState() { | |
| 93 if (isSelectable()) mMoreIcon.setClickable(!mDelegate.isSelectionEnabled
()); | |
| 94 } | |
| 95 | |
| 96 /** | |
| 97 * @return Whether this row is selectable. | |
| 98 */ | |
| 99 protected boolean isSelectable() { | |
| 100 return true; | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * Show drop-down menu after user click on more-info icon | |
| 105 * @param view The anchor view for the menu | |
| 106 */ | |
| 107 private void showMenu(View view) { | |
| 108 if (mPopupMenu == null) { | |
| 109 mPopupMenu = new ListPopupWindow(getContext(), null, 0, | |
| 110 R.style.EnhancedBookmarkMenuStyle); | |
| 111 mPopupMenu.setAdapter(new ArrayAdapter<String>( | |
| 112 getContext(), R.layout.eb_popup_item, new String[] { | |
| 113 getContext().getString(R.string.enhanced_bookmark_it
em_select), | |
| 114 getContext().getString(R.string.enhanced_bookmark_it
em_edit), | |
| 115 getContext().getString(R.string.enhanced_bookmark_it
em_move), | |
| 116 getContext().getString(R.string.enhanced_bookmark_it
em_delete)}) { | |
| 117 private static final int MOVE_POSITION = 2; | |
| 118 | |
| 119 @Override | |
| 120 public boolean areAllItemsEnabled() { | |
| 121 return false; | |
| 122 } | |
| 123 | |
| 124 @Override | |
| 125 public boolean isEnabled(int position) { | |
| 126 if (position == MOVE_POSITION) { | |
| 127 BookmarkItem bookmark = mDelegate.getModel().getBookmark
ById(mBookmarkId); | |
| 128 if (bookmark == null) return false; | |
| 129 return bookmark.isMovable(); | |
| 130 } | |
| 131 return true; | |
| 132 } | |
| 133 | |
| 134 @Override | |
| 135 public View getView(int position, View convertView, ViewGroup pa
rent) { | |
| 136 View view = super.getView(position, convertView, parent); | |
| 137 view.setEnabled(isEnabled(position)); | |
| 138 return view; | |
| 139 } | |
| 140 }); | |
| 141 mPopupMenu.setAnchorView(view); | |
| 142 mPopupMenu.setWidth(getResources().getDimensionPixelSize( | |
| 143 R.dimen.enhanced_bookmark_item_popup_width)); | |
| 144 mPopupMenu.setVerticalOffset(-view.getHeight()); | |
| 145 mPopupMenu.setModal(true); | |
| 146 mPopupMenu.setOnItemClickListener(new OnItemClickListener() { | |
| 147 @Override | |
| 148 public void onItemClick(AdapterView<?> parent, View view, int po
sition, | |
| 149 long id) { | |
| 150 if (position == 0) { | |
| 151 setChecked(mDelegate.toggleSelectionForBookmark(mBookmar
kId)); | |
| 152 } else if (position == 1) { | |
| 153 BookmarkItem item = mDelegate.getModel().getBookmarkById
(mBookmarkId); | |
| 154 if (item.isFolder()) { | |
| 155 EnhancedBookmarkAddEditFolderActivity.startEditFolde
rActivity( | |
| 156 getContext(), item.getId()); | |
| 157 } else { | |
| 158 EnhancedBookmarkUtils.startEditActivity( | |
| 159 getContext(), item.getId(), null); | |
| 160 } | |
| 161 } else if (position == 2) { | |
| 162 EnhancedBookmarkFolderSelectActivity.startFolderSelectAc
tivity(getContext(), | |
| 163 mBookmarkId); | |
| 164 } else if (position == 3) { | |
| 165 mDelegate.getModel().deleteBookmarks(mBookmarkId); | |
| 166 } | |
| 167 mPopupMenu.dismiss(); | |
| 168 } | |
| 169 }); | |
| 170 } | |
| 171 mPopupMenu.show(); | |
| 172 mPopupMenu.getListView().setDivider(null); | |
| 173 } | |
| 174 | |
| 175 // FrameLayout implementations. | |
| 176 | |
| 177 @Override | |
| 178 protected void onFinishInflate() { | |
| 179 super.onFinishInflate(); | |
| 180 | |
| 181 mIconImageView = (ImageView) findViewById(R.id.bookmark_image); | |
| 182 mTitleView = (TextView) findViewById(R.id.title); | |
| 183 | |
| 184 if (isSelectable()) { | |
| 185 mHighlightView = (EnhancedBookmarkItemHighlightView) findViewById(R.
id.highlight); | |
| 186 | |
| 187 mMoreIcon = (TintedImageButton) findViewById(R.id.more); | |
| 188 mMoreIcon.setVisibility(VISIBLE); | |
| 189 mMoreIcon.setOnClickListener(new OnClickListener() { | |
| 190 @Override | |
| 191 public void onClick(View view) { | |
| 192 showMenu(view); | |
| 193 } | |
| 194 }); | |
| 195 } | |
| 196 | |
| 197 setOnClickListener(this); | |
| 198 setOnLongClickListener(this); | |
| 199 } | |
| 200 | |
| 201 @Override | |
| 202 protected void onAttachedToWindow() { | |
| 203 super.onAttachedToWindow(); | |
| 204 mIsAttachedToWindow = true; | |
| 205 if (mDelegate != null) initialize(); | |
| 206 } | |
| 207 | |
| 208 @Override | |
| 209 protected void onDetachedFromWindow() { | |
| 210 super.onDetachedFromWindow(); | |
| 211 mIsAttachedToWindow = false; | |
| 212 cleanup(); | |
| 213 } | |
| 214 | |
| 215 // OnClickListener implementation. | |
| 216 | |
| 217 @Override | |
| 218 public final void onClick(View view) { | |
| 219 assert view == this; | |
| 220 | |
| 221 if (mDelegate.isSelectionEnabled() && isSelectable()) { | |
| 222 onLongClick(view); | |
| 223 } else { | |
| 224 onClick(); | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 // OnLongClickListener implementation. | |
| 229 | |
| 230 @Override | |
| 231 public boolean onLongClick(View view) { | |
| 232 assert view == this; | |
| 233 if (!isSelectable()) return false; | |
| 234 setChecked(mDelegate.toggleSelectionForBookmark(mBookmarkId)); | |
| 235 return true; | |
| 236 } | |
| 237 | |
| 238 // Checkable implementations. | |
| 239 | |
| 240 @Override | |
| 241 public boolean isChecked() { | |
| 242 return mHighlightView.isChecked(); | |
| 243 } | |
| 244 | |
| 245 @Override | |
| 246 public void toggle() { | |
| 247 setChecked(!isChecked()); | |
| 248 } | |
| 249 | |
| 250 @Override | |
| 251 public void setChecked(boolean checked) { | |
| 252 mHighlightView.setChecked(checked); | |
| 253 } | |
| 254 | |
| 255 // EnhancedBookmarkUIObserver implementations. | |
| 256 | |
| 257 @Override | |
| 258 public void onEnhancedBookmarkDelegateInitialized(EnhancedBookmarkDelegate d
elegate) { | |
| 259 assert mDelegate == null; | |
| 260 mDelegate = delegate; | |
| 261 if (mIsAttachedToWindow) initialize(); | |
| 262 } | |
| 263 | |
| 264 @Override | |
| 265 public void onDestroy() { | |
| 266 cleanup(); | |
| 267 } | |
| 268 | |
| 269 @Override | |
| 270 public void onAllBookmarksStateSet() { | |
| 271 } | |
| 272 | |
| 273 @Override | |
| 274 public void onFolderStateSet(BookmarkId folder) { | |
| 275 } | |
| 276 | |
| 277 @Override | |
| 278 public void onFilterStateSet(EnhancedBookmarkFilter filter) { | |
| 279 } | |
| 280 | |
| 281 @Override | |
| 282 public void onSelectionStateChange(List<BookmarkId> selectedBookmarks) { | |
| 283 updateSelectionState(); | |
| 284 } | |
| 285 } | |
| OLD | NEW |