package com.strmr.ps.background;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.media.tv.TvContract;
import android.net.Uri;
import android.util.Log;
import androidx.credentials.exceptions.publickeycredential.DomExceptionUtils;
import androidx.media3.container.NalUnitUtil;
import androidx.media3.exoplayer.rtsp.RtspHeaders;
import androidx.media3.exoplayer.upstream.CmcdData;
import androidx.media3.extractor.AacUtil;
import com.strmr.ps.data.model.ContentItem;
import com.strmr.ps.data.model.trakt.TraktMediaList;
import com.strmr.ps.data.repository.ContentRepository;
import com.strmr.ps.data.repository.ContinueWatchingRepository;
import com.strmr.ps.data.repository.TraktMediaRepository;
import dagger.hilt.android.qualifiers.ApplicationContext;
import defpackage.ah;
import defpackage.bd;
import defpackage.ck1;
import defpackage.cr4;
import defpackage.cs;
import defpackage.e6a;
import defpackage.f21;
import defpackage.fh7;
import defpackage.hh7;
import defpackage.hn2;
import defpackage.ik2;
import defpackage.io6;
import defpackage.j65;
import defpackage.jn2;
import defpackage.kp6;
import defpackage.mg6;
import defpackage.mna;
import defpackage.n41;
import defpackage.n89;
import defpackage.ng6;
import defpackage.q38;
import defpackage.rg6;
import defpackage.s38;
import defpackage.v65;
import defpackage.vx8;
import defpackage.y41;
import defpackage.zr7;
import defpackage.zx9;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CancellationException;
import javax.inject.Inject;
import javax.inject.Singleton;
import kotlin.Metadata;
import kotlin.coroutines.jvm.internal.Boxing;
import kotlin.coroutines.jvm.internal.ContinuationImpl;

/* JADX INFO: loaded from: classes.dex */
@Singleton
@Metadata(d1 = {"\u0000\"\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0006\b\u0007\u0018\u00002\u00020\u0001:\u0002\f\rB+\b\u0007\u0012\b\b\u0001\u0010\u0003\u001a\u00020\u0002\u0012\u0006\u0010\u0005\u001a\u00020\u0004\u0012\u0006\u0010\u0007\u001a\u00020\u0006\u0012\u0006\u0010\t\u001a\u00020\b¢\u0006\u0004\b\n\u0010\u000b¨\u0006\u000e"}, d2 = {"Lcom/strmr/ps/background/HomeChannelManager;", "", "Landroid/content/Context;", "context", "Lcom/strmr/ps/data/repository/ContinueWatchingRepository;", "continueWatchingRepository", "Lcom/strmr/ps/data/repository/TraktMediaRepository;", "traktMediaRepository", "Lcom/strmr/ps/data/repository/ContentRepository;", "contentRepository", "<init>", "(Landroid/content/Context;Lcom/strmr/ps/data/repository/ContinueWatchingRepository;Lcom/strmr/ps/data/repository/TraktMediaRepository;Lcom/strmr/ps/data/repository/ContentRepository;)V", "b", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public final class HomeChannelManager {
    public final Context a;
    public final ContinueWatchingRepository b;
    public final TraktMediaRepository c;
    public final ContentRepository d;
    public final j65 e;
    public final ng6 f;

    /* JADX WARN: Failed to restore enum class, 'enum' modifier and super class removed */
    /* JADX WARN: Unknown enum class pattern. Please report as an issue! */
    @Metadata(d1 = {"\u0000\n\n\u0002\u0018\u0002\n\u0002\u0010\u0010\n\u0000\b\u0086\u0081\u0002\u0018\u00002\b\u0012\u0004\u0012\u00020\u00000\u0001¨\u0006\u0002"}, d2 = {"Lcom/strmr/ps/background/HomeChannelManager$a;", "", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class a {
        public static final a c;
        public static final a d;
        public static final a e;
        public static final /* synthetic */ a[] f;
        public static final /* synthetic */ hn2 i;
        public final String a;
        public final String b;

        static {
            a aVar = new a(ContentRepository.CATEGORY_CONTINUE_WATCHING, 0, "Continue Watching", "channel_id_cw");
            c = aVar;
            a aVar2 = new a("TV_CONTINUE_WATCHING", 1, "TV Continue Watching", "channel_id_tv_cw");
            d = aVar2;
            a aVar3 = new a("MOVIE_CONTINUE_WATCHING", 2, "Movie Continue Watching", "channel_id_movie_cw");
            e = aVar3;
            a[] aVarArr = {aVar, aVar2, aVar3, new a("MOVIE_WATCHLIST", 3, "Movie Watchlist", "channel_id_movie_watchlist"), new a("TV_WATCHLIST", 4, "TV Show Watchlist", "channel_id_tv_watchlist"), new a("MOVIE_COLLECTION", 5, "Movie Collection", "channel_id_movie_collection"), new a("TV_COLLECTION", 6, "TV Show Collection", "channel_id_tv_collection"), new a(ContentRepository.CATEGORY_TRENDING_MOVIES, 7, "Trending Movies", "channel_id_trending_movies"), new a(ContentRepository.CATEGORY_TRENDING_SHOWS, 8, "Trending TV Shows", "channel_id_trending_shows")};
            f = aVarArr;
            i = jn2.a(aVarArr);
        }

        public a(String str, int i2, String str2, String str3) {
            this.a = str2;
            this.b = str3;
        }

        public static a valueOf(String str) {
            return (a) Enum.valueOf(a.class, str);
        }

        public static a[] values() {
            return (a[]) f.clone();
        }
    }

    @Metadata(d1 = {"\u0000\u0018\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0010\b\n\u0002\b\u0005\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\u0006\u001a\u00020\u00058\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0006\u0010\u0007R\u0014\u0010\b\u001a\u00020\u00058\u0002X\u0082T¢\u0006\u0006\n\u0004\b\b\u0010\u0007R\u0014\u0010\t\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\t\u0010\u0004¨\u0006\n"}, d2 = {"Lcom/strmr/ps/background/HomeChannelManager$b;", "", "", "TAG", "Ljava/lang/String;", "", "MAX_PROGRAMS", "I", "SYNTHETIC_DURATION_MS", "PREFS_NAME", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class b {
    }

    @Metadata(k = 3, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public /* synthetic */ class c {
        static {
            int[] iArr = new int[a.values().length];
            try {
                iArr[0] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                a aVar = a.c;
                iArr[1] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                a aVar2 = a.c;
                iArr[2] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                a aVar3 = a.c;
                iArr[3] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                a aVar4 = a.c;
                iArr[4] = 5;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                a aVar5 = a.c;
                iArr[5] = 6;
            } catch (NoSuchFieldError unused6) {
            }
            try {
                a aVar6 = a.c;
                iArr[6] = 7;
            } catch (NoSuchFieldError unused7) {
            }
            try {
                a aVar7 = a.c;
                iArr[7] = 8;
            } catch (NoSuchFieldError unused8) {
            }
            try {
                a aVar8 = a.c;
                iArr[8] = 9;
            } catch (NoSuchFieldError unused9) {
            }
        }
    }

    @Inject
    public HomeChannelManager(@ApplicationContext @kp6 Context context, @kp6 ContinueWatchingRepository continueWatchingRepository, @kp6 TraktMediaRepository traktMediaRepository, @kp6 ContentRepository contentRepository) {
        cr4.g(context, "context");
        cr4.g(continueWatchingRepository, "continueWatchingRepository");
        cr4.g(traktMediaRepository, "traktMediaRepository");
        cr4.g(contentRepository, "contentRepository");
        this.a = context;
        this.b = continueWatchingRepository;
        this.c = traktMediaRepository;
        this.d = contentRepository;
        this.e = v65.a(new ah(this, 15));
        this.f = rg6.a();
    }

    public static Uri b(ContentItem contentItem) {
        String imdbId = contentItem.getImdbId();
        if (imdbId == null) {
            return null;
        }
        if (contentItem.isMovie()) {
            return Uri.parse("strmr://movie/".concat(imdbId));
        }
        Integer continueWatchingSeason = contentItem.getContinueWatchingSeason();
        Integer continueWatchingEpisode = contentItem.getContinueWatchingEpisode();
        if (continueWatchingSeason == null || continueWatchingEpisode == null) {
            return Uri.parse("strmr://series/".concat(imdbId));
        }
        return Uri.parse("strmr://series/" + imdbId + DomExceptionUtils.SEPARATOR + continueWatchingSeason + DomExceptionUtils.SEPARATOR + continueWatchingEpisode);
    }

    public static Bitmap h(String str) throws IOException {
        URLConnection uRLConnectionOpenConnection = new URL(str).openConnection();
        cr4.e(uRLConnectionOpenConnection, "null cannot be cast to non-null type java.net.HttpURLConnection");
        HttpURLConnection httpURLConnection = (HttpURLConnection) uRLConnectionOpenConnection;
        httpURLConnection.setConnectTimeout(10000);
        httpURLConnection.setReadTimeout(10000);
        httpURLConnection.setRequestProperty(RtspHeaders.USER_AGENT, "Strmr/1.0");
        httpURLConnection.setInstanceFollowRedirects(true);
        try {
            if (httpURLConnection.getResponseCode() != 200) {
                return null;
            }
            InputStream inputStream = httpURLConnection.getInputStream();
            try {
                Bitmap bitmapDecodeStream = BitmapFactory.decodeStream(inputStream);
                f21.a(inputStream, null);
                return bitmapDecodeStream;
            } finally {
            }
        } finally {
            httpURLConnection.disconnect();
        }
    }

    public final Uri a(String str) {
        Uri uri = Uri.parse("content://" + this.a.getPackageName() + ".channelimages/" + str);
        cr4.f(uri, "parse(...)");
        return uri;
    }

    /* JADX WARN: Finally extract failed */
    /* JADX WARN: Removed duplicated region for block: B:63:0x01a6 A[Catch: Exception -> 0x003e, TryCatch #1 {Exception -> 0x003e, blocks: (B:3:0x0006, B:4:0x0019, B:7:0x0021, B:10:0x0041, B:28:0x009b, B:34:0x00a5, B:35:0x00ba, B:37:0x00c0, B:38:0x00ec, B:39:0x0110, B:41:0x0116, B:43:0x012e, B:44:0x0132, B:46:0x0138, B:47:0x0148, B:49:0x014f, B:50:0x0157, B:52:0x015e, B:56:0x0180, B:58:0x0184, B:62:0x01a1, B:64:0x01b0, B:65:0x01b4, B:67:0x01ba, B:69:0x01c8, B:63:0x01a6, B:71:0x01d0, B:32:0x00a1, B:33:0x00a4, B:30:0x009f, B:13:0x0060, B:14:0x0068, B:16:0x006e, B:19:0x0075, B:21:0x007b, B:23:0x0081, B:26:0x008d), top: B:77:0x0006, inners: #0, #2 }] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void c() {
        Object next;
        try {
            List list = a.i;
            ArrayList arrayList = new ArrayList(y41.p(list, 10));
            Iterator it = ((cs) list).iterator();
            while (it.hasNext()) {
                arrayList.add("strmr_" + ((a) it.next()).name());
            }
            Set setBr = y41.br(arrayList);
            LinkedHashMap linkedHashMap = new LinkedHashMap();
            Cursor cursorQuery = this.a.getContentResolver().query(zx9.b.a, new String[]{"_id", "internal_provider_id"}, null, null, null);
            int i = 0;
            if (cursorQuery != null) {
                try {
                    int columnIndex = cursorQuery.getColumnIndex("_id");
                    int columnIndex2 = cursorQuery.getColumnIndex("internal_provider_id");
                    while (cursorQuery.moveToNext()) {
                        String string = cursorQuery.getString(columnIndex2);
                        if (string != null && n89.ar(string, "strmr_", false)) {
                            Object arrayList2 = linkedHashMap.get(string);
                            if (arrayList2 == null) {
                                arrayList2 = new ArrayList();
                                linkedHashMap.put(string, arrayList2);
                            }
                            ((List) arrayList2).add(Long.valueOf(cursorQuery.getLong(columnIndex)));
                        }
                    }
                    cursorQuery.close();
                } catch (Throwable th) {
                    try {
                        throw th;
                    } catch (Throwable th2) {
                        f21.a(cursorQuery, th);
                        throw th2;
                    }
                }
            }
            int size = linkedHashMap.size();
            ArrayList arrayList3 = new ArrayList(linkedHashMap.size());
            for (Map.Entry entry : linkedHashMap.entrySet()) {
                arrayList3.add(entry.getKey() + "=" + ((List) entry.getValue()).size());
            }
            Log.w("HomeChannelManager", "cleanup: found " + size + " strmr channel groups: " + arrayList3);
            for (Map.Entry entry2 : linkedHashMap.entrySet()) {
                String str = (String) entry2.getKey();
                List list2 = (List) entry2.getValue();
                if (!setBr.contains(str)) {
                    Iterator it2 = list2.iterator();
                    while (it2.hasNext()) {
                        g(((Number) it2.next()).longValue());
                        i++;
                    }
                } else if (list2.size() > 1) {
                    Iterator it3 = ((cs) a.i).iterator();
                    while (true) {
                        if (!it3.hasNext()) {
                            next = null;
                            break;
                        }
                        next = it3.next();
                        if (cr4.b("strmr_" + ((a) next).name(), str)) {
                            break;
                        }
                    }
                    a aVar = (a) next;
                    if (aVar != null) {
                        long j = k().getLong(aVar.b, -1L);
                        Long lValueOf = list2.contains(Long.valueOf(j)) ? Long.valueOf(j) : null;
                        long jLongValue = lValueOf != null ? lValueOf.longValue() : ((Number) y41.z(list2)).longValue();
                        Iterator it4 = list2.iterator();
                        while (it4.hasNext()) {
                            long jLongValue2 = ((Number) it4.next()).longValue();
                            if (jLongValue2 != jLongValue) {
                                g(jLongValue2);
                                i++;
                            }
                        }
                    }
                }
            }
            if (i > 0) {
                Log.w("HomeChannelManager", "Cleaned up " + i + " orphan/duplicate channels");
            }
        } catch (Exception e) {
            Log.w("HomeChannelManager", "Failed to cleanup orphan channels", e);
        }
    }

    public final void d() {
        File[] fileArrListFiles;
        try {
            File externalFilesDir = this.a.getExternalFilesDir("channel_thumbs");
            if (externalFilesDir == null || (fileArrListFiles = externalFilesDir.listFiles()) == null) {
                return;
            }
            for (File file : fileArrListFiles) {
                file.delete();
            }
        } catch (Exception unused) {
        }
    }

    public final void e() {
        try {
            Iterator it = ((cs) a.i).iterator();
            while (it.hasNext()) {
                k().edit().remove(((a) it.next()).b).apply();
            }
            try {
                Cursor cursorQuery = this.a.getContentResolver().query(zx9.b.a, new String[]{"_id", "internal_provider_id"}, null, null, null);
                if (cursorQuery != null) {
                    try {
                        int columnIndex = cursorQuery.getColumnIndex("_id");
                        int columnIndex2 = cursorQuery.getColumnIndex("internal_provider_id");
                        while (cursorQuery.moveToNext()) {
                            String string = cursorQuery.getString(columnIndex2);
                            if (string != null && n89.ar(string, "strmr_", false)) {
                                g(cursorQuery.getLong(columnIndex));
                            }
                        }
                        cursorQuery.close();
                    } finally {
                    }
                }
            } catch (Exception unused) {
            }
            n();
            Log.w("HomeChannelManager", "All channels cleared");
        } catch (Exception e) {
            Log.e("HomeChannelManager", "Failed to clear channels", e);
        }
    }

    public final Uri f(Integer num, String str, String str2) {
        File externalFilesDir;
        if (str != null && str2 != null && (externalFilesDir = this.a.getExternalFilesDir("channel_thumbs")) != null) {
            File file = new File(externalFilesDir, "thumb_" + num + ".jpg");
            if (file.exists() && System.currentTimeMillis() - file.lastModified() < 86400000) {
                String name = file.getName();
                cr4.f(name, "getName(...)");
                return a(name);
            }
            try {
                Bitmap bitmapH = h(str);
                if (bitmapH != null) {
                    Bitmap bitmapH2 = h(str2);
                    if (bitmapH2 == null) {
                        bitmapH.recycle();
                        return null;
                    }
                    Bitmap bitmapCopy = bitmapH.copy(Bitmap.Config.ARGB_8888, true);
                    Canvas canvas = new Canvas(bitmapCopy);
                    float width = bitmapCopy.getWidth();
                    float height = bitmapCopy.getHeight();
                    Paint paint = new Paint();
                    float f = height * 0.4f;
                    paint.setShader(new LinearGradient(0.0f, height, 0.0f, f, Integer.MIN_VALUE, 0, Shader.TileMode.CLAMP));
                    canvas.drawRect(0.0f, f, width, height, paint);
                    double d = width;
                    float fMin = Math.min(((int) (0.47d * d)) / bitmapH2.getWidth(), ((int) (((double) height) * 0.31d)) / bitmapH2.getHeight());
                    int width2 = (int) (bitmapH2.getWidth() * fMin);
                    int height2 = (int) (bitmapH2.getHeight() * fMin);
                    Bitmap bitmapCreateScaledBitmap = Bitmap.createScaledBitmap(bitmapH2, width2, height2, true);
                    cr4.f(bitmapCreateScaledBitmap, "createScaledBitmap(...)");
                    float f2 = (float) (d * 0.052d);
                    canvas.drawBitmap(bitmapCreateScaledBitmap, f2, (height - height2) - f2, (Paint) null);
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    try {
                        bitmapCopy.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
                        fileOutputStream.close();
                        bitmapH.recycle();
                        bitmapH2.recycle();
                        bitmapCreateScaledBitmap.recycle();
                        bitmapCopy.recycle();
                        String name2 = file.getName();
                        cr4.f(name2, "getName(...)");
                        return a(name2);
                    } finally {
                    }
                }
            } catch (Exception e) {
                Log.w("HomeChannelManager", "Failed to composite for tmdbId=" + num, e);
            }
        }
        return null;
    }

    public final void g(long j) {
        try {
            this.a.getContentResolver().delete(TvContract.buildChannelUri(j), null, null);
        } catch (Exception unused) {
        }
    }

    public final Bitmap i() {
        Context context = this.a;
        try {
            Drawable applicationIcon = context.getPackageManager().getApplicationIcon(context.getPackageName());
            cr4.f(applicationIcon, "getApplicationIcon(...)");
            Bitmap bitmapCreateBitmap = Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
            cr4.f(bitmapCreateBitmap, "createBitmap(...)");
            Canvas canvas = new Canvas(bitmapCreateBitmap);
            applicationIcon.setBounds(0, 0, 512, 512);
            applicationIcon.draw(canvas);
            return bitmapCreateBitmap;
        } catch (Exception e) {
            n41.ab("Failed to render app icon: ", e.getMessage(), "HomeChannelManager");
            return null;
        }
    }

    public final long j(a aVar) {
        Long lValueOf;
        Cursor cursorQuery;
        SharedPreferences sharedPreferencesK = k();
        String str = aVar.b;
        String str2 = aVar.a;
        long j = sharedPreferencesK.getLong(str, -1L);
        Context context = this.a;
        if (j != -1) {
            boolean z = false;
            try {
                cursorQuery = context.getContentResolver().query(TvContract.buildChannelUri(j), new String[]{"_id"}, null, null, null);
                if (cursorQuery != null) {
                    try {
                        boolean zMoveToFirst = cursorQuery.moveToFirst();
                        cursorQuery.close();
                        z = zMoveToFirst;
                    } finally {
                    }
                }
            } catch (Exception unused) {
            }
            if (z) {
                o(j);
                return j;
            }
        }
        k().edit().remove(str).apply();
        String strN = n41.n("strmr_", aVar.name());
        String string = null;
        try {
            cursorQuery = context.getContentResolver().query(zx9.b.a, new String[]{"_id", "internal_provider_id"}, null, null, null);
        } catch (Exception e) {
            Log.w("HomeChannelManager", "Failed to query channels for providerId=" + strN, e);
        }
        if (cursorQuery != null) {
            try {
                int columnIndex = cursorQuery.getColumnIndex("_id");
                int columnIndex2 = cursorQuery.getColumnIndex("internal_provider_id");
                while (cursorQuery.moveToNext()) {
                    if (cr4.b(cursorQuery.getString(columnIndex2), strN)) {
                        lValueOf = Long.valueOf(cursorQuery.getLong(columnIndex));
                        cursorQuery.close();
                        break;
                    }
                    Log.w("HomeChannelManager", "Failed to query channels for providerId=" + strN, e);
                    lValueOf = null;
                }
                cursorQuery.close();
                lValueOf = null;
            } finally {
            }
        } else {
            lValueOf = null;
        }
        if (lValueOf != null) {
            k().edit().putLong(str, lValueOf.longValue()).apply();
            o(lValueOf.longValue());
            Log.w("HomeChannelManager", "Recovered channel '" + str2 + "' id=" + lValueOf + " from TvProvider");
            return lValueOf.longValue();
        }
        try {
            fh7.a aVar2 = new fh7.a();
            ContentValues contentValues = new ContentValues();
            aVar2.a = contentValues;
            contentValues.put("display_name", str2.toString());
            Uri uri = Uri.parse("strmr://home");
            if (uri != null) {
                string = uri.toString();
            }
            contentValues.put("app_link_intent_uri", string);
            contentValues.put("internal_provider_id", strN);
            Uri uriInsert = context.getContentResolver().insert(zx9.b.a, new ContentValues(aVar2.a().a));
            if (uriInsert == null) {
                throw new Exception("Channel insert returned null");
            }
            long id = ContentUris.parseId(uriInsert);
            o(id);
            k().edit().putLong(str, id).apply();
            try {
                TvContract.requestChannelBrowsable(context, id);
            } catch (Exception e2) {
                Log.w("HomeChannelManager", "requestChannelBrowsable failed for " + str2 + ": " + e2.getMessage());
            }
            Log.w("HomeChannelManager", "Created channel '" + str2 + "' id=" + id);
            return id;
        } catch (Exception e3) {
            Log.w("HomeChannelManager", "Failed to create channel ".concat(str2), e3);
            return -1L;
        }
    }

    public final SharedPreferences k() {
        return (SharedPreferences) this.e.getValue();
    }

    /* JADX WARN: Removed duplicated region for block: B:19:0x0068  */
    /* JADX WARN: Removed duplicated region for block: B:31:0x00aa  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean l(ContentItem contentItem) {
        Integer continueWatchingSeason;
        Integer continueWatchingEpisode;
        String showTitle;
        String backdropUrl;
        mna.a aVar = new mna.a();
        aVar.a.put("watch_next_type", (Integer) 0);
        aVar.a.put("last_engagement_time_utc_millis", Long.valueOf(System.currentTimeMillis()));
        aVar.a.put("internal_provider_id", n41.k("strmr_", contentItem.getTmdbId()));
        String title = contentItem.getTitle();
        if (title == null || n89.ab(title)) {
            title = null;
        }
        if (title == null) {
            title = "Untitled";
        }
        if (contentItem.isMovie()) {
            aVar.c(0);
            aVar.a.put("title", title);
        } else {
            aVar.c(3);
            String showTitle2 = contentItem.getShowTitle();
            if (showTitle2 == null) {
                showTitle2 = title;
                aVar.a.put("title", showTitle2);
                continueWatchingSeason = contentItem.getContinueWatchingSeason();
                continueWatchingEpisode = contentItem.getContinueWatchingEpisode();
                if (continueWatchingSeason != null && continueWatchingSeason.intValue() > 0) {
                    aVar.a.put("season_display_number", String.valueOf(continueWatchingSeason.intValue()));
                }
                if (continueWatchingEpisode != null && continueWatchingEpisode.intValue() > 0) {
                    aVar.a.put("episode_display_number", String.valueOf(continueWatchingEpisode.intValue()));
                }
                showTitle = contentItem.getShowTitle();
                if (showTitle != null) {
                    if ((n89.ab(showTitle) ? null : showTitle) != null) {
                        aVar.a.put("episode_title", title);
                    }
                }
            } else {
                if (n89.ab(showTitle2)) {
                    showTitle2 = null;
                }
                if (showTitle2 == null) {
                }
                aVar.a.put("title", showTitle2);
                continueWatchingSeason = contentItem.getContinueWatchingSeason();
                continueWatchingEpisode = contentItem.getContinueWatchingEpisode();
                if (continueWatchingSeason != null) {
                    aVar.a.put("season_display_number", String.valueOf(continueWatchingSeason.intValue()));
                }
                if (continueWatchingEpisode != null) {
                    aVar.a.put("episode_display_number", String.valueOf(continueWatchingEpisode.intValue()));
                }
                showTitle = contentItem.getShowTitle();
                if (showTitle != null) {
                }
            }
        }
        String overview = contentItem.getOverview();
        if (overview != null) {
            aVar.a.put("short_description", overview);
        }
        if (!contentItem.isTvShow() || (backdropUrl = contentItem.getEpisodeStillUrl()) == null) {
            backdropUrl = contentItem.getBackdropUrl();
        }
        Uri uriF = f(Integer.valueOf(contentItem.getTmdbId()), backdropUrl, contentItem.getLogoUrl());
        if (uriF != null) {
            aVar.a(uriF);
        } else {
            if (backdropUrl == null) {
                backdropUrl = contentItem.getPosterUrl();
            }
            if (backdropUrl != null) {
                aVar.a(Uri.parse(backdropUrl));
            }
        }
        Double watchProgress = contentItem.getWatchProgress();
        if (watchProgress != null && watchProgress.doubleValue() > 0.0d) {
            double dA = zr7.a(watchProgress.doubleValue());
            aVar.a.put("duration_millis", Integer.valueOf(AacUtil.AAC_LC_MAX_RATE_BYTES_PER_SECOND));
            aVar.a.put("last_playback_position_millis", Integer.valueOf((int) (dA * ((double) AacUtil.AAC_LC_MAX_RATE_BYTES_PER_SECOND))));
        }
        Uri uriB = b(contentItem);
        if (uriB != null) {
            aVar.b(uriB);
            aVar.b(Uri.parse(new Intent("android.intent.action.VIEW", uriB).toUri(1)));
        }
        try {
            ContentResolver contentResolver = this.a.getContentResolver();
            Uri uri = zx9.h.a;
            ContentValues contentValues = new ContentValues(aVar.a);
            contentValues.remove("browsable");
            return contentResolver.insert(uri, contentValues) != null;
        } catch (Exception e) {
            Log.w("HomeChannelManager", "Failed to insert Watch Next: tmdbId=" + contentItem.getTmdbId(), e);
            return false;
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:27:0x0090, code lost:
    
        if (r11 == r0) goto L94;
     */
    /* JADX WARN: Code restructure failed: missing block: B:35:0x00a6, code lost:
    
        if (r11 == r0) goto L94;
     */
    /* JADX WARN: Code restructure failed: missing block: B:43:0x00c3, code lost:
    
        if (r11 == r0) goto L94;
     */
    /* JADX WARN: Code restructure failed: missing block: B:51:0x00e0, code lost:
    
        if (r11 == r0) goto L94;
     */
    /* JADX WARN: Code restructure failed: missing block: B:59:0x00fd, code lost:
    
        if (r11 == r0) goto L94;
     */
    /* JADX WARN: Code restructure failed: missing block: B:67:0x011a, code lost:
    
        if (r11 == r0) goto L94;
     */
    /* JADX WARN: Code restructure failed: missing block: B:75:0x012e, code lost:
    
        if (r12 == r0) goto L94;
     */
    /* JADX WARN: Code restructure failed: missing block: B:84:0x015a, code lost:
    
        if (r12 == r0) goto L94;
     */
    /* JADX WARN: Code restructure failed: missing block: B:93:0x0186, code lost:
    
        if (r12 == r0) goto L94;
     */
    /* JADX WARN: Code restructure failed: missing block: B:94:0x0188, code lost:
    
        return r0;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:8:0x0014  */
    /* JADX WARN: Removed duplicated region for block: B:99:0x019d  */
    /* JADX WARN: Type inference failed for: r9v12, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r9v15, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r9v18, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r9v2, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r9v20 */
    /* JADX WARN: Type inference failed for: r9v23, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r9v3, types: [java.util.ArrayList] */
    /* JADX WARN: Type inference failed for: r9v4, types: [java.util.ArrayList] */
    /* JADX WARN: Type inference failed for: r9v6, types: [java.util.List] */
    /* JADX WARN: Type inference failed for: r9v9, types: [java.util.List] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object m(a aVar, ContinuationImpl continuationImpl) {
        i iVar;
        Object objM149getMediaListPagedyxL6bBk$default;
        Object objM149getMediaListPagedyxL6bBk$default2;
        Object objM149getMediaListPagedyxL6bBk$default3;
        Object objM149getMediaListPagedyxL6bBk$default4;
        Object objM91getTrendingMoviesgIAlus;
        Object objM93getTrendingShowsgIAlus;
        ?? arrayList;
        String imdbId;
        if (continuationImpl instanceof i) {
            iVar = (i) continuationImpl;
            int i = iVar.c;
            if ((i & Integer.MIN_VALUE) != 0) {
                iVar.c = i - Integer.MIN_VALUE;
            } else {
                iVar = new i(this, continuationImpl);
            }
        }
        i iVar2 = iVar;
        Object objLoad = iVar2.a;
        ck1 ck1Var = ck1.a;
        int i2 = iVar2.c;
        Object obj = ik2.a;
        switch (i2) {
            case 0:
                s38.b(objLoad);
                int iOrdinal = aVar.ordinal();
                TraktMediaRepository traktMediaRepository = this.c;
                ContentRepository contentRepository = this.d;
                ContinueWatchingRepository continueWatchingRepository = this.b;
                switch (iOrdinal) {
                    case 0:
                        iVar2.c = 1;
                        objLoad = continueWatchingRepository.load(false, iVar2);
                        break;
                    case 1:
                        iVar2.c = 2;
                        objLoad = continueWatchingRepository.load(false, iVar2);
                        break;
                    case 2:
                        iVar2.c = 3;
                        objLoad = continueWatchingRepository.load(false, iVar2);
                        break;
                    case 3:
                        TraktMediaList traktMediaList = TraktMediaList.MOVIE_WATCHLIST;
                        iVar2.c = 4;
                        objM149getMediaListPagedyxL6bBk$default = TraktMediaRepository.m149getMediaListPagedyxL6bBk$default(traktMediaRepository, traktMediaList, 0, 0, false, iVar2, 14, null);
                        break;
                    case 4:
                        TraktMediaList traktMediaList2 = TraktMediaList.TV_WATCHLIST;
                        iVar2.c = 5;
                        objM149getMediaListPagedyxL6bBk$default2 = TraktMediaRepository.m149getMediaListPagedyxL6bBk$default(traktMediaRepository, traktMediaList2, 0, 0, false, iVar2, 14, null);
                        break;
                    case 5:
                        TraktMediaList traktMediaList3 = TraktMediaList.MOVIE_COLLECTION;
                        iVar2.c = 6;
                        objM149getMediaListPagedyxL6bBk$default3 = TraktMediaRepository.m149getMediaListPagedyxL6bBk$default(traktMediaRepository, traktMediaList3, 0, 0, false, iVar2, 14, null);
                        break;
                    case 6:
                        TraktMediaList traktMediaList4 = TraktMediaList.TV_COLLECTION;
                        iVar2.c = 7;
                        objM149getMediaListPagedyxL6bBk$default4 = TraktMediaRepository.m149getMediaListPagedyxL6bBk$default(traktMediaRepository, traktMediaList4, 0, 0, false, iVar2, 14, null);
                        break;
                    case 7:
                        iVar2.c = 8;
                        objM91getTrendingMoviesgIAlus = contentRepository.m91getTrendingMoviesgIAlus(false, iVar2);
                        break;
                    case 8:
                        iVar2.c = 9;
                        objM93getTrendingShowsgIAlus = contentRepository.m93getTrendingShowsgIAlus(false, iVar2);
                        break;
                    default:
                        throw new io6();
                }
                break;
            case 1:
                s38.b(objLoad);
                arrayList = (List) objLoad;
                ArrayList arrayList2 = new ArrayList();
                for (Object obj2 : (Iterable) arrayList) {
                    ContentItem contentItem = (ContentItem) obj2;
                    if (contentItem.isContent() && (imdbId = contentItem.getImdbId()) != null && !n89.ab(imdbId)) {
                        arrayList2.add(obj2);
                    }
                }
                return y41.bk(arrayList2, 20);
            case 2:
                s38.b(objLoad);
                arrayList = new ArrayList();
                for (Object obj3 : (Iterable) objLoad) {
                    if (((ContentItem) obj3).isTvShow()) {
                        arrayList.add(obj3);
                    }
                }
                ArrayList arrayList22 = new ArrayList();
                while (r12.hasNext()) {
                }
                return y41.bk(arrayList22, 20);
            case 3:
                s38.b(objLoad);
                arrayList = new ArrayList();
                for (Object obj4 : (Iterable) objLoad) {
                    if (((ContentItem) obj4).isMovie()) {
                        arrayList.add(obj4);
                    }
                }
                ArrayList arrayList222 = new ArrayList();
                while (r12.hasNext()) {
                }
                return y41.bk(arrayList222, 20);
            case 4:
                s38.b(objLoad);
                objM149getMediaListPagedyxL6bBk$default = ((q38) objLoad).a;
                if (!(objM149getMediaListPagedyxL6bBk$default instanceof q38.b)) {
                    obj = objM149getMediaListPagedyxL6bBk$default;
                }
                arrayList = (List) obj;
                ArrayList arrayList2222 = new ArrayList();
                while (r12.hasNext()) {
                }
                return y41.bk(arrayList2222, 20);
            case 5:
                s38.b(objLoad);
                objM149getMediaListPagedyxL6bBk$default2 = ((q38) objLoad).a;
                if (!(objM149getMediaListPagedyxL6bBk$default2 instanceof q38.b)) {
                    obj = objM149getMediaListPagedyxL6bBk$default2;
                }
                arrayList = (List) obj;
                ArrayList arrayList22222 = new ArrayList();
                while (r12.hasNext()) {
                }
                return y41.bk(arrayList22222, 20);
            case 6:
                s38.b(objLoad);
                objM149getMediaListPagedyxL6bBk$default3 = ((q38) objLoad).a;
                if (!(objM149getMediaListPagedyxL6bBk$default3 instanceof q38.b)) {
                    obj = objM149getMediaListPagedyxL6bBk$default3;
                }
                arrayList = (List) obj;
                ArrayList arrayList222222 = new ArrayList();
                while (r12.hasNext()) {
                }
                return y41.bk(arrayList222222, 20);
            case 7:
                s38.b(objLoad);
                objM149getMediaListPagedyxL6bBk$default4 = ((q38) objLoad).a;
                if (!(objM149getMediaListPagedyxL6bBk$default4 instanceof q38.b)) {
                    obj = objM149getMediaListPagedyxL6bBk$default4;
                }
                arrayList = (List) obj;
                ArrayList arrayList2222222 = new ArrayList();
                while (r12.hasNext()) {
                }
                return y41.bk(arrayList2222222, 20);
            case 8:
                s38.b(objLoad);
                objM91getTrendingMoviesgIAlus = ((q38) objLoad).a;
                if (!(objM91getTrendingMoviesgIAlus instanceof q38.b)) {
                    obj = objM91getTrendingMoviesgIAlus;
                }
                arrayList = (List) obj;
                ArrayList arrayList22222222 = new ArrayList();
                while (r12.hasNext()) {
                }
                return y41.bk(arrayList22222222, 20);
            case 9:
                s38.b(objLoad);
                objM93getTrendingShowsgIAlus = ((q38) objLoad).a;
                if (!(objM93getTrendingShowsgIAlus instanceof q38.b)) {
                    obj = objM93getTrendingShowsgIAlus;
                }
                arrayList = (List) obj;
                ArrayList arrayList222222222 = new ArrayList();
                while (r12.hasNext()) {
                }
                return y41.bk(arrayList222222222, 20);
            default:
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
        }
    }

    public final void n() {
        Context context = this.a;
        try {
            Cursor cursorQuery = context.getContentResolver().query(zx9.h.a, mna.d, null, null, null);
            if (cursorQuery != null) {
                while (cursorQuery.moveToNext()) {
                    try {
                        ContentValues contentValues = mna.a(cursorQuery).a;
                        String asString = contentValues.getAsString("internal_provider_id");
                        if (asString != null && n89.ar(asString, "strmr_", false)) {
                            ContentResolver contentResolver = context.getContentResolver();
                            Long asLong = contentValues.getAsLong("_id");
                            contentResolver.delete(ContentUris.withAppendedId(zx9.h.a, asLong == null ? -1L : asLong.longValue()), null, null);
                        }
                    } finally {
                    }
                }
                cursorQuery.close();
            }
        } catch (Exception e) {
            Log.w("HomeChannelManager", "Failed to remove Watch Next programs", e);
        }
    }

    public final void o(long j) {
        try {
            Bitmap bitmapI = i();
            if (bitmapI == null) {
                return;
            }
            OutputStream outputStreamOpenOutputStream = this.a.getContentResolver().openOutputStream(TvContract.buildChannelLogoUri(j));
            if (outputStreamOpenOutputStream != null) {
                try {
                    bitmapI.compress(Bitmap.CompressFormat.PNG, 100, outputStreamOpenOutputStream);
                    outputStreamOpenOutputStream.close();
                } finally {
                }
            }
            bitmapI.recycle();
        } catch (Exception e) {
            Log.w("HomeChannelManager", "Could not set channel logo for channelId=" + j + ": " + e.getMessage());
        }
    }

    /* JADX WARN: Can't wrap try/catch for region: R(13:0|2|(2:4|(1:6)(1:7))(0)|8|82|(1:(1:(1:(5:13|14|65|77|78)(2:22|23))(9:24|86|25|47|(1:49)|50|40|(4:42|87|43|(6:46|47|(0)|50|40|(1:62)(0)))(0)|64))(1:33))(3:34|(1:37)|64)|89|38|39|40|(0)(0)|64|(1:(0))) */
    /* JADX WARN: Code restructure failed: missing block: B:63:0x0125, code lost:
    
        if (r11.r(r3) == r4) goto L64;
     */
    /* JADX WARN: Code restructure failed: missing block: B:67:0x012b, code lost:
    
        r2 = r5;
     */
    /* JADX WARN: Code restructure failed: missing block: B:68:0x012d, code lost:
    
        r2 = r5;
     */
    /* JADX WARN: Code restructure failed: missing block: B:70:0x0131, code lost:
    
        r0 = th;
     */
    /* JADX WARN: Code restructure failed: missing block: B:72:0x0133, code lost:
    
        r0 = e;
     */
    /* JADX WARN: Code restructure failed: missing block: B:74:0x0135, code lost:
    
        r0 = move-exception;
     */
    /* JADX WARN: Code restructure failed: missing block: B:80:0x0146, code lost:
    
        r2.c(null);
     */
    /* JADX WARN: Code restructure failed: missing block: B:81:0x0149, code lost:
    
        throw r0;
     */
    /* JADX WARN: Code restructure failed: missing block: B:92:?, code lost:
    
        throw r0;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:42:0x00b7 A[Catch: all -> 0x003b, Exception -> 0x003e, CancellationException -> 0x0041, TRY_LEAVE, TryCatch #8 {all -> 0x003b, blocks: (B:14:0x0036, B:65:0x0128, B:76:0x0137, B:79:0x0145, B:40:0x00b1, B:42:0x00b7, B:43:0x00be, B:61:0x0116, B:62:0x0117), top: B:82:0x002a }] */
    /* JADX WARN: Removed duplicated region for block: B:49:0x00e7 A[Catch: all -> 0x005b, Exception -> 0x005f, CancellationException -> 0x0062, TRY_LEAVE, TryCatch #7 {all -> 0x005b, blocks: (B:25:0x0056, B:47:0x00d6, B:49:0x00e7, B:55:0x00f5), top: B:82:0x002a }] */
    /* JADX WARN: Removed duplicated region for block: B:62:0x0117 A[Catch: all -> 0x003b, Exception -> 0x003e, CancellationException -> 0x0041, TryCatch #8 {all -> 0x003b, blocks: (B:14:0x0036, B:65:0x0128, B:76:0x0137, B:79:0x0145, B:40:0x00b1, B:42:0x00b7, B:43:0x00be, B:61:0x0116, B:62:0x0117), top: B:82:0x002a }] */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0019  */
    /* JADX WARN: Type inference failed for: r2v0, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r2v1 */
    /* JADX WARN: Type inference failed for: r2v10 */
    /* JADX WARN: Type inference failed for: r2v12 */
    /* JADX WARN: Type inference failed for: r2v14 */
    /* JADX WARN: Type inference failed for: r2v15 */
    /* JADX WARN: Type inference failed for: r2v18, types: [java.lang.Object, mg6] */
    /* JADX WARN: Type inference failed for: r2v2, types: [mg6] */
    /* JADX WARN: Type inference failed for: r2v21 */
    /* JADX WARN: Type inference failed for: r2v23 */
    /* JADX WARN: Type inference failed for: r2v28 */
    /* JADX WARN: Type inference failed for: r2v29 */
    /* JADX WARN: Type inference failed for: r2v3 */
    /* JADX WARN: Type inference failed for: r2v30 */
    /* JADX WARN: Type inference failed for: r2v31 */
    /* JADX WARN: Type inference failed for: r2v4, types: [mg6] */
    /* JADX WARN: Type inference failed for: r2v5 */
    /* JADX WARN: Type inference failed for: r2v6 */
    /* JADX WARN: Type inference failed for: r8v0 */
    /* JADX WARN: Type inference failed for: r8v1 */
    /* JADX WARN: Type inference failed for: r8v10 */
    /* JADX WARN: Type inference failed for: r8v11 */
    /* JADX WARN: Type inference failed for: r8v12 */
    /* JADX WARN: Type inference failed for: r8v15, types: [mg6] */
    /* JADX WARN: Type inference failed for: r8v16 */
    /* JADX WARN: Type inference failed for: r8v17 */
    /* JADX WARN: Type inference failed for: r8v18 */
    /* JADX WARN: Type inference failed for: r8v19 */
    /* JADX WARN: Type inference failed for: r8v4 */
    /* JADX WARN: Type inference failed for: r8v5 */
    /* JADX WARN: Type inference failed for: r8v6 */
    /* JADX WARN: Type inference failed for: r8v9 */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:46:0x00d1 -> B:47:0x00d6). Please report as a decompilation issue!!! */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:55:0x00f5 -> B:50:0x00ea). Please report as a decompilation issue!!! */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object p(ContinuationImpl continuationImpl) throws Throwable {
        j jVar;
        Object objBoxInt;
        ?? r2;
        mg6 mg6Var;
        HomeChannelManager homeChannelManager;
        ?? r22;
        HomeChannelManager homeChannelManager2;
        Iterator it;
        ?? r8;
        ?? r23;
        ?? r24 = "Updating ";
        if (continuationImpl instanceof j) {
            jVar = (j) continuationImpl;
            int i = jVar.i;
            if ((i & Integer.MIN_VALUE) != 0) {
                jVar.i = i - Integer.MIN_VALUE;
            } else {
                jVar = new j(this, continuationImpl);
            }
        }
        Object objM = jVar.e;
        ck1 ck1Var = ck1.a;
        int i2 = jVar.i;
        ?? r82 = 1;
        try {
            try {
                try {
                } catch (Throwable th) {
                    th = th;
                    r24 = r82;
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (CancellationException e) {
            throw e;
        } catch (Exception e2) {
            e = e2;
        }
        if (i2 == 0) {
            s38.b(objM);
            jVar.a = this;
            ng6 ng6Var = this.f;
            jVar.b = ng6Var;
            jVar.i = 1;
            if (ng6Var.a(jVar) != ck1Var) {
                mg6Var = ng6Var;
                homeChannelManager = this;
            }
            return ck1Var;
        }
        if (i2 != 1) {
            if (i2 != 2) {
                if (i2 != 3) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                mg6 mg6Var2 = (mg6) jVar.a;
                s38.b(objM);
                r23 = mg6Var2;
                objBoxInt = e6a.a;
                r2 = r23;
                r2.c(null);
                return objBoxInt;
            }
            a aVar = jVar.d;
            it = jVar.c;
            r82 = jVar.b;
            homeChannelManager2 = (HomeChannelManager) jVar.a;
            try {
                s38.b(objM);
                r82 = r82;
            } catch (CancellationException e3) {
                throw e3;
            } catch (Exception e4) {
                e = e4;
                try {
                    Boxing.boxInt(Log.w("HomeChannelManager", "Failed to update channel " + aVar.a, e));
                    r8 = r82;
                    r22 = r8;
                    if (it.hasNext()) {
                    }
                    return ck1Var;
                } catch (CancellationException e5) {
                    throw e5;
                } catch (Exception e6) {
                    e = e6;
                    r24 = r82;
                    objBoxInt = Boxing.boxInt(Log.e("HomeChannelManager", "Failed to update channels", e));
                    r2 = r24;
                }
            }
            List list = (List) objM;
            String str = aVar.a;
            list.size();
            long j = homeChannelManager2.j(aVar);
            r8 = r82;
            if (j != -1) {
                homeChannelManager2.q(j, aVar, list);
                r8 = r82;
            }
            r22 = r8;
            if (it.hasNext()) {
                a aVar2 = (a) it.next();
                try {
                } catch (CancellationException e7) {
                    throw e7;
                } catch (Exception e8) {
                    e = e8;
                    r82 = r22;
                    aVar = aVar2;
                    Boxing.boxInt(Log.w("HomeChannelManager", "Failed to update channel " + aVar.a, e));
                    r8 = r82;
                    r22 = r8;
                    if (it.hasNext()) {
                    }
                    return ck1Var;
                }
                String str2 = aVar2.a;
                jVar.a = homeChannelManager2;
                jVar.b = r22;
                jVar.c = it;
                jVar.d = aVar2;
                jVar.i = 2;
                objM = homeChannelManager2.m(aVar2, jVar);
                if (objM != ck1Var) {
                    r82 = r22;
                    aVar = aVar2;
                    List list2 = (List) objM;
                    String str3 = aVar.a;
                    list2.size();
                    long j2 = homeChannelManager2.j(aVar);
                    r8 = r82;
                    if (j2 != -1) {
                    }
                    r22 = r8;
                    if (it.hasNext()) {
                        jVar.a = r22;
                        jVar.b = null;
                        jVar.c = null;
                        jVar.d = null;
                        jVar.i = 3;
                        r23 = r22;
                    }
                }
            }
            return ck1Var;
        }
        mg6Var = jVar.b;
        homeChannelManager = (HomeChannelManager) jVar.a;
        s38.b(objM);
        homeChannelManager.c();
        homeChannelManager.d();
        Collection collection = a.i;
        Log.w("HomeChannelManager", "Updating " + ((bd) collection).getD() + " channels...");
        r22 = mg6Var;
        homeChannelManager2 = homeChannelManager;
        it = ((cs) collection).iterator();
        if (it.hasNext()) {
        }
        return ck1Var;
    }

    /* JADX WARN: Removed duplicated region for block: B:22:0x00a2  */
    /* JADX WARN: Removed duplicated region for block: B:29:0x00b4  */
    /* JADX WARN: Removed duplicated region for block: B:40:0x00f0  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void q(long j, a aVar, List list) {
        Integer continueWatchingSeason;
        Integer continueWatchingEpisode;
        String showTitle;
        String backdropUrl;
        Double watchProgress;
        Context context = this.a;
        String str = null;
        context.getContentResolver().delete(zx9.d.a.buildUpon().appendQueryParameter("channel", String.valueOf(j)).build(), null, null);
        Iterator it = list.iterator();
        int i = 0;
        int i2 = 0;
        while (it.hasNext()) {
            int i3 = i + 1;
            ContentItem contentItem = (ContentItem) it.next();
            hh7.a aVar2 = new hh7.a();
            aVar2.a.put("channel_id", Long.valueOf(j));
            aVar2.a.put("weight", Integer.valueOf(20 - i));
            aVar2.a.put("internal_provider_id", n41.k("strmr_", contentItem.getTmdbId()));
            String title = contentItem.getTitle();
            if (title == null || n89.ab(title)) {
                title = str;
            }
            if (title == null) {
                title = "Untitled";
            }
            if (contentItem.isMovie()) {
                aVar2.c(0);
                aVar2.a.put("title", title);
            } else {
                aVar2.c(3);
                String showTitle2 = contentItem.getShowTitle();
                if (showTitle2 == null) {
                    showTitle2 = title;
                    aVar2.a.put("title", showTitle2);
                    if (aVar != a.c || aVar == a.d || aVar == a.e) {
                        continueWatchingSeason = contentItem.getContinueWatchingSeason();
                        continueWatchingEpisode = contentItem.getContinueWatchingEpisode();
                        if (continueWatchingSeason != null && continueWatchingSeason.intValue() > 0) {
                            aVar2.a.put("season_display_number", String.valueOf(continueWatchingSeason.intValue()));
                        }
                        if (continueWatchingEpisode != null && continueWatchingEpisode.intValue() > 0) {
                            aVar2.a.put("episode_display_number", String.valueOf(continueWatchingEpisode.intValue()));
                        }
                        showTitle = contentItem.getShowTitle();
                        if (showTitle != null) {
                            if (n89.ab(showTitle)) {
                                showTitle = str;
                            }
                            if (showTitle != null) {
                                aVar2.a.put("episode_title", title);
                            }
                        }
                    }
                } else {
                    if (n89.ab(showTitle2)) {
                        showTitle2 = str;
                    }
                    if (showTitle2 == null) {
                    }
                    aVar2.a.put("title", showTitle2);
                    if (aVar != a.c) {
                        continueWatchingSeason = contentItem.getContinueWatchingSeason();
                        continueWatchingEpisode = contentItem.getContinueWatchingEpisode();
                        if (continueWatchingSeason != null) {
                            aVar2.a.put("season_display_number", String.valueOf(continueWatchingSeason.intValue()));
                        }
                        if (continueWatchingEpisode != null) {
                            aVar2.a.put("episode_display_number", String.valueOf(continueWatchingEpisode.intValue()));
                        }
                        showTitle = contentItem.getShowTitle();
                        if (showTitle != null) {
                        }
                    }
                }
            }
            String overview = contentItem.getOverview();
            if (overview != null) {
                aVar2.a.put("short_description", overview);
            }
            if (!contentItem.isTvShow() || (backdropUrl = contentItem.getEpisodeStillUrl()) == null) {
                backdropUrl = contentItem.getBackdropUrl();
            }
            Uri uriF = f(Integer.valueOf(contentItem.getTmdbId()), backdropUrl, contentItem.getLogoUrl());
            if (uriF != null) {
                aVar2.a(uriF);
            } else {
                if (backdropUrl == null) {
                    backdropUrl = contentItem.getPosterUrl();
                }
                if (backdropUrl != null) {
                    aVar2.a(Uri.parse(backdropUrl));
                }
            }
            if ((aVar == a.c || aVar == a.d || aVar == a.e) && (watchProgress = contentItem.getWatchProgress()) != null && watchProgress.doubleValue() > 0.0d) {
                double dA = zr7.a(watchProgress.doubleValue());
                aVar2.a.put("duration_millis", Integer.valueOf(AacUtil.AAC_LC_MAX_RATE_BYTES_PER_SECOND));
                aVar2.a.put("last_playback_position_millis", Integer.valueOf((int) (dA * ((double) AacUtil.AAC_LC_MAX_RATE_BYTES_PER_SECOND))));
            }
            Uri uriB = b(contentItem);
            if (uriB != null) {
                aVar2.b(uriB);
                aVar2.b(Uri.parse(new Intent("android.intent.action.VIEW", uriB).toUri(1)));
            }
            try {
                ContentResolver contentResolver = context.getContentResolver();
                Uri uri = zx9.d.a;
                ContentValues contentValues = new ContentValues(aVar2.a);
                contentValues.remove("browsable");
                if (contentResolver.insert(uri, contentValues) != null) {
                    i2++;
                }
            } catch (Exception e) {
                Log.w("HomeChannelManager", "Failed to insert program: tmdbId=" + contentItem.getTmdbId(), e);
            }
            i = i3;
            str = null;
        }
        Log.w("HomeChannelManager", aVar.a + ": " + i2 + DomExceptionUtils.SEPARATOR + list.size() + " programs");
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object r(ContinuationImpl continuationImpl) {
        k kVar;
        HomeChannelManager homeChannelManager;
        String imdbId;
        if (continuationImpl instanceof k) {
            kVar = (k) continuationImpl;
            int i = kVar.d;
            if ((i & Integer.MIN_VALUE) != 0) {
                kVar.d = i - Integer.MIN_VALUE;
            } else {
                kVar = new k(this, continuationImpl);
            }
        }
        Object objLoad = kVar.b;
        ck1 ck1Var = ck1.a;
        int i2 = kVar.d;
        int i3 = 0;
        try {
            if (i2 == 0) {
                s38.b(objLoad);
                ContinueWatchingRepository continueWatchingRepository = this.b;
                kVar.a = this;
                kVar.d = 1;
                objLoad = continueWatchingRepository.load(false, kVar);
                if (objLoad == ck1Var) {
                    return ck1Var;
                }
                homeChannelManager = this;
            } else {
                if (i2 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                homeChannelManager = kVar.a;
                s38.b(objLoad);
            }
            ArrayList arrayList = new ArrayList();
            for (Object obj : (Iterable) objLoad) {
                ContentItem contentItem = (ContentItem) obj;
                if (contentItem.isContent() && (imdbId = contentItem.getImdbId()) != null && !n89.ab(imdbId)) {
                    arrayList.add(obj);
                }
            }
            List listBk = y41.bk(arrayList, 20);
            homeChannelManager.n();
            Iterator it = listBk.iterator();
            while (it.hasNext()) {
                if (homeChannelManager.l((ContentItem) it.next())) {
                    i3++;
                }
            }
            Log.w("HomeChannelManager", "Watch Next: " + i3 + DomExceptionUtils.SEPARATOR + listBk.size() + " programs");
        } catch (CancellationException e) {
            throw e;
        } catch (Exception e2) {
            Log.e("HomeChannelManager", "Failed to update Watch Next row", e2);
        }
        return e6a.a;
    }
}
