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.view.LayoutInflater; |
| 8 import android.view.View; |
| 9 import android.view.ViewGroup; |
| 10 import android.widget.BaseAdapter; |
| 11 import android.widget.TextView; |
| 12 |
| 13 import com.google.android.apps.chrome.R; |
| 14 |
| 15 import org.chromium.components.bookmarks.BookmarkId; |
| 16 |
| 17 import java.util.ArrayList; |
| 18 import java.util.HashSet; |
| 19 import java.util.List; |
| 20 import java.util.Set; |
| 21 |
| 22 /** |
| 23 * BaseAdapter for EnhancedBookmarkDrawerListView. It manages items to list ther
e. |
| 24 */ |
| 25 class EnhancedBookmarkDrawerListViewAdapter extends BaseAdapter { |
| 26 static final int TYPE_FOLDER = 0; |
| 27 static final int TYPE_ALL_ITEMS = -1; |
| 28 static final int TYPE_DIVIDER = -2; |
| 29 static final int TYPE_FILTER = -3; |
| 30 static final int TYPE_FILTERS_TITLE = -4; |
| 31 static final int TYPE_FOLDERS_TITLE = -5; |
| 32 |
| 33 static final int VIEW_TYPE_ITEM = 0; |
| 34 static final int VIEW_TYPE_DIVIDER = 1; |
| 35 static final int VIEW_TYPE_TITLE = 2; |
| 36 |
| 37 private EnhancedBookmarkDelegate mDelegate; |
| 38 private List<Item> mTopSection = new ArrayList<Item>(); |
| 39 private List<Item> mMiddleSection = new ArrayList<Item>(); |
| 40 private List<Item> mBottomSection = new ArrayList<Item>(); |
| 41 // array containing the order of sections |
| 42 private List<?>[] mSections = {mTopSection, mMiddleSection, mBottomSection}; |
| 43 |
| 44 private BookmarkId mDesktopNodeId = null; |
| 45 private BookmarkId mMobileNodeId = null; |
| 46 private BookmarkId mOthersNodeId = null; |
| 47 private List<BookmarkId> mManagedAndPartnerFolderIds = null; |
| 48 |
| 49 /** |
| 50 * Represents each item in the list. |
| 51 */ |
| 52 static class Item { |
| 53 final int mType; |
| 54 final BookmarkId mFolderId; |
| 55 final String mFilter; |
| 56 |
| 57 Item(int itemType) { |
| 58 mType = itemType; |
| 59 mFolderId = null; |
| 60 mFilter = null; |
| 61 } |
| 62 |
| 63 Item(BookmarkId folderId) { |
| 64 assert folderId != null; |
| 65 mType = TYPE_FOLDER; |
| 66 mFolderId = folderId; |
| 67 mFilter = null; |
| 68 } |
| 69 |
| 70 Item(String filter) { |
| 71 assert filter != null; |
| 72 mType = TYPE_FILTER; |
| 73 mFilter = filter; |
| 74 mFolderId = null; |
| 75 } |
| 76 |
| 77 @Override |
| 78 public int hashCode() { |
| 79 // hash function generated by Eclipse |
| 80 final int prime = 31; |
| 81 int result = 1; |
| 82 result = prime * result + ((mFilter == null) ? 0 : mFilter.hashCode(
)); |
| 83 result = prime * result + ((mFolderId == null) ? 0 : mFolderId.hashC
ode()); |
| 84 result = prime * result + mType; |
| 85 return result; |
| 86 } |
| 87 |
| 88 @Override |
| 89 public boolean equals(Object obj) { |
| 90 if (this == obj) return true; |
| 91 if (obj == null) return false; |
| 92 if (getClass() != obj.getClass()) return false; |
| 93 Item other = (Item) obj; |
| 94 if (mFilter == null) { |
| 95 if (other.mFilter != null) return false; |
| 96 } else if (!mFilter.equals(other.mFilter)) { |
| 97 return false; |
| 98 } |
| 99 if (mFolderId == null) { |
| 100 if (other.mFolderId != null) return false; |
| 101 } else if (!mFolderId.equals(other.mFolderId)) { |
| 102 return false; |
| 103 } |
| 104 if (mType != other.mType) { |
| 105 return false; |
| 106 } |
| 107 return true; |
| 108 } |
| 109 } |
| 110 |
| 111 private void repopulateTopSection() { |
| 112 mTopSection.clear(); |
| 113 mTopSection.add(new Item(TYPE_ALL_ITEMS)); |
| 114 |
| 115 if (mDelegate.getModel().getBookmarkCountForFolder(mMobileNodeId) > 0) { |
| 116 mTopSection.add(new Item(mMobileNodeId)); |
| 117 } |
| 118 if (mDelegate.getModel().getBookmarkCountForFolder(mDesktopNodeId) > 0)
{ |
| 119 mTopSection.add(new Item(mDesktopNodeId)); |
| 120 } |
| 121 if (mDelegate.getModel().getBookmarkCountForFolder(mOthersNodeId) > 0) { |
| 122 mTopSection.add(new Item(mOthersNodeId)); |
| 123 } |
| 124 |
| 125 if (mManagedAndPartnerFolderIds != null) { |
| 126 for (BookmarkId id : mManagedAndPartnerFolderIds) { |
| 127 mTopSection.add(new Item(id)); |
| 128 } |
| 129 } |
| 130 } |
| 131 |
| 132 void setFilters(List<String> filters) { |
| 133 mBottomSection.clear(); |
| 134 if (filters.size() > 0) { |
| 135 // Add a divider and title to the top of the section. |
| 136 mBottomSection.add(new Item(TYPE_DIVIDER)); |
| 137 mBottomSection.add(new Item(TYPE_FILTERS_TITLE)); |
| 138 } |
| 139 |
| 140 // Add the rest of the items. |
| 141 for (String filter : filters) { |
| 142 mBottomSection.add(new Item(filter)); |
| 143 } |
| 144 } |
| 145 |
| 146 void updateList() { |
| 147 mDesktopNodeId = mDelegate.getModel().getDesktopFolderId(); |
| 148 mMobileNodeId = mDelegate.getModel().getMobileFolderId(); |
| 149 mOthersNodeId = mDelegate.getModel().getOtherFolderId(); |
| 150 mManagedAndPartnerFolderIds = mDelegate.getModel().getTopLevelFolderIDs(
true, false); |
| 151 repopulateTopSection(); |
| 152 |
| 153 setTopFolders(mDelegate.getModel().getTopLevelFolderIDs(false, true)); |
| 154 setFilters(mDelegate.getModel().getFilters()); |
| 155 notifyDataSetChanged(); |
| 156 } |
| 157 |
| 158 /** |
| 159 * Sets folders to show. |
| 160 */ |
| 161 void setTopFolders(List<BookmarkId> folders) { |
| 162 mMiddleSection.clear(); |
| 163 |
| 164 if (folders.size() > 0) { |
| 165 // Add a divider and title to the top of the section. |
| 166 mMiddleSection.add(new Item(TYPE_DIVIDER)); |
| 167 mMiddleSection.add(new Item(TYPE_FOLDERS_TITLE)); |
| 168 } |
| 169 |
| 170 // Add the rest of the items. |
| 171 for (BookmarkId id : folders) { |
| 172 mMiddleSection.add(new Item(id)); |
| 173 } |
| 174 } |
| 175 |
| 176 /** |
| 177 * Clear everything so that it doesn't have any entry. |
| 178 */ |
| 179 void clear() { |
| 180 mTopSection.clear(); |
| 181 mMiddleSection.clear(); |
| 182 mBottomSection.clear(); |
| 183 } |
| 184 |
| 185 void setEnhancedBookmarkUIDelegate(EnhancedBookmarkDelegate delegate) { |
| 186 mDelegate = delegate; |
| 187 } |
| 188 |
| 189 /** |
| 190 * Get the position in the list of a given item |
| 191 * @param item Item to search for |
| 192 * @return index of the item or -1 if not found |
| 193 */ |
| 194 int positionOfItem(Item item) { |
| 195 int offset = 0; |
| 196 for (List<?> section : mSections) { |
| 197 int index = section.indexOf(item); |
| 198 if (index != -1) { |
| 199 return offset + index; |
| 200 } |
| 201 // If not found in current section, offset the potential result by |
| 202 // the section size. |
| 203 offset += section.size(); |
| 204 } |
| 205 return -1; |
| 206 } |
| 207 |
| 208 /** |
| 209 * Get the position in the list of a given bookmark folder id |
| 210 * @param id Id of bookmark folder |
| 211 * @return index of bookmark folder or -1 if not found |
| 212 */ |
| 213 int positionOfBookmarkId(BookmarkId id) { |
| 214 return positionOfItem(new Item(id)); |
| 215 } |
| 216 |
| 217 /** |
| 218 * Get the position in the list of a given filter string |
| 219 * @param filter Filter to search for |
| 220 * @return index of bookmark folder or -1 if not found |
| 221 */ |
| 222 int positionOfFilter(String filter) { |
| 223 return positionOfItem(new Item(filter)); |
| 224 } |
| 225 |
| 226 /** |
| 227 * Get item position of the given mode. |
| 228 */ |
| 229 int getItemPosition(int state, Object modeDetail) { |
| 230 if (state == EnhancedBookmarkDelegate.STATE_ALL_BOOKMARKS) { |
| 231 return 0; |
| 232 } else if (state == EnhancedBookmarkDelegate.STATE_FOLDER) { |
| 233 Set<BookmarkId> topLevelFolderParents = new HashSet<>(); |
| 234 topLevelFolderParents.addAll(mDelegate.getModel().getTopLevelFolderP
arentIDs()); |
| 235 topLevelFolderParents.add(mDesktopNodeId); |
| 236 topLevelFolderParents.add(mOthersNodeId); |
| 237 topLevelFolderParents.add(mMobileNodeId); |
| 238 |
| 239 // Find top folder id that contains |folder|. |
| 240 BookmarkId topFolderId = (BookmarkId) modeDetail; |
| 241 while (true) { |
| 242 BookmarkId parentId = |
| 243 mDelegate.getModel().getBookmarkById(topFolderId).getPar
entId(); |
| 244 if (topLevelFolderParents.contains(parentId)) { |
| 245 break; |
| 246 } |
| 247 topFolderId = parentId; |
| 248 } |
| 249 return positionOfBookmarkId(topFolderId); |
| 250 } else if (state == EnhancedBookmarkDelegate.STATE_FILTER) { |
| 251 String filter = (String) modeDetail; |
| 252 return positionOfFilter(filter); |
| 253 } |
| 254 |
| 255 return -1; |
| 256 } |
| 257 |
| 258 // BaseAdapter implementations. |
| 259 |
| 260 @Override |
| 261 public boolean areAllItemsEnabled() { |
| 262 return false; |
| 263 } |
| 264 |
| 265 @Override |
| 266 public boolean isEnabled(int position) { |
| 267 Item item = (Item) getItem(position); |
| 268 return item.mType != TYPE_DIVIDER && item.mType != TYPE_FILTERS_TITLE |
| 269 && item.mType != TYPE_FOLDERS_TITLE; |
| 270 } |
| 271 |
| 272 @Override |
| 273 public int getItemViewType(int position) { |
| 274 Item item = (Item) getItem(position); |
| 275 if (item.mType == TYPE_DIVIDER) { |
| 276 return VIEW_TYPE_DIVIDER; |
| 277 } else if (item.mType == TYPE_FILTERS_TITLE || item.mType == TYPE_FOLDER
S_TITLE) { |
| 278 return VIEW_TYPE_TITLE; |
| 279 } else { |
| 280 return VIEW_TYPE_ITEM; |
| 281 } |
| 282 } |
| 283 |
| 284 @Override |
| 285 public int getViewTypeCount() { |
| 286 return 3; |
| 287 } |
| 288 |
| 289 @Override |
| 290 public int getCount() { |
| 291 int sum = 0; |
| 292 for (List<?> section : mSections) { |
| 293 sum += section.size(); |
| 294 } |
| 295 return sum; |
| 296 } |
| 297 |
| 298 @Override |
| 299 public long getItemId(int position) { |
| 300 return position; |
| 301 } |
| 302 |
| 303 @Override |
| 304 public Object getItem(int position) { |
| 305 if (position < 0) { |
| 306 return null; |
| 307 } |
| 308 for (List<?> section : mSections) { |
| 309 if (position < section.size()) { |
| 310 return section.get(position); |
| 311 } |
| 312 position -= section.size(); |
| 313 } |
| 314 return null; |
| 315 } |
| 316 |
| 317 @Override |
| 318 public View getView(int position, View convertView, ViewGroup parent) { |
| 319 final Item item = (Item) getItem(position); |
| 320 |
| 321 // Inflate view if convertView is null. |
| 322 if (convertView == null) { |
| 323 final int itemViewType = getItemViewType(position); |
| 324 if (itemViewType == VIEW_TYPE_ITEM) { |
| 325 convertView = LayoutInflater.from(parent.getContext()).inflate( |
| 326 R.layout.eb_drawer_item, parent, false); |
| 327 } else if (itemViewType == VIEW_TYPE_DIVIDER) { |
| 328 convertView = LayoutInflater.from(parent.getContext()).inflate( |
| 329 R.layout.eb_list_divider, parent, false); |
| 330 } else if (itemViewType == VIEW_TYPE_TITLE) { |
| 331 convertView = LayoutInflater.from(parent.getContext()).inflate( |
| 332 R.layout.eb_drawer_title, parent, false); |
| 333 } else { |
| 334 assert false : "Invalid item view type."; |
| 335 } |
| 336 } |
| 337 |
| 338 if (item.mType == TYPE_DIVIDER) return convertView; |
| 339 |
| 340 if (item.mType == TYPE_FILTERS_TITLE) { |
| 341 String title = convertView.getContext().getResources().getString( |
| 342 R.string.enhanced_bookmark_drawer_auto_folders); |
| 343 ((TextView) convertView).setText(title); |
| 344 return convertView; |
| 345 } |
| 346 |
| 347 if (item.mType == TYPE_FOLDERS_TITLE) { |
| 348 String title = convertView.getContext().getResources().getString( |
| 349 R.string.enhanced_bookmark_drawer_folders); |
| 350 ((TextView) convertView).setText(title); |
| 351 return convertView; |
| 352 } |
| 353 |
| 354 final EnhancedBookmarkDrawerListItemView listItemView = |
| 355 (EnhancedBookmarkDrawerListItemView) convertView; |
| 356 String title; |
| 357 int iconDrawableId; |
| 358 |
| 359 switch (item.mType) { |
| 360 case TYPE_ALL_ITEMS: |
| 361 title = listItemView.getContext().getResources() |
| 362 .getString(R.string.enhanced_bookmark_drawer_all_items); |
| 363 iconDrawableId = R.drawable.btn_star_filled; |
| 364 break; |
| 365 case TYPE_FOLDER: |
| 366 title = mDelegate.getModel().getBookmarkById(item.mFolderId).get
Title(); |
| 367 if (mManagedAndPartnerFolderIds != null |
| 368 && mManagedAndPartnerFolderIds.contains(item.mFolderId))
{ |
| 369 iconDrawableId = R.drawable.eb_managed; |
| 370 } else if (item.mFolderId.equals(mMobileNodeId)) { |
| 371 iconDrawableId = R.drawable.eb_mobile; |
| 372 } else if (item.mFolderId.equals(mDesktopNodeId)) { |
| 373 iconDrawableId = R.drawable.eb_bookmarks_bar; |
| 374 } else if (item.mFolderId.equals(mOthersNodeId)) { |
| 375 iconDrawableId = R.drawable.eb_others; |
| 376 } else { |
| 377 iconDrawableId = 0; |
| 378 } |
| 379 break; |
| 380 case TYPE_FILTER: |
| 381 title = item.mFilter; |
| 382 iconDrawableId = 0; |
| 383 break; |
| 384 default: |
| 385 title = ""; |
| 386 iconDrawableId = 0; |
| 387 assert false; |
| 388 } |
| 389 |
| 390 listItemView.setText(title); |
| 391 listItemView.setIcon(iconDrawableId); |
| 392 |
| 393 return convertView; |
| 394 } |
| 395 } |
OLD | NEW |