Life Simulator: Game Dev v0.6版本的 MD5 值为:ecca8c682c00d3b861fbed7f58e41c8d

以下内容为反编译后的 AvocarrotLogger.java 源代码,内容仅作参考


package com.avocarrot.androidsdk.logging;

import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import com.amazon.device.ads.WebRequest;
import com.applovin.sdk.AppLovinEventTypes;
import com.avocarrot.androidsdk.Avocarrot;
import com.avocarrot.androidsdk.DeviceInfo;
import com.avocarrot.androidsdk.DynamicConfiguration;
import com.tapjoy.TJAdUnitConstants;
import com.tapjoy.TapjoyConstants;
import com.vungle.publisher.FullScreenAdActivity;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.json.JSONException;
import org.json.JSONObject;

public class AvocarrotLogger {
    static Integer timeout;
    private static String SERVER_LOGGER = "https://sdklogs.avocarrot.com";
    private static final Handler handler = new Handler(Looper.getMainLooper());
    static ExecutorService executorService = Executors.newSingleThreadExecutor();
    protected static boolean isLocalLoggerEnabled = false;
    protected static Levels userLevel = null;
    protected static JSONObject extraInfo = new JSONObject();
    static List<String> pendingEvents = new ArrayList();
    static Runnable batchEvents = new Runnable() {
        @Override
        public void run() {
            AvocarrotLogger.executorService.submit(AvocarrotLogger.sendEvents);
        }
    };
    static Runnable sendEvents = new Runnable() {
        @Override
        public void run() {
            if (AvocarrotLogger.pendingEvents.size() > 0) {
                String message = "[" + TextUtils.join(",", AvocarrotLogger.pendingEvents) + "]";
                AvocarrotLogger.pendingEvents.clear();
                AvocarrotLogger.sendEventToServer(AvocarrotLogger.SERVER_LOGGER, message);
                AvocarrotLogger.handler.postDelayed(AvocarrotLogger.batchEvents, AvocarrotLogger.timeout.intValue());
            }
        }
    };

    public static void addExtraInfo(String key, String value) {
        try {
            extraInfo.put(key, value);
        } catch (JSONException e) {
        }
    }

    public static void addAdInfo(String adId) {
        addExtraInfo(FullScreenAdActivity.AD_ID_EXTRA_KEY, adId);
    }

    public static void addPlacementInfo(String placementName) {
        addExtraInfo("placement", placementName);
    }

    public static void disable() {
        isLocalLoggerEnabled = false;
    }

    public static void enableWithLevel(String level) {
        if (level.equals("ERROR") || level.equals("WARN") || level.equals("INFO") || level.equals("DEBUG")) {
            userLevel = Levels.valueOf(level);
        } else {
            userLevel = Levels.valueOf("ALL");
        }
        isLocalLoggerEnabled = true;
    }

    public static void AvocarrotLog(Levels level, String message) {
        AvocarrotLog(level, message, null, new String[0]);
    }

    public static void AvocarrotLog(Levels level, String message, Throwable throwable, String... extra) {
        AvocarrotLog(false, level, message, throwable, extra);
    }

    public static void AvocarrotLog(boolean onlyServer, Levels level, String message, Throwable throwable, String... extra) {
        if (!TextUtils.isEmpty(message)) {
            if (timeout == null) {
                timeout = DynamicConfiguration.getIntFallbackToDefault(DynamicConfiguration.GENERAL, DynamicConfiguration.Settings.loggerBatchTimeout);
                if (timeout == null) {
                    timeout = 1000;
                }
            }
            AvocarrotLogger logger = new AvocarrotLogger();
            if (!onlyServer) {
                logger.logToLogCat(level, message, throwable);
            }
            logger.logToServer(level, message, throwable, extra);
        }
    }

    void logToLogCat(Levels level, @NonNull String message, Throwable throwable) {
        if (isLocalLoggerEnabled && level.compareTo(userLevel) >= 0) {
            String printMessage = message;
            if (throwable != null) {
                printMessage = printMessage + ": " + throwable.toString();
            }
            switch (level) {
                case ERROR:
                    Log.e("Avocarrot", "[ERROR]: " + printMessage);
                    return;
                case WARN:
                    Log.w("Avocarrot", "[WARN]:  " + printMessage);
                    return;
                case INFO:
                    Log.i("Avocarrot", "[INFO]:  " + printMessage);
                    return;
                case DEBUG:
                    return;
                default:
                    Log.d("Avocarrot", "[ALL]:   " + printMessage);
                    return;
            }
        }
    }

