package com.glu.plugins.gluanalytics; import android.annotation.TargetApi; import android.app.ActivityManager; import android.content.Context; import android.os.Process; import android.os.SystemClock; import android.view.Choreographer; import com.fyber.inneractive.sdk.external.InneractiveMediationDefs; import com.glu.plugins.gluanalytics.util.Common; import com.glu.plugins.gluanalytics.util.YLogger; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; /* loaded from: classes2.dex */ public class GluPerformance implements IPerformance { private static long preSDKLoadStartTimeStamp; private ActivityManager mActivityManager; private IAnalytics mAnalytics; private boolean mDevBuild; private ScheduledThreadPoolExecutor mExecutor; private boolean mHasVersionChanged; private boolean mIsFirstLaunch; private double mLatestFPS; PerfSample mPreInitSample; private boolean mRunning; private double mSampleInterval; private double mTargetFrameRate; private final Object lock = new Object(); private final YLogger mLog = YLogger.create(getClass()); final String LOG_NAME_PERFORMANCE = "PERFORMANCE"; final String LOG_EVENT_SOURCE_SDK = "SDK_STANDARD"; final String APP_PERFORMANCE = "APP_PERFORMANCE"; private HashMap mPerfSamples = new HashMap<>(); private int mFrameCount = 0; private long mLastFrameTime = 0; private FrameTask mFrameTask = new FrameTask(); @Override // com.glu.plugins.gluanalytics.IPerformance public void internal_SetAnalytics(IAnalytics iAnalytics) { this.mAnalytics = iAnalytics; } public static /* synthetic */ int access$408(GluPerformance gluPerformance) { int i = gluPerformance.mFrameCount; gluPerformance.mFrameCount = i + 1; return i; } public GluPerformance(Context context, double d, double d2, boolean z, double d3, boolean z2, boolean z3) { this.mSampleInterval = d; this.mTargetFrameRate = d2; this.mDevBuild = z; this.mIsFirstLaunch = z2; this.mHasVersionChanged = z3; this.mActivityManager = (ActivityManager) context.getSystemService("activity"); this.mLatestFPS = d2; ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1); this.mExecutor = scheduledThreadPoolExecutor; scheduledThreadPoolExecutor.scheduleAtFixedRate(new SampleTask(), 0L, (long) (d * 1000.0d), TimeUnit.MILLISECONDS); this.mRunning = true; this.mPreInitSample = null; logPreBootSample(d3); perfSampleStart("APP_PERFORMANCE", 7, null); } /* JADX INFO: Access modifiers changed from: private */ public double getUsedMemory() { return this.mActivityManager.getProcessMemoryInfo(new int[]{Process.myPid()})[0].getTotalPss() / 1024.0f; } @Override // com.glu.plugins.gluanalytics.IPerformance public void destroy() { if (this.mRunning) { perfSampleEnd("APP_PERFORMANCE", null); this.mRunning = false; } } @Override // com.glu.plugins.gluanalytics.IPerformance public void onResume() { this.mFrameCount = 0; this.mLastFrameTime = 0L; this.mFrameTask.start(); if (!this.mRunning) { this.mRunning = true; perfSampleStart("APP_PERFORMANCE", 7, null); } for (String str : this.mPerfSamples.keySet()) { Map map = this.mPerfSamples.get(str).userData; if (map != null && map.containsKey("autoPause")) { perfSampleAction(str, 1); } } } @Override // com.glu.plugins.gluanalytics.IPerformance public void onPause() { if (this.mRunning) { perfSampleEnd("APP_PERFORMANCE", null); this.mRunning = false; } this.mFrameTask.end(); synchronized (this.lock) { try { for (Map.Entry entry : this.mPerfSamples.entrySet()) { PerfSample value = entry.getValue(); value.isSuspended = true; Map map = value.userData; if (map != null && map.containsKey("autoPause")) { perfSampleAction(entry.getKey(), 0); } } } catch (Throwable th) { throw th; } } } @Override // com.glu.plugins.gluanalytics.IPerformance public void internal_LogLaunchOverhead() { PerfSample perfSample = this.mPreInitSample; if (perfSample != null) { flush(perfSample); this.mPreInitSample = null; } } @Override // com.glu.plugins.gluanalytics.IPerformance public void perfSampleStart(String str, int i, Map map) { synchronized (this.lock) { try { if (this.mPerfSamples.containsKey(str)) { this.mLog.e("PerfSampleStart", InneractiveMediationDefs.GENDER_MALE, "placement has already exist, ignored.", "placement", str); return; } PerfSample perfSample = new PerfSample(); perfSample.placement = str; perfSample.type = i; perfSample.userData = map; perfSample.maxMemory = 0.0d; perfSample.totalMemory = 0.0d; perfSample.startMemory = this.mDevBuild ? getUsedMemory() : 0.0d; perfSample.endMemory = 0.0d; perfSample.minFPS = this.mTargetFrameRate; perfSample.maxFPS = 0.0d; perfSample.totalFPS = 0.0d; perfSample.lastFPS = 0.0d; perfSample.sampleCount = 0L; perfSample.startTimeStamp = SystemClock.uptimeMillis(); perfSample.isSuspended = false; perfSample.isPaused = false; perfSample.startPausedTimeStamp = 0.0d; perfSample.pausedTimeTotal = 0.0d; perfSample.preInitTime = 0.0d; synchronized (this.lock) { this.mPerfSamples.put(str, perfSample); } } catch (Throwable th) { throw th; } } } @Override // com.glu.plugins.gluanalytics.IPerformance public void perfSampleEnd(String str, Map map) { synchronized (this.lock) { try { if (!this.mPerfSamples.containsKey(str)) { this.mLog.e("PerfSampleEnd", InneractiveMediationDefs.GENDER_MALE, "placement is not exist, ignored.", "placement", str); return; } PerfSample perfSample = this.mPerfSamples.get(str); this.mPerfSamples.remove(str); if (map != null && map.size() != 0) { perfSample.userData = map; } perfSample.endMemory = this.mDevBuild ? getUsedMemory() : 0.0d; flush(perfSample); } catch (Throwable th) { throw th; } } } @Override // com.glu.plugins.gluanalytics.IPerformance public PerfMeasure perfGetValues() { PerfMeasure perfMeasure = new PerfMeasure(); boolean containsKey = this.mPerfSamples.containsKey("APP_PERFORMANCE"); PerfSample perfSample = this.mPerfSamples.get("APP_PERFORMANCE"); if (!containsKey || (perfSample.type & 4) == 0 || perfSample.isPaused) { perfMeasure.averageFPS = -1.0d; perfMeasure.lastFPS = -1.0d; perfMeasure.minFPS = -1.0d; perfMeasure.maxFPS = -1.0d; } else { perfMeasure.averageFPS = perfSample.totalFPS / perfSample.sampleCount; perfMeasure.lastFPS = perfSample.lastFPS; perfMeasure.minFPS = perfSample.minFPS; perfMeasure.maxFPS = perfSample.maxFPS; } if (!containsKey || (perfSample.type & 2) == 0 || perfSample.isPaused) { perfMeasure.startMem = -1.0d; perfMeasure.maxMem = -1.0d; perfMeasure.currentMem = -1.0d; } else { perfMeasure.startMem = perfSample.startMemory; perfMeasure.maxMem = perfSample.maxMemory; perfMeasure.currentMem = getUsedMemory(); } if (!containsKey || (perfSample.type & 2) == 0 || perfSample.isPaused) { perfMeasure.time = -1.0d; } else { perfMeasure.time = (SystemClock.uptimeMillis() - perfSample.startTimeStamp) - perfSample.pausedTimeTotal; } return perfMeasure; } @Override // com.glu.plugins.gluanalytics.IPerformance public boolean perfSampleAction(String str, int i) { if (!this.mPerfSamples.containsKey(str)) { return false; } PerfSample perfSample = this.mPerfSamples.get(str); if (i != 0) { if (i != 1) { if (i == 2) { synchronized (this.lock) { this.mPerfSamples.remove(str); } } } else { if (!perfSample.isPaused) { return false; } perfSample.isPaused = false; perfSample.pausedTimeTotal += SystemClock.uptimeMillis() - perfSample.startPausedTimeStamp; } } else { if (perfSample.isPaused) { return false; } perfSample.isPaused = true; perfSample.startPausedTimeStamp = SystemClock.uptimeMillis(); } return true; } public static void perfSampleLogLaunchOverhead() { preSDKLoadStartTimeStamp = SystemClock.uptimeMillis(); } public void logPreBootSample(double d) { if (0 != preSDKLoadStartTimeStamp || d > 0.0d) { PerfSample perfSample = new PerfSample(); this.mPreInitSample = perfSample; perfSample.placement = "LAUNCH_OVERHEAD"; perfSample.userData = null; perfSample.type = 1; if (d > 0.0d) { perfSample.preInitTime = d * 1000.0d; } else { perfSample.startTimeStamp = preSDKLoadStartTimeStamp; } perfSample.isSuspended = false; } } private void flush(PerfSample perfSample) { HashMap hashMap; if (perfSample == null) { return; } if (perfSample.userData == null) { hashMap = new HashMap(); } else { hashMap = new HashMap(perfSample.userData); } HashMap hashMap2 = hashMap; if (this.mIsFirstLaunch) { hashMap2.put("isFirstLaunch", "true"); } if (this.mHasVersionChanged) { hashMap2.put("wasVersionUpdated", "true"); } if ((perfSample.type & 2) != 0) { if (this.mDevBuild) { hashMap2.put("start_mem", Double.valueOf(perfSample.startMemory)); hashMap2.put("end_mem", Double.valueOf(perfSample.endMemory)); } if (perfSample.sampleCount != 0) { hashMap2.put("max_mem", Double.valueOf(perfSample.maxMemory)); hashMap2.put("avg_mem", Double.valueOf(perfSample.totalMemory / perfSample.sampleCount)); } else if (this.mDevBuild) { hashMap2.put("max_mem", Double.valueOf(Math.max(perfSample.startMemory, perfSample.endMemory))); hashMap2.put("avg_mem", Double.valueOf((perfSample.startMemory + perfSample.endMemory) * 0.5d)); } } if ((perfSample.type & 4) != 0 && perfSample.sampleCount != 0) { hashMap2.put("min_fps", Double.valueOf(perfSample.minFPS)); hashMap2.put("avg_fps", Double.valueOf(perfSample.totalFPS / perfSample.sampleCount)); hashMap2.put("target_fps", Double.valueOf(this.mTargetFrameRate)); } if ((perfSample.type & 1) != 0) { double d = perfSample.preInitTime; if (d > 0.0d) { hashMap2.put("time_ms", Double.valueOf(d)); } else { hashMap2.put("time_ms", Double.valueOf((SystemClock.uptimeMillis() - perfSample.startTimeStamp) - perfSample.pausedTimeTotal)); } if (perfSample.isSuspended) { hashMap2.put("is_suspended", Boolean.TRUE); } } this.mAnalytics.internal_perfEvent(perfSample.placement, "PERFORMANCE", String.valueOf(perfSample.type), null, "SDK_STANDARD", hashMap2); } @TargetApi(16) public class FrameTask implements Choreographer.FrameCallback { private Choreographer choreographer; public FrameTask() { try { this.choreographer = Choreographer.getInstance(); start(); } catch (Exception unused) { GluPerformance.this.mLog.e("PerfSample", InneractiveMediationDefs.GENDER_MALE, "GluPerformance is created in a thread without looper!"); Common.runOnUIThread(new Runnable() { // from class: com.glu.plugins.gluanalytics.GluPerformance.FrameTask.1 @Override // java.lang.Runnable public void run() { FrameTask.this.choreographer = Choreographer.getInstance(); FrameTask.this.start(); } }); } } public void start() { Choreographer choreographer = this.choreographer; if (choreographer != null) { choreographer.postFrameCallback(this); } } public void end() { Choreographer choreographer = this.choreographer; if (choreographer != null) { choreographer.removeFrameCallback(this); } } @Override // android.view.Choreographer.FrameCallback public void doFrame(long j) { if (GluPerformance.this.mLastFrameTime == 0) { GluPerformance.this.mLastFrameTime = j; } else { GluPerformance.access$408(GluPerformance.this); double d = (j - GluPerformance.this.mLastFrameTime) * 1.0E-9d; if (d >= GluPerformance.this.mSampleInterval) { GluPerformance.this.mLastFrameTime = j; GluPerformance.this.mLatestFPS = r5.mFrameCount / d; GluPerformance.this.mFrameCount = 0; } } this.choreographer.postFrameCallback(this); } } public class SampleTask implements Runnable { public SampleTask() { } @Override // java.lang.Runnable public void run() { synchronized (GluPerformance.this.lock) { if (GluPerformance.this.mPerfSamples.size() != 0 && GluPerformance.this.mRunning) { double usedMemory = GluPerformance.this.getUsedMemory(); double min = Math.min(GluPerformance.this.mLatestFPS, GluPerformance.this.mTargetFrameRate); synchronized (GluPerformance.this.lock) { try { for (PerfSample perfSample : GluPerformance.this.mPerfSamples.values()) { if (!perfSample.isPaused) { perfSample.sampleCount++; int i = perfSample.type; if ((i & 2) != 0) { perfSample.totalMemory += usedMemory; if (perfSample.maxMemory < usedMemory) { perfSample.maxMemory = usedMemory; } } if ((i & 4) != 0) { perfSample.totalFPS += min; perfSample.lastFPS = min; if (perfSample.minFPS > min) { perfSample.minFPS = min; } if (perfSample.maxFPS < min) { perfSample.maxFPS = min; } } } } } finally { } } } } } } public class PerfSample { double endMemory; boolean isPaused; boolean isSuspended; double lastFPS; double maxFPS; double maxMemory; double minFPS; double pausedTimeTotal; String placement; double preInitTime; long sampleCount; double startMemory; double startPausedTimeStamp; double startTimeStamp; double totalFPS; double totalMemory; int type; Map userData; private PerfSample() { } } }