<commit_before>/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.facebook.fresco.samples.showcase

import android.app.Application
import android.content.res.Resources
import androidx.preference.PreferenceManager
import com.facebook.common.executors.UiThreadImmediateExecutorService
import com.facebook.common.internal.Suppliers
import com.facebook.common.logging.FLog
import com.facebook.common.memory.manager.NoOpDebugMemoryManager
import com.facebook.drawee.backends.pipeline.DraweeConfig
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.backends.pipeline.info.ImagePerfData
import com.facebook.drawee.backends.pipeline.info.ImagePerfDataListener
import com.facebook.flipper.android.AndroidFlipperClient
import com.facebook.flipper.android.utils.FlipperUtils
import com.facebook.flipper.perflogger.NoOpFlipperPerfLogger
import com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin
import com.facebook.flipper.plugins.fresco.FrescoFlipperRequestListener
import com.facebook.flipper.plugins.inspector.DescriptorMapping
import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin
import com.facebook.fresco.samples.showcase.misc.DebugOverlaySupplierSingleton
import com.facebook.fresco.samples.showcase.misc.ImageUriProvider
import com.facebook.fresco.samples.showcase.misc.LogcatRequestListener2
import com.facebook.fresco.samples.showcase.settings.SettingsFragment.KEY_VITO_KOTLIN
import com.facebook.fresco.vito.core.DefaultFrescoVitoConfig
import com.facebook.fresco.vito.core.FrescoVitoConfig
import com.facebook.fresco.vito.core.impl.DebugOverlayHandler
import com.facebook.fresco.vito.init.FrescoVito
import com.facebook.fresco.vito.provider.FrescoVitoProvider
import com.facebook.fresco.vito.provider.impl.NoOpCallerContextVerifier
import com.facebook.fresco.vito.provider.impl.kotlin.KFrescoVitoProvider
import com.facebook.fresco.vito.tools.liveeditor.ImageSelector
import com.facebook.fresco.vito.tools.liveeditor.ImageTracker
import com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory
import com.facebook.imagepipeline.core.ImagePipelineConfig
import com.facebook.imagepipeline.core.ImagePipelineFactory
import com.facebook.imagepipeline.debug.FlipperCacheKeyFactory
import com.facebook.imagepipeline.debug.FlipperImageTracker
import com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig
import com.facebook.imagepipeline.listener.ForwardingRequestListener
import com.facebook.imagepipeline.listener.RequestListener
import com.facebook.imagepipeline.listener.RequestLoggingListener
import com.facebook.imagepipeline.memory.BitmapCounterConfig
import com.facebook.imagepipeline.memory.BitmapCounterProvider
import com.facebook.imagepipeline.stetho.FrescoStethoPlugin
import com.facebook.stetho.Stetho
import com.facebook.stetho.okhttp3.StethoInterceptor
import okhttp3.OkHttpClient

/** Showcase Application implementation where we set up Fresco */
class ShowcaseApplication : Application() {

  private var frescoFlipperPlugin: FrescoFlipperPlugin? = null

  override fun onCreate() {
    super.onCreate()
    imageUriProvider = ImageUriProvider(applicationContext)
    FLog.setMinimumLoggingLevel(FLog.VERBOSE)
    val forwardingRequestListener = ForwardingRequestListener()
    val requestListeners =
        HashSet<RequestListener>().apply {
          add(forwardingRequestListener)
          add(RequestLoggingListener())
        }

    val requestListener2s = setOf(LogcatRequestListener2())

    val okHttpClient = OkHttpClient.Builder().addNetworkInterceptor(StethoInterceptor()).build()

    val imagePipelineConfigBuilder =
        OkHttpImagePipelineConfigFactory.newBuilder(this, okHttpClient)
            .setRequestListeners(requestListeners)
            .setRequestListener2s(requestListener2s)
            .setProgressiveJpegConfig(SimpleProgressiveJpegConfig())
            .setImageDecoderConfig(CustomImageFormatConfigurator.createImageDecoderConfig(this))
            .experiment()
            .setBitmapPrepareToDraw(true, 0, Integer.MAX_VALUE, true)

    if (shouldEnableFlipper()) {
      imagePipelineConfigBuilder.setCacheKeyFactory(FlipperCacheKeyFactory(sFlipperImageTracker))
    }

    imagePipelineConfigBuilder.experiment().setDownsampleIfLargeBitmap(true)

    val imagePipelineConfig = imagePipelineConfigBuilder.build()
    ImagePipelineConfig.defaultImageRequestConfig.isProgressiveRenderingEnabled = true

    val draweeConfigBuilder = DraweeConfig.newBuilder()
    CustomImageFormatConfigurator.addCustomDrawableFactories(this, draweeConfigBuilder)

    draweeConfigBuilder.setDebugOverlayEnabledSupplier(
        DebugOverlaySupplierSingleton.getInstance(applicationContext))

    if (shouldEnableFlipper()) {
      draweeConfigBuilder.setImagePerfDataListener(
          object : ImagePerfDataListener {
            override fun onImageLoadStatusUpdated(
                imagePerfData: ImagePerfData,
                imageLoadStatus: Int
            ) {
              frescoFlipperPlugin
                  ?.flipperImageTracker?.onImageLoadStatusUpdated(imagePerfData, imageLoadStatus)
              frescoFlipperPlugin?.onImageLoadStatusUpdated(imagePerfData, imageLoadStatus)
            }

            override fun onImageVisibilityUpdated(
                imagePerfData: ImagePerfData,
                visibilityState: Int
            ) {
              // nop
            }
          })
    }

    BitmapCounterProvider.initialize(
        BitmapCounterConfig.newBuilder()
            .setMaxBitmapCount(BitmapCounterConfig.DEFAULT_MAX_BITMAP_COUNT)
            .build())
    Fresco.initialize(this, imagePipelineConfig, draweeConfigBuilder.build())
    imageTracker = ImageTracker()
    initVito(resources)
    imageSelector =
        ImageSelector(
            imageTracker, FrescoVitoProvider.getImagePipeline(), FrescoVitoProvider.getController())
    val context = this
    Stetho.initialize(
        Stetho.newInitializerBuilder(context)
            .enableDumpapp {
              Stetho.DefaultDumperPluginsBuilder(context).provide(FrescoStethoPlugin()).finish()
            }
            .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(context))
            .build())

    if (shouldEnableFlipper()) {
      frescoFlipperPlugin =
          FrescoFlipperPlugin(
              sFlipperImageTracker,
              Fresco.getImagePipelineFactory().platformBitmapFactory,
              null,
              NoOpDebugMemoryManager(),
              NoOpFlipperPerfLogger(),
              null,
              null)
      forwardingRequestListener.addRequestListener(
          FrescoFlipperRequestListener(frescoFlipperPlugin!!.flipperImageTracker))
      AndroidFlipperClient.getInstance(context).apply {
        addPlugin(InspectorFlipperPlugin(context, DescriptorMapping.withDefaults()))
        addPlugin(frescoFlipperPlugin)
        start()
      }
    }
  }

  private fun initVito(
      resources: Resources,
      vitoConfig: FrescoVitoConfig = DefaultFrescoVitoConfig(),
  ) {
    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(KEY_VITO_KOTLIN, false)) {
      FrescoVito.initialize(
          KFrescoVitoProvider(
              vitoConfig,
              ImagePipelineFactory.getInstance().imagePipeline,
              FrescoVito.createImagePipelineUtils(Suppliers.BOOLEAN_TRUE),
              ImagePipelineFactory.getInstance()
                  .imagePipeline.config.executorSupplier.forLightweightBackgroundTasks(),
              UiThreadImmediateExecutorService.getInstance(),
              NoOpCallerContextVerifier(),
              DebugOverlayHandler(DebugOverlaySupplierSingleton.getInstance(applicationContext))))
    } else {
      FrescoVito.initialize(
          resources = resources,
          vitoConfig = vitoConfig,
          debugOverlayEnabledSupplier =
              DebugOverlaySupplierSingleton.getInstance(applicationContext),
          vitoImagePerfListener = imageTracker)
    }
  }

  private fun shouldEnableFlipper(): Boolean {<commit_msg>Update ktfmt component on FBS:master<commit_after>/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.facebook.fresco.samples.showcase

import android.app.Application
import android.content.res.Resources
import androidx.preference.PreferenceManager
import com.facebook.common.executors.UiThreadImmediateExecutorService
import com.facebook.common.internal.Suppliers
import com.facebook.common.logging.FLog
import com.facebook.common.memory.manager.NoOpDebugMemoryManager
import com.facebook.drawee.backends.pipeline.DraweeConfig
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.backends.pipeline.info.ImagePerfData
import com.facebook.drawee.backends.pipeline.info.ImagePerfDataListener
import com.facebook.flipper.android.AndroidFlipperClient
import com.facebook.flipper.android.utils.FlipperUtils
import com.facebook.flipper.perflogger.NoOpFlipperPerfLogger
import com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin
import com.facebook.flipper.plugins.fresco.FrescoFlipperRequestListener
import com.facebook.flipper.plugins.inspector.DescriptorMapping
import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin
import com.facebook.fresco.samples.showcase.misc.DebugOverlaySupplierSingleton
import com.facebook.fresco.samples.showcase.misc.ImageUriProvider
import com.facebook.fresco.samples.showcase.misc.LogcatRequestListener2
import com.facebook.fresco.samples.showcase.settings.SettingsFragment.KEY_VITO_KOTLIN
import com.facebook.fresco.vito.core.DefaultFrescoVitoConfig
import com.facebook.fresco.vito.core.FrescoVitoConfig
import com.facebook.fresco.vito.core.impl.DebugOverlayHandler
import com.facebook.fresco.vito.init.FrescoVito
import com.facebook.fresco.vito.provider.FrescoVitoProvider
import com.facebook.fresco.vito.provider.impl.NoOpCallerContextVerifier
import com.facebook.fresco.vito.provider.impl.kotlin.KFrescoVitoProvider
import com.facebook.fresco.vito.tools.liveeditor.ImageSelector
import com.facebook.fresco.vito.tools.liveeditor.ImageTracker
import com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory
import com.facebook.imagepipeline.core.ImagePipelineConfig
import com.facebook.imagepipeline.core.ImagePipelineFactory
import com.facebook.imagepipeline.debug.FlipperCacheKeyFactory
import com.facebook.imagepipeline.debug.FlipperImageTracker
import com.facebook.imagepipeline.decoder.SimpleProgressiveJpegConfig
import com.facebook.imagepipeline.listener.ForwardingRequestListener
import com.facebook.imagepipeline.listener.RequestListener
import com.facebook.imagepipeline.listener.RequestLoggingListener
import com.facebook.imagepipeline.memory.BitmapCounterConfig
import com.facebook.imagepipeline.memory.BitmapCounterProvider
import com.facebook.imagepipeline.stetho.FrescoStethoPlugin
import com.facebook.stetho.Stetho
import com.facebook.stetho.okhttp3.StethoInterceptor
import okhttp3.OkHttpClient

/** Showcase Application implementation where we set up Fresco */
class ShowcaseApplication : Application() {

  private var frescoFlipperPlugin: FrescoFlipperPlugin? = null

  override fun onCreate() {
    super.onCreate()
    imageUriProvider = ImageUriProvider(applicationContext)
    FLog.setMinimumLoggingLevel(FLog.VERBOSE)
    val forwardingRequestListener = ForwardingRequestListener()
    val requestListeners =
        HashSet<RequestListener>().apply {
          add(forwardingRequestListener)
          add(RequestLoggingListener())
        }

    val requestListener2s = setOf(LogcatRequestListener2())

    val okHttpClient = OkHttpClient.Builder().addNetworkInterceptor(StethoInterceptor()).build()

    val imagePipelineConfigBuilder =
        OkHttpImagePipelineConfigFactory.newBuilder(this, okHttpClient)
            .setRequestListeners(requestListeners)
            .setRequestListener2s(requestListener2s)
            .setProgressiveJpegConfig(SimpleProgressiveJpegConfig())
            .setImageDecoderConfig(CustomImageFormatConfigurator.createImageDecoderConfig(this))
            .experiment()
            .setBitmapPrepareToDraw(true, 0, Integer.MAX_VALUE, true)

    if (shouldEnableFlipper()) {
      imagePipelineConfigBuilder.setCacheKeyFactory(FlipperCacheKeyFactory(sFlipperImageTracker))
    }

    imagePipelineConfigBuilder.experiment().setDownsampleIfLargeBitmap(true)

    val imagePipelineConfig = imagePipelineConfigBuilder.build()
    ImagePipelineConfig.defaultImageRequestConfig.isProgressiveRenderingEnabled = true

    val draweeConfigBuilder = DraweeConfig.newBuilder()
    CustomImageFormatConfigurator.addCustomDrawableFactories(this, draweeConfigBuilder)

    draweeConfigBuilder.setDebugOverlayEnabledSupplier(
        DebugOverlaySupplierSingleton.getInstance(applicationContext))

    if (shouldEnableFlipper()) {
      draweeConfigBuilder.setImagePerfDataListener(
          object : ImagePerfDataListener {
            override fun onImageLoadStatusUpdated(
                imagePerfData: ImagePerfData,
                imageLoadStatus: Int
            ) {
              frescoFlipperPlugin
                  ?.flipperImageTracker
                  ?.onImageLoadStatusUpdated(imagePerfData, imageLoadStatus)
              frescoFlipperPlugin?.onImageLoadStatusUpdated(imagePerfData, imageLoadStatus)
            }

            override fun onImageVisibilityUpdated(
                imagePerfData: ImagePerfData,
                visibilityState: Int
            ) {
              // nop
            }
          })
    }

    BitmapCounterProvider.initialize(
        BitmapCounterConfig.newBuilder()
            .setMaxBitmapCount(BitmapCounterConfig.DEFAULT_MAX_BITMAP_COUNT)
            .build())
    Fresco.initialize(this, imagePipelineConfig, draweeConfigBuilder.build())
    imageTracker = ImageTracker()
    initVito(resources)
    imageSelector =
        ImageSelector(
            imageTracker, FrescoVitoProvider.getImagePipeline(), FrescoVitoProvider.getController())
    val context = this
    Stetho.initialize(
        Stetho.newInitializerBuilder(context)
            .enableDumpapp {
              Stetho.DefaultDumperPluginsBuilder(context).provide(FrescoStethoPlugin()).finish()
            }
            .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(context))
            .build())

    if (shouldEnableFlipper()) {
      frescoFlipperPlugin =
          FrescoFlipperPlugin(
              sFlipperImageTracker,
              Fresco.getImagePipelineFactory().platformBitmapFactory,
              null,
              NoOpDebugMemoryManager(),
              NoOpFlipperPerfLogger(),
              null,
              null)
      forwardingRequestListener.addRequestListener(
          FrescoFlipperRequestListener(frescoFlipperPlugin!!.flipperImageTracker))
      AndroidFlipperClient.getInstance(context).apply {
        addPlugin(InspectorFlipperPlugin(context, DescriptorMapping.withDefaults()))
        addPlugin(frescoFlipperPlugin)
        start()
      }
    }
  }

  private fun initVito(
      resources: Resources,
      vitoConfig: FrescoVitoConfig = DefaultFrescoVitoConfig(),
  ) {
    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean(KEY_VITO_KOTLIN, false)) {
      FrescoVito.initialize(
          KFrescoVitoProvider(
              vitoConfig,
              ImagePipelineFactory.getInstance().imagePipeline,
              FrescoVito.createImagePipelineUtils(Suppliers.BOOLEAN_TRUE),
              ImagePipelineFactory.getInstance()
                  .imagePipeline
                  .config
                  .executorSupplier
                  .forLightweightBackgroundTasks(),
              UiThreadImmediateExecutorService.getInstance(),
              NoOpCallerContextVerifier(),
              DebugOverlayHandler(DebugOverlaySupplierSingleton.getInstance(applicationContext))))
    } else {
      FrescoVito.initialize(
          resources = resources,
          vitoConfig = vitoConfig,
          debugOverlayEnabledSupplier =
              DebugOverlaySupplierSingleton.getInstance(applicationContext),
          vitoImagePerfListener = imageTracker)
    }
  }

  private fun shouldEnableFlipper(): Boolean {
{'fraction_non_alphanumeric': 0.07262027543529698, 'fraction_numerical': 0.001092034217072135, 'mean_word_length': 3.8783663805859723, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4337279', 'n_tokens_mistral': 4455, 'n_tokens_neox': 4480, 'n_words': 592}