<commit_before>package com.Litterfeldt.AStory.customClasses;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.media.MediaMetadataRetriever;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import com.Litterfeldt.AStory.pagerView;

import java.io.File;
import java.util.*;

public class SqlConnector extends SQLiteOpenHelper{

    private static final String DATABASE_NAME = "astory_cache.db";
    private static final int DATABASE_VERSION = 3;

    //Tables
    private static final String TABLE_NAME_SAVED_STATE = "S_SAVEDSTATE";
    private static final String TABLE_NAME_CHAPTER_LIST ="S_CHAPTERLIST";
    private static final String TABLE_NAME_BOOK_IMG = "S_IMAGECACHE";

    //image cache collumns
    private static final String COLUMN_BOOK_IMG_BOOK_NAME ="BOOK_TITLE";
    private static final String COLUMN_BOOK_IMG_BOOK_IMG ="IMG_BLOB";

    //saved_state columns
    private static final String COLUMN_SAVED_STATE_STATE_ID ="ID";
    private static final String COLUMN_SAVED_STATE_BOOK_ID ="BOOK_ID";
    private static final String COLUMN_SAVED_STATE_CHAPTER_ID ="CHAPTER_ID";
    private static final String COLUMN_SAVED_STATE_CURRENT_TIME_POS="TIME_POS";
    private static final String COLUMN_SAVED_STATE_BOOKMARK_TAG="MADE_BY";

    //chapter_list columns
    private static final String COLUMN_CHAPTER_LIST_CHAPTER_ID ="ID";
    private static final String COLUMN_CHAPTER_LIST_CHAPTER_NR="CHAPTER_NR";
    private static final String COLUMN_CHAPTER_LIST_CHAPTER_PATH="FILEPATH";
    private static final String COLUMN_CHAPTER_LIST_CHAPTER_DURATION ="DURATION";
    private static final String COLUMN_BOOK_LIST_NAME="BOOK_TITLE";
    private static final String COLUMN_BOOK_LIST_AUTHOR="BOOK_AUTHOR";
    private static final String COLUMN_BOOK_LIST_CHAPTER_COUNT="NUMBER_OF_CHAPTERS";

    //Misc
    private boolean imageIsCached = false;
    private static File defaultDir;
    private static SQLiteDatabase db;
    private static MediaMetadataRetriever mmr;
    private static final Set<String> acceptedFormats = new HashSet<String>(Arrays.asList(
            new String[]{"mp3", "m4a", "aac", "flac"}
    ));
    private static final String selectAllBooks = "Select "+COLUMN_BOOK_LIST_NAME+", "+COLUMN_BOOK_LIST_AUTHOR+", "+COLUMN_CHAPTER_LIST_CHAPTER_PATH+" from S_CHAPTERLIST where CHAPTER_NR = 1 order by BOOK_AUTHOR;";
    private static final String selectBook = "Select BOOK_TITLE, BOOK_AUTHOR, FILEPATH, DURATION, CHAPTER_NR, NUMBER_OF_CHAPTERS from S_CHAPTERLIST where BOOK_TITLE = '?' order by CHAPTER_NR;";
    private static final String selectBookNameFromID = "Select BOOK_TITLE from S_CHAPTERLIST where ID = '?';";
    private static final String selectBookIDFromName = "Select ID from S_CHAPTERLIST where BOOK_TITLE = '?';";
    private static final String getimage = "Select IMG_BLOB from S_IMAGECACHE where BOOK_TITLE = '?';";
    private static final String getLastSave = "Select "+COLUMN_SAVED_STATE_BOOK_ID+", "+COLUMN_SAVED_STATE_CHAPTER_ID+", "+COLUMN_SAVED_STATE_CURRENT_TIME_POS+" from S_SAVEDSTATE;";

    private int chaptercounter;
    private Context context;



