com.facebook.internal.FileLruCache Java Examples

The following examples show how to use com.facebook.internal.FileLruCache. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestUtils.java    From FacebookImageShareIntent with MIT License 6 votes vote down vote up
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);
}
 
Example #2
Source File: ImageResponseCache.java    From aws-mobile-self-paced-labs-samples with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: UrlRedirectCache.java    From aws-mobile-self-paced-labs-samples with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #4
Source File: ImageResponseCache.java    From HypFacebook with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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;
}
 
Example #5
Source File: UrlRedirectCache.java    From HypFacebook with BSD 2-Clause "Simplified" License 6 votes vote down vote up
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);
    }
}
 
Example #6
Source File: ImageResponseCache.java    From FacebookNewsfeedSample-Android with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #7
Source File: UrlRedirectCache.java    From FacebookNewsfeedSample-Android with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #8
Source File: LikeActionController.java    From kognitivo with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #9
Source File: ImageResponseCache.java    From aws-mobile-self-paced-labs-samples with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #10
Source File: ImageResponseCache.java    From HypFacebook with BSD 2-Clause "Simplified" License 5 votes vote down vote up
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;
}
 
Example #11
Source File: ImageResponseCache.java    From FacebookNewsfeedSample-Android with Apache License 2.0 5 votes vote down vote up
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;
}