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); } } }
public static <T> ArrayList<T> arrayList(T... ts) { ArrayList<T> arrayList = new ArrayList<T>(ts.length); for (T t : ts) { arrayList.add(t); } return arrayList; }
public static <T> List<T> asListNoNulls(T... array) { ArrayList<T> result = new ArrayList<T>(); for (T t : array) { if (t != null) { result.add(t); } } return result; }
public List<T> getGraphObjectsById(Collection<String> ids) { Set<String> idSet = new HashSet<String>(); idSet.addAll(ids); ArrayList<T> result = new ArrayList<T>(idSet.size()); for (String id : idSet) { T graphObject = graphObjectsById.get(id); if (graphObject != null) { result.add(graphObject); } } return result; }
private void processImageResponse( ImageResponse response, String graphObjectId, ImageView imageView) { pendingRequests.remove(graphObjectId); if (response.getError() != null) { callOnErrorListener(response.getError()); } if (imageView == null) { // This was a pre-fetch request. if (response.getBitmap() != null) { // Is the cache too big? if (prefetchedPictureCache.size() >= MAX_PREFETCHED_PICTURES) { // Find the oldest one and remove it. String oldestId = prefetchedProfilePictureIds.remove(0); prefetchedPictureCache.remove(oldestId); } prefetchedPictureCache.put(graphObjectId, response); } } else if (graphObjectId.equals(imageView.getTag())) { Exception error = response.getError(); Bitmap bitmap = response.getBitmap(); if (error == null && bitmap != null) { imageView.setImageBitmap(bitmap); imageView.setTag(response.getRequest().getImageUri()); } } }
private void onFriendPickerDone(FriendPickerFragment fragment) { FragmentManager fm = getSupportFragmentManager(); fm.popBackStack(); String results = ""; Collection<GraphUser> selection = fragment.getSelection(); if (selection != null && selection.size() > 0) { ArrayList<String> names = new ArrayList<String>(); for (GraphUser user : selection) { names.add(user.getName()); } results = TextUtils.join(", ", names); } else { results = "no friends"; } showAlert("rar", results); }