    void logToServer(Levels level, String message, Throwable throwable, String... extra) {
        try {
            String levelSetting = DynamicConfiguration.getString(DynamicConfiguration.GENERAL, DynamicConfiguration.Settings.logger);
            if (TextUtils.isEmpty(levelSetting)) {
                levelSetting = DynamicConfiguration.getDefaultString(DynamicConfiguration.Settings.logger);
            }
            Levels serverLevel = null;
            try {
                serverLevel = Levels.valueOf(levelSetting.toUpperCase());
            } catch (Exception e) {
            }
            if (serverLevel != null && level.compareTo(serverLevel) >= 0) {
                Map<String, String> logToServer = new HashMap<>();
                logToServer.put(AppLovinEventTypes.USER_COMPLETED_LEVEL, level.toString());
                logToServer.put(TJAdUnitConstants.String.MESSAGE, message);
                logToServer.put("stacktrace", Log.getStackTraceString(throwable));
                logToServer.put("timestamp", Long.toString(System.currentTimeMillis()));
                Iterator<?> keys = extraInfo.keys();
                while (keys.hasNext()) {
                    String key = keys.next();
                    logToServer.put(key, extraInfo.optString(key));
                }
                Avocarrot avocarrot = Avocarrot.getInstance();
                if (avocarrot != null) {
                    logToServer.put("apiKey", avocarrot.getApiKey());
                    logToServer.put("sandbox", avocarrot.isInSandbox() ? "TRUE" : "FALSE");
                    logToServer.put("sdk", Avocarrot.getSDKVersion());
                    DeviceInfo deviceInfo = avocarrot.getDeviceInfo();
                    if (deviceInfo != null) {
                        logToServer.put("buildVersion", deviceInfo.getDeviceManufacturer());
                        logToServer.put("deviceModel", deviceInfo.getDeviceModel());
                        logToServer.put(TapjoyConstants.TJC_PLATFORM, deviceInfo.getPlatform());
                        logToServer.put("os", deviceInfo.getOSVersion());
                        logToServer.put("package", deviceInfo.getPackageName());
                        logToServer.put("appName", deviceInfo.getAppName());
                        logToServer.put("appVersion", deviceInfo.getVersionName());
                        logToServer.put("uuid", deviceInfo.getUUID());
                        logToServer.put("carrier", deviceInfo.getCarrier());
                        logToServer.put("language", deviceInfo.getLanguage());
                        logToServer.put("mcc", deviceInfo.getMCC());
                        logToServer.put("mnc", deviceInfo.getMNC());
                        logToServer.put("connectionType", deviceInfo.getOpenRTBConnectionType());
                        logToServer.put("orientation", deviceInfo.getOrientation());
                        logToServer.put("simCountryIso", deviceInfo.getSimCountryIso());
                    }
                }
                if (extra != null) {
                    int count = extra.length;
                    for (int i = 0; i < count; i += 2) {
                        if (i + 1 < count) {
                            logToServer.put(extra[i], extra[i + 1]);
                        } else {
                            logToServer.put("extra", extra[i]);
                        }
                    }
                }
                sendEventToServer(new JSONObject(logToServer));
            }
        } catch (Exception e2) {
            logToLogCat(Levels.INFO, "Could not propagate event to remote endpoint", e2);
        }
    }

    void sendEventToServer(JSONObject event) {
        pendingEvents.add(event.toString());
        handler.removeCallbacks(batchEvents);
        handler.postDelayed(batchEvents, timeout.intValue());
    }

    static void sendEventToServer(String server, String event) {
        try {
            URL url = new URL(server);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            try {
                Integer timeout2 = DynamicConfiguration.getInt(DynamicConfiguration.GENERAL, DynamicConfiguration.Settings.loggerReqTimeout);
                if (timeout2 != null) {
                    connection.setConnectTimeout(timeout2.intValue());
                    connection.setReadTimeout(timeout2.intValue());
                }
                connection.setRequestProperty("Content-Type", WebRequest.CONTENT_TYPE_JSON);
                connection.setDoOutput(true);
                connection.setRequestMethod("POST");
                Writer writer = new OutputStreamWriter(connection.getOutputStream());
                writer.write(event);
                writer.flush();
                writer.close();
                connection.getResponseCode();
                connection.disconnect();
            } catch (Exception e) {
                connection.disconnect();
            } catch (Throwable th) {
                connection.disconnect();
                throw th;
            }
        } catch (Exception e2) {
        }
    }

    public enum Levels {
        DEBUG("DEBUG"),
        ALL("ALL"),
        INFO("INFO"),
        WARN("WARN"),
        ERROR("ERROR");

        private String text;

        Levels(String text) {
            this.text = text;
        }
    }
}