/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

package com.facebook.react.modules.fresco;

import java.util.HashSet;

import android.content.Context;
import android.support.annotation.Nullable;

import com.facebook.cache.common.CacheKey;
import com.facebook.cache.disk.DiskCacheConfig;
import com.facebook.common.internal.AndroidPredicates;
import com.facebook.common.soloader.SoLoaderShim;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.backends.okhttp.OkHttpImagePipelineConfigFactory;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.facebook.imagepipeline.core.ImagePipelineFactory;
import com.facebook.imagepipeline.listener.RequestListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.modules.common.ModuleDataCleaner;
import com.facebook.react.modules.network.OkHttpClientProvider;
import com.facebook.soloader.SoLoader;

import com.squareup.okhttp.OkHttpClient;

/**
 * Module to initialize the Fresco library.
 *
 * <p>Does not expose any methods to JavaScript code. For initialization and cleanup only.
 */
public class FrescoModule extends ReactContextBaseJavaModule implements
    ModuleDataCleaner.Cleanable {

  @Nullable private RequestListener mRequestListener;
  @Nullable private DiskCacheConfig mDiskCacheConfig;

  public FrescoModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  public FrescoModule(ReactApplicationContext reactContext, RequestListener listener) {
    super(reactContext);
    mRequestListener = listener;
  }

  public FrescoModule(
      ReactApplicationContext reactContext,
      RequestListener listener,
      DiskCacheConfig diskCacheConfig) {
    super(reactContext);
    mRequestListener = listener;
    mDiskCacheConfig = diskCacheConfig;
  }

  @Override
  public void initialize() {
    super.initialize();
    // Make sure the SoLoaderShim is configured to use our loader for native libraries.
    // This code can be removed if using Fresco from Maven rather than from source
    SoLoaderShim.setHandler(new FrescoHandler());

    HashSet<RequestListener> requestListeners = new HashSet<>();
    requestListeners.add(new SystraceRequestListener());
    if (mRequestListener != null) {
      requestListeners.add(mRequestListener);
    }

    Context context = this.getReactApplicationContext().getApplicationContext();
    OkHttpClient okHttpClient = OkHttpClientProvider.getOkHttpClient();
    ImagePipelineConfig.Builder builder =
        OkHttpImagePipelineConfigFactory.newBuilder(context, okHttpClient);

    builder
        .setDownsampleEnabled(false)
        .setRequestListeners(requestListeners);

    if (mDiskCacheConfig != null) {
      builder.setMainDiskCacheConfig(mDiskCacheConfig);
    }

    ImagePipelineConfig config = builder.build();
    Fresco.initialize(context, config);
  }

  @Override
  public String getName() {
    return "FrescoModule";
  }

  @Override
  public void clearSensitiveData() {
    // Clear image cache.
    ImagePipelineFactory imagePipelineFactory = Fresco.getImagePipelineFactory();
    imagePipelineFactory.getBitmapMemoryCache().removeAll(AndroidPredicates.<CacheKey>True());
    imagePipelineFactory.getEncodedMemoryCache().removeAll(AndroidPredicates.<CacheKey>True());
    imagePipelineFactory.getMainDiskStorageCache().clearAll();
    imagePipelineFactory.getSmallImageDiskStorageCache().clearAll();
  }

  private static class FrescoHandler implements SoLoaderShim.Handler {
    @Override
    public void loadLibrary(String libraryName) {
      SoLoader.loadLibrary(libraryName);
    }
  }
}

@@ -1549,106 +1549,47 @@
 %0A%0A  
-@Nullable private RequestListener mRequestListener;%0A  @Nullable private DiskCacheConfig mDiskCache
+private @Nullable ImagePipelineConfig m
 Conf
@@ -1659,34 +1659,77 @@
 ) %7B%0A    
-super(reactContext
+this(reactContext, getDefaultConfig(reactContext, null, null)
 );%0A  %7D%0A%0A
@@ -1812,37 +1812,36 @@
 listener) %7B%0A    
-super
+this
 (reactContext);%0A
@@ -1841,33 +1841,40 @@
 text
-);%0A    mRequestListener =
+, getDefaultConfig(reactContext,
  lis
@@ -1874,24 +1874,32 @@
 xt, listener
+, null))
 ;%0A  %7D%0A%0A  pub
@@ -2037,21 +2037,20 @@
 ) %7B%0A    
-super
+this
 (reactCo
@@ -2058,77 +2058,205 @@
 text
-);%0A    mRequestListener = listener;%0A    mDiskCacheConfig = diskCacheC
+, getDefaultConfig(reactContext, listener, diskCacheConfig));%0A  %7D%0A%0A  public FrescoModule(ReactApplicationContext reactContext, ImagePipelineConfig config) %7B%0A    super(reactContext);%0A    mConfig = c
 onfi
@@ -2554,219 +2554,8 @@
 );%0A%0A
-    HashSet%3CRequestListener%3E requestListeners = new HashSet%3C%3E();%0A    requestListeners.add(new SystraceRequestListener());%0A    if (mRequestListener != null) %7B%0A      requestListeners.add(mRequestListener);%0A    %7D%0A%0A
     
@@ -2576,13 +2576,8 @@
 t = 
-this.
 getR
@@ -2634,480 +2634,63 @@
     
-OkHttpClient okHttpClient = OkHttpClientProvider.getOkHttpClient();%0A    ImagePipelineConfig.Builder builder =%0A        OkHttpImagePipelineConfigFactory.newBuilder(context, okHttpClient);%0A%0A    builder%0A        .setDownsampleEnabled(false)%0A        .setRequestListeners(requestListeners);%0A%0A    if (mDiskCacheConfig != null) %7B%0A      builder.setMainDiskCacheConfig(mDiskCacheConfig);%0A    %7D%0A%0A    ImagePipelineConfig config = builder.build();%0A    Fresco.initialize(context, config)
+Fresco.initialize(context, mConfig);%0A    mConfig = null
 ;%0A  
@@ -3249,16 +3249,825 @@
 );%0A  %7D%0A%0A
+  private static ImagePipelineConfig getDefaultConfig(%0A      Context context,%0A      @Nullable RequestListener listener,%0A      @Nullable DiskCacheConfig diskCacheConfig) %7B%0A    HashSet%3CRequestListener%3E requestListeners = new HashSet%3C%3E();%0A    requestListeners.add(new SystraceRequestListener());%0A    if (listener != null) %7B%0A      requestListeners.add(listener);%0A    %7D%0A%0A    OkHttpClient okHttpClient = OkHttpClientProvider.getOkHttpClient();%0A    ImagePipelineConfig.Builder builder =%0A        OkHttpImagePipelineConfigFactory.newBuilder(context.getApplicationContext(), okHttpClient);%0A%0A    builder%0A        .setDownsampleEnabled(false)%0A        .setRequestListeners(requestListeners);%0A%0A    if (diskCacheConfig != null) %7B%0A      builder.setMainDiskCacheConfig(diskCacheConfig);%0A    %7D%0A%0A    return builder.build();%0A  %7D%0A%0A
   privat

Allow to pass in `ImagePipelineConfig` to `FrescoModule`