APKPure.apk(点击下载) / SLAReporter.java


package com.tencent.raft.raftframework.sla;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.TrafficStats;
import android.os.Build;
import android.text.TextUtils;
import android.util.Pair;
import com.j256.ormlite.stmt.query.SimpleComparison;
import com.tencent.raft.ipc.EIPCConst;
import com.tencent.raft.measure.utils.MeasureConst;
import com.tencent.raft.raftframework.RAFT;
import com.tencent.raft.raftframework.exception.RAFTCallWrongProcessException;
import com.tencent.raft.raftframework.exception.RAFTIPCNotSupportException;
import com.tencent.raft.raftframework.exception.RAFTNotDeclareServiceException;
import com.tencent.raft.raftframework.exception.RAFTServiceNotFoundException;
import com.tencent.raft.raftframework.exception.RAFTServiceNotMatchException;
import com.tencent.raft.raftframework.log.ILogDelegate;
import com.tencent.raft.raftframework.log.RLog;
import com.tencent.raft.raftframework.util.AppUtils;
import com.tencent.raft.raftframework.util.ProcessUtil;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;

public class SLAReporter {
    public static final int GROUP_ID_MUST_REPORT = -1;
    public static final String PERMISSION_NET = "android.permission.INTERNET";
    public static final double REPORT_NS_MS = 1000000.0d;
    private static final int SINGER_SERVICE_MAX_REPORT_COUNT = 5;
    public static final String TAG = "SLAReporter";
    private StringBuilder baseURL;
    private Context context;
    private final Executor executor;
    private long frameworkStartupNs;
    private int groupId = 1;
    private boolean isInit = false;
    public boolean open = true;
    private final ConcurrentHashMap<String, e.w.f.b.a.a> reportService = new ConcurrentHashMap<>(64);

    public class a implements Runnable {
        public final /* synthetic */ String b;
        public final /* synthetic */ String c;
        public final /* synthetic */ List d;

        /* renamed from: e  reason: collision with root package name */
        public final /* synthetic */ String f4508e;

        /* renamed from: f  reason: collision with root package name */
        public final /* synthetic */ long f4509f;

        /* renamed from: g  reason: collision with root package name */
        public final /* synthetic */ String f4510g;

        public a(String str, String str2, List list, String str3, long j2, String str4) {
            this.b = str;
            this.c = str2;
            this.d = list;
            this.f4508e = str3;
            this.f4509f = j2;
            this.f4510g = str4;
        }

        public void run() {
            SLAReporter.this.initIfNeeded();
            SLAReporter.this.recordServiceReportCount(this.b, this.c);
            List<Pair> list = this.d;
            if (list == null) {
                list = new ArrayList();
            }
            SLAReporter.this.updateParamList(this.b, this.f4508e, this.f4509f, this.c, this.f4510g, list);
            StringBuilder sb = new StringBuilder(SLAReporter.this.baseURL);
            for (Pair pair : list) {
                sb.append("&");
                sb.append((String) pair.first);
                sb.append(SimpleComparison.EQUAL_TO_OPERATION);
                sb.append(SLAReporter.urlEncodeParam((String) pair.second));
            }
            if (RAFT.getConfig().isDebugVersion()) {
                ILogDelegate logDelegate = RAFT.getConfig().getLogDelegate();
                StringBuilder Z = e.d.a.a.a.Z("reportInfo=");
                Z.append(sb.toString());
                logDelegate.debug(SLAReporter.TAG, Z.toString());
            }
            SLAReporter.this.reportInner(sb.toString());
        }
    }

    public SLAReporter(Context context2, Executor executor2) {
        this.context = context2;
        this.executor = executor2;
    }

    private int applyForGroupId() {
        int i2 = this.groupId;
        this.groupId = i2 + 1;
        return i2;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private synchronized void initIfNeeded() {
        if (!this.isInit) {
            StringBuilder sb = new StringBuilder("https://h.trace.qq.com/kv");
            this.baseURL = sb;
            sb.append("?attaid=");
            sb.append("0c500034918");
            StringBuilder sb2 = this.baseURL;
            sb2.append("&token=");
            sb2.append("4483379541");
            this.baseURL.append("&client_type=android");
            this.isInit = true;
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void recordServiceReportCount(String str, String str2) {
        if (SLAConstant.GET_SERVICE_COST.compareTo(str) == 0 || SLAConstant.GET_SERVICE_SUCCESS.compareTo(str) == 0) {
            synchronized (this.reportService) {
                e.w.f.b.a.a aVar = new e.w.f.b.a.a(1);
                e.w.f.b.a.a put = this.reportService.put(str2, aVar);
                if (put != null) {
                    aVar.f12552a = put.f12552a + 1;
                }
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    /* JADX WARNING: Removed duplicated region for block: B:22:0x0083  */
    /* JADX WARNING: Removed duplicated region for block: B:25:? A[RETURN, SYNTHETIC] */
    private void reportInner(String str) {
        Throwable th;
        HttpURLConnection httpURLConnection;
        Exception e2;
        if (Build.VERSION.SDK_INT >= 26) {
            TrafficStats.getAndSetThreadStatsTag(26);
        }
        HttpURLConnection httpURLConnection2 = null;
        try {
            httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
            try {
                httpURLConnection.setConnectTimeout(MeasureConst.DEFAULT_REPORT_DELAY_TIME);
                httpURLConnection.setRequestMethod("GET");
                int responseCode = httpURLConnection.getResponseCode();
                if (RAFT.getConfig().isDebugVersion()) {
                    RLog.d(TAG, "report code: " + responseCode + ", path: " + str);
                }
            } catch (Exception e3) {
                e2 = e3;
                try {
                    RLog.w(TAG, "report error: " + e2 + ", path: " + str);
                    if (httpURLConnection == null) {
                    }
                    httpURLConnection.disconnect();
                } catch (Throwable th2) {
                    th = th2;
                    httpURLConnection2 = httpURLConnection;
                    if (httpURLConnection2 != null) {
                    }
                    throw th;
                }
            }
        } catch (Exception e4) {
            httpURLConnection = null;
            e2 = e4;
            RLog.w(TAG, "report error: " + e2 + ", path: " + str);
            if (httpURLConnection == null) {
                return;
            }
            httpURLConnection.disconnect();
        } catch (Throwable th3) {
            th = th3;
            if (httpURLConnection2 != null) {
                httpURLConnection2.disconnect();
            }
            throw th;
        }
        httpURLConnection.disconnect();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void updateParamList(String str, String str2, long j2, String str3, String str4, List<Pair<String, String>> list) {
        String str5;
        list.add(Pair.create("app_debug", RAFT.getConfig().isDebugVersion() ? "0" : "1"));
        list.add(Pair.create("event_name", str));
        list.add(Pair.create("event_value", str2));
        list.add(Pair.create("event_cost", j2 + ""));
        if (!TextUtils.isEmpty(str3)) {
            list.add(Pair.create("r1", str3));
        }
        if (!TextUtils.isEmpty(str4)) {
            list.add(Pair.create("r2", str4));
        }
        Context context2 = this.context;
        String str6 = "app_bundle_id";
        if (context2 != null) {
            list.add(Pair.create("app_name", AppUtils.getApplicationName(context2)));
            list.add(Pair.create(str6, this.context.getPackageName()));
            list.add(Pair.create("app_version_name", AppUtils.getAppVersionName(this.context)));
            list.add(Pair.create("app_version_code", String.valueOf(AppUtils.getAppVersionCode(this.context))));
            str5 = ProcessUtil.getProcessName(this.context);
            if (TextUtils.isEmpty(str5)) {
                str5 = "main";
            }
            str6 = "processName";
        } else {
            list.add(Pair.create("app_name", "RAFTAndroid"));
            str5 = "com.tencent.raft";
        }
        list.add(Pair.create(str6, str5));
        list.add(Pair.create("raft_version", "0.5.2.13"));
    }

    /* access modifiers changed from: private */
    public static String urlEncodeParam(String str) {
        if (!(str == null || str.length() == 0)) {
            try {
                return URLEncoder.encode(str, "UTF-8");
            } catch (UnsupportedEncodingException unused) {
                RLog.d(TAG, "UnsupportedEncodingException:UTF-8");
            }
        }
        return "null";
    }

    private boolean whetherReportWithSample(int i2) {
        return i2 == -1 || i2 % 100 == 1;
    }

    public Executor getExecutor() {
        return this.executor;
    }

    public void open(boolean z) {
        this.open = z;
        RLog.d(TAG, "set open: " + z + " ,real: " + this.open);
    }

    public void release() {
        this.context = null;
    }

    public void report(int i2, String str, String str2, long j2, String str3, String str4) {
        report(i2, str, str2, j2, str3, str4, null);
    }

    public void report(int i2, String str, String str2, long j2, String str3, String str4, List<Pair<String, String>> list) {
        if (whetherReportWithSample(i2)) {
            this.executor.execute(new a(str, str3, list, str2, j2, str4));
        }
    }

    public void report(String str, String str2, long j2, String str3, String str4) {
        report(-1, str, str2, j2, str3, str4);
    }

    public void reportGetIPCService(long j2, Class<?> cls, RAFTIPCNotSupportException.Reason reason) {
        int applyForGroupId;
        String canonicalName;
        String str;
        String str2;
        String str3;
        if (this.open) {
            long nanoTime = System.nanoTime() - j2;
            if (reason != null) {
                int ordinal = reason.ordinal();
                str3 = ordinal != 0 ? ordinal != 1 ? ordinal != 2 ? ordinal != 3 ? "" : "UNSUPPORTED_PROXY" : "UNSUPPORTED_THREAD" : "UNSUPPORTED_METHOD" : "UNSUPPORTED_PROCESS";
                applyForGroupId = -1;
                canonicalName = cls.getCanonicalName();
                str = SLAConstant.GET_IPC_SERVICE;
                str2 = "0";
            } else {
                applyForGroupId = applyForGroupId();
                double d = (double) nanoTime;
                Double.isNaN(d);
                nanoTime = (long) (d / 1000000.0d);
                canonicalName = cls.getCanonicalName();
                str = SLAConstant.GET_IPC_SERVICE;
                str2 = "1";
                str3 = "";
            }
            report(applyForGroupId, str, str2, nanoTime, canonicalName, str3);
        }
    }

    public void reportGetServiceException(long j2, Class<?> cls, int i2, Throwable th) {
        if (this.open) {
            ArrayList arrayList = new ArrayList();
            arrayList.add(Pair.create("exception_message", th.getMessage()));
            arrayList.add(Pair.create("exception_type", th.getClass().getSimpleName()));
            String valueOf = ((th instanceof RAFTServiceNotFoundException) || (th instanceof RAFTCallWrongProcessException) || (th instanceof RAFTNotDeclareServiceException) || (th instanceof RAFTServiceNotMatchException)) ? String.valueOf(1) : String.valueOf(0);
            report(applyForGroupId(), SLAConstant.GET_SERVICE_COST, valueOf, System.nanoTime() - j2, cls.getCanonicalName(), String.valueOf(i2), arrayList);
            report(-1, SLAConstant.GET_SERVICE_ERROR, valueOf, System.nanoTime() - j2, cls.getCanonicalName(), String.valueOf(i2), arrayList);
        }
    }

    public void reportGetServiceFinish(long j2, boolean z, Class<?> cls, int i2) {
        reportGetServiceFinish(j2, z, cls.getCanonicalName(), i2);
    }

    public void reportGetServiceFinish(long j2, boolean z, String str, int i2) {
        if (this.open) {
            if (z || i2 != 5) {
                e.w.f.b.a.a aVar = this.reportService.get(str);
                if (aVar == null || aVar.f12552a <= 5) {
                    int applyForGroupId = applyForGroupId();
                    long nanoTime = System.nanoTime() - j2;
                    double d = (double) nanoTime;
                    Double.isNaN(d);
                    Double.isNaN(d);
                    report(applyForGroupId, SLAConstant.GET_SERVICE_COST, String.valueOf(d / 1000000.0d), nanoTime, str, String.valueOf(i2));
                    report(applyForGroupId, SLAConstant.GET_SERVICE_SUCCESS, z ? "1" : "0", 0, str, String.valueOf(i2));
                }
            }
        }
    }

    public void reportGetStandardServiceFinish(long j2, boolean z, Class<?> cls) {
        if (this.open) {
            report(applyForGroupId(), SLAConstant.GET_STANDARD_INTERFACE_SERVICE, z ? "1" : "0", System.nanoTime() - j2, cls.getCanonicalName(), "");
        }
    }

    public void reportOnFrameworkStartup() {
        if (this.open) {
            this.frameworkStartupNs = System.nanoTime();
            SharedPreferences.Editor edit = this.context.getSharedPreferences(MeasureConst.CRASH_MONITOR_SP_NAME, 0).edit();
            edit.putString("a321c41aa2", "0.5.2.13");
            edit.apply();
        }
    }

    public void reportOnFrameworkStartupFinish(Exception exc) {
        if (this.open) {
            SharedPreferences sharedPreferences = this.context.getSharedPreferences("raft_config", 0);
            if (!sharedPreferences.getBoolean("app_sla_report", false)) {
                SharedPreferences.Editor edit = sharedPreferences.edit();
                edit.putBoolean("app_sla_report", true);
                edit.apply();
                ArrayList arrayList = new ArrayList();
                arrayList.add(Pair.create("app_name", AppUtils.getApplicationName(this.context)));
                arrayList.add(Pair.create("app_bundle_id", this.context.getPackageName()));
                report(-1, SLAConstant.APP_INFO, "0", 0, null, null, arrayList);
                long nanoTime = System.nanoTime() - this.frameworkStartupNs;
                double d = (double) nanoTime;
                Double.isNaN(d);
                report(EIPCConst.SLI_KEY_LAUNCH_COST, String.valueOf(d / 1000000.0d), nanoTime, exc == null ? "1" : "0", exc == null ? "" : exc.getMessage());
                report(EIPCConst.SLI_KEY_LAUNCH_SUCCESS, "1", 0, null, null);
            }
        }
    }

    public void sampleReport(String str, String str2, long j2) {
        report(applyForGroupId(), str, str2, j2, null, null);
    }
}