/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  android.content.Context
 *  android.os.Build
 *  android.os.Build$VERSION
 *  android.os.Environment
 *  android.util.Log
 *  java.io.File
 *  java.io.FileOutputStream
 *  java.io.IOException
 *  java.io.OutputStream
 *  java.io.PrintWriter
 *  java.lang.Class
 *  java.lang.Integer
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.Throwable
 *  java.lang.UnsupportedOperationException
 *  java.lang.reflect.Method
 */
package com.google.code.microlog4android.appender;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import com.google.code.microlog4android.Level;
import com.google.code.microlog4android.appender.AbstractAppender;
import com.google.code.microlog4android.format.Formatter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Method;

public class FileAppender
extends AbstractAppender {
    public static final String DEFAULT_FILENAME = "microlog.txt";
    private static final String TAG = "Microlog.FileAppender";
    private boolean append;
    private String fileName;
    Context mContext;
    private File mSdCardLogFile;
    private PrintWriter writer;

    public FileAppender() {
        this.fileName = "microlog.txt";
        this.append = false;
        this.mSdCardLogFile = null;
        this.mContext = null;
    }

    public FileAppender(Context context) {
        this.fileName = "microlog.txt";
        this.append = false;
        this.mSdCardLogFile = null;
        this.mContext = null;
        this.mContext = context;
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException();
    }

    @Override
    public void close() throws IOException {
        FileAppender fileAppender = this;
        synchronized (fileAppender) {
            Log.i((String)"Microlog.FileAppender", (String)"Closing the FileAppender");
            if (this.writer != null) {
                this.writer.close();
            }
            return;
        }
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    @Override
    public void doLog(String string2, String string3, long l, Level level, Object object, Throwable throwable) {
        FileAppender fileAppender = this;
        synchronized (fileAppender) {
            if (this.logOpen && this.formatter != null && this.writer != null) {
                this.writer.println(this.formatter.format(string2, string3, l, level, object, throwable));
                this.writer.flush();
                if (throwable != null) {
                    throwable.printStackTrace();
                }
            } else if (this.formatter == null) {
                Log.e((String)"Microlog.FileAppender", (String)"Please set a formatter.");
            }
            return;
        }
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    protected File getExternalStorageDirectory() {
        FileAppender fileAppender = this;
        synchronized (fileAppender) {
            Context context;
            File file = Environment.getExternalStorageDirectory();
            if (new Integer(Build.VERSION.SDK) >= 8 && (context = this.mContext) != null) {
                try {
                    file = (File)Context.class.getMethod("getExternalFilesDir", new Class[]{String.class}).invoke((Object)this.mContext, new Object[1]);
                }
                catch (Throwable var5_4) {
                    Log.e((String)"Microlog.FileAppender", (String)"Could not execute method getExternalFilesDir() on sdk >=8", (Throwable)var5_4);
                }
            }
            if (file != null && !file.exists() && !file.mkdirs()) {
                file = null;
                Log.e((String)"Microlog.FileAppender", (String)("mkdirs failed on externalStorageDirectory " + null));
            }
            return file;
        }
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public File getLogFile() {
        FileAppender fileAppender = this;
        synchronized (fileAppender) {
            File file;
            if (this.mSdCardLogFile != null) return this.mSdCardLogFile;
            if (Environment.getExternalStorageState().equals((Object)"mounted") && (file = this.getExternalStorageDirectory()) != null) {
                this.mSdCardLogFile = new File(file, this.fileName);
            }
            if (this.mSdCardLogFile != null) return this.mSdCardLogFile;
            Log.e((String)"Microlog.FileAppender", (String)"Unable to open log file from external storage");
            return this.mSdCardLogFile;
        }
    }

    @Override
    public long getLogSize() {
        return -1;
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    @Override
    public void open() throws IOException {
        FileAppender fileAppender = this;
        synchronized (fileAppender) {
            File file = this.getLogFile();
            this.logOpen = false;
            if (file != null) {
                FileOutputStream fileOutputStream;
                if (!file.exists() && !file.createNewFile()) {
                    Log.e((String)"Microlog.FileAppender", (String)"Unable to create new log file");
                }
                if ((fileOutputStream = new FileOutputStream(file, this.append)) != null) {
                    this.writer = new PrintWriter((OutputStream)fileOutputStream);
                    this.logOpen = true;
                } else {
                    Log.e((String)"Microlog.FileAppender", (String)"Failed to create the log file (no stream)");
                }
            }
            return;
        }
    }

    public void setAppend(boolean bl) {
        this.append = bl;
    }

    public void setFileName(String string2) {
        if (string2 != null) {
            this.fileName = string2;
        }
    }
}