    public SqlConnector(Context context){
        super(context, DATABASE_NAME , null, DATABASE_VERSION);
        this.context = context;
        defaultDir = new File(Environment.getExternalStorageDirectory() +"/Audiobooks");
        if(!defaultDir.exists()){
            defaultDir.mkdir();
            defaultDir = new File(Environment.getExternalStorageDirectory() +"/Audiobooks");
        }

        db=getWritableDatabase();
        mmr = new MediaMetadataRetriever();

    }
    public void allocateBooks(){

        File[] files = defaultDir.listFiles();
        if(files == null){
            Log.e("ASTORY","The audiobook-folder is empty");
        }
        else {
        for (File f : files){
            imageIsCached = false;
            if(f.isDirectory()){
                makeChapterlist(f);
            }
            else if (f.isFile()){
                String fullpath = f.getAbsolutePath();
                int dot = fullpath.lastIndexOf(".");
                String filetype= fullpath.substring(dot+1);
                if(acceptedFormats.contains(filetype.toLowerCase())){
                    addChapter(fullpath);
                }}}
        }
    }
    private void makeChapterlist(File directory){
        File files[] = directory.listFiles();
        chaptercounter = 0;
        for (File f : files){
            if (f.isFile()){
                String audiopath = getAudioFilePath(f);
                if(audiopath != null){
                  addChapter(audiopath);
                }}}}
    private void addChapter(String path){
        mmr.setDataSource(path);
        String bookName = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
        String author = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
        String numOfChapters = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS);
        String chapterNum = "";
        if (chapterNum==""){
            chaptercounter++;
            chapterNum = Integer.toString(chaptercounter);
        }
        Log.e("DEV","Chapternumber: "+chapterNum);
        String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        String insert = "INSERT INTO "
                +TABLE_NAME_CHAPTER_LIST
                +"("+ COLUMN_BOOK_LIST_NAME
                +","+COLUMN_BOOK_LIST_AUTHOR
                +","+COLUMN_BOOK_LIST_CHAPTER_COUNT
                +","+COLUMN_CHAPTER_LIST_CHAPTER_NR
                +","+COLUMN_CHAPTER_LIST_CHAPTER_DURATION
                +","+COLUMN_CHAPTER_LIST_CHAPTER_PATH+")"
                +" VALUES ("
                +"'"+bookName.replace("'","''")+"'"
                +",'"+author.replace("'","''")+"'"
                +","+numOfChapters
                +","+chapterNum
                +","+duration
                +",'"+path.replace("'","''")+"');";
        db.execSQL(insert);
        if(imageIsCached == false){
            ContentValues initialValues = new ContentValues();
            initialValues.put(COLUMN_BOOK_IMG_BOOK_IMG,(byte[]) mmr.getEmbeddedPicture());
            initialValues.put(COLUMN_BOOK_IMG_BOOK_NAME,bookName);
            db.insert(TABLE_NAME_BOOK_IMG, null, initialValues);
            imageIsCached = true;

        }
    }
    public void newSave(int bookID, int chapterNum, int currentTimePos){
        clearCach();
        String insert = "INSERT INTO "
                +TABLE_NAME_SAVED_STATE
                +"("+ COLUMN_SAVED_STATE_BOOK_ID
                +","+COLUMN_SAVED_STATE_CURRENT_TIME_POS
                +","+COLUMN_SAVED_STATE_CHAPTER_ID
                +","+COLUMN_SAVED_STATE_BOOKMARK_TAG+")"
                +" VALUES ("
                +bookID
                +","+currentTimePos
                +","+chapterNum
                +",'no');";
        db.execSQL(insert);

    }
    public int[] getSave(){
        Cursor cur = db.rawQuery(getLastSave,null);
        cur.moveToFirst();
        int[] i = new int[3];
        i[0]=cur.getInt(0);
        i[1]=cur.getInt(1);
        i[2]=cur.getInt(2);
        return i;


    }
    public byte[] getPicture(String bookname){
        Cursor cur = db.rawQuery(getimage.replace("?",bookname.replace("'","''")),null);
        cur.moveToFirst();
        Log.i("DEV",cur.getBlob(0).toString());
        return cur.getBlob(0);
    }


    private static String getAudioFilePath(File f){
        String fullpath = f.getAbsolutePath();
        int dot = fullpath.lastIndexOf(".");
        String filetype= fullpath.substring(dot+1);
        if(acceptedFormats.contains(filetype.toLowerCase())){
            return fullpath;
        }
        else{
            return null;
        }
    }<commit_msg>Algorithm mcan now find the folderstructure before going for the punch<commit_after>package com.Litterfeldt.AStory.customClasses;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.media.MediaMetadataRetriever;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import com.Litterfeldt.AStory.pagerView;

import java.io.File;
import java.util.*;

public class SqlConnector extends SQLiteOpenHelper{

    private static final String DATABASE_NAME = "astory_cache.db";
    private static final int DATABASE_VERSION = 3;

