<commit_before>package com.canyapan.dietdiaryapp.fragments;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.canyapan.dietdiaryapp.Application;
import com.canyapan.dietdiaryapp.R;
import com.canyapan.dietdiaryapp.adapters.SpinnerArrayAdapter;
import com.canyapan.dietdiaryapp.adapters.SpinnerItem;
import com.canyapan.dietdiaryapp.db.DatabaseHelper;
import com.canyapan.dietdiaryapp.helpers.ResourcesHelper;
import com.opencsv.CSVReader;

import org.apache.commons.io.input.BOMInputStream;
import org.apache.commons.lang3.ArrayUtils;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class ImportFragment extends Fragment {
    public static final String TAG = "ImportFragment";
    private static final String KEY_SELECTED_FILE_SERIALIZABLE = "FILE";
    private static final int REQUEST_EXTERNAL_STORAGE = 20;

    private OnFragmentInteractionListener mListener;

    private LinearLayout mLinearLayout;
    private Spinner spFiles;
    private File mSelectedFile = null;
    private File[] mFiles = null;

    private DatabaseHelper mDatabaseHelper;
    private ProgressDialog mProgressDialog;
    private ImportAsyncTask mAsyncTask = null;

    public ImportFragment() {
    }

    public static ImportFragment newInstance() {
        return new ImportFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        setHasOptionsMenu(true);

        mDatabaseHelper = new DatabaseHelper(getContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            mSelectedFile = (File) savedInstanceState.getSerializable(KEY_SELECTED_FILE_SERIALIZABLE);
        }

        mLinearLayout = (LinearLayout) inflater.inflate(R.layout.fragment_import_linearlayout, container, false);

        spFiles = (Spinner) mLinearLayout.findViewById(R.id.spFiles);
        loadFiles();

        if (null != mAsyncTask) {
            // To start progress dialog again.
            mAsyncTask.onPreExecute();
        }

        return mLinearLayout;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        if (spFiles.getSelectedItemPosition() > 0) {
            outState.putSerializable(KEY_SELECTED_FILE_SERIALIZABLE, mFiles[spFiles.getSelectedItemPosition()]);
        }<commit_msg>bugfix: Index out of bounds exception on import fragment's on save instance state.<commit_after>package com.canyapan.dietdiaryapp.fragments;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.canyapan.dietdiaryapp.Application;
import com.canyapan.dietdiaryapp.R;
import com.canyapan.dietdiaryapp.adapters.SpinnerArrayAdapter;
import com.canyapan.dietdiaryapp.adapters.SpinnerItem;
import com.canyapan.dietdiaryapp.db.DatabaseHelper;
import com.canyapan.dietdiaryapp.helpers.ResourcesHelper;
import com.opencsv.CSVReader;

import org.apache.commons.io.input.BOMInputStream;
import org.apache.commons.lang3.ArrayUtils;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class ImportFragment extends Fragment {
    public static final String TAG = "ImportFragment";
    private static final String KEY_SELECTED_FILE_SERIALIZABLE = "FILE";
    private static final int REQUEST_EXTERNAL_STORAGE = 20;

    private OnFragmentInteractionListener mListener;

    private LinearLayout mLinearLayout;
    private Spinner spFiles;
    private File mSelectedFile = null;
    private File[] mFiles = null;

    private DatabaseHelper mDatabaseHelper;
    private ProgressDialog mProgressDialog;
    private ImportAsyncTask mAsyncTask = null;

    public ImportFragment() {
    }

    public static ImportFragment newInstance() {
        return new ImportFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        setHasOptionsMenu(true);

        mDatabaseHelper = new DatabaseHelper(getContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            mSelectedFile = (File) savedInstanceState.getSerializable(KEY_SELECTED_FILE_SERIALIZABLE);
        }

        mLinearLayout = (LinearLayout) inflater.inflate(R.layout.fragment_import_linearlayout, container, false);

        spFiles = (Spinner) mLinearLayout.findViewById(R.id.spFiles);
        loadFiles();

        if (null != mAsyncTask) {
            // To start progress dialog again.
            mAsyncTask.onPreExecute();
        }

        return mLinearLayout;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        if (spFiles.getSelectedItemPosition() > 0) {
            outState.putSerializable(KEY_SELECTED_FILE_SERIALIZABLE, mFiles[spFiles.getSelectedItemPosition() - 1]);
        }
{'fraction_non_alphanumeric': 0.08510063487775227, 'fraction_numerical': 0.0014858841010401188, 'mean_word_length': 4.2547906316536555, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2278588', 'n_tokens_mistral': 2128, 'n_tokens_neox': 2046, 'n_words': 437}