<commit_before>/*
 * Copyright 2014 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.apps.muzei.render

import android.app.ActivityManager
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.opengl.GLSurfaceView
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.os.bundleOf

import com.google.android.apps.muzei.util.ImageBlurrer
import com.google.android.apps.muzei.util.blur
import com.google.android.apps.muzei.util.roundMult4
import com.squareup.picasso.Picasso
import com.squareup.picasso.Target
import java.lang.Exception

class MuzeiRendererFragment : Fragment(), RenderController.Callbacks, MuzeiBlurRenderer.Callbacks {

    companion object {

        private const val ARG_DEMO_MODE = "demo_mode"
        private const val ARG_DEMO_FOCUS = "demo_focus"

        fun createInstance(demoMode: Boolean, demoFocus: Boolean = false): MuzeiRendererFragment {
            return MuzeiRendererFragment().apply {
                arguments = bundleOf(
                        ARG_DEMO_MODE to demoMode,
                        ARG_DEMO_FOCUS to demoFocus)
            }
        }
    }

    private var muzeiView: MuzeiView? = null
    private lateinit var simpleDemoModeImageView: ImageView
    private var demoMode: Boolean = false
    private var demoFocus: Boolean = false

    private val simpleDemoModeLoadedTarget = object : Target {

        override fun onBitmapLoaded(bitmap: Bitmap?, loadedFrom: Picasso.LoadedFrom?) {
            simpleDemoModeImageView.setImageBitmap(if (!demoFocus) {
                bitmap.blur(requireContext())?.apply {
                    // Dim
                    val c = Canvas(this)
                    c.drawColor(Color.argb(255 - MuzeiBlurRenderer.DEFAULT_MAX_DIM,
                            0, 0, 0))
                }
            } else {
                bitmap
            })
        }
        override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
        }

        override fun onPrepareLoad(drawable: Drawable?) {}
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        demoMode = arguments?.getBoolean(ARG_DEMO_MODE, false) ?: true
        demoFocus = arguments?.getBoolean(ARG_DEMO_FOCUS, false) ?: true
    }

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        val activityManager = requireContext().getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager

        if (demoMode && activityManager.isLowRamDevice) {
            val dm = resources.displayMetrics
            var targetWidth = dm.widthPixels
            var targetHeight = dm.heightPixels
            if (!demoFocus) {
                targetHeight = (ImageBlurrer.MAX_SUPPORTED_BLUR_PIXELS * 10000 / MuzeiBlurRenderer.DEFAULT_BLUR)
                        .roundMult4()
                targetWidth = (dm.widthPixels * 1f / dm.heightPixels * targetHeight).toInt().roundMult4()
            }

            simpleDemoModeImageView = ImageView(context).apply {
                scaleType = ImageView.ScaleType.CENTER_CROP
            }
            Picasso.get()
                    .load("file:///android_asset/starrynight.jpg")
                    .resize(targetWidth, targetHeight)
                    .centerCrop()
                    .into(simpleDemoModeLoadedTarget)
            return simpleDemoModeImageView
        } else {
            muzeiView = MuzeiView(requireContext())
            return muzeiView
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        muzeiView?.renderController?.visible = !hidden
    }

    override fun onDestroyView() {
        super.onDestroyView()
        muzeiView = null
    }

    override fun onPause() {
        super.onPause()
        muzeiView?.onPause()
    }

    override fun onResume() {
        super.onResume()
        muzeiView?.onResume()
    }

    override fun queueEventOnGlThread(event: () -> Unit) {
        muzeiView?.queueEvent({ event() })
    }<commit_msg>Cancel demo requests when MuzeiRendererFragment is destroyed<commit_after>/*
 * Copyright 2014 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.apps.muzei.render

import android.app.ActivityManager
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.opengl.GLSurfaceView
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.os.bundleOf

import com.google.android.apps.muzei.util.ImageBlurrer
import com.google.android.apps.muzei.util.blur
import com.google.android.apps.muzei.util.roundMult4
import com.squareup.picasso.Picasso
import com.squareup.picasso.Target
import java.lang.Exception

class MuzeiRendererFragment : Fragment(), RenderController.Callbacks, MuzeiBlurRenderer.Callbacks {

    companion object {

        private const val ARG_DEMO_MODE = "demo_mode"
        private const val ARG_DEMO_FOCUS = "demo_focus"

        fun createInstance(demoMode: Boolean, demoFocus: Boolean = false): MuzeiRendererFragment {
            return MuzeiRendererFragment().apply {
                arguments = bundleOf(
                        ARG_DEMO_MODE to demoMode,
                        ARG_DEMO_FOCUS to demoFocus)
            }
        }
    }

    private var muzeiView: MuzeiView? = null
    private lateinit var simpleDemoModeImageView: ImageView
    private var demoMode: Boolean = false
    private var demoFocus: Boolean = false

    private val simpleDemoModeLoadedTarget = object : Target {

        override fun onBitmapLoaded(bitmap: Bitmap?, loadedFrom: Picasso.LoadedFrom?) {
            simpleDemoModeImageView.setImageBitmap(if (!demoFocus) {
                bitmap.blur(requireContext())?.apply {
                    // Dim
                    val c = Canvas(this)
                    c.drawColor(Color.argb(255 - MuzeiBlurRenderer.DEFAULT_MAX_DIM,
                            0, 0, 0))
                }
            } else {
                bitmap
            })
        }
        override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
        }

        override fun onPrepareLoad(drawable: Drawable?) {}
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        demoMode = arguments?.getBoolean(ARG_DEMO_MODE, false) ?: true
        demoFocus = arguments?.getBoolean(ARG_DEMO_FOCUS, false) ?: true
    }

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        val activityManager = requireContext().getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager

        if (demoMode && activityManager.isLowRamDevice) {
            val dm = resources.displayMetrics
            var targetWidth = dm.widthPixels
            var targetHeight = dm.heightPixels
            if (!demoFocus) {
                targetHeight = (ImageBlurrer.MAX_SUPPORTED_BLUR_PIXELS * 10000 / MuzeiBlurRenderer.DEFAULT_BLUR)
                        .roundMult4()
                targetWidth = (dm.widthPixels * 1f / dm.heightPixels * targetHeight).toInt().roundMult4()
            }

            simpleDemoModeImageView = ImageView(context).apply {
                scaleType = ImageView.ScaleType.CENTER_CROP
            }
            Picasso.get()
                    .load("file:///android_asset/starrynight.jpg")
                    .resize(targetWidth, targetHeight)
                    .centerCrop()
                    .into(simpleDemoModeLoadedTarget)
            return simpleDemoModeImageView
        } else {
            muzeiView = MuzeiView(requireContext())
            return muzeiView
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        muzeiView?.renderController?.visible = !hidden
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Picasso.get().cancelRequest(simpleDemoModeLoadedTarget)
        muzeiView = null
    }

    override fun onPause() {
        super.onPause()
        muzeiView?.onPause()
    }

    override fun onResume() {
        super.onResume()
        muzeiView?.onResume()
    }

    override fun queueEventOnGlThread(event: () -> Unit) {
        muzeiView?.queueEvent({ event() })
    }
{'fraction_non_alphanumeric': 0.08130081300813008, 'fraction_numerical': 0.004817825956037338, 'mean_word_length': 2.4217032967032965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '142967', 'n_tokens_mistral': 2780, 'n_tokens_neox': 2627, 'n_words': 631}