    //Tables
    private static final String TABLE_NAME_SAVED_STATE = "S_SAVEDSTATE";
    private static final String TABLE_NAME_CHAPTER_LIST ="S_CHAPTERLIST";
    private static final String TABLE_NAME_BOOK_IMG = "S_IMAGECACHE";

    //image cache collumns
    private static final String COLUMN_BOOK_IMG_BOOK_NAME ="BOOK_TITLE";
    private static final String COLUMN_BOOK_IMG_BOOK_IMG ="IMG_BLOB";

    //saved_state columns
    private static final String COLUMN_SAVED_STATE_STATE_ID ="ID";
    private static final String COLUMN_SAVED_STATE_BOOK_ID ="BOOK_ID";
    private static final String COLUMN_SAVED_STATE_CHAPTER_ID ="CHAPTER_ID";
    private static final String COLUMN_SAVED_STATE_CURRENT_TIME_POS="TIME_POS";
    private static final String COLUMN_SAVED_STATE_BOOKMARK_TAG="MADE_BY";

    //chapter_list columns
    private static final String COLUMN_CHAPTER_LIST_CHAPTER_ID ="ID";
    private static final String COLUMN_CHAPTER_LIST_CHAPTER_NR="CHAPTER_NR";
    private static final String COLUMN_CHAPTER_LIST_CHAPTER_PATH="FILEPATH";
    private static final String COLUMN_CHAPTER_LIST_CHAPTER_DURATION ="DURATION";
    private static final String COLUMN_BOOK_LIST_NAME="BOOK_TITLE";
    private static final String COLUMN_BOOK_LIST_AUTHOR="BOOK_AUTHOR";
    private static final String COLUMN_BOOK_LIST_CHAPTER_COUNT="NUMBER_OF_CHAPTERS";

    //Misc
    private boolean imageIsCached = false;
    private static File defaultDir;
    private static SQLiteDatabase db;
    private static MediaMetadataRetriever mmr;
    private static final Set<String> acceptedFormats = new HashSet<String>(Arrays.asList(
            new String[]{"mp3", "m4a", "aac", "flac"}
    ));
    private static final String selectAllBooks = "Select "+COLUMN_BOOK_LIST_NAME+", "+COLUMN_BOOK_LIST_AUTHOR+", "+COLUMN_CHAPTER_LIST_CHAPTER_PATH+" from S_CHAPTERLIST where CHAPTER_NR = 1 order by BOOK_AUTHOR;";
    private static final String selectBook = "Select BOOK_TITLE, BOOK_AUTHOR, FILEPATH, DURATION, CHAPTER_NR, NUMBER_OF_CHAPTERS from S_CHAPTERLIST where BOOK_TITLE = '?' order by CHAPTER_NR;";
    private static final String selectBookNameFromID = "Select BOOK_TITLE from S_CHAPTERLIST where ID = '?';";
    private static final String selectBookIDFromName = "Select ID from S_CHAPTERLIST where BOOK_TITLE = '?';";
    private static final String getimage = "Select IMG_BLOB from S_IMAGECACHE where BOOK_TITLE = '?';";
    private static final String getLastSave = "Select "+COLUMN_SAVED_STATE_BOOK_ID+", "+COLUMN_SAVED_STATE_CHAPTER_ID+", "+COLUMN_SAVED_STATE_CURRENT_TIME_POS+" from S_SAVEDSTATE;";

    private int chaptercounter;
    private ArrayList<String> bookPathHerarchy;
    private Context context;



