Esempio n. 1
0
  public void prioritizeViewRange(int firstVisibleItem, int lastVisibleItem, int prefetchBuffer) {
    if ((lastVisibleItem < firstVisibleItem) || (sectionKeys.size() == 0)) {
      return;
    }

    // We want to prioritize requests for items which are visible but do not have pictures
    // loaded yet. We also want to pre-fetch pictures for items which are not yet visible
    // but are within a buffer on either side of the visible items, on the assumption that
    // they will be visible soon. For these latter items, we'll store the images in memory
    // in the hopes we can immediately populate their image view when needed.

    // Prioritize the requests in reverse order since each call to prioritizeRequest will just
    // move it to the front of the queue. And we want the earliest ones in the range to be at
    // the front of the queue, so all else being equal, the list will appear to populate from
    // the top down.
    for (int i = lastVisibleItem; i >= 0; i--) {
      SectionAndItem<T> sectionAndItem = getSectionAndItem(i);
      if (sectionAndItem.graphObject != null) {
        String id = getIdOfGraphObject(sectionAndItem.graphObject);
        ImageRequest request = pendingRequests.get(id);
        if (request != null) {
          ImageDownloader.prioritizeRequest(request);
        }
      }
    }

    // For items which are not visible, but within the buffer on either side, we want to
    // fetch those items and store them in a small in-memory cache of bitmaps.
    int start = Math.max(0, firstVisibleItem - prefetchBuffer);
    int end = Math.min(lastVisibleItem + prefetchBuffer, getCount() - 1);
    ArrayList<T> graphObjectsToPrefetchPicturesFor = new ArrayList<T>();
    // Add the IDs before and after the visible range.
    for (int i = start; i < firstVisibleItem; ++i) {
      SectionAndItem<T> sectionAndItem = getSectionAndItem(i);
      if (sectionAndItem.graphObject != null) {
        graphObjectsToPrefetchPicturesFor.add(sectionAndItem.graphObject);
      }
    }
    for (int i = lastVisibleItem + 1; i <= end; ++i) {
      SectionAndItem<T> sectionAndItem = getSectionAndItem(i);
      if (sectionAndItem.graphObject != null) {
        graphObjectsToPrefetchPicturesFor.add(sectionAndItem.graphObject);
      }
    }
    for (T graphObject : graphObjectsToPrefetchPicturesFor) {
      URI uri = getPictureUriOfGraphObject(graphObject);
      final String id = getIdOfGraphObject(graphObject);

      // This URL already have been requested for pre-fetching, but we want to act in an LRU manner,
      // so move
      // it to the end of the list regardless.
      boolean alreadyPrefetching = prefetchedProfilePictureIds.remove(id);
      prefetchedProfilePictureIds.add(id);

      // If we've already requested it for pre-fetching, no need to do so again.
      if (!alreadyPrefetching) {
        downloadProfilePicture(id, uri, null);
      }
    }
  }
Esempio n. 2
0
  protected void populateGraphObjectView(View view, T graphObject) {
    String id = getIdOfGraphObject(graphObject);
    view.setTag(id);

    CharSequence title = getTitleOfGraphObject(graphObject);
    TextView titleView = (TextView) view.findViewById(R.id.com_facebook_picker_title);
    if (titleView != null) {
      titleView.setText(title, TextView.BufferType.SPANNABLE);
    }

    CharSequence subtitle = getSubTitleOfGraphObject(graphObject);
    TextView subtitleView = (TextView) view.findViewById(R.id.picker_subtitle);
    if (subtitleView != null) {
      if (subtitle != null) {
        subtitleView.setText(subtitle, TextView.BufferType.SPANNABLE);
        subtitleView.setVisibility(View.VISIBLE);
      } else {
        subtitleView.setVisibility(View.GONE);
      }
    }

    if (getShowCheckbox()) {
      CheckBox checkBox = (CheckBox) view.findViewById(R.id.com_facebook_picker_checkbox);
      updateCheckboxState(checkBox, isGraphObjectSelected(id));
    }

    if (getShowPicture()) {
      URI pictureURI = getPictureUriOfGraphObject(graphObject);

      if (pictureURI != null) {
        ImageView profilePic = (ImageView) view.findViewById(R.id.com_facebook_picker_image);

        // See if we have already pre-fetched this; if not, download it.
        if (prefetchedPictureCache.containsKey(id)) {
          ImageResponse response = prefetchedPictureCache.get(id);
          profilePic.setImageBitmap(response.getBitmap());
          profilePic.setTag(response.getRequest().getImageUri());
        } else {
          downloadProfilePicture(id, pictureURI, profilePic);
        }
      }
    }
  }