package com.strmr.ps;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.media3.container.NalUnitUtil;
import androidx.media3.exoplayer.upstream.CmcdData;
import com.google.android.gms.security.ProviderInstaller;
import com.strmr.ps.background.CatalogRefreshWorker;
import com.strmr.ps.background.DebridAccountRefreshWorker;
import com.strmr.ps.background.HomeChannelManager;
import com.strmr.ps.background.HomeChannelUpdateWorker;
import com.strmr.ps.background.TraktSyncWorker;
import com.strmr.ps.data.analytics.AnalyticsHelper;
import com.strmr.ps.data.event.TraktErrorNotifier;
import com.strmr.ps.data.local.AppDatabase;
import com.strmr.ps.data.remote.TraktETagInterceptor;
import com.strmr.ps.data.repository.AppConfigRepository;
import com.strmr.ps.data.repository.DebridLibraryRepository;
import com.strmr.ps.data.repository.PreferencesRepository;
import com.strmr.ps.data.repository.RealDebridRepository;
import com.strmr.ps.data.repository.ScreenConfigRepository;
import com.strmr.ps.data.repository.TraktAccountRepository;
import com.strmr.ps.data.repository.TraktStatusProvider;
import com.strmr.ps.data.repository.TraktSyncRepository;
import com.strmr.ps.data.repository.WatchStatusRepository;
import com.strmr.ps.data.subscription.ProSubscriptionManager;
import com.strmr.ps.data.supabase.DeviceManager;
import com.strmr.ps.service.PlaybackService;
import dagger.hilt.android.HiltAndroidApp;
import defpackage.a13;
import defpackage.ae8;
import defpackage.bk1;
import defpackage.cr4;
import defpackage.e17;
import defpackage.ed1;
import defpackage.fn6;
import defpackage.g17;
import defpackage.gh;
import defpackage.gj1;
import defpackage.hs2;
import defpackage.k63;
import defpackage.kq;
import defpackage.ll7;
import defpackage.m02;
import defpackage.m12;
import defpackage.m99;
import defpackage.n41;
import defpackage.n89;
import defpackage.ng1;
import defpackage.osa;
import defpackage.r21;
import defpackage.rd3;
import defpackage.rx4;
import defpackage.sc2;
import defpackage.uw6;
import defpackage.vx8;
import defpackage.wj0;
import defpackage.x57;
import defpackage.y03;
import defpackage.y41;
import defpackage.zd9;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import kotlin.Metadata;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;

/* JADX INFO: loaded from: classes.dex */
@Metadata(d1 = {"\u0000\f\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0004\b\u0007\u0018\u00002\u00020\u0001:\u0001\u0004B\u0007¢\u0006\u0004\b\u0002\u0010\u0003¨\u0006\u0005"}, d2 = {"Lcom/strmr/ps/StrmrApp;", "Landroid/app/Application;", "<init>", "()V", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@HiltAndroidApp
@vx8
public final class StrmrApp extends d {
    public static volatile e17 w;
    public AppDatabase c;
    public TraktAccountRepository d;
    public RealDebridRepository e;
    public AnalyticsHelper f;
    public ScreenConfigRepository i;
    public AppConfigRepository j;
    public ProSubscriptionManager k;
    public DeviceManager l;
    public WatchStatusRepository m;
    public TraktStatusProvider n;
    public PreferencesRepository o;
    public DebridLibraryRepository p;
    public TraktETagInterceptor q;
    public TraktErrorNotifier r;
    public TraktSyncRepository s;
    public HomeChannelManager t;
    public final ng1 u;
    public final Handler v;

    @Metadata(d1 = {"\u0000 \n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0010\u000e\n\u0002\b\u0004\n\u0002\u0010\b\n\u0002\b\u0002\n\u0002\u0018\u0002\n\u0002\b\u0003\b\u0086\u0003\u0018\u00002\u00020\u0001R\u0014\u0010\u0003\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0003\u0010\u0004R\u0014\u0010\u0005\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0005\u0010\u0004R\u0014\u0010\u0006\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0006\u0010\u0004R\u0014\u0010\b\u001a\u00020\u00078\u0002X\u0082T¢\u0006\u0006\n\u0004\b\b\u0010\tR\u0018\u0010\u000b\u001a\u0004\u0018\u00010\n8\u0002@\u0002X\u0082\u000e¢\u0006\u0006\n\u0004\b\u000b\u0010\f¨\u0006\r"}, d2 = {"Lcom/strmr/ps/StrmrApp$a;", "", "", "TAG", "Ljava/lang/String;", "PREFS_NAME", "KEY_DATA_SCHEMA_VERSION", "", "CURRENT_DATA_SCHEMA_VERSION", "I", "Le17;", "proxyServerInstance", "Le17;", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class a {
    }

    public StrmrApp() {
        gj1.b bVarB = zd9.b();
        m12 m12Var = sc2.a;
        this.u = bk1.a(gj1.b.a.c((rx4) bVarB, m02.c));
        this.v = new Handler(Looper.getMainLooper());
    }

    public final AppDatabase a() {
        AppDatabase appDatabase = this.c;
        if (appDatabase != null) {
            return appDatabase;
        }
        cr4.n("database");
        throw null;
    }

    @Override // com.strmr.ps.d, android.app.Application
    public final void onCreate() {
        a13 a13Var;
        String absolutePath;
        fn6 fn6Var = fn6.b;
        super.onCreate();
        try {
            ProviderInstaller.installIfNeeded(this);
        } catch (Exception unused) {
        }
        int i = PlaybackService.b;
        NotificationChannel notificationChannel = new NotificationChannel("playback_channel", "Media Playback", 2);
        notificationChannel.setDescription("Shows when media is playing to keep the app active");
        notificationChannel.setShowBadge(false);
        int i2 = 1;
        notificationChannel.setLockscreenVisibility(1);
        Object systemService = getSystemService("notification");
        NotificationManager notificationManager = systemService instanceof NotificationManager ? (NotificationManager) systemService : null;
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(notificationChannel);
        }
        ExecutorService executorService = kq.a;
        if (!kq.e) {
            File externalFilesDir = getExternalFilesDir(null);
            File file = externalFilesDir != null ? new File(externalFilesDir, "logs") : new File(getFilesDir(), "logs");
            kq.c = file;
            if (!file.exists()) {
                file.mkdirs();
            }
            kq.d = new File(file, "strmr_log.txt");
            kq.e = true;
            StringBuilder sb = new StringBuilder();
            sb.append(n89.al(60, "="));
            sb.append('\n');
            sb.append("App Started: " + kq.b.format(new Date()));
            sb.append("\nApp Version: 1.1.72 (1010723)\n");
            sb.append("Device: " + Build.MANUFACTURER + " " + Build.MODEL);
            sb.append('\n');
            sb.append("Android: " + Build.VERSION.RELEASE + " (API " + Build.VERSION.SDK_INT + ")");
            sb.append('\n');
            File file2 = kq.d;
            if (file2 == null || (absolutePath = file2.getAbsolutePath()) == null) {
                absolutePath = "unknown";
            }
            sb.append("Log Location: ".concat(absolutePath));
            sb.append('\n');
            sb.append(n89.al(60, "="));
            sb.append('\n');
            kq.a.execute(new r21(sb.toString(), 7));
            if (!kq.g.getAndSet(true)) {
                Thread thread = new Thread(new gh(i2));
                thread.setName("AppLogger-ErrorCapture");
                thread.setDaemon(true);
                thread.start();
            }
        }
        ae8.a = getApplicationContext().getSharedPreferences("season_pack_prefs", 0);
        SharedPreferences sharedPreferences = getSharedPreferences("strmr_app_prefs", 0);
        int i3 = sharedPreferences.getInt("data_schema_version", 0);
        if (i3 < 1) {
            kq.h("StrmrApp", "Data schema upgrade from " + i3 + " to 1");
            wj0.c(this.u, null, null, new w(this, null), 3);
            sharedPreferences.edit().putInt("data_schema_version", 1).apply();
            kq.h("StrmrApp", "Data schema migration complete");
        }
        try {
            a13Var = (a13) y03.c().b(a13.class);
        } catch (IllegalStateException e) {
            n41.ab("Firebase not configured for this build variant: ", e.getMessage(), "StrmrApp");
        }
        if (a13Var == null) {
            throw new NullPointerException("FirebaseCrashlytics component is not present.");
        }
        a13Var.a();
        AnalyticsHelper analyticsHelper = this.f;
        if (analyticsHelper == null) {
            cr4.n("analyticsHelper");
            throw null;
        }
        analyticsHelper.a("app_version", "1.1.72");
        AnalyticsHelper analyticsHelper2 = this.f;
        if (analyticsHelper2 == null) {
            cr4.n("analyticsHelper");
            throw null;
        }
        analyticsHelper2.a("version_code", "1010723");
        wj0.c(this.u, null, null, new l(this, null), 3);
        wj0.c(this.u, null, null, new m(this, null), 3);
        DeviceManager deviceManager = this.l;
        if (deviceManager == null) {
            cr4.n("deviceManager");
            throw null;
        }
        if (deviceManager.h()) {
            wj0.c(this.u, null, null, new v(this, null), 3);
        }
        TraktAccountRepository traktAccountRepository = this.d;
        if (traktAccountRepository == null) {
            cr4.n("traktAccountRepository");
            throw null;
        }
        k63.p(new rd3(traktAccountRepository.getAuthEvents(), new C0523r(this, null)), this.u);
        TraktErrorNotifier traktErrorNotifier = this.r;
        if (traktErrorNotifier == null) {
            cr4.n("traktErrorNotifier");
            throw null;
        }
        k63.p(new rd3(traktErrorNotifier.b, new s(this, null)), this.u);
        RealDebridRepository realDebridRepository = this.e;
        if (realDebridRepository == null) {
            cr4.n("realDebridRepository");
            throw null;
        }
        k63.p(new rd3(realDebridRepository.getAuthEvents(), new o(this, null)), this.u);
        ed1.a aVar = new ed1.a();
        aVar.a = fn6Var;
        aVar.b = true;
        ed1 ed1VarA = aVar.a();
        TimeUnit timeUnit = TimeUnit.HOURS;
        cr4.g(timeUnit, "repeatIntervalTimeUnit");
        x57.a aVar2 = new x57.a(CatalogRefreshWorker.class);
        aVar2.c.d(timeUnit.toMillis(24L), timeUnit.toMillis(3L));
        aVar2.c.j = ed1VarA;
        TimeUnit timeUnit2 = TimeUnit.SECONDS;
        osa.d(this).b("catalog_refresh_periodic", (x57) ((x57.a) aVar2.d(4L)).a());
        TraktSyncWorker.a.d(this);
        PreferencesRepository preferencesRepository = this.o;
        if (preferencesRepository == null) {
            cr4.n("preferencesRepository");
            throw null;
        }
        if (System.currentTimeMillis() - preferencesRepository.getLastTraktSyncAtMs() > 7200000) {
            TraktSyncWorker.a.b(this);
        }
        wj0.c(this.u, null, null, new t(this, null), 3);
        SharedPreferences sharedPreferences2 = getSharedPreferences("strmr_app_prefs", 0);
        if (!sharedPreferences2.getBoolean("debrid_library_flag_migrated", false)) {
            wj0.c(this.u, null, null, new n(this, sharedPreferences2, null), 3);
        }
        SharedPreferences sharedPreferences3 = getSharedPreferences("strmr_app_prefs", 0);
        if (!sharedPreferences3.getBoolean("other_catalog_rows_cleaned", false)) {
            wj0.c(this.u, null, null, new k(this, sharedPreferences3, null), 3);
        }
        ed1.a aVar3 = new ed1.a();
        aVar3.a = fn6Var;
        aVar3.b = true;
        ed1 ed1VarA2 = aVar3.a();
        x57.a aVar4 = new x57.a(DebridAccountRefreshWorker.class, 12L);
        aVar4.c.j = ed1VarA2;
        osa.d(this).b("debrid_account_refresh_periodic", (x57) aVar4.a());
        ed1.a aVar5 = new ed1.a();
        aVar5.a = fn6Var;
        ed1 ed1VarA3 = aVar5.a();
        uw6.a aVar6 = new uw6.a(DebridAccountRefreshWorker.class);
        aVar6.c.j = ed1VarA3;
        uw6 uw6Var = (uw6) aVar6.a();
        osa osaVarD = osa.d(this);
        hs2 hs2Var = hs2.b;
        osaVarD.getClass();
        osaVarD.c("debrid_account_refresh_one_time", hs2Var, Collections.singletonList(uw6Var));
        e17 e17Var = new e17(new OkHttpClient.Builder().connectTimeout(10L, timeUnit2).readTimeout(60L, timeUnit2).followRedirects(true).followSslRedirects(true).retryOnConnectionFailure(true).protocols(y41.am(Protocol.HTTP_1_1)).connectionPool(new ConnectionPool(8, 2L, TimeUnit.MINUTES)).addInterceptor(new m99()).build(), new ll7());
        try {
            e17Var.start(5000, true);
            e17Var.d = true;
            Log.w("ParallelProxy", "Proxy server started on port " + e17Var.getListeningPort());
            w = e17Var;
        } catch (Exception e2) {
            Log.e("ParallelProxy", "Failed to start proxy server", e2);
            e17Var.d = false;
            Log.e("StrmrApp", "Parallel proxy failed to start — direct URLs will be used");
        }
        if (getPackageManager().hasSystemFeature("android.software.leanback")) {
            Log.w("StrmrApp", "TV detected — scheduling Watch Next channel updates");
            ed1.a aVar7 = new ed1.a();
            aVar7.a = fn6Var;
            aVar7.b = true;
            ed1 ed1VarA4 = aVar7.a();
            TimeUnit timeUnit3 = TimeUnit.HOURS;
            x57.a aVar8 = new x57.a(HomeChannelUpdateWorker.class, 1L);
            aVar8.c.j = ed1VarA4;
            osa.d(this).b("home_channel_update", (x57) aVar8.a());
            HomeChannelUpdateWorker.a.a(this);
            TraktSyncRepository traktSyncRepository = this.s;
            if (traktSyncRepository != null) {
                k63.p(new rd3(traktSyncRepository.getSyncCompleted(), new u(this, null)), this.u);
            } else {
                cr4.n("traktSyncRepository");
                throw null;
            }
        }
    }

    @Override // android.app.Application, android.content.ComponentCallbacks
    public final void onLowMemory() {
        super.onLowMemory();
        e17 e17Var = w;
        if (e17Var != null) {
            e17Var.a();
        }
        com.bumptech.glide.a.b(this).a();
    }

    @Override // android.app.Application
    public final void onTerminate() {
        super.onTerminate();
        e17 e17Var = w;
        if (e17Var != null) {
            e17Var.d = false;
            Collection collectionValues = e17Var.c.values();
            cr4.f(collectionValues, "<get-values>(...)");
            Iterator it = collectionValues.iterator();
            while (it.hasNext()) {
                ((g17) it.next()).a();
            }
            e17Var.c.clear();
            e17Var.stop();
            Log.w("ParallelProxy", "Proxy server stopped");
        }
        w = null;
    }

    @Override // android.app.Application, android.content.ComponentCallbacks2
    public final void onTrimMemory(int i) {
        e17 e17Var;
        super.onTrimMemory(i);
        com.bumptech.glide.a.b(this).e(i);
        if ((i == 10 || i == 15 || i == 60 || i == 80) && (e17Var = w) != null) {
            e17Var.a();
        }
    }
}
