<commit_before>package com.booxs.app;


import android.app.Activity;
import android.app.Fragment;
import android.app.LoaderManager;
import android.content.Loader;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.booxs.app.ebook.EbookContent;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.epub.EpubReader;


/**
 * A simple {@link Fragment} subclass.
 */
public class EbookDetailFragment extends Fragment implements LoaderManager.LoaderCallbacks<File> {
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    public static final String ARG_EBOOK = "Ebook";
    private static final int DETAIL_LOADER_ID = 89465845;
    private static final String TAG = "EbookDetailFragment";

    private EbookContent.EbookItem mEbook;
    private EbookFragment.OnFragmentInteractionListener mListener;
    private EbookDetailLoader mDetailEbookLoader;

    public EbookDetailFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mEbook = getArguments().getParcelable(ARG_EBOOK);
        }
        LoaderManager lm = getLoaderManager();
        if (lm != null) {
            lm.initLoader(DETAIL_LOADER_ID, null, this).forceLoad();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (EbookFragment.OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_ebook_detail, container, false);
    }


    @Override
    public Loader<File> onCreateLoader(int i, Bundle bundle) {
        mDetailEbookLoader = null;
        if (mEbook != null && mListener != null) {
            mDetailEbookLoader = new EbookDetailLoader(getActivity(), mEbook, mListener.getDropboxApi());
        }
        return mDetailEbookLoader;
    }

    @Override
    public void onLoadFinished(Loader<File> objectLoader, File file) {
        try {
            View view = getView();
            Book book = (new EpubReader()).readEpub(new FileInputStream(file));
            if (book != null && view != null) {
                ImageView cover = (ImageView) view.findViewById(R.id.IvDetailCover);
                Bitmap cover_bitmap = BitmapFactory.decodeStream(book.getCoverImage().getInputStream());
                cover.setImageBitmap(cover_bitmap);
                TextView tvTitle = (TextView) view.findViewById(R.id.TvDetailTitle);
                tvTitle.setText(book.getTitle());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onLoaderReset(Loader<File> objectLoader) {

    }
}<commit_msg> Epub reading (using lazy loader of the library) in background and separately: first the title and then the cover.<commit_after>package com.booxs.app;


import android.app.Activity;
import android.app.Fragment;
import android.app.LoaderManager;
import android.content.Loader;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.booxs.app.ebook.EbookContent;

import java.io.File;
import java.io.IOException;

import nl.siegmann.epublib.Constants;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.epub.EpubReader;


/**
 * A simple {@link Fragment} subclass.
 */
public class EbookDetailFragment extends Fragment implements LoaderManager.LoaderCallbacks<File> {
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    public static final String ARG_EBOOK = "Ebook";
    private static final int DETAIL_LOADER_ID = 89;
    private static final String TAG = "EbookDetailFragment";

    private EbookContent.EbookItem mEbook;
    private EbookDetailLoader mDetailEbookLoader;
    private Book book_file;
    private ProgressBar pb;

    public EbookDetailFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mEbook = getArguments().getParcelable(ARG_EBOOK);
        }
        // Retain this fragment across configuration changes.
        LoaderManager lm = getLoaderManager();
        if (lm != null) {
            lm.initLoader(DETAIL_LOADER_ID, null, this).forceLoad();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        pb = (ProgressBar) activity.findViewById(R.id.progressBar);
        activity.getActionBar().setTitle(R.string.title_activity_main);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_ebook_detail, container, false);
    }


    @Override
    public Loader<File> onCreateLoader(int i, Bundle bundle) {
        Log.d(TAG, "onCreateLoader");
        mDetailEbookLoader = null;
        if (mEbook != null) {
            mDetailEbookLoader = new EbookDetailLoader(getActivity(), mEbook, LoginActivity.mDBApi);
            if (pb != null)
                pb.setVisibility(View.VISIBLE);
        }
        return mDetailEbookLoader;
    }

    @Override
    public void onLoadFinished(Loader<File> objectLoader, File file) {
        Log.d(TAG, "onLoadFinished");
        LoadBookCoverTask loadBookCoverTask = new LoadBookCoverTask();
        LoadBookTitleTask loadBookTitleTask = new LoadBookTitleTask();
        //sequential asynctask execution, first one and the the other, not at the same time
        loadBookTitleTask.execute(file.getPath());
        loadBookCoverTask.execute(file.getPath());
    }

    @Override
    public void onLoaderReset(Loader<File> objectLoader) {

    }

    private class LoadBookTitleTask extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            if (pb != null)
                pb.setVisibility(View.VISIBLE);
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... title) {
            if (book_file == null) {
                try {
                    book_file = (new EpubReader()).readEpubLazy(title[0], Constants.CHARACTER_ENCODING);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            Bitmap cover_bitmap = null;
            try {
                cover_bitmap = BitmapFactory.decodeStream(book_file.getCoverImage().getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return book_file.getTitle();
        }

        @Override
        protected void onPostExecute(String title) {
            super.onPostExecute(title);
            View view = getView();
            if (view != null) {
                TextView tvTitle = (TextView) view.findViewById(R.id.TvDetailTitle);
                tvTitle.setText(title);
            }
            if (pb != null)
                pb.setVisibility(View.INVISIBLE);
        }
    }

    private class LoadBookCoverTask extends AsyncTask<String, Void, Bitmap> {

        @Override
        protected void onPreExecute() {
            if (pb != null)
                pb.setVisibility(View.VISIBLE);
            super.onPreExecute();
        }

        @Override
        protected Bitmap doInBackground(String... title) {
            if (book_file == null) {
                try {
                    book_file = (new EpubReader()).readEpubLazy(title[0], Constants.CHARACTER_ENCODING);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            Bitmap cover_bitmap = null;
            try {
                cover_bitmap = BitmapFactory.decodeStream(book_file.getCoverImage().getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return cover_bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            View view = getView();
            if (view != null) {
                ImageView cover = (ImageView) view.findViewById(R.id.IvDetailCover);
                cover.setImageBitmap(bitmap);
            }
            if (pb != null)
                pb.setVisibility(View.INVISIBLE);
        }
    }
}
{'fraction_non_alphanumeric': 0.09219710386498593, 'fraction_numerical': 0.0012501302218981144, 'mean_word_length': 2.401842664776754, 'pattern_counts': {'":': 0, '<': 13, '<?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': '1083773', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2532, 'n_words': 585}