package ῲ.ῲ.㴹;

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.HashMap;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
import ඓ.K.K.K.C1293;

/* renamed from: ῲ.ῲ.㴹.ʬ  reason: contains not printable characters */
/* compiled from: ActivityChooserModel */
public class C1984 extends DataSetObservable {

    /* renamed from: 㑃  reason: contains not printable characters */
    public static final String f6734 = C1984.class.getSimpleName();

    /* renamed from: ʬ  reason: contains not printable characters */
    public final List<C1985> f6735;

    /* renamed from: ඓ  reason: contains not printable characters */
    public final String f6736;

    /* renamed from: ᕣ  reason: contains not printable characters */
    public boolean f6737;

    /* renamed from: ᨨ  reason: contains not printable characters */
    public boolean f6738;

    /* renamed from: ῲ  reason: contains not printable characters */
    public final List<C1987> f6739;

    /* renamed from: ⁱ  reason: contains not printable characters */
    public int f6740;

    /* renamed from: K  reason: contains not printable characters */
    public final Object f6741;

    /* renamed from: 㳳  reason: contains not printable characters */
    public boolean f6742;

    /* renamed from: 㴹  reason: contains not printable characters */
    public Intent f6743;

    /* renamed from: 㸾  reason: contains not printable characters */
    public final Context f6744;

    /* renamed from: 㿎  reason: contains not printable characters */
    public boolean f6745;

    /* renamed from: ῲ.ῲ.㴹.ʬ$ʬ  reason: contains not printable characters */
    /* compiled from: ActivityChooserModel */
    public static final class C1985 {

        /* renamed from: ʬ  reason: contains not printable characters */
        public final float f6746;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final long f6747;

        /* renamed from: K  reason: contains not printable characters */
        public final ComponentName f6748;

        public C1985(ComponentName componentName, long j, float f) {
            this.f6748 = componentName;
            this.f6747 = j;
            this.f6746 = f;
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null || C1985.class != obj.getClass()) {
                return false;
            }
            C1985 r8 = (C1985) obj;
            ComponentName componentName = this.f6748;
            if (componentName == null) {
                if (r8.f6748 != null) {
                    return false;
                }
            } else if (!componentName.equals(r8.f6748)) {
                return false;
            }
            return this.f6747 == r8.f6747 && Float.floatToIntBits(this.f6746) == Float.floatToIntBits(r8.f6746);
        }

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