    public SqlConnector(Context context){
        super(context, DATABASE_NAME , null, DATABASE_VERSION);
        this.context = context;
        defaultDir = new File(Environment.getExternalStorageDirectory() +"/Audiobooks");
        if(!defaultDir.exists()){
            defaultDir.mkdir();
            defaultDir = new File(Environment.getExternalStorageDirectory() +"/Audiobooks");
        }

        db=getWritableDatabase();
        mmr = new MediaMetadataRetriever();

    }
    public ArrayList<String> allocateBookFolderHerarchy(){
        ArrayList<String> bookPathList = new ArrayList<String>();

        File[] files = defaultDir.listFiles();

        if (files == null){
            return null;
        }else {
            for (File f : files){
                if(f.isDirectory()){
                    bookPathList.add(f.getAbsolutePath());
                }
                else if (f.isFile()){
                    String fullpath = f.getAbsolutePath();
                    String filetype = fullpath.substring(fullpath.lastIndexOf(".")+1);

                    if(acceptedFormats.contains(filetype.toLowerCase())){
                        bookPathList.add(fullpath);
                    }}}
            bookPathHerarchy = bookPathList;
            return bookPathList;
        }
    }
    public void allocateBooks(){

        File[] files = defaultDir.listFiles();
        if(files == null){
            Log.e("ASTORY","The audiobook-folder is empty");
        }
        else {
        for (File f : files){
            imageIsCached = false;
            if(f.isDirectory()){
                makeChapterlist(f);
            }
            else if (f.isFile()){
                String fullpath = f.getAbsolutePath();
                int dot = fullpath.lastIndexOf(".");
                String filetype= fullpath.substring(dot+1);
                if(acceptedFormats.contains(filetype.toLowerCase())){
                    addChapter(fullpath);
                }}}
        }
    }
    private void makeChapterlist(File directory){
        File files[] = directory.listFiles();
        chaptercounter = 0;
        for (File f : files){
            if (f.isFile()){
                String audiopath = getAudioFilePath(f);
                if(audiopath != null){
                  addChapter(audiopath);
                }}}}
    private void addChapter(String path){
        mmr.setDataSource(path);
        String bookName = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
        String author = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
        String numOfChapters = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS);
        String chapterNum = "";
        if (chapterNum==""){
            chaptercounter++;
            chapterNum = Integer.toString(chaptercounter);
        }
        Log.e("DEV","Chapternumber: "+chapterNum);
        String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
        String insert = "INSERT INTO "
                +TABLE_NAME_CHAPTER_LIST
                +"("+ COLUMN_BOOK_LIST_NAME
                +","+COLUMN_BOOK_LIST_AUTHOR
                +","+COLUMN_BOOK_LIST_CHAPTER_COUNT
                +","+COLUMN_CHAPTER_LIST_CHAPTER_NR
                +","+COLUMN_CHAPTER_LIST_CHAPTER_DURATION
                +","+COLUMN_CHAPTER_LIST_CHAPTER_PATH+")"
                +" VALUES ("
                +"'"+bookName.replace("'","''")+"'"
                +",'"+author.replace("'","''")+"'"
                +","+numOfChapters
                +","+chapterNum
                +","+duration
                +",'"+path.replace("'","''")+"');";
        db.execSQL(insert);
        if(imageIsCached == false){
            ContentValues initialValues = new ContentValues();
            initialValues.put(COLUMN_BOOK_IMG_BOOK_IMG,(byte[]) mmr.getEmbeddedPicture());
            initialValues.put(COLUMN_BOOK_IMG_BOOK_NAME,bookName);
            db.insert(TABLE_NAME_BOOK_IMG, null, initialValues);
            imageIsCached = true;

        }
    }
    public void newSave(int bookID, int chapterNum, int currentTimePos){
        clearCach();
        String insert = "INSERT INTO "
                +TABLE_NAME_SAVED_STATE
                +"("+ COLUMN_SAVED_STATE_BOOK_ID
                +","+COLUMN_SAVED_STATE_CURRENT_TIME_POS
                +","+COLUMN_SAVED_STATE_CHAPTER_ID
                +","+COLUMN_SAVED_STATE_BOOKMARK_TAG+")"
                +" VALUES ("
                +bookID
                +","+currentTimePos
                +","+chapterNum
                +",'no');";
        db.execSQL(insert);

    }
    public int[] getSave(){
        Cursor cur = db.rawQuery(getLastSave,null);
        cur.moveToFirst();
        int[] i = new int[3];
        i[0]=cur.getInt(0);
        i[1]=cur.getInt(1);
        i[2]=cur.getInt(2);
        return i;


    }
    public byte[] getPicture(String bookname){
        Cursor cur = db.rawQuery(getimage.replace("?",bookname.replace("'","''")),null);
        cur.moveToFirst();
        return cur.getBlob(0);
    }


    private static String getAudioFilePath(File f){
        String fullpath = f.getAbsolutePath();
        int dot = fullpath.lastIndexOf(".");
        String filetype= fullpath.substring(dot+1);
        if(acceptedFormats.contains(filetype.toLowerCase())){
            return fullpath;
        }
        else{
            return null;
        }
    }
{'fraction_non_alphanumeric': 0.10505074485132648, 'fraction_numerical': 0.0018992225057866937, 'mean_word_length': 2.766204738489048, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4624801', 'n_tokens_mistral': 5125, 'n_tokens_neox': 4934, 'n_words': 989}