<commit_before>package jp.toastkid.yobidashi.pdf

import android.annotation.TargetApi
import android.content.Context
import android.graphics.Bitmap
import android.graphics.pdf.PdfRenderer
import android.net.Uri
import android.os.Build
import android.os.ParcelFileDescriptor
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.libs.ActivityOptionsFactory
import jp.toastkid.yobidashi.libs.ImageCache
import jp.toastkid.yobidashi.libs.ImagePreviewActivity
import java.io.File

/**
 * PDF Viewer's adapter.
 *
 * @param context [Context]
 * @author toastkidjp
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
class Adapter(private val context: Context): RecyclerView.Adapter<ViewHolder>() {

    /**
     * Layout inflater.
     */
    private val layoutInflater: LayoutInflater = LayoutInflater.from(context)

    /**
     * File descriptor.
     */
    private var fileDescriptor: ParcelFileDescriptor? = null

    /**
     * PDF renderer.
     */
    private var pdfRenderer: PdfRenderer? = null

    private val pdfImageFactory = PdfImageFactory()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
            ViewHolder(DataBindingUtil.inflate(layoutInflater, R.layout.item_pdf_content, parent, false))

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        pdfRenderer?.let {
            val image: Bitmap = pdfImageFactory.invoke(it.openPage(position))
            holder.setImage(image)
            holder.setIndicator(position + 1, itemCount)
            holder.setOnLongTap(
                    View.OnLongClickListener { view ->
                        val cachedBitmapFile: File = ImageCache.saveBitmap(context, image)
                        context.startActivity(
                                ImagePreviewActivity.makeIntent(context, cachedBitmapFile),
                                ActivityOptionsFactory.makeScaleUpBundle(view).toBundle()
                        )
                        true
                    }
            )
        }
    }

    override fun getItemCount(): Int = pdfRenderer?.pageCount ?: 0

    /**
     * Load PDF from [Uri].
     *
     * @param uri
     * @throws SecurityException
     */
    fun load(uri: Uri) {
        val contentResolver = context.contentResolver
        fileDescriptor = contentResolver.openFileDescriptor(uri, "r")
        pdfRenderer?.close()
        pdfRenderer = fileDescriptor?.let { PdfRenderer(it) }
        notifyDataSetChanged()
    }

    /**
     * Dispose disposables.
     */
    fun dispose() {
        pdfRenderer?.close()
        fileDescriptor?.close()
    }
}<commit_msg>refactor #442 Extract layout ID to constant.<commit_after>package jp.toastkid.yobidashi.pdf

import android.annotation.TargetApi
import android.content.Context
import android.graphics.Bitmap
import android.graphics.pdf.PdfRenderer
import android.net.Uri
import android.os.Build
import android.os.ParcelFileDescriptor
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import jp.toastkid.yobidashi.R
import jp.toastkid.yobidashi.libs.ActivityOptionsFactory
import jp.toastkid.yobidashi.libs.ImageCache
import jp.toastkid.yobidashi.libs.ImagePreviewActivity
import java.io.File

/**
 * PDF Viewer's adapter.
 *
 * @param context [Context]
 * @author toastkidjp
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
class Adapter(private val context: Context): RecyclerView.Adapter<ViewHolder>() {

    /**
     * Layout inflater.
     */
    private val layoutInflater: LayoutInflater = LayoutInflater.from(context)

    /**
     * File descriptor.
     */
    private var fileDescriptor: ParcelFileDescriptor? = null

    /**
     * PDF renderer.
     */
    private var pdfRenderer: PdfRenderer? = null

    private val pdfImageFactory = PdfImageFactory()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
            ViewHolder(DataBindingUtil.inflate(layoutInflater, LAYOUT_ID, parent, false))

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        pdfRenderer?.let {
            val image: Bitmap = pdfImageFactory.invoke(it.openPage(position))
            holder.setImage(image)
            holder.setIndicator(position + 1, itemCount)
            holder.setOnLongTap(
                    View.OnLongClickListener { view ->
                        val cachedBitmapFile: File = ImageCache.saveBitmap(context, image)
                        context.startActivity(
                                ImagePreviewActivity.makeIntent(context, cachedBitmapFile),
                                ActivityOptionsFactory.makeScaleUpBundle(view).toBundle()
                        )
                        true
                    }
            )
        }
    }

    override fun getItemCount(): Int = pdfRenderer?.pageCount ?: 0

    /**
     * Load PDF from [Uri].
     *
     * @param uri
     * @throws SecurityException
     */
    fun load(uri: Uri) {
        val contentResolver = context.contentResolver
        fileDescriptor = contentResolver.openFileDescriptor(uri, "r")
        pdfRenderer?.close()
        pdfRenderer = fileDescriptor?.let { PdfRenderer(it) }
        notifyDataSetChanged()
    }

    /**
     * Dispose disposables.
     */
    fun dispose() {
        pdfRenderer?.close()
        fileDescriptor?.close()
    }

    companion object {

        @LayoutRes
        private const val LAYOUT_ID = R.layout.item_pdf_content

    }
}
{'fraction_non_alphanumeric': 0.08969696969696969, 'fraction_numerical': 0.0012121212121212121, 'mean_word_length': 2.561035758323058, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '2364497', 'n_tokens_mistral': 1635, 'n_tokens_neox': 1540, 'n_words': 314}