        public String toString() {
            StringBuilder r0 = C1293.m3402("[", "; activity:");
            r0.append(this.f6748);
            r0.append("; time:");
            r0.append(this.f6747);
            r0.append("; weight:");
            r0.append(new BigDecimal((double) this.f6746));
            r0.append("]");
            return r0.toString();
        }
    }

    /* renamed from: ῲ.ῲ.㴹.ʬ$ඓ  reason: contains not printable characters */
    /* compiled from: ActivityChooserModel */
    public final class AsyncTaskC1986 extends AsyncTask<Object, Void, Void> {
        public AsyncTaskC1986() {
        }

        /* Return type fixed from 'java.lang.Object' to match base method */
        /* JADX WARNING: Code restructure failed: missing block: B:10:0x006d, code lost:
            if (r15 != null) goto L_0x00d1;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:16:0x008f, code lost:
            if (r15 == null) goto L_0x00f4;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:19:0x00af, code lost:
            if (r15 == null) goto L_0x00f4;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:22:0x00cf, code lost:
            if (r15 == null) goto L_0x00f4;
         */
        /* JADX WARNING: Code restructure failed: missing block: B:24:?, code lost:
            r15.close();
         */
        @Override // android.os.AsyncTask
        public Void doInBackground(Object[] objArr) {
            List list = (List) objArr[0];
            String str = (String) objArr[1];
            try {
                FileOutputStream openFileOutput = C1984.this.f6744.openFileOutput(str, 0);
                XmlSerializer newSerializer = Xml.newSerializer();
                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++) {
                        C1985 r10 = (C1985) list.remove(0);
                        newSerializer.startTag(null, "historical-record");
                        newSerializer.attribute(null, "activity", r10.f6748.flattenToString());
                        newSerializer.attribute(null, "time", String.valueOf(r10.f6747));
                        newSerializer.attribute(null, "weight", String.valueOf(r10.f6746));
                        newSerializer.endTag(null, "historical-record");
                    }
                    newSerializer.endTag(null, "historical-records");
                    newSerializer.endDocument();
                    C1984.this.f6745 = true;
                } catch (IllegalArgumentException e) {
                    String str2 = C1984.f6734;
                    Log.e(str2, "Error writing historical record file: " + C1984.this.f6736, e);
                    C1984.this.f6745 = true;
                } catch (IllegalStateException e2) {
                    String str3 = C1984.f6734;
                    Log.e(str3, "Error writing historical record file: " + C1984.this.f6736, e2);
                    C1984.this.f6745 = true;
                } catch (IOException e3) {
                    String str4 = C1984.f6734;
                    Log.e(str4, "Error writing historical record file: " + C1984.this.f6736, e3);
                    C1984.this.f6745 = true;
                } catch (Throwable th) {
                    C1984.this.f6745 = true;
                    if (openFileOutput != null) {
                        try {
                            openFileOutput.close();
                        } catch (IOException unused) {
                        }
                    }
                    throw th;
                }
            } catch (FileNotFoundException e4) {
                String str5 = C1984.f6734;
                Log.e(str5, "Error writing historical record file: " + str, e4);
            }
            return null;
        }
    }

    /* renamed from: ῲ.ῲ.㴹.ʬ$ῲ  reason: contains not printable characters */
    /* compiled from: ActivityChooserModel */
    public static final class C1987 implements Comparable<C1987> {

        /* renamed from: ʬ  reason: contains not printable characters */
        public float f6750;

        /* renamed from: ῲ  reason: contains not printable characters */
        public final ResolveInfo f6751;

        public C1987(ResolveInfo resolveInfo) {
            this.f6751 = resolveInfo;
        }

        /* JADX DEBUG: Method arguments types fixed to match base method, original types: [java.lang.Object] */
        @Override // java.lang.Comparable
        public int compareTo(C1987 r2) {
            return Float.floatToIntBits(r2.f6750) - Float.floatToIntBits(this.f6750);
        }

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

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

        public String toString() {
            StringBuilder r0 = C1293.m3402("[", "resolveInfo:");
            r0.append(this.f6751.toString());
            r0.append("; weight:");
            r0.append(new BigDecimal((double) this.f6750));
            r0.append("]");
            return r0.toString();
        }
    }

    /* renamed from: ῲ.ῲ.㴹.ʬ$K  reason: contains not printable characters */
    /* compiled from: ActivityChooserModel */
    public interface AbstractC1988 {
    }

    /* renamed from: ῲ.ῲ.㴹.ʬ$㸾  reason: contains not printable characters */
    /* compiled from: ActivityChooserModel */
    public interface AbstractC1989 {
    }

    static {
        new HashMap();
    }

    public void setOnChooseActivityListener(AbstractC1989 r2) {
        synchronized (this.f6741) {
        }
    }

    /* renamed from: ʬ  reason: contains not printable characters */
    public ResolveInfo m5136() {
        synchronized (this.f6741) {
            m5143();
            if (this.f6739.isEmpty()) {
                return null;
            }
            return this.f6739.get(0).f6751;
        }
    }

    /* renamed from: ඓ  reason: contains not printable characters */
    public final void m5138() {
        int size = this.f6735.size() - this.f6740;
        if (size > 0) {
            this.f6742 = true;
            for (int i = 0; i < size; i++) {
                this.f6735.remove(0);
            }
        }
    }

    /* renamed from: ῲ  reason: contains not printable characters */
    public int m5139() {
        int size;
        synchronized (this.f6741) {
            m5143();
            size = this.f6739.size();
        }
        return size;
    }

    /* renamed from: K  reason: contains not printable characters */
    public int m5141(ResolveInfo resolveInfo) {
        synchronized (this.f6741) {
            m5143();
            List<C1987> list = this.f6739;
            int size = list.size();
            for (int i = 0; i < size; i++) {
                if (list.get(i).f6751 == resolveInfo) {
                    return i;
                }
            }
            return -1;
        }
    }

    /* renamed from: 㸾  reason: contains not printable characters */
    public int m5145() {
        int size;
        synchronized (this.f6741) {
            m5143();
            size = this.f6735.size();
        }
        return size;
    }

    /* renamed from: ῲ  reason: contains not printable characters */
    public ResolveInfo m5140(int i) {
        ResolveInfo resolveInfo;
        synchronized (this.f6741) {
            m5143();
            resolveInfo = this.f6739.get(i).f6751;
        }
        return resolveInfo;
    }

    /* renamed from: ʬ  reason: contains not printable characters */
    public void m5137(int i) {
        synchronized (this.f6741) {
            m5143();
            C1987 r6 = this.f6739.get(i);
            C1987 r1 = this.f6739.get(0);
            m5144(new C1985(new ComponentName(r6.f6751.activityInfo.packageName, r6.f6751.activityInfo.name), System.currentTimeMillis(), r1 != null ? (r1.f6750 - r6.f6750) + 5.0f : 1.0f));
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public Intent m5142(int i) {
        synchronized (this.f6741) {
            if (this.f6743 == null) {
                return null;
            }
            m5143();
            C1987 r7 = this.f6739.get(i);
            ComponentName componentName = new ComponentName(r7.f6751.activityInfo.packageName, r7.f6751.activityInfo.name);
            Intent intent = new Intent(this.f6743);
            intent.setComponent(componentName);
            m5144(new C1985(componentName, System.currentTimeMillis(), 1.0f));
            return intent;
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:51:0x0104, code lost:
        if (r4 == null) goto L_0x0110;
     */
    /* renamed from: K  reason: contains not printable characters */
    public final void m5143() {
        int i;
        int i2 = 0;
        if (!this.f6737 || this.f6743 == null) {
            i = 0;
        } else {
            this.f6737 = false;
            this.f6739.clear();
            List<ResolveInfo> queryIntentActivities = this.f6744.getPackageManager().queryIntentActivities(this.f6743, 0);
            int size = queryIntentActivities.size();
            for (int i3 = 0; i3 < size; i3++) {
                this.f6739.add(new C1987(queryIntentActivities.get(i3)));
            }
            i = 1;
        }
        if (this.f6745 && this.f6742 && !TextUtils.isEmpty(this.f6736)) {
            this.f6745 = false;
            this.f6738 = true;
            try {
                FileInputStream openFileInput = this.f6744.openFileInput(this.f6736);
                try {
                    XmlPullParser newPullParser = Xml.newPullParser();
                    newPullParser.setInput(openFileInput, "UTF-8");
                    while (i2 != 1 && i2 != 2) {
                        i2 = newPullParser.next();
                    }
                    if ("historical-records".equals(newPullParser.getName())) {
                        List<C1985> list = this.f6735;
                        list.clear();
                        while (true) {
                            int next = newPullParser.next();
                            if (next == 1) {
                                if (openFileInput != null) {
                                }
                            } else if (next != 3) {
                                if (next != 4) {
                                    if ("historical-record".equals(newPullParser.getName())) {
                                        String attributeValue = newPullParser.getAttributeValue(null, "activity");
                                        list.add(new C1985(ComponentName.unflattenFromString(attributeValue), Long.parseLong(newPullParser.getAttributeValue(null, "time")), Float.parseFloat(newPullParser.getAttributeValue(null, "weight"))));
                                    } else {
                                        throw new XmlPullParserException("Share records file not well-formed.");
                                    }
                                }
                            }
                        }
                        i2 = 1;
                    } else {
                        throw new XmlPullParserException("Share records file does not start with historical-records tag.");
                    }
                } catch (XmlPullParserException e) {
                    String str = f6734;
                    Log.e(str, "Error reading historical recrod file: " + this.f6736, e);
                } catch (IOException e2) {
                    String str2 = f6734;
                    Log.e(str2, "Error reading historical recrod file: " + this.f6736, e2);
                    if (openFileInput != null) {
                        openFileInput.close();
                    }
                } catch (Throwable th) {
                    if (openFileInput != null) {
                        try {
                            openFileInput.close();
                        } catch (IOException unused) {
                        }
                    }
                    throw th;
                }
            } catch (FileNotFoundException unused2) {
            }
        }
        int i4 = i | i2;
        m5138();
        if (i4 != 0) {
            notifyChanged();
        }
    }

    /* renamed from: K  reason: contains not printable characters */
    public final boolean m5144(C1985 r8) {
        boolean add = this.f6735.add(r8);
        if (add) {
            this.f6742 = true;
            m5138();
            if (this.f6738) {
                if (this.f6742) {
                    this.f6742 = false;
                    if (!TextUtils.isEmpty(this.f6736)) {
                        new AsyncTaskC1986().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new ArrayList(this.f6735), this.f6736);
                    }
                }
                notifyChanged();
            } else {
                throw new IllegalStateException("No preceding call to #readHistoricalData");
            }
        }
        return add;
    }
}
