package com.ubtrobot.analytics; import android.content.Context; import android.content.IntentFilter; import android.content.SharedPreferences; import android.os.Handler; import android.os.Looper; import android.os.SystemClock; import android.util.Log; import com.ubt.jimu.base.entities.Course; import com.ubt.jimu.base.mvp.SingleClickListener; import com.ubtrobot.analytics.NetWorkChangeReceiver; import com.ubtrobot.analytics.Retry; import com.ubtrobot.analytics.device.DeviceInfo; import com.ubtrobot.analytics.event.ActivenessEventSource; import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; /* loaded from: classes2.dex */ public class AnalyticsServiceImpl implements Analytics { private static final long q = TimeUnit.MINUTES.toMillis(5); private volatile long d; private DeviceInfo f; private boolean g; private Executor h; private MemEventStorage i; private EventStorage j; private RealtimeReport k; private HttpReport l; private ActivenessEventSource n; private Handler o; private NetWorkChangeReceiver p; private final byte[] a = new byte[0]; private final byte[] b = new byte[0]; private volatile long c = b() + 20; private long e = q; private boolean m = true; private class RealtimeReport { private volatile Set a; private Context b; private MemEventStorage c; private boolean d; private boolean e; private Retry f; private Runnable g; /* JADX INFO: Access modifiers changed from: private */ public boolean g() { synchronized (AnalyticsServiceImpl.this.b) { int a = this.c.a(); List a2 = this.c.a(a); boolean b = AnalyticsServiceImpl.this.b(a2); Log.i("Analytics", "Forthwith report event report result:" + b); if (!b) { this.d = true; return false; } if (!this.d) { this.c.b(a); return true; } this.d = false; try { this.c.b(a); AnalyticsServiceImpl.this.j.a(a2); return true; } catch (IOException unused) { Log.w("Analytics", "Remove mDiskStorage is fail."); this.d = true; return false; } } } private RealtimeReport(Context context) { this.b = context; this.c = new MemEventStorage(new MemoryEventStorage()); this.a = new HashSet(); f(); this.g = new Runnable(AnalyticsServiceImpl.this) { // from class: com.ubtrobot.analytics.AnalyticsServiceImpl.RealtimeReport.1 @Override // java.lang.Runnable public void run() { if (RealtimeReport.this.g()) { RealtimeReport.this.f.b(); } } }; this.f = new Retry(new Retry.RetryListener(AnalyticsServiceImpl.this) { // from class: com.ubtrobot.analytics.AnalyticsServiceImpl.RealtimeReport.2 @Override // com.ubtrobot.analytics.Retry.RetryListener public void a() { Log.i("Analytics", "Report retrying ..."); AnalyticsServiceImpl.this.h.execute(RealtimeReport.this.g); } }); } /* JADX INFO: Access modifiers changed from: private */ public boolean b(boolean z) { synchronized (AnalyticsServiceImpl.this.b) { try { if (!z) { try { if (!a()) { Log.i("Analytics", "Realtime event sp is updated."); return false; } } catch (ReportException e) { Log.w("Analytics", "Request realtime is fail.", e); this.e = false; return false; } } RealtimeEvent a = AnalyticsServiceImpl.this.l.a(b()); if (a == null) { this.e = false; return false; } Log.i("Analytics", "Realtime: " + a.toString()); this.a.addAll(a.b()); a(a.a(), this.a); this.e = true; return true; } catch (Throwable th) { throw th; } } } /* JADX INFO: Access modifiers changed from: private */ public void c() { this.a.addAll(this.b.getSharedPreferences("realtime_event_id", 0).getStringSet("event_id_set", new HashSet())); } private long d() { return this.b.getSharedPreferences("realtime_event_id", 0).getLong("next_update_time", 0L); } /* JADX INFO: Access modifiers changed from: private */ public void e() { Log.i("Analytics", "Force refresh realtime eventId list."); a(true); } private void f() { Log.i("Analytics", "Refresh realtime eventId list."); a(false); } private void a(final boolean z) { AnalyticsServiceImpl.this.h.execute(new Runnable() { // from class: com.ubtrobot.analytics.AnalyticsServiceImpl.RealtimeReport.3 @Override // java.lang.Runnable public void run() { if (RealtimeReport.this.b(z)) { Log.i("Analytics", "Read realtime event from sp."); RealtimeReport.this.c(); } } }); } private boolean a() { if (!this.e) { return true; } if (d() > System.currentTimeMillis()) { Log.i("Analytics", "Update realtime sp time:" + d()); return false; } this.e = false; return true; } private void a(String str, Set set) { SharedPreferences sharedPreferences = this.b.getSharedPreferences("realtime_event_id", 0); long currentTimeMillis = System.currentTimeMillis() + 3600000; if (str.equals(b())) { Log.i("Analytics", "Update realtime sp: next_update_time."); sharedPreferences.edit().putLong("next_update_time", currentTimeMillis).apply(); } else { Log.i("Analytics", "Update realtime sp."); sharedPreferences.edit().putString(Course.TYPE_BLOCKLY, str).putStringSet("event_id_set", set).putLong("next_update_time", currentTimeMillis).apply(); } } private String b() { return this.b.getSharedPreferences("realtime_event_id", 0).getString(Course.TYPE_BLOCKLY, ""); } /* JADX INFO: Access modifiers changed from: private */ public boolean a(String str) { if (AnalyticsServiceImpl.this.k.a()) { f(); } return this.a.contains(str); } /* JADX INFO: Access modifiers changed from: private */ public void a(final Event event) { AnalyticsServiceImpl.this.h.execute(new Runnable() { // from class: com.ubtrobot.analytics.AnalyticsServiceImpl.RealtimeReport.4 @Override // java.lang.Runnable public void run() { RealtimeReport.this.c.a(event); if (RealtimeReport.this.g()) { return; } AnalyticsServiceImpl.this.c((List) Collections.singletonList(event)); RealtimeReport.this.f.a(); } }); } } private class TimerReportRunnable implements Runnable { private long a = a(); public TimerReportRunnable() { } private long a() { Calendar calendar = Calendar.getInstance(); calendar.set(calendar.get(1), calendar.get(2), calendar.get(5), 23, 59, 59); return calendar.getTimeInMillis(); } private void b() { if (System.currentTimeMillis() > this.a) { AnalyticsServiceImpl.this.g(); c(); this.a = a(); } } private void c() { AnalyticsServiceImpl.this.h.execute(new Runnable() { // from class: com.ubtrobot.analytics.AnalyticsServiceImpl.TimerReportRunnable.1 @Override // java.lang.Runnable public void run() { AnalyticsServiceImpl.this.f(); } }); } @Override // java.lang.Runnable public void run() { b(); AnalyticsServiceImpl.this.o.postDelayed(this, 15000L); } } public AnalyticsServiceImpl(Context context, String str, String str2, String str3, ActivenessEventSource activenessEventSource, Executor executor) { this.h = executor; Strategy strategy = Strategy.DEFAULT; this.i = new MemEventStorage(new MemoryEventStorage()); this.l = new HttpReport(context, str, str2, str3); this.j = new DiskEventStorage(context); this.k = new RealtimeReport(context); this.n = activenessEventSource; a(this.n); this.o = new Handler(Looper.getMainLooper()); this.o.post(new TimerReportRunnable()); a(context); c(); } private static class MemEventStorage implements EventStorage { private final EventStorage a; MemEventStorage(EventStorage eventStorage) { this.a = eventStorage; } @Override // com.ubtrobot.analytics.EventStorage public void a(Event event) { try { this.a.a(event); } catch (IOException e) { throw new IllegalStateException("Impossible(writeEvent) IOException on MemoryStorage", e); } } @Override // com.ubtrobot.analytics.EventStorage public void b(List list) { try { this.a.b(list); } catch (IOException e) { throw new IllegalStateException("Impossible(writeEvents) IOException on MemoryStorage", e); } } @Override // com.ubtrobot.analytics.EventStorage public int a() { try { return this.a.a(); } catch (IOException e) { throw new IllegalStateException("Impossible(getEventCount) IOException on MemoryStorage", e); } } @Override // com.ubtrobot.analytics.EventStorage public void b(int i) { try { this.a.b(i); } catch (IOException e) { throw new IllegalStateException("Impossible IOException on MemoryStorage", e); } } @Override // com.ubtrobot.analytics.EventStorage public List a(int i) { try { return this.a.a(i); } catch (IOException e) { throw new IllegalStateException("Impossible IOException on MemoryStorage", e); } } @Override // com.ubtrobot.analytics.EventStorage public void a(List list) throws IOException { throw new IllegalStateException("MemEventStorage nonsupport removeEvents(List)."); } } private void c() { this.h.execute(new Runnable() { // from class: com.ubtrobot.analytics.AnalyticsServiceImpl.1 @Override // java.lang.Runnable public void run() { Log.i("Analytics", "First report."); AnalyticsServiceImpl.this.f(); } }); } private List d() { ArrayList arrayList = new ArrayList(); try { return this.j.a(50); } catch (IOException unused) { Log.w("Analytics", "Operate event disk storage failed."); return arrayList; } } private void e() { try { if (this.f == null) { Log.w("Analytics", "Please AnalyticsServiceImpl.setDeviceInfo."); } else { this.l.a(this.f); this.g = true; } } catch (ReportException unused) { this.g = false; } } /* JADX INFO: Access modifiers changed from: private */ public void f() { synchronized (this.b) { while (true) { if (!a()) { break; } List d = d(); if (!b(d)) { Log.i("Analytics", "Report fail, count: " + d.size()); break; } Log.i("Analytics", "Report success, count: " + d.size()); a(d); } if (!this.g) { e(); } this.d = b() + this.e; } } /* JADX INFO: Access modifiers changed from: private */ public void g() { synchronized (this.a) { while (this.i.a() > 0) { List a = this.i.a(SingleClickListener.FAST_CLICK_DELAY_TIME); if (a.isEmpty()) { return; } if (c(a)) { this.i.b(a.size()); } } this.c = b() + 20; } } /* JADX INFO: Access modifiers changed from: private */ public boolean h() { return b() - this.d > 0; } private boolean i() { return this.i.a() >= 500 || b() - this.c >= 0; } private void a(Context context) { this.p = new NetWorkChangeReceiver(new NetWorkChangeReceiver.INetWorkChange() { // from class: com.ubtrobot.analytics.AnalyticsServiceImpl.2 @Override // com.ubtrobot.analytics.NetWorkChangeReceiver.INetWorkChange public void a() { AnalyticsServiceImpl.this.k.e(); } }); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.net.wifi.WIFI_STATE_CHANGED"); intentFilter.addAction("android.net.wifi.STATE_CHANGE"); intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); context.registerReceiver(this.p, intentFilter); } public void b(boolean z) { this.l.a(z); } private void b(Event event) { this.i.a(event); if (i() || h()) { this.h.execute(new Runnable() { // from class: com.ubtrobot.analytics.AnalyticsServiceImpl.4 @Override // java.lang.Runnable public void run() { AnalyticsServiceImpl.this.g(); if (AnalyticsServiceImpl.this.h()) { Log.i("Analytics", "Report start..."); AnalyticsServiceImpl.this.f(); } } }); } } /* JADX INFO: Access modifiers changed from: private */ public boolean c(List list) { try { this.j.b(list); return true; } catch (IOException unused) { Log.w("Analytics", "Save events to disk failed."); return false; } } private long b() { return SystemClock.elapsedRealtime(); } private void a(final ActivenessEventSource activenessEventSource) { this.h.execute(new Runnable() { // from class: com.ubtrobot.analytics.AnalyticsServiceImpl.3 @Override // java.lang.Runnable public void run() { try { List a = activenessEventSource.a(); int size = a.size(); if (size <= 0) { Log.i("Analytics", "ActivenessEvent is not exist."); return; } Log.i("Analytics", "ActivenessEvent count:" + size); boolean z = false; int i = 0; int i2 = 0; while (true) { if (i >= size) { break; } i += 50; if (i > size) { i = size; } if (!AnalyticsServiceImpl.this.c(a.subList(i2, i))) { z = true; break; } i2 = i; } if (z) { Log.i("Analytics", "ActivenessEvent record to event table fail."); } else { Log.i("Analytics", "ActivenessEvent record to event table success."); activenessEventSource.a(size); } } catch (IOException unused) { } } }); } /* JADX INFO: Access modifiers changed from: private */ public boolean b(List list) { try { this.l.a(list); return true; } catch (ReportException e) { Log.e("Analytics", "Report Error:" + e.getMessage()); if (e.causedByInternalServerError()) { Log.w("Analytics", "Report(reportEvents) events failed due to server error."); } return false; } } public void a(boolean z) { this.m = z; } public void a(Event event) { if (this.m) { if (this.k.a(event.getEventId())) { Log.i("Analytics", "Event is forthwith report event."); this.k.a(event); } else { Log.i("Analytics", "Event is not forthwith report event."); b(event); } } } private boolean a() { try { return this.j.a() > 0; } catch (IOException unused) { Log.w("Analytics", "Report(getEventCount) events failed due to server error."); return false; } } private void a(List list) { try { this.j.b(list.size()); } catch (IOException unused) { Log.w("Analytics", "Report(removeEvents) events failed due to server error."); } } public void a(DeviceInfo deviceInfo) { this.f = deviceInfo; } }