package com.freak.asteroid.randomiser;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.Vector;

public class RandomiseThread extends Thread {

    private final File mStartFolder;

    public RandomiseThread(File startFolder) {
        mStartFolder = startFolder;
    }

    @Override
    public void run() {
        if (mStartFolder != null && mStartFolder.isDirectory()) {
            parse(mStartFolder);
        }
    }

    private Vector<File> parse(File folder) {
        Vector<File> files = new Vector<>();
        File[] filesTab = folder.listFiles();

        for (File aFilesTab : filesTab) {
            if (aFilesTab.getName().endsWith("mp3")) {
                files.add(aFilesTab);
            } else if (aFilesTab.isDirectory()) {
                files.addAll(parse(aFilesTab));
            }
        }
        
        File playlist = new File(mStartFolder, folder.getName() + ".m3u");
        randomiseAndWrite(files, playlist);

        return files;
    }

    private void randomiseAndWrite(Vector<File> files, File playlist) {
        String[] filesNames = new String[files.size()];
        Random random = new Random();
        int index;

        for (int i = 0 ; i < files.size() ; i++) {
            if (files.size() > 1) {
                index = random.nextInt() % files.size();
            }
            else {
                index = 0;
            }
            filesNames[i] = files.get(index).getAbsolutePath();
            files.remove(index);
        }

        if (!playlist.exists()) {
            try {
                playlist.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        PrintWriter writer;
        try {
            writer = new PrintWriter(playlist, "UTF-8");
            for (String filesName : filesNames) {
                writer.println(filesName);
            }
            writer.close();
        } catch (FileNotFoundException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}

@@ -33,16 +33,42 @@
 miser;%0A%0A
+import android.util.Log;%0A%0A
 import j
@@ -317,16 +317,120 @@
 read %7B%0A%0A
+    private static final String TAG = %22RandomiseThread%22;%0A    private static final boolean DEBUG = true;%0A
     priv
@@ -741,32 +741,105 @@
 (File folder) %7B%0A
+        if (DEBUG)%0A            Log.d(TAG, %22Parse %22 + folder.getName());%0A%0A
         Vector%3CF
@@ -1103,52 +1103,524 @@
 ) %7B%0A
-                files.addAll(parse(aFilesTab
+%0A                if (DEBUG)%0A                    Log.d(TAG, %22Found directory %22 + aFilesTab.getName());%0A%0A                Vector%3CFile%3E subFiles = parse(aFilesTab);%0A%0A                if (DEBUG) %7B%0A                    Log.d(TAG, aFilesTab.getName() + %22 contains %22 + subFiles.size() + %22 files%22);%0A                    Log.d(TAG, %22Vector size was %22 + files.size());%0A                %7D%0A%0A                files.addAll(parse(aFilesTab));%0A%0A                if (DEBUG)%0A                    Log.d(TAG, %22Vector size is now %22 + files.size(
 ));%0A
@@ -1768,24 +1768,105 @@
 playlist);%0A%0A
+%0A        if (DEBUG)%0A            Log.d(TAG, %22Done parsing %22 + folder.getName());%0A%0A
         retu
@@ -2102,23 +2102,28 @@
  %3C files
-.size()
+Names.length
  ; i++) 
@@ -2233,69 +2233,244 @@
     
-%7D%0A            else %7B%0A                index = 0;%0A            %7D
+    if (index %3C 0)%0A                    index *= -1;%0A            %7D%0A            else %7B%0A                index = 0;%0A            %7D%0A%0A            if (DEBUG)%0A                Log.d(TAG, %22i=%22 + i + %22, size=%22 + files.size() + %22, index=%22 + index);%0A
 %0A   

Fix issue and add traces in randomise thread