package com.xixun.launcher;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

import java.io.CharArrayWriter;
import java.io.PrintWriter;

public class Util {
    public static StringBuffer buf = new StringBuffer();

    public static void println(String msg) {
        System.out.println(msg);
        if(buf.length()>1000000) buf.replace(0, 100000, "");
        buf.append(msg).append("\n");
    }
    public static void printStackTrace(Throwable e) {
        println(toStackTrace(e));
    }

    public static String toStr(Throwable e) {
        var traces = e.getStackTrace();
        var msg = e.toString();
        for(var trace : traces) if(trace.getClassName().startsWith("com.xixun.launcher.")) {
            msg += "\n  at "+trace.getFileName()+'.'+trace.getMethodName()+':'+trace.getLineNumber();
        }
        return msg;
    }
    public static String toStackTrace(Throwable e) {
        var out = new CharArrayWriter();
        var writer = new PrintWriter(out);
        e.printStackTrace(writer);
        writer.flush();
        return out.toString();
    }

    public static Toast makeText(Context context, CharSequence text) {
        var toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
        return toast;
    }
    public static String programsDir, backImgFile;
}
