static InputStream interceptAndCacheImageStream(Context context, HttpURLConnection connection) throws IOException { InputStream stream = null; if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { URL url = connection.getURL(); stream = connection.getInputStream(); // Default stream in case caching fails if (isCDNURL(url)) { try { FileLruCache cache = getCache(context); // Wrap stream with a caching stream stream = cache.interceptAndPut( url.toString(), new BufferedHttpInputStream(stream, connection)); } catch (IOException e) { // Caching is best effort } } } return stream; }
static void cacheUrlRedirect(Context context, URL fromUrl, URL toUrl) { if (fromUrl == null || toUrl == null) { return; } OutputStream redirectStream = null; try { FileLruCache cache = getCache(context); redirectStream = cache.openPutStream(fromUrl.toString(), REDIRECT_CONTENT_TAG); redirectStream.write(toUrl.toString().getBytes()); } catch (IOException e) { // Caching is best effort } finally { Utility.closeQuietly(redirectStream); } }
public static void clearFileLruCache(final FileLruCache cache) throws InterruptedException { // since the cache clearing happens in a separate thread, we need to wait until // the clear is complete before we can check for the existence of the old files synchronized (cache) { cache.clearCache(); Settings.getExecutor().execute(new Runnable() { @Override public void run() { synchronized (cache) { cache.notifyAll(); } } }); cache.wait(CACHE_CLEAR_TIMEOUT); } // sleep a little more just to make sure all the files are deleted. Thread.sleep(CACHE_CLEAR_TIMEOUT); }
private synchronized static void performFirstInitialize() { if (isInitialized) { return; } handler = new Handler(Looper.getMainLooper()); Context appContext = FacebookSdk.getApplicationContext(); SharedPreferences sharedPreferences = appContext.getSharedPreferences( LIKE_ACTION_CONTROLLER_STORE, Context.MODE_PRIVATE); objectSuffix = sharedPreferences.getInt(LIKE_ACTION_CONTROLLER_STORE_OBJECT_SUFFIX_KEY, 1); controllerDiskCache = new FileLruCache(TAG, new FileLruCache.Limits()); registerAccessTokenTracker(); CallbackManagerImpl.registerStaticCallback( CallbackManagerImpl.RequestCodeOffset.Like.toRequestCode(), new CallbackManagerImpl.Callback() { @Override public boolean onActivityResult(int resultCode, Intent data) { return handleOnActivityResult( CallbackManagerImpl.RequestCodeOffset.Like.toRequestCode(), resultCode, data); } }); isInitialized = true; }
static InputStream getCachedImageStream(URL url, Context context) { InputStream imageStream = null; if (url != null) { if (isCDNURL(url)) { try { FileLruCache cache = getCache(context); imageStream = cache.get(url.toString()); } catch (IOException e) { Logger.log(LoggingBehavior.CACHE, Log.WARN, TAG, e.toString()); } } } return imageStream; }
static URL getRedirectedUrl(Context context, URL url) { if (url == null) { return null; } String urlString = url.toString(); URL finalUrl = null; InputStreamReader reader = null; try { InputStream stream; FileLruCache cache = getCache(context); boolean redirectExists = false; while ((stream = cache.get(urlString, REDIRECT_CONTENT_TAG)) != null) { redirectExists = true; // Get the redirected url reader = new InputStreamReader(stream); char[] buffer = new char[128]; int bufferLength; StringBuilder urlBuilder = new StringBuilder(); while ((bufferLength = reader.read(buffer, 0, buffer.length)) > 0) { urlBuilder.append(buffer, 0, bufferLength); } Utility.closeQuietly(reader); // Iterate to the next url in the redirection urlString = urlBuilder.toString(); } if (redirectExists) { finalUrl = new URL(urlString); } } catch (MalformedURLException e) { // caching is best effort, so ignore the exception } catch (IOException ioe) { } finally { Utility.closeQuietly(reader); } return finalUrl; }