<commit_before>package ca.josephroque.bowlingcompanion.utils

import android.app.Activity
import android.graphics.Bitmap
import android.widget.Toast
import android.os.Environment
import java.io.File
import java.io.FileOutputStream
import java.lang.Exception
import android.provider.MediaStore
import android.content.ContentValues
import ca.josephroque.bowlingcompanion.R
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.launch
import android.content.Intent
import android.graphics.Canvas
import android.net.Uri
import android.util.Log
import android.view.View
import ca.josephroque.bowlingcompanion.common.Android
import ca.josephroque.bowlingcompanion.games.Game
import ca.josephroque.bowlingcompanion.games.views.ScoreSheet

/**
 * Copyright (C) 2018 <NAME>
 *
 * Utility functions for sharing data from the app.
 */
object ShareUtils {
    @Suppress("unused")
    private const val TAG = "ShareUtils"

    private const val exportFileType = "png"

    fun shareGames(activity: Activity, games: List<Game>) {
        launch(CommonPool) {
            val bitmap = buildBitmap(activity, games)
            val destination = saveBitmap(activity, games.size, bitmap)
            bitmap.recycle()

            if (destination == null) { return@launch }

            val shareIntent = Intent(Intent.ACTION_SEND)
            shareIntent.type = "image/$exportFileType"
            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(destination))

            launch(Android) {
                activity.startActivity(Intent.createChooser(shareIntent, activity.resources.getString(R.string.share_image)))

                Analytics.trackShareImage(games.size)
            }
        }
    }

    fun saveGames(activity: Activity, games: List<Game>) {
        if (Permission.WriteExternalStorage.requestPermission(activity)) {
            launch(CommonPool) {
                val bitmap = buildBitmap(activity, games)
                val destination = saveBitmap(activity, games.size, bitmap)
                bitmap.recycle()

                if (destination == null) { return@launch }

                launch(Android) {
                    Toast.makeText(activity, activity.resources.getString(R.string.image_export_success), Toast.LENGTH_SHORT).show()

                    Analytics.trackSaveImage(games.size)
                }
            }
        }
    }

    private fun buildBitmap(activity: Activity, games: List<Game>): Bitmap {
        val scoreSheet = ScoreSheet(activity)
        scoreSheet.frameNumbersEnabled = false
        scoreSheet.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
        val scoreSheetHeight = scoreSheet.measuredHeight

        val bitmapWidth = scoreSheet.measuredWidth
        val bitmapHeight = scoreSheetHeight * games.size
        val bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)

        games.forEachIndexed { index, game ->
            scoreSheet.apply(-1, -1, game)
            val scoreSheetBitmap = scoreSheet.toBitmap()
            canvas.drawBitmap(scoreSheetBitmap, 0F, (index * scoreSheetHeight).toFloat(), null)
            scoreSheetBitmap.recycle()
        }

        return bitmap
    }

    private fun saveBitmap(activity: Activity, numberOfGames: Int, bitmap: Bitmap): File? {
        // Get filepath to store the image
        val externalStorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
        var destination = File(externalStorage, "BowlingCompanion")
        destination.mkdirs()
        destination = File(destination, "bc_${System.currentTimeMillis()}.$exportFileType")
<commit_msg>Add separators between games shared<commit_after>package ca.josephroque.bowlingcompanion.utils

import android.app.Activity
import android.graphics.Bitmap
import android.widget.Toast
import android.os.Environment
import java.io.File
import java.io.FileOutputStream
import java.lang.Exception
import android.provider.MediaStore
import android.content.ContentValues
import ca.josephroque.bowlingcompanion.R
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.launch
import android.content.Intent
import android.graphics.Canvas
import android.graphics.Paint
import android.net.Uri
import android.util.Log
import android.view.View
import ca.josephroque.bowlingcompanion.common.Android
import ca.josephroque.bowlingcompanion.games.Game
import ca.josephroque.bowlingcompanion.games.views.ScoreSheet

/**
 * Copyright (C) 2018 <NAME>
 *
 * Utility functions for sharing data from the app.
 */
object ShareUtils {
    @Suppress("unused")
    private const val TAG = "ShareUtils"

    private const val exportFileType = "png"

    private const val interGameBorderWidth = 4

    fun shareGames(activity: Activity, games: List<Game>) {
        launch(CommonPool) {
            val bitmap = buildBitmap(activity, games)
            val destination = saveBitmap(activity, games.size, bitmap)
            bitmap.recycle()

            if (destination == null) { return@launch }

            val shareIntent = Intent(Intent.ACTION_SEND)
            shareIntent.type = "image/$exportFileType"
            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(destination))

            launch(Android) {
                activity.startActivity(Intent.createChooser(shareIntent, activity.resources.getString(R.string.share_image)))

                Analytics.trackShareImage(games.size)
            }
        }
    }

    fun saveGames(activity: Activity, games: List<Game>) {
        if (Permission.WriteExternalStorage.requestPermission(activity)) {
            launch(CommonPool) {
                val bitmap = buildBitmap(activity, games)
                val destination = saveBitmap(activity, games.size, bitmap)
                bitmap.recycle()

                if (destination == null) { return@launch }

                launch(Android) {
                    Toast.makeText(activity, activity.resources.getString(R.string.image_export_success), Toast.LENGTH_SHORT).show()

                    Analytics.trackSaveImage(games.size)
                }
            }
        }
    }

    private fun buildBitmap(activity: Activity, games: List<Game>): Bitmap {
        val scoreSheet = ScoreSheet(activity)
        scoreSheet.frameNumbersEnabled = false
        scoreSheet.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
        val scoreSheetHeight = scoreSheet.measuredHeight

        val bitmapWidth = scoreSheet.measuredWidth
        val bitmapHeight = (scoreSheetHeight * games.size) + (interGameBorderWidth * games.size - 1)
        val bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)

        val paintBlackLine = Paint()
        paintBlackLine.color = android.graphics.Color.BLACK

        games.forEachIndexed { index, game ->
            scoreSheet.apply(-1, -1, game)
            val scoreSheetBitmap = scoreSheet.toBitmap()
            canvas.drawBitmap(scoreSheetBitmap, 0F, (index * (scoreSheetHeight + interGameBorderWidth)).toFloat(), null)
            for (i in 1..interGameBorderWidth) {
                val y = (index * (scoreSheetHeight + interGameBorderWidth) - i).toFloat()
                canvas.drawLine(0F, y, bitmapWidth.toFloat(), y, paintBlackLine)
            }
            scoreSheetBitmap.recycle()
        }

        return bitmap
    }

    private fun saveBitmap(activity: Activity, numberOfGames: Int, bitmap: Bitmap): File? {
        // Get filepath to store the image
        val externalStorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
        var destination = File(externalStorage, "BowlingCompanion")
        destination.mkdirs()
        destination = File(destination, "bc_${System.currentTimeMillis()}.$exportFileType")

{'fraction_non_alphanumeric': 0.08944115049829696, 'fraction_numerical': 0.0032799293553677305, 'mean_word_length': 2.9384003974167907, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '6542002', 'n_tokens_mistral': 2199, 'n_tokens_neox': 2063, 'n_words': 435}