万能钥匙.apk(点击下载) / MyLog.java


package com.lantern.core.log;

import android.content.Context;
import android.os.Environment;
import com.appara.core.BLText;
import com.appara.feed.util.DateUtil;
import com.lantern.core.business.IPubParams;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyLog {
    private static final String ENDSWITH = ".tmp";
    private static final String EVENT_LOG = "eventLog.txt";
    private static String PATH_SDCARD_DIR = (Environment.getExternalStorageDirectory().getPath() + File.separator + "wifilog");
    private static final String SAVE_LOG = "saveLog.txt";
    private static final String SEND_LOG = "sendLog.txt";
    private static final String SERVER_URL = "http://wifi3a.51y5.net/qiyun-api/upload";
    private static Boolean SWITCH = Boolean.FALSE;
    public static volatile Object evLock = new Object();
    private static ExecutorService fixedThreadPool = Executors.newCachedThreadPool();
    private static SimpleDateFormat logfile = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
    private static SimpleDateFormat myLogSdf = new SimpleDateFormat(DateUtil.yyyy_MM_dd_HH_mm_ss);
    public static volatile Object sdLock = new Object();
    public static volatile Object svLock = new Object();
    private Context mContext;
    private IPubParams mPubParams;

    public MyLog(Context context, IPubParams iPubParams) {
        this.mContext = context;
        this.mPubParams = iPubParams;
        fixedThreadPool = Executors.newCachedThreadPool();
    }

    /* access modifiers changed from: private */
    public static void writeFile(String str, String str2, String str3) {
        Date date = new Date();
        String str4 = myLogSdf.format(date) + "    " + str2 + "    " + str3;
        File file = new File(PATH_SDCARD_DIR);
        if (!file.exists()) {
            file.mkdirs();
        }
        try {
            FileWriter fileWriter = new FileWriter(new File(file, str), true);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(str4);
            bufferedWriter.newLine();
            bufferedWriter.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void event(final String str, final String str2) {
        if (SWITCH.booleanValue()) {
            fixedThreadPool.execute(new Runnable() {
                /* class com.lantern.core.log.MyLog.AnonymousClass1 */

                public final void run() {
                    synchronized (MyLog.evLock) {
                        MyLog.writeFile(MyLog.EVENT_LOG, str, str2);
                    }
                }
            });
        }
    }

    public static void save(final String str, final String str2) {
        if (SWITCH.booleanValue()) {
            fixedThreadPool.execute(new Runnable() {
                /* class com.lantern.core.log.MyLog.AnonymousClass2 */

                public final void run() {
                    synchronized (MyLog.svLock) {
                        MyLog.writeFile(MyLog.SAVE_LOG, str, str2);
                    }
                }
            });
        }
    }

    public static void send(final String str, final String str2) {
        if (SWITCH.booleanValue()) {
            fixedThreadPool.execute(new Runnable() {
                /* class com.lantern.core.log.MyLog.AnonymousClass3 */

                public final void run() {
                    synchronized (MyLog.sdLock) {
                        MyLog.writeFile(MyLog.SEND_LOG, str, str2);
                    }
                }
            });
        }
    }

    public void upload() {
        fixedThreadPool.execute(new Runnable() {
            /* class com.lantern.core.log.MyLog.AnonymousClass4 */

            public void run() {
                synchronized (MyLog.evLock) {
                    MyLog.this.renameFile(MyLog.EVENT_LOG);
                }
                synchronized (MyLog.svLock) {
                    MyLog.this.renameFile(MyLog.SAVE_LOG);
                }
                synchronized (MyLog.sdLock) {
                    MyLog.this.renameFile(MyLog.SEND_LOG);
                }
                MyLog.this.uploadTmpFiles();
            }
        });
    }

    /* access modifiers changed from: package-private */
    public static class MyFilter implements FilenameFilter {
        private String type;

        public MyFilter(String str) {
            this.type = str;
        }

        public boolean accept(File file, String str) {
            return str.endsWith(this.type);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void uploadTmpFiles() {
        String[] list = new File(PATH_SDCARD_DIR).list(new MyFilter(ENDSWITH));
        if (list != null) {
            for (final String str : list) {
                fixedThreadPool.execute(new Runnable() {
                    /* class com.lantern.core.log.MyLog.AnonymousClass5 */

                    public void run() {
                        MyLog myLog = MyLog.this;
                        myLog.uploadTmpFile(MyLog.PATH_SDCARD_DIR + File.separator + str);
                    }
                });
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void uploadTmpFile(String str) {
        HashMap hashMap = new HashMap();
        hashMap.put("deviceFlag", this.mPubParams.getIMEI());
        post(str, hashMap);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private boolean renameFile(String str) {
        Date date = new Date();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(logfile.format(date));
        stringBuffer.append("_");
        stringBuffer.append(this.mPubParams.getIMEI());
        stringBuffer.append("_");
        stringBuffer.append(str);
        stringBuffer.append(ENDSWITH);
        File file = new File(PATH_SDCARD_DIR, stringBuffer.toString());
        if (file.exists()) {
            file.delete();
        }
        File file2 = new File(PATH_SDCARD_DIR, str);
        if (file2.exists()) {
            return file2.renameTo(file);
        }
        return false;
    }

    public static int post(String str, Map<String, String> map) {
        String[] split = str.split("/");
        String str2 = split[split.length - 1];
        File file = new File(str);
        int i = 0;
        if (!file.exists() || !file.isFile()) {
            return 0;
        }
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(SERVER_URL).openConnection();
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
            httpURLConnection.setRequestProperty("ENCTYPE", "multipart/form-data");
            httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=".concat(String.valueOf("*****")));
            httpURLConnection.setRequestProperty("files", str2);
            DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
            dataOutputStream.writeBytes("--" + "*****" + BLText.CRLF);
            StringBuffer stringBuffer = new StringBuffer();
            for (String str3 : map.keySet()) {
                dataOutputStream.writeBytes("--" + "*****" + BLText.CRLF);
                stringBuffer.append("Content-Disposition: form-data; name=\"" + str3 + "\"" + BLText.CRLF);
                stringBuffer.append(BLText.CRLF);
                stringBuffer.append(map.get(str3) + BLText.CRLF);
            }
            dataOutputStream.writeBytes(stringBuffer.toString());
            dataOutputStream.writeBytes("--" + "*****" + BLText.CRLF);
            dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"files\";filename=\"" + str2 + "\"" + BLText.CRLF);
            dataOutputStream.writeBytes(BLText.CRLF);
            int min = Math.min(fileInputStream.available(), 1048576);
            byte[] bArr = new byte[min];
            int read = fileInputStream.read(bArr, 0, min);
            while (read > 0) {
                dataOutputStream.write(bArr, 0, min);
                min = Math.min(fileInputStream.available(), 1048576);
                read = fileInputStream.read(bArr, 0, min);
            }
            dataOutputStream.writeBytes(BLText.CRLF);
            dataOutputStream.writeBytes("--" + "*****" + "--" + BLText.CRLF);
            i = httpURLConnection.getResponseCode();
            httpURLConnection.getResponseMessage();
            fileInputStream.close();
            dataOutputStream.flush();
            dataOutputStream.close();
            if (i == 200) {
                file.delete();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e2) {
            e2.printStackTrace();
        } catch (IOException e3) {
            e3.printStackTrace();
        }
        return i;
    }
}