package com.githang.android.superlog;/**
 * Created by msdx on 2014/4/22.
 */

import android.util.Log;

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

/**
 * User: Geek_Soledad(msdx.android@qq.com)
 * Date: 2014-04-22
 * Time: 17:22
 * FIXME56
 */
public class LogSendUtil {
    private static final String LOG_TAG = LogSendUtil.class.getSimpleName();

    static void send() throws Exception {
        File sendDir = sendReady();
        File[] logs = sendDir.listFiles();
        if(logs.length == 0) {
            Log.i(LOG_TAG, "no log file need to send");
            return;
        }
        LogConfig config = SuperLog.config;
        LogSender sender = new LogSender(config.sendEmailUser, config.sendEmailPassword, config.sendEmail, config.receiveEmail, config.sendEmailHost, config.sendEmailPort, config.mailSubject, "");
        for (File log: logs) {
            sender.addAttachment(log.getPath(), log.getName());
        }
        sender.send();
    }

    private static File sendReady() {
        File sendDir = new File(SuperLog.config.logPath, "send");
        if(!sendDir.exists()) {
            sendDir.mkdirs();
        }
        File logDir = new File(SuperLog.config.logPath);

        FileFilter ff = new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                return pathname.isFile();
            }
        };
        File[] logs = logDir.listFiles(ff);
        try {
            File currentLog = LogFileUtil.getLogFile().getCanonicalFile();
            for (File logFile : logs) {
                Log.d("LogSend", logFile.getName());
                if(logFile.getCanonicalFile().equals(currentLog)) {
//                    continue;
                }
                logFile.renameTo(new File(sendDir, logFile.getName()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sendDir;

    }
}

@@ -1016,23 +1016,52 @@
 end();%0D%0A
+        afterSend(sendDir);%0D%0A
     %7D%0D%0A
-
 %0D%0A    pr
@@ -1788,10 +1788,8 @@
  %7B%0D%0A
-//
     
@@ -1815,16 +1815,16 @@
 tinue;%0D%0A
+
         
@@ -2027,16 +2027,489 @@
 ndDir;%0D%0A
+    %7D%0D%0A%0D%0A    private static void afterSend(File sendDir) %7B%0D%0A        File sendOutDir = new File(SuperLog.config.logPath, %22sended%22);%0D%0A        if(!sendOutDir.exists()) %7B%0D%0A            sendOutDir.mkdirs();%0D%0A        %7D%0D%0A        File%5B%5D logs = sendDir.listFiles();%0D%0A        for (File logFile: logs) %7B%0D%0A            logFile.renameTo(new File(sendOutDir, logFile.getName()));%0D%0A        %7D%0D%0A        if(SuperLog.config.deleteFileAfterSend) %7B%0D%0A            sendDir.deleteOnExit();%0D%0A        %7D
 %0D%0A    %7D%0D

Move log after send success.