public static void prioritizeRequest(ImageRequest request) { RequestKey key = new RequestKey(request.getImageUri(), request.getCallerTag()); synchronized (pendingRequests) { DownloaderContext downloaderContext = pendingRequests.get(key); if (downloaderContext != null) { downloaderContext.workItem.moveToFront(); } } }
/** * Getter for the Uri of the profile picture. * * @param width The desired width for the profile picture. * @param height The desired height for the profile picture. * @return The Uri of the profile picture. */ public Uri getProfilePictureUri( int width, int height) { return ImageRequest.getProfilePictureUri(this.id, width, height); }
public ImageRequest build() { return new ImageRequest(this); } }
/** * Downloads the image specified in the passed in request. * If a callback is specified, it is guaranteed to be invoked on the calling thread. * @param request Request to process */ public static void downloadAsync(ImageRequest request) { if (request == null) { return; } // NOTE: This is the ONLY place where the original request's Url is read. From here on, // we will keep track of the Url separately. This is because we might be dealing with a // redirect response and the Url might change. We can't create our own new ImageRequests // for these changed Urls since the caller might be doing some book-keeping with the // requests object reference. So we keep the old references and just map them to new urls in // the downloader. RequestKey key = new RequestKey(request.getImageUri(), request.getCallerTag()); synchronized (pendingRequests) { DownloaderContext downloaderContext = pendingRequests.get(key); if (downloaderContext != null) { downloaderContext.request = request; downloaderContext.isCancelled = false; downloaderContext.workItem.moveToFront(); } else { enqueueCacheRead(request, key, request.isCachedRedirectAllowed()); } } }
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 static void enqueueDownload(ImageRequest request, RequestKey key) { enqueueRequest( request, key, downloadQueue, new DownloadImageWorkItem(request.getContext(), key)); }
private static void issueResponse( RequestKey key, final Exception error, final Bitmap bitmap, final boolean isCachedRedirect) { // Once the old downloader context is removed, we are thread-safe since this is the // only reference to it DownloaderContext completedRequestContext = removePendingRequest(key); if (completedRequestContext != null && !completedRequestContext.isCancelled) { final ImageRequest request = completedRequestContext.request; final ImageRequest.Callback callback = request.getCallback(); if (callback != null) { getHandler().post(new Runnable() { @Override public void run() { ImageResponse response = new ImageResponse( request, error, isCachedRedirect, bitmap); callback.onCompleted(response); } }); } } }
ImageResponse response = prefetchedPictureCache.get(id); profilePic.setImageBitmap(response.getBitmap()); profilePic.setTag(response.getRequest().getImageUri()); } else { downloadProfilePicture(id, pictureURI, profilePic);
/** * Downloads the image specified in the passed in request. * If a callback is specified, it is guaranteed to be invoked on the calling thread. * @param request Request to process */ public static void downloadAsync(ImageRequest request) { if (request == null) { return; } // NOTE: This is the ONLY place where the original request's Url is read. From here on, // we will keep track of the Url separately. This is because we might be dealing with a // redirect response and the Url might change. We can't create our own new ImageRequests // for these changed Urls since the caller might be doing some book-keeping with the request's // object reference. So we keep the old references and just map them to new urls in the downloader RequestKey key = new RequestKey(request.getImageUri(), request.getCallerTag()); synchronized (pendingRequests) { DownloaderContext downloaderContext = pendingRequests.get(key); if (downloaderContext != null) { downloaderContext.request = request; downloaderContext.isCancelled = false; downloaderContext.workItem.moveToFront(); } else { enqueueCacheRead(request, key, request.isCachedRedirectAllowed()); } } }
private static void enqueueCacheRead( ImageRequest request, RequestKey key, boolean allowCachedRedirects) { enqueueRequest( request, key, cacheReadQueue, new CacheReadWorkItem(request.getContext(), key, allowCachedRedirects)); }
private static void issueResponse( RequestKey key, final Exception error, final Bitmap bitmap, final boolean isCachedRedirect) { // Once the old downloader context is removed, we are thread-safe since this is the // only reference to it DownloaderContext completedRequestContext = removePendingRequest(key); if (completedRequestContext != null && !completedRequestContext.isCancelled) { final ImageRequest request = completedRequestContext.request; final ImageRequest.Callback callback = request.getCallback(); if (callback != null) { getHandler().post(new Runnable() { @Override public void run() { ImageResponse response = new ImageResponse( request, error, isCachedRedirect, bitmap); callback.onCompleted(response); } }); } } }
private void processImageResponse(String id, ImageResponse response) { if (response != null) { Bitmap bitmap = response.getBitmap(); if (bitmap != null) { BitmapDrawable drawable = new BitmapDrawable( UserSettingsFragment.this.getResources(), bitmap); drawable.setBounds(0, 0, getResources().getDimensionPixelSize( R.dimen.usersettings_fragment_profile_picture_width), getResources().getDimensionPixelSize( R.dimen.usersettings_fragment_profile_picture_height)); userProfilePic = drawable; userProfilePicID = id; connectedStateLabel.setCompoundDrawables(null, drawable, null, null); connectedStateLabel.setTag(response.getRequest().getImageUri()); } } } }
public static boolean cancelRequest(ImageRequest request) { boolean cancelled = false; RequestKey key = new RequestKey(request.getImageUri(), request.getCallerTag()); synchronized (pendingRequests) { DownloaderContext downloaderContext = pendingRequests.get(key); if (downloaderContext != null) { // If we were able to find the request in our list of pending requests, then we will // definitely be able to prevent an ImageResponse from being issued. This is // regardless of whether a cache-read or network-download is underway for this // request. cancelled = true; if (downloaderContext.workItem.cancel()) { pendingRequests.remove(key); } else { // May be attempting a cache-read right now. So keep track of the cancellation // to prevent network calls etc downloaderContext.isCancelled = true; } } } return cancelled; }
private ImageRequest getImageRequest() { ImageRequest request = null; ImageRequest.Builder requestBuilder = new ImageRequest.Builder( getActivity(), ImageRequest.getProfilePictureUri( user.optString("id"), getResources().getDimensionPixelSize( R.dimen.usersettings_fragment_profile_picture_width), getResources().getDimensionPixelSize( R.dimen.usersettings_fragment_profile_picture_height))); request = requestBuilder.setCallerTag(this) .setCallback( new ImageRequest.Callback() { @Override public void onCompleted(ImageResponse response) { processImageResponse(user.optString("id"), response); } }) .build(); return request; }
private static void enqueueDownload(ImageRequest request, RequestKey key) { enqueueRequest( request, key, downloadQueue, new DownloadImageWorkItem(request.getContext(), key)); }
public ImageRequest build() { return new ImageRequest(this); } }
private void processImageResponse(String id, ImageResponse response) { if (response != null) { Bitmap bitmap = response.getBitmap(); if (bitmap != null) { BitmapDrawable drawable = new BitmapDrawable( UserSettingsFragment.this.getResources(), bitmap); drawable.setBounds(0, 0, getResources().getDimensionPixelSize( R.dimen.usersettings_fragment_profile_picture_width), getResources().getDimensionPixelSize( R.dimen.usersettings_fragment_profile_picture_height)); userProfilePic = drawable; userProfilePicID = id; connectedStateLabel.setCompoundDrawables(null, drawable, null, null); connectedStateLabel.setTag(response.getRequest().getImageUri()); } } } }
public static void prioritizeRequest(ImageRequest request) { RequestKey key = new RequestKey(request.getImageUri(), request.getCallerTag()); synchronized (pendingRequests) { DownloaderContext downloaderContext = pendingRequests.get(key); if (downloaderContext != null) { downloaderContext.workItem.moveToFront(); } } }
private ImageRequest getImageRequest() { ImageRequest request = null; ImageRequest.Builder requestBuilder = new ImageRequest.Builder( getActivity(), ImageRequest.getProfilePictureUri( user.optString("id"), getResources().getDimensionPixelSize( R.dimen.usersettings_fragment_profile_picture_width), getResources().getDimensionPixelSize( R.dimen.usersettings_fragment_profile_picture_height))); request = requestBuilder.setCallerTag(this) .setCallback( new ImageRequest.Callback() { @Override public void onCompleted(ImageResponse response) { processImageResponse(user.optString("id"), response); } }) .build(); return request; }
private static void enqueueCacheRead(ImageRequest request, RequestKey key, boolean allowCachedRedirects) { enqueueRequest( request, key, cacheReadQueue, new CacheReadWorkItem(request.getContext(), key, allowCachedRedirects)); }