package X6FRs_4.KQgRr5e_0.LGe8pk_15;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.database.DataSetObservable;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import android.util.Xml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
/* compiled from: ActivityChooserModel.java */
/* loaded from: classes.dex */
public class PGPF6hQ_2 extends DataSetObservable {
    public static final String QxJ9d_1 = PGPF6hQ_2.class.getSimpleName();
    public int KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public Mqv2r_3 f737KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public QxJ9d_1 f738KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final Context f739KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public Intent f740KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final Object f741KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final String f742KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public final List<KQgRr5e_0> f743KQgRr5e_0;

    /* renamed from: KQgRr5e_0  reason: collision with other field name */
    public boolean f744KQgRr5e_0;

    /* renamed from: Mqv2r_3  reason: collision with root package name */
    public boolean f3167Mqv2r_3;
    public boolean PGPF6hQ_2;

    /* renamed from: QxJ9d_1  reason: collision with other field name */
    public final List<C0057PGPF6hQ_2> f745QxJ9d_1;

    /* renamed from: QxJ9d_1  reason: collision with other field name */
    public boolean f746QxJ9d_1;

    /* compiled from: ActivityChooserModel.java */
    /* loaded from: classes.dex */
    public static final class KQgRr5e_0 implements Comparable<KQgRr5e_0> {
        public float KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final ResolveInfo f747KQgRr5e_0;

        public KQgRr5e_0(ResolveInfo resolveInfo) {
            this.f747KQgRr5e_0 = resolveInfo;
        }

        @Override // java.lang.Comparable
        /* renamed from: KQgRr5e_0 */
        public int compareTo(KQgRr5e_0 kQgRr5e_0) {
            return Float.floatToIntBits(kQgRr5e_0.KQgRr5e_0) - Float.floatToIntBits(this.KQgRr5e_0);
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            return obj != null && KQgRr5e_0.class == obj.getClass() && Float.floatToIntBits(this.KQgRr5e_0) == Float.floatToIntBits(((KQgRr5e_0) obj).KQgRr5e_0);
        }

        public int hashCode() {
            return Float.floatToIntBits(this.KQgRr5e_0) + 31;
        }

        public String toString() {
            return "[resolveInfo:" + this.f747KQgRr5e_0.toString() + "; weight:" + new BigDecimal(this.KQgRr5e_0) + "]";
        }
    }

    /* compiled from: ActivityChooserModel.java */
    /* loaded from: classes.dex */
    public interface Mqv2r_3 {
        boolean KQgRr5e_0(PGPF6hQ_2 pGPF6hQ_2, Intent intent);
    }

    /* compiled from: ActivityChooserModel.java */
    /* renamed from: X6FRs_4.KQgRr5e_0.LGe8pk_15.PGPF6hQ_2$PGPF6hQ_2  reason: collision with other inner class name */
    /* loaded from: classes.dex */
    public static final class C0057PGPF6hQ_2 {
        public final float KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final long f748KQgRr5e_0;

        /* renamed from: KQgRr5e_0  reason: collision with other field name */
        public final ComponentName f749KQgRr5e_0;

        public C0057PGPF6hQ_2(String str, long j, float f) {
            this(ComponentName.unflattenFromString(str), j, f);
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj != null && C0057PGPF6hQ_2.class == obj.getClass()) {
                C0057PGPF6hQ_2 c0057PGPF6hQ_2 = (C0057PGPF6hQ_2) obj;
                ComponentName componentName = this.f749KQgRr5e_0;
                if (componentName == null) {
                    if (c0057PGPF6hQ_2.f749KQgRr5e_0 != null) {
                        return false;
                    }
                } else if (!componentName.equals(c0057PGPF6hQ_2.f749KQgRr5e_0)) {
                    return false;
                }
                return this.f748KQgRr5e_0 == c0057PGPF6hQ_2.f748KQgRr5e_0 && Float.floatToIntBits(this.KQgRr5e_0) == Float.floatToIntBits(c0057PGPF6hQ_2.KQgRr5e_0);
            }
            return false;
        }

        public int hashCode() {
            ComponentName componentName = this.f749KQgRr5e_0;
            int hashCode = componentName == null ? 0 : componentName.hashCode();
            long j = this.f748KQgRr5e_0;
            return ((((hashCode + 31) * 31) + ((int) (j ^ (j >>> 32)))) * 31) + Float.floatToIntBits(this.KQgRr5e_0);
        }

        public String toString() {
            return "[; activity:" + this.f749KQgRr5e_0 + "; time:" + this.f748KQgRr5e_0 + "; weight:" + new BigDecimal(this.KQgRr5e_0) + "]";
        }

        public C0057PGPF6hQ_2(ComponentName componentName, long j, float f) {
            this.f749KQgRr5e_0 = componentName;
            this.f748KQgRr5e_0 = j;
            this.KQgRr5e_0 = f;
        }
    }

    /* compiled from: ActivityChooserModel.java */
    /* loaded from: classes.dex */
    public interface QxJ9d_1 {
        void KQgRr5e_0(Intent intent, List<KQgRr5e_0> list, List<C0057PGPF6hQ_2> list2);
    }

    /* compiled from: ActivityChooserModel.java */
    /* loaded from: classes.dex */
    public final class X6FRs_4 extends AsyncTask<Object, Void, Void> {
        public X6FRs_4() {
        }

        /* JADX WARN: Code restructure failed: missing block: B:10:0x006d, code lost:
            if (r15 != null) goto L15;
         */
        /* JADX WARN: Code restructure failed: missing block: B:11:0x006f, code lost:
            r15.close();
         */
        /* JADX WARN: Code restructure failed: missing block: B:18:0x0092, code lost:
            if (r15 == null) goto L13;
         */
        /* JADX WARN: Code restructure failed: missing block: B:23:0x00b2, code lost:
            if (r15 == null) goto L13;
         */
        /* JADX WARN: Code restructure failed: missing block: B:28:0x00d2, code lost:
            if (r15 == null) goto L13;
         */
        /* JADX WARN: Code restructure failed: missing block: B:30:0x00d5, code lost:
            return null;
         */
        @Override // android.os.AsyncTask
        /* renamed from: KQgRr5e_0 */
        /*
            Code decompiled incorrectly, please refer to instructions dump.
        */
        public Void doInBackground(Object... objArr) {
            List list = (List) objArr[0];
            String str = (String) objArr[1];
            try {
                FileOutputStream openFileOutput = PGPF6hQ_2.this.f739KQgRr5e_0.openFileOutput(str, 0);
                XmlSerializer newSerializer = Xml.newSerializer();
                try {
                    try {
                        try {
                            newSerializer.setOutput(openFileOutput, null);
                            newSerializer.startDocument("UTF-8", true);
                            newSerializer.startTag(null, "historical-records");
                            int size = list.size();
                            for (int i = 0; i < size; i++) {
                                C0057PGPF6hQ_2 c0057PGPF6hQ_2 = (C0057PGPF6hQ_2) list.remove(0);
                                newSerializer.startTag(null, "historical-record");
                                newSerializer.attribute(null, "activity", c0057PGPF6hQ_2.f749KQgRr5e_0.flattenToString());
                                newSerializer.attribute(null, "time", String.valueOf(c0057PGPF6hQ_2.f748KQgRr5e_0));
                                newSerializer.attribute(null, "weight", String.valueOf(c0057PGPF6hQ_2.KQgRr5e_0));
                                newSerializer.endTag(null, "historical-record");
                            }
                            newSerializer.endTag(null, "historical-records");
                            newSerializer.endDocument();
                            PGPF6hQ_2.this.f744KQgRr5e_0 = true;
                        } catch (Throwable th) {
                            PGPF6hQ_2.this.f744KQgRr5e_0 = true;
                            if (openFileOutput != null) {
                                try {
                                    openFileOutput.close();
                                } catch (IOException unused) {
                                }
                            }
                            throw th;
                        }
                    } catch (IllegalStateException e) {
                        String str2 = PGPF6hQ_2.QxJ9d_1;
                        Log.e(str2, "Error writing historical record file: " + PGPF6hQ_2.this.f742KQgRr5e_0, e);
                        PGPF6hQ_2.this.f744KQgRr5e_0 = true;
                    }
                } catch (IOException e2) {
                    String str3 = PGPF6hQ_2.QxJ9d_1;
                    Log.e(str3, "Error writing historical record file: " + PGPF6hQ_2.this.f742KQgRr5e_0, e2);
                    PGPF6hQ_2.this.f744KQgRr5e_0 = true;
                } catch (IllegalArgumentException e3) {
                    String str4 = PGPF6hQ_2.QxJ9d_1;
                    Log.e(str4, "Error writing historical record file: " + PGPF6hQ_2.this.f742KQgRr5e_0, e3);
                    PGPF6hQ_2.this.f744KQgRr5e_0 = true;
                }
            } catch (FileNotFoundException e4) {
                String str5 = PGPF6hQ_2.QxJ9d_1;
                Log.e(str5, "Error writing historical record file: " + str, e4);
                return null;
            }
        }
    }

    static {
        new HashMap();
    }

    public int KQgRr5e_0() {
        int size;
        synchronized (this.f741KQgRr5e_0) {
            m423KQgRr5e_0();
            size = this.f743KQgRr5e_0.size();
        }
        return size;
    }

    public final void Mqv2r_3() {
        XmlPullParser newPullParser;
        try {
            FileInputStream openFileInput = this.f739KQgRr5e_0.openFileInput(this.f742KQgRr5e_0);
            try {
                try {
                    try {
                        newPullParser = Xml.newPullParser();
                        newPullParser.setInput(openFileInput, "UTF-8");
                        for (int i = 0; i != 1 && i != 2; i = newPullParser.next()) {
                        }
                    } catch (IOException e) {
                        String str = QxJ9d_1;
                        Log.e(str, "Error reading historical recrod file: " + this.f742KQgRr5e_0, e);
                        if (openFileInput == null) {
                            return;
                        }
                    }
                } catch (XmlPullParserException e2) {
                    String str2 = QxJ9d_1;
                    Log.e(str2, "Error reading historical recrod file: " + this.f742KQgRr5e_0, e2);
                    if (openFileInput == null) {
                        return;
                    }
                }
                if ("historical-records".equals(newPullParser.getName())) {
                    List<C0057PGPF6hQ_2> list = this.f745QxJ9d_1;
                    list.clear();
                    while (true) {
                        int next = newPullParser.next();
                        if (next == 1) {
                            if (openFileInput == null) {
                                return;
                            }
                        } else if (next != 3 && next != 4) {
                            if ("historical-record".equals(newPullParser.getName())) {
                                list.add(new C0057PGPF6hQ_2(newPullParser.getAttributeValue(null, "activity"), Long.parseLong(newPullParser.getAttributeValue(null, "time")), Float.parseFloat(newPullParser.getAttributeValue(null, "weight"))));
                            } else {
                                throw new XmlPullParserException("Share records file not well-formed.");
                            }
                        }
                    }
                    try {
                        openFileInput.close();
                    } catch (IOException unused) {
                    }
                } else {
                    throw new XmlPullParserException("Share records file does not start with historical-records tag.");
                }
            } catch (Throwable th) {
                if (openFileInput != null) {
                    try {
                        openFileInput.close();
                    } catch (IOException unused2) {
                    }
                }
                throw th;
            }
        } catch (FileNotFoundException unused3) {
        }
    }

    /* renamed from: PGPF6hQ_2  reason: collision with other method in class */
    public final boolean m426PGPF6hQ_2() {
        if (this.f738KQgRr5e_0 == null || this.f740KQgRr5e_0 == null || this.f743KQgRr5e_0.isEmpty() || this.f745QxJ9d_1.isEmpty()) {
            return false;
        }
        this.f738KQgRr5e_0.KQgRr5e_0(this.f740KQgRr5e_0, this.f743KQgRr5e_0, Collections.unmodifiableList(this.f745QxJ9d_1));
        return true;
    }

    /* renamed from: QxJ9d_1  reason: collision with other method in class */
    public final void m427QxJ9d_1() {
        if (this.f746QxJ9d_1) {
            if (this.PGPF6hQ_2) {
                this.PGPF6hQ_2 = false;
                if (TextUtils.isEmpty(this.f742KQgRr5e_0)) {
                    return;
                }
                new X6FRs_4().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new ArrayList(this.f745QxJ9d_1), this.f742KQgRr5e_0);
                return;
            }
            return;
        }
        throw new IllegalStateException("No preceding call to #readHistoricalData");
    }

    /* renamed from: KQgRr5e_0  reason: collision with other method in class */
    public ResolveInfo m422KQgRr5e_0(int i) {
        ResolveInfo resolveInfo;
        synchronized (this.f741KQgRr5e_0) {
            m423KQgRr5e_0();
            resolveInfo = this.f743KQgRr5e_0.get(i).f747KQgRr5e_0;
        }
        return resolveInfo;
    }

    public final void PGPF6hQ_2() {
        int size = this.f745QxJ9d_1.size() - this.KQgRr5e_0;
        if (size <= 0) {
            return;
        }
        this.PGPF6hQ_2 = true;
        for (int i = 0; i < size; i++) {
            this.f745QxJ9d_1.remove(0);
        }
    }

    public int QxJ9d_1() {
        int size;
        synchronized (this.f741KQgRr5e_0) {
            m423KQgRr5e_0();
            size = this.f745QxJ9d_1.size();
        }
        return size;
    }

    public int KQgRr5e_0(ResolveInfo resolveInfo) {
        synchronized (this.f741KQgRr5e_0) {
            m423KQgRr5e_0();
            List<KQgRr5e_0> list = this.f743KQgRr5e_0;
            int size = list.size();
            for (int i = 0; i < size; i++) {
                if (list.get(i).f747KQgRr5e_0 == resolveInfo) {
                    return i;
                }
            }
            return -1;
        }
    }

    /* renamed from: QxJ9d_1  reason: collision with other method in class */
    public final boolean m428QxJ9d_1() {
        if (this.f744KQgRr5e_0 && this.PGPF6hQ_2 && !TextUtils.isEmpty(this.f742KQgRr5e_0)) {
            this.f744KQgRr5e_0 = false;
            this.f746QxJ9d_1 = true;
            Mqv2r_3();
            return true;
        }
        return false;
    }

    public Intent KQgRr5e_0(int i) {
        synchronized (this.f741KQgRr5e_0) {
            if (this.f740KQgRr5e_0 == null) {
                return null;
            }
            m423KQgRr5e_0();
            KQgRr5e_0 kQgRr5e_0 = this.f743KQgRr5e_0.get(i);
            ComponentName componentName = new ComponentName(kQgRr5e_0.f747KQgRr5e_0.activityInfo.packageName, kQgRr5e_0.f747KQgRr5e_0.activityInfo.name);
            Intent intent = new Intent(this.f740KQgRr5e_0);
            intent.setComponent(componentName);
            if (this.f737KQgRr5e_0 != null) {
                if (this.f737KQgRr5e_0.KQgRr5e_0(this, new Intent(intent))) {
                    return null;
                }
            }
            KQgRr5e_0(new C0057PGPF6hQ_2(componentName, System.currentTimeMillis(), 1.0f));
            return intent;
        }
    }

    /* renamed from: KQgRr5e_0  reason: collision with other method in class */
    public ResolveInfo m421KQgRr5e_0() {
        synchronized (this.f741KQgRr5e_0) {
            m423KQgRr5e_0();
            if (this.f743KQgRr5e_0.isEmpty()) {
                return null;
            }
            return this.f743KQgRr5e_0.get(0).f747KQgRr5e_0;
        }
    }

    /* renamed from: KQgRr5e_0  reason: collision with other method in class */
    public void m424KQgRr5e_0(int i) {
        synchronized (this.f741KQgRr5e_0) {
            m423KQgRr5e_0();
            KQgRr5e_0 kQgRr5e_0 = this.f743KQgRr5e_0.get(i);
            KQgRr5e_0 kQgRr5e_02 = this.f743KQgRr5e_0.get(0);
            KQgRr5e_0(new C0057PGPF6hQ_2(new ComponentName(kQgRr5e_0.f747KQgRr5e_0.activityInfo.packageName, kQgRr5e_0.f747KQgRr5e_0.activityInfo.name), System.currentTimeMillis(), kQgRr5e_02 != null ? (kQgRr5e_02.KQgRr5e_0 - kQgRr5e_0.KQgRr5e_0) + 5.0f : 1.0f));
        }
    }

    /* renamed from: KQgRr5e_0  reason: collision with other method in class */
    public final void m423KQgRr5e_0() {
        boolean m425KQgRr5e_0 = m425KQgRr5e_0() | m428QxJ9d_1();
        PGPF6hQ_2();
        if (m425KQgRr5e_0) {
            m426PGPF6hQ_2();
            notifyChanged();
        }
    }

    /* renamed from: KQgRr5e_0  reason: collision with other method in class */
    public final boolean m425KQgRr5e_0() {
        if (!this.f3167Mqv2r_3 || this.f740KQgRr5e_0 == null) {
            return false;
        }
        this.f3167Mqv2r_3 = false;
        this.f743KQgRr5e_0.clear();
        List<ResolveInfo> queryIntentActivities = this.f739KQgRr5e_0.getPackageManager().queryIntentActivities(this.f740KQgRr5e_0, 0);
        int size = queryIntentActivities.size();
        for (int i = 0; i < size; i++) {
            this.f743KQgRr5e_0.add(new KQgRr5e_0(queryIntentActivities.get(i)));
        }
        return true;
    }

    public final boolean KQgRr5e_0(C0057PGPF6hQ_2 c0057PGPF6hQ_2) {
        boolean add = this.f745QxJ9d_1.add(c0057PGPF6hQ_2);
        if (add) {
            this.PGPF6hQ_2 = true;
            PGPF6hQ_2();
            m427QxJ9d_1();
            m426PGPF6hQ_2();
            notifyChanged();
        }
        return add;
    }
}
