package com.flurry.android;

import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.SystemClock;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import com.flurry.org.apache.avro.io.BinaryDecoder;
import com.flurry.org.apache.avro.io.BinaryEncoder;
import com.flurry.org.apache.avro.io.DecoderFactory;
import com.flurry.org.apache.avro.io.EncoderFactory;
import com.flurry.org.apache.avro.specific.SpecificDatumReader;
import com.flurry.org.apache.avro.specific.SpecificDatumWriter;
import com.flurry.org.apache.avro.specific.SpecificRecordBase;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: /storage/emulated/0/Documents/jadec/sources/com.UBI.A90.WW/dex-files/0.dex */
public final class be implements ai {
    static String a = "FlurryAgent";
    static int b = 5;
    private static List<Integer> e;
    private static DecoderFactory f;
    private static v g;
    private long A;
    private long B;
    private long C;
    private Map<Integer, ByteBuffer> D;
    private String E;
    private Handler F;
    private ICustomAdNetworkHandler G;
    private FlurryAdListener H;
    private volatile boolean I;
    private Map<String, String> O;
    private AdUnit P;
    private final Map<String, String> Q;
    private final Set<String> R;
    w c;
    bd d;
    private ax h;
    private r i;
    private Display j;
    private String m;
    private String n;
    private String o;
    private String p;
    private volatile float u;
    private volatile float v;
    private volatile Map<String, String> w;
    private bb x;
    private AdUnit y;
    private String z;
    private boolean k = false;
    private File l = null;
    private boolean q = true;
    private boolean r = false;
    private volatile String s = null;
    private volatile String t = null;
    private volatile List<bb> J = new CopyOnWriteArrayList();
    private volatile Map<String, bb> K = new HashMap();
    private volatile List<bb> L = new ArrayList();
    private volatile List<String> M = new ArrayList();
    private volatile boolean N = false;

    /* JADX INFO: Access modifiers changed from: package-private */
    public be() {
        HashMap hashMap = new HashMap();
        hashMap.put("open", "directOpen");
        hashMap.put("expand", "doExpand");
        hashMap.put("collapse", "doCollapse");
        this.Q = Collections.unmodifiableMap(hashMap);
        HashSet hashSet = new HashSet();
        hashSet.addAll(Arrays.asList("closeAd", "processRedirect", "nextFrame", "nextAdUnit", "notifyUser"));
        this.R = Collections.unmodifiableSet(hashSet);
        HandlerThread handlerThread = new HandlerThread("FlurryAdThread");
        handlerThread.start();
        this.F = new Handler(handlerThread.getLooper());
        this.c = new w();
        e = Arrays.asList(0, 1, 2, 3, 4, 5);
        f = new DecoderFactory();
        g = new v(this);
        this.i = new r(this);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(Context context, n nVar) {
        if (!this.I) {
            this.z = nVar.a;
            this.d = new bd(context);
            this.I = true;
        }
        this.h = nVar.b;
        this.m = nVar.d;
        this.n = nVar.e;
        this.o = Build.VERSION.RELEASE;
        this.p = Build.DEVICE;
        this.j = ((WindowManager) context.getSystemService("window")).getDefaultDisplay();
        this.l = context.getFileStreamPath(".flurryadlog." + Integer.toString(this.z.hashCode(), 16));
        this.q = c(context, null);
        this.w = new HashMap();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(Context context, long j, long j2) {
        this.A = j;
        this.B = j2;
        this.C = 0L;
        this.d.a(context);
        this.d.a();
        this.r = false;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(Context context) {
        this.d.a(context);
        this.d.b();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a() {
        this.d.a((Context) null);
        j();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final long b() {
        return this.A;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static boolean d(Context context) {
        if (context.checkCallingOrSelfPermission("android.permission.ACCESS_NETWORK_STATE") != 0) {
            return true;
        }
        NetworkInfo activeNetworkInfo = ((ConnectivityManager) context.getSystemService("connectivity")).getActiveNetworkInfo();
        boolean z = activeNetworkInfo != null && (activeNetworkInfo.isConnected() || activeNetworkInfo.isRoaming());
        if (!z) {
            bc.d(a, "No connectivity found.");
            return z;
        }
        return z;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final String c() {
        return this.z;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(ICustomAdNetworkHandler iCustomAdNetworkHandler) {
        if (iCustomAdNetworkHandler != null) {
            this.G = iCustomAdNetworkHandler;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final synchronized long d() {
        long elapsedRealtime = SystemClock.elapsedRealtime() - this.B;
        if (elapsedRealtime <= this.C) {
            elapsedRealtime = this.C + 1;
            this.C = elapsedRealtime;
        }
        this.C = elapsedRealtime;
        return this.C;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(String str) {
        this.E = str;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(Map<Integer, ByteBuffer> map) {
        this.D = map;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(List<bb> list) {
        this.L = list;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(FlurryAdListener flurryAdListener) {
        if (flurryAdListener != null) {
            this.H = flurryAdListener;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final String e() {
        return this.E;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void b(Map<String, String> map) {
        if (map != null) {
            this.O = map;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void f() {
        this.O = null;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final boolean g() {
        return this.I;
    }

    private boolean o() {
        if (!this.I) {
            bc.d(a, "Flurry Ads not initialized");
        }
        if (this.E == null) {
            bc.d(a, "Cannot identify UDID.");
        }
        return this.I;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(boolean z) {
        this.N = z;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final synchronized void a(bb bbVar) {
        if (this.J.size() < 32767) {
            this.J.add(bbVar);
            this.K.put(bbVar.b(), bbVar);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final boolean a(Context context, String str, FlurryAdSize flurryAdSize, ViewGroup viewGroup, long j) {
        if (o()) {
            if (j < 1) {
                boolean b2 = this.c.b(str);
                if (d(context) && !((KeyguardManager) context.getSystemService("keyguard")).inKeyguardRestrictedInputMode()) {
                    new GetAdAsyncTask(context, str, flurryAdSize, viewGroup, this).execute(new Void[0]);
                }
                return b2;
            }
            t a2 = this.d.a(viewGroup);
            t a3 = this.d.a(context, (View) viewGroup, str);
            if (a3 == null) {
                a3 = this.d.a(this, context, viewGroup, str, flurryAdSize);
                this.d.b(context, a3);
            }
            t tVar = a3;
            for (t tVar2 : this.d.a(context, viewGroup, str)) {
                if (!tVar2.equals(a2)) {
                    this.d.a(context, tVar2);
                    tVar2.c().removeView(tVar2);
                }
            }
            ar a4 = this.d.a(tVar, j);
            if (a4.a()) {
                if (!tVar.equals(a2)) {
                    if (a2 != null) {
                        this.d.a(context, a2);
                        viewGroup.removeView(a2);
                    }
                    viewGroup.addView(tVar);
                }
                if (tVar.a() > 0 && !tVar.b()) {
                    this.d.a(tVar);
                }
            }
            boolean z = a4.a() || a4.b();
            bc.a(a, "Ad is being returned: " + z);
            return z;
        }
        return false;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(Context context, ViewGroup viewGroup) {
        t a2;
        if (o() && (a2 = this.d.a(viewGroup)) != null) {
            viewGroup.removeView(a2);
            this.d.a(context, a2);
        }
    }

    private boolean c(Context context, String str) {
        String packageName = context.getPackageName();
        if (str == null) {
            str = "market://details?id=" + packageName;
        }
        return a(context, str, "android.intent.action.VIEW");
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public static boolean a(Context context, String str, String str2) {
        Intent intent = new Intent(str2);
        intent.setData(Uri.parse(str));
        return y.a(context, intent);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void b(String str) {
        this.s = str;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void c(String str) {
        this.t = str;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(float f2, float f3) {
        this.u = f2;
        this.v = f3;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void c(Map<String, String> map) {
        if (map != null && map.size() > 0) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                this.w.put(entry.getKey(), entry.getValue());
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void h() {
        this.w.clear();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final Map<String, String> i() {
        return this.w;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(Context context, String str) {
        if (str.startsWith("market://details?id=")) {
            String substring = str.substring("market://details?id=".length());
            if (this.q) {
                try {
                    y.a(context, str);
                    return;
                } catch (Exception e2) {
                    bc.b(a, "Cannot launch Google Play url " + str, e2);
                    return;
                }
            }
            y.a(context, "https://market.android.com/details?id=" + substring);
            return;
        }
        bc.d(a, "Unexpected Google Play url scheme: " + str);
    }

    private void a(Context context, String str, boolean z) {
        if (o()) {
            this.F.post(new c(this, str, context, z));
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final boolean b(Context context, String str) {
        for (int i = 0; i < 5; i++) {
            if (d(context)) {
                HttpResponse a2 = y.a(this.h, str, 10000, 15000, true);
                if (a2 != null && a2.getStatusLine().getStatusCode() == 200) {
                    String str2 = "URL hit succeeded for: " + str;
                    return true;
                }
            } else {
                try {
                    Thread.sleep(100L);
                } catch (InterruptedException e2) {
                    e2.getMessage();
                }
            }
        }
        return false;
    }

    private String a(bb bbVar, AdUnit adUnit, u uVar, String str) {
        Pattern compile = Pattern.compile(".*?(%\\{\\w+\\}).*?");
        Matcher matcher = compile.matcher(str);
        while (matcher.matches()) {
            str = this.i.a(bbVar, adUnit, str, matcher.group(1));
            matcher = compile.matcher(str);
        }
        return str;
    }

    private synchronized void a(SdkLogResponse sdkLogResponse) {
        if (sdkLogResponse.a().toString().equals("success")) {
            this.J.removeAll(this.L);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void b(Context context) {
        bc.c(a, "Init'ing ads.");
        if (!this.r) {
            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            a("", displayMetrics.widthPixels, displayMetrics.heightPixels, true, (FlurryAdSize) null, 1L);
            this.r = true;
            return;
        }
        bc.c(a, "Ads already init'ed, will not init them again this session");
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final boolean a(Context context, String str, FlurryAdSize flurryAdSize, long j) {
        boolean z = false;
        long currentTimeMillis = System.currentTimeMillis();
        while (y.a(currentTimeMillis + j)) {
            if (this.c.b(str)) {
                return true;
            }
            if (!z) {
                a(str, y.a(context, y.a(context)), y.a(context, y.b(context)), false, flurryAdSize, j);
                z = true;
            }
        }
        return false;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final q a(Context context, String str, int i, int i2, boolean z, FlurryAdSize flurryAdSize, long j) {
        Collections.emptyMap();
        q qVar = new q(null, false, false, null);
        if (o() && this.d.a(context, str) != null) {
            long currentTimeMillis = System.currentTimeMillis();
            boolean z2 = false;
            while (true) {
                q qVar2 = qVar;
                if (!y.a(currentTimeMillis + j)) {
                    return qVar2;
                }
                AdUnit d = d(str);
                if (d != null) {
                    q a2 = a(context, d);
                    if (this.c.b(str) || z2) {
                        qVar = a2;
                    } else {
                        a(str, i, i2, false, flurryAdSize, 1L);
                        z2 = true;
                        qVar = a2;
                    }
                } else {
                    if (!z2) {
                        a(str, i, i2, false, flurryAdSize, j);
                        z2 = true;
                    }
                    AdUnit d2 = d(str);
                    qVar = d2 != null ? a(context, d2) : qVar2;
                }
                if (!qVar.d()) {
                    try {
                        Thread.sleep(j / 10);
                    } catch (InterruptedException e2) {
                        bc.b(a, "Ad Request thread interrupted.");
                        return qVar;
                    }
                } else {
                    return qVar;
                }
            }
        } else {
            return qVar;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(String str, int i, int i2, boolean z, FlurryAdSize flurryAdSize, long j) {
        if (!this.M.contains(str)) {
            if (str != null) {
                if (j > 0) {
                    this.F.post(new h(this, str, i, i2, z, flurryAdSize));
                    return;
                }
                a(str, i, i2, z, flurryAdSize, "/v3/getAds.do");
                return;
            }
            bc.a(a, "ad space name should not be null");
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final AdUnit d(String str) {
        return this.c.a(str);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final synchronized void j() {
        this.F.post(new f(this));
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void e(String str) {
        byte[] p = p();
        if (p != null) {
            a("", p, str);
        }
    }

    private byte[] p() {
        List<AdReportedId> q = q();
        synchronized (this.J) {
            List<SdkAdLog> a2 = g.a(this.J);
            if (a2.size() == 0) {
                return null;
            }
            SdkLogRequest build = SdkLogRequest.a().setApiKey(this.z).setAdReportedIds(q).setSdkAdLogs(a2).setTestDevice(false).setAgentTimestamp(System.currentTimeMillis()).build();
            String str = "Got ad log request:" + build.toString();
            SpecificDatumWriter specificDatumWriter = new SpecificDatumWriter(SdkLogRequest.class);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            BinaryEncoder directBinaryEncoder = EncoderFactory.get().directBinaryEncoder(byteArrayOutputStream, null);
            try {
                specificDatumWriter.write(build, directBinaryEncoder);
                directBinaryEncoder.flush();
                return byteArrayOutputStream.toByteArray();
            } catch (IOException e2) {
                e2.getMessage();
                return null;
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(String str, int i, int i2, boolean z, FlurryAdSize flurryAdSize, String str2) {
        this.M.add(str);
        byte[] a2 = a(str, i, i2, z, flurryAdSize);
        if (a2 != null) {
            a(str, a2, str2);
        }
        this.M.remove(str);
    }

    private List<AdReportedId> q() {
        ArrayList arrayList = new ArrayList();
        arrayList.add(AdReportedId.a().setId(ByteBuffer.wrap(this.E.getBytes())).setType(0).build());
        for (Map.Entry<Integer, ByteBuffer> entry : this.D.entrySet()) {
            arrayList.add(AdReportedId.a().setId(entry.getValue()).setType(entry.getKey().intValue()).build());
        }
        return arrayList;
    }

    private byte[] a(String str, int i, int i2, boolean z, FlurryAdSize flurryAdSize) {
        AdRequest build = AdRequest.a().setApiKey(this.z).setAdSpaceName("").setBindings(e).setAdReportedIds(q()).setLocation(Location.a().setLat(this.u).setLon(this.v).build()).setTestDevice(this.N).setAgentVersion(Integer.toString(FlurryAgent.getAgentVersion())).setSessionId(this.A).setAdViewContainer(AdViewContainer.a().setScreenHeight(this.j.getHeight()).setScreenWidth(this.j.getWidth()).setViewHeight(i2).setViewWidth(i).build()).setLocale(this.m).setTimezone(this.n).setOsVersion(this.o).setDevicePlatform(this.p).build();
        if (z) {
            build.a(Boolean.valueOf(z));
        } else {
            build.a(str);
        }
        if (flurryAdSize != null) {
            build.a(TestAds.a().setAdspacePlacement(flurryAdSize.a()).build());
        }
        if (this.O != null) {
            HashMap hashMap = new HashMap();
            for (Map.Entry<String, String> entry : this.O.entrySet()) {
                hashMap.put(entry.getKey(), entry.getValue());
            }
            build.a(hashMap);
        }
        String str2 = "Got ad request  " + build;
        SpecificDatumWriter specificDatumWriter = new SpecificDatumWriter(AdRequest.class);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        BinaryEncoder directBinaryEncoder = EncoderFactory.get().directBinaryEncoder(byteArrayOutputStream, null);
        try {
            specificDatumWriter.write(build, directBinaryEncoder);
            directBinaryEncoder.flush();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e2) {
            e2.getMessage();
            return new byte[0];
        }
    }

    private boolean a(String str, byte[] bArr, String str2) {
        String str3;
        String str4;
        if (str2 == null) {
            return false;
        }
        if (str2.equals("/v3/getAds.do")) {
            StringBuilder sb = new StringBuilder();
            if (this.s != null) {
                str4 = this.s;
            } else {
                str4 = FlurryAgent.getUseHttps() ? "https://ads.flurry.com" : "http://ads.flurry.com";
            }
            str3 = sb.append(str4).append(str2).toString();
        } else {
            str3 = (this.t != null ? this.t : FlurryAgent.getUseHttps() ? "https://adlog.flurry.com" : "http://adlog.flurry.com") + str2;
        }
        byte[] a2 = a(bArr, str3);
        if (a2 != null) {
            try {
                if (str2.equals("/v3/getAds.do")) {
                    a(str, a2);
                } else {
                    b(a2);
                }
            } catch (IOException e2) {
                bc.b(a, "IOException: " + e2.toString());
            }
        }
        return true;
    }

    private byte[] a(byte[] bArr, String str) {
        ByteArrayEntity byteArrayEntity = new ByteArrayEntity(bArr);
        byteArrayEntity.setContentType("avro/binary");
        HttpPost httpPost = new HttpPost(str);
        httpPost.setEntity(byteArrayEntity);
        httpPost.setHeader("accept", "avro/binary");
        httpPost.setHeader("FM-Checksum", Integer.toString(a(bArr)));
        HttpParams basicHttpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(basicHttpParams, 10000);
        HttpConnectionParams.setSoTimeout(basicHttpParams, 15000);
        httpPost.getParams().setBooleanParameter("http.protocol.expect-continue", false);
        try {
            HttpResponse execute = this.h.a(basicHttpParams).execute(httpPost);
            int statusCode = execute.getStatusLine().getStatusCode();
            if (statusCode == 200 && execute.getEntity() != null && execute.getEntity().getContentLength() != 0) {
                bc.c(a, "Request successful");
                byte[] a2 = a(execute.getEntity().getContent());
                byteArrayEntity.consumeContent();
                String num = Integer.toString(a(a2));
                if (!execute.containsHeader("FM-Checksum") || execute.getFirstHeader("FM-Checksum").getValue().equals(num)) {
                    return a2;
                }
            } else {
                bc.b(a, "Request failed with HTTP " + statusCode);
            }
        } catch (IOException e2) {
            bc.b(a, "IOException: " + e2.toString());
        }
        return null;
    }

    private static byte[] a(InputStream inputStream) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bArr = new byte[128];
        while (true) {
            int read = inputStream.read(bArr);
            if (read != -1) {
                byteArrayOutputStream.write(bArr, 0, read);
            } else {
                return byteArrayOutputStream.toByteArray();
            }
        }
    }

    private static <A extends SpecificRecordBase> A a(byte[] bArr, Class<A> cls) {
        try {
            return (A) new SpecificDatumReader(cls).read(null, f.binaryDecoder(new ByteArrayInputStream(bArr), (BinaryDecoder) null));
        } catch (ClassCastException e2) {
            String str = "ClassCastException in parseAvroBinary:" + e2.getMessage();
            return null;
        }
    }

    private static int a(byte[] bArr) {
        CrcMessageDigest crcMessageDigest = new CrcMessageDigest();
        crcMessageDigest.update(bArr);
        return crcMessageDigest.getChecksum();
    }

    private void b(byte[] bArr) {
        SdkLogResponse sdkLogResponse = (SdkLogResponse) a(bArr, SdkLogResponse.class);
        if (sdkLogResponse != null) {
            String str = "got an AdLogResponse:" + sdkLogResponse;
            if (sdkLogResponse.a().toString().equals("success")) {
                a(sdkLogResponse);
                return;
            }
            for (CharSequence charSequence : sdkLogResponse.b()) {
                bc.b(a, charSequence.toString());
            }
        }
    }

    private void a(String str, byte[] bArr) {
        AdResponse adResponse = (AdResponse) a(bArr, AdResponse.class);
        if (adResponse != null) {
            y.a(4, a, "got an AdResponse:" + adResponse);
            if (adResponse.b() != null && adResponse.b().size() > 0) {
                bc.b(a, "Ad request returned errors: ");
                for (CharSequence charSequence : adResponse.b()) {
                    bc.b(a, charSequence.toString());
                }
                if (this.H != null) {
                    this.H.spaceDidFailToReceiveAd(str);
                }
            } else if (adResponse.a() != null && adResponse.a().size() < 1) {
                bc.d(a, "Ad request successful but server delivered no ad units.");
                if (this.H != null) {
                    this.H.spaceDidFailToReceiveAd(str);
                }
            } else {
                if (o()) {
                    this.c.a(adResponse);
                }
                if (this.H != null) {
                    this.H.spaceDidReceiveAd(str);
                }
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final synchronized void k() {
        DataOutputStream dataOutputStream;
        try {
            if (ad.a(this.l)) {
                dataOutputStream = new DataOutputStream(new FileOutputStream(this.l));
                try {
                    dataOutputStream.writeShort(46586);
                    synchronized (this.J) {
                        a(this.J, dataOutputStream);
                    }
                    dataOutputStream.writeShort(0);
                    y.a(dataOutputStream);
                } catch (Throwable th) {
                    th = th;
                    bc.b(a, "", th);
                    y.a(dataOutputStream);
                }
            } else {
                y.a((Closeable) null);
            }
        } catch (Throwable th2) {
            th = th2;
            dataOutputStream = null;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final synchronized void l() {
        new j(this).execute(new Void[0]);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final synchronized void a(DataInputStream dataInputStream) {
        while (dataInputStream.readUnsignedShort() != 0) {
            synchronized (this.J) {
                this.J.add(new bb(dataInputStream));
            }
        }
        this.k = true;
    }

    private synchronized void a(List<bb> list, DataOutputStream dataOutputStream) {
        int size = list.size();
        for (int i = 0; i < size; i++) {
            try {
                list.get(i).a(dataOutputStream);
            } catch (IOException e2) {
                bc.a(a, "unable to convert adLog to byte[]: " + list.get(i).b());
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final synchronized bb a(bb bbVar, String str, boolean z, Map<String, String> map) {
        synchronized (bbVar) {
            if (!this.J.contains(bbVar)) {
                this.J.add(bbVar);
            }
            bbVar.a(a(str, z, map));
        }
        return bbVar;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final bb a(String str, String str2, boolean z, Map<String, String> map) {
        bb bbVar = this.K.get(str);
        if (bbVar == null) {
            bbVar = y.a(this, str);
        }
        return a(bbVar, str2, true, map);
    }

    private synchronized an a(String str, boolean z, Map<String, String> map) {
        return new an(str, z, d(), map);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void b(bb bbVar) {
        this.x = bbVar;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final bb m() {
        return this.x;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final void a(AdUnit adUnit) {
        this.y = adUnit;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final AdUnit n() {
        return this.y;
    }

    private static List<u> a(AdFrame adFrame, ba baVar) {
        String str;
        Map<String, String> map;
        ArrayList arrayList = new ArrayList();
        List<Callback> e2 = adFrame.e();
        String str2 = baVar.a;
        for (Callback callback : e2) {
            if (callback.a().toString().equals(str2)) {
                for (CharSequence charSequence : callback.b()) {
                    HashMap hashMap = new HashMap();
                    String obj = charSequence.toString();
                    int indexOf = obj.indexOf(63);
                    if (indexOf != -1) {
                        str = obj.substring(0, indexOf);
                        map = y.f(obj.substring(indexOf + 1));
                    } else {
                        str = obj;
                        map = hashMap;
                    }
                    arrayList.add(new u(str, map, baVar));
                }
            }
        }
        return arrayList;
    }

    public final void a(ba baVar, ai aiVar, int i) {
        u uVar;
        boolean z;
        String str = "onEvent:event=" + baVar.a + ",params=" + baVar.b;
        List<u> a2 = a(baVar.d.c().get(baVar.f), baVar);
        for (Map.Entry<String, String> entry : this.Q.entrySet()) {
            if (entry.getKey().equals(baVar.a)) {
                a2.add(new u(entry.getValue(), baVar.b, baVar));
            }
        }
        if (baVar.a.equals("adWillClose")) {
            if (this.H != null) {
                this.H.onAdClosed(baVar.d.a().toString());
            }
            Iterator<u> it = a2.iterator();
            while (true) {
                if (!it.hasNext()) {
                    z = false;
                    break;
                } else if (this.R.contains(it.next().a)) {
                    z = true;
                    break;
                }
            }
            if (!z) {
                a2.add(new u("closeAd", Collections.emptyMap(), baVar));
            }
        }
        if (baVar.a.equals("renderFailed") && this.H != null) {
            this.H.onRenderFailed(baVar.d.a().toString());
        }
        u uVar2 = null;
        for (u uVar3 : a2) {
            if (uVar3.a.equals("logEvent")) {
                uVar3.b.put("__sendToServer", "true");
                uVar = uVar3;
            } else {
                uVar = uVar2;
            }
            if (uVar3.a.equals("loadAdComponents")) {
                for (Map.Entry<String, String> entry2 : uVar3.c.b.entrySet()) {
                    uVar3.b.put(entry2.getKey().toString(), entry2.getValue().toString());
                }
            }
            aiVar.a(uVar3, this, i + 1);
            uVar2 = uVar;
        }
        if (uVar2 == null) {
            HashMap hashMap = new HashMap();
            hashMap.put("__sendToServer", "false");
            aiVar.a(new u("logEvent", hashMap, baVar), this, i + 1);
        }
    }

    @Override // com.flurry.android.ai
    public final void a(u uVar, be beVar, int i) {
        long parseLong;
        String str;
        String str2 = "performAction:action=" + uVar.a + ",params=" + uVar.b + ",triggering event=" + uVar.c.a;
        String str3 = uVar.a;
        Context context = uVar.c.c;
        bb bbVar = uVar.c.e;
        AdUnit adUnit = uVar.c.d;
        if (i > 10) {
            r0 = "Maximum depth for event/action loop exceeded when performing action:" + str3 + "," + uVar.b + ",triggered by:" + uVar.c.a;
        } else if (str3.equals("directOpen")) {
            if (uVar.c.b.containsKey("url")) {
                String str4 = uVar.c.b.get("url");
                Intent intent = new Intent();
                intent.setClass(context, FlurryFullscreenTakeoverActivity.class);
                intent.putExtra("url", str4);
                if (y.a(context, intent)) {
                    context.startActivity(intent);
                    return;
                }
                bc.d(a, "Can't start FlurryFullscreenTakeoverActivity, was it declared in the manifest? Falling back to default browser");
                y.a(context, str4);
                return;
            }
            bc.b(a, "failed to perform directOpen action: no url in " + uVar.c.a);
        } else if (str3.equals("processRedirect")) {
            if (uVar.b.containsKey("url")) {
                if (this.H != null) {
                    this.H.onApplicationExit(adUnit.a().toString());
                }
                String str5 = uVar.b.get("url");
                if (str5.startsWith("http")) {
                    String a2 = a(bbVar, adUnit, uVar, str5);
                    try {
                        str = new ae(this, context, a2).execute(new Void[0]).get();
                    } catch (InterruptedException e2) {
                        e2.getMessage();
                        str = "";
                    } catch (ExecutionException e3) {
                        e3.getMessage();
                        str = "";
                    }
                    if (str != null) {
                        a(context, str, true);
                        return;
                    } else {
                        bc.d(a, "Redirect URL could not be found for: " + a2);
                        return;
                    }
                }
                a(context, str5, false);
            }
        } else if (str3.equals("verifyUrl")) {
            if (uVar.b.containsKey("url")) {
                if (uVar.b.get("url").startsWith("http")) {
                    beVar.a(new ba(a(context, a(bbVar, adUnit, uVar, uVar.b.get("url")), "android.intent.action.VIEW") ? "urlVerified" : "urlNotVerified", Collections.emptyMap(), uVar.c.c, adUnit, bbVar, uVar.c.f), this, i + 1);
                } else {
                    beVar.a(new ba(c(context, new StringBuilder().append("market://details?id=").append(uVar.b.get("url")).toString()) ? "urlVerified" : "urlNotVerified", Collections.emptyMap(), uVar.c.c, adUnit, bbVar, uVar.c.f), this, i + 1);
                }
            }
        } else if (str3.equals("sendUrlAsync")) {
            if (uVar.b.containsKey("url")) {
                if (this.H != null) {
                    this.H.onApplicationExit(adUnit.a().toString());
                }
                String a3 = a(bbVar, adUnit, uVar, uVar.b.get("url"));
                String str6 = "url after is: " + a3;
                this.F.post(new d(this, context, a3));
            }
        } else if (str3.equals("sendAdLogs")) {
            j();
        } else if (str3.equals("logEvent")) {
            boolean z = uVar.b.containsKey("__sendToServer") && uVar.b.get("__sendToServer").equals("true");
            uVar.b.remove("__sendToServer");
            a(uVar.c.e, uVar.c.a, z, uVar.b);
        } else if (!str3.equals("nextFrame")) {
            if (str3.equals("nextAdUnit")) {
                t a4 = this.d.a(context, adUnit.a().toString());
                if (a4 != null) {
                    if (uVar.b.containsKey("delay")) {
                        try {
                            parseLong = Long.parseLong(uVar.b.get("delay"));
                        } catch (NumberFormatException e4) {
                            String str7 = "caught NumberFormatException with delay parameter in nextAdUnit:" + uVar.b.get("delay");
                        }
                        a4.a((int) (parseLong * 1000));
                        this.d.a(a4);
                        return;
                    }
                    parseLong = 30;
                    a4.a((int) (parseLong * 1000));
                    this.d.a(a4);
                    return;
                }
                this.P = this.c.a(uVar.c.d.a().toString());
            } else if (str3.equals("launchPackage")) {
                if (uVar.b.containsKey("package")) {
                    try {
                        Intent launchIntentForPackage = context.getPackageManager().getLaunchIntentForPackage(uVar.b.get("package"));
                        if (launchIntentForPackage != null) {
                            context.startActivity(launchIntentForPackage);
                        } else {
                            a(context, "market://details?id=" + uVar.b.get("package"), false);
                        }
                    } catch (PackageManager.NameNotFoundException e5) {
                        e5.getMessage();
                    }
                }
            } else {
                String str8 = "Unknown action:" + str3 + ",triggered by:" + uVar.c.a;
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public final q a(Context context, AdUnit adUnit) {
        bb bbVar;
        AdFrame adFrame;
        String str;
        String str2;
        AdUnit adUnit2;
        int i;
        String str3;
        o oVar;
        boolean z;
        FlurryAdType flurryAdType;
        this.P = adUnit;
        List<AdFrame> c = adUnit.c();
        if (c.size() > 0) {
            AdFrame adFrame2 = c.get(0);
            int intValue = adFrame2.a().intValue();
            String obj = adFrame2.c().toString();
            String obj2 = adFrame2.c().toString();
            String obj3 = adFrame2.d().d().toString();
            bb a2 = a(adFrame2.f().toString(), "requested", true, (Map<String, String>) null);
            a(new ba("filled", Collections.emptyMap(), context, adUnit, a2, 0), this, 1);
            if (this.P == null) {
                return new q(null, false, false, adUnit);
            }
            if (this.P != adUnit) {
                AdUnit adUnit3 = this.P;
                adFrame = adUnit3.c().get(0);
                i = adFrame.a().intValue();
                String obj4 = adFrame.c().toString();
                str2 = adFrame.c().toString();
                str = adFrame.d().d().toString();
                bbVar = a(adFrame.f().toString(), "requested", true, (Map<String, String>) null);
                str3 = obj4;
                adUnit2 = adUnit3;
            } else {
                bbVar = a2;
                adFrame = adFrame2;
                str = obj3;
                str2 = obj2;
                adUnit2 = adUnit;
                i = intValue;
                str3 = obj;
            }
            if (this.H != null) {
                FlurryAdListener flurryAdListener = this.H;
                String obj5 = adUnit2.a().toString();
                int intValue2 = adFrame.a().intValue();
                String obj6 = adFrame.d().d().toString();
                if (intValue2 == 3) {
                    flurryAdType = FlurryAdType.VIDEO_TAKEOVER;
                } else {
                    flurryAdType = obj6.equals(AdCreative.kFormatTakeover) ? FlurryAdType.WEB_TAKEOVER : FlurryAdType.WEB_BANNER;
                }
                if (!flurryAdListener.shouldDisplayAd(obj5, flurryAdType)) {
                    return new q(null, false, false, adUnit2);
                }
            }
            String str4 = "Processing ad request for binding: " + i + ", networkType: " + str3 + ", format: " + str;
            if (adUnit2.d().intValue() == 1 || i == 2 || i == 1 || i == 3) {
                if (str.equals(AdCreative.kFormatTakeover)) {
                    this.x = bbVar;
                    this.y = adUnit2;
                    String str5 = "opening takeover activity, display: " + str2 + ", content: " + str3;
                    z = true;
                    oVar = null;
                } else {
                    o ajVar = new aj(context, this, bbVar, adUnit2, 0);
                    ajVar.initLayout(context);
                    oVar = ajVar;
                    z = false;
                }
            } else if (i == 4) {
                AdSpaceLayout d = adFrame.d();
                AdCreative adCreative = new AdCreative(d.b().intValue(), d.a().intValue(), d.d().toString(), d.c().toString(), d.e().toString());
                if (str3.equalsIgnoreCase("Admob")) {
                    String str6 = "Retrieving BannerView for:" + str3;
                    o xVar = new x(context, this, bbVar, adCreative);
                    xVar.initLayout(context);
                    xVar.d = 0;
                    xVar.c = adUnit2;
                    oVar = xVar;
                    z = false;
                } else if (str3.equalsIgnoreCase("Millennial Media")) {
                    String str7 = "Retrieving BannerView for:" + str3;
                    o agVar = new ag(context, this, bbVar, adCreative);
                    agVar.initLayout(context);
                    agVar.d = 0;
                    agVar.c = adUnit2;
                    oVar = agVar;
                    z = false;
                } else if (str3.equalsIgnoreCase("InMobi")) {
                    String str8 = "Retrieving BannerView for:" + str3;
                    o aaVar = new aa(context, this, bbVar, adCreative);
                    aaVar.initLayout(context);
                    aaVar.d = 0;
                    aaVar.c = adUnit2;
                    oVar = aaVar;
                    z = false;
                } else if (str3.equalsIgnoreCase("Mobclix")) {
                    String str9 = "Retrieving BannerView for:" + str3;
                    o acVar = new ac(context, this, bbVar, adCreative);
                    acVar.initLayout(context);
                    acVar.d = 0;
                    acVar.c = adUnit2;
                    oVar = acVar;
                    z = false;
                } else if (str3.equalsIgnoreCase("Jumptap")) {
                    String str10 = "Retrieving BannerView for:" + str3;
                    o gVar = new g(context, this, bbVar, adCreative);
                    gVar.initLayout(context);
                    gVar.d = 0;
                    gVar.c = adUnit2;
                    oVar = gVar;
                    z = false;
                } else {
                    ICustomAdNetworkHandler iCustomAdNetworkHandler = this.G;
                    if (iCustomAdNetworkHandler != null) {
                        o adFromNetwork = iCustomAdNetworkHandler.getAdFromNetwork(context, adCreative, str3);
                        if (adFromNetwork != null) {
                            adFromNetwork.a = this;
                            adFromNetwork.b = bbVar;
                            adFromNetwork.initLayout(context);
                            adFromNetwork.d = 0;
                            adFromNetwork.c = adUnit2;
                            oVar = adFromNetwork;
                            z = false;
                        } else {
                            bc.d(a, "CustomAdNetworkHandler returned null banner view");
                            oVar = adFromNetwork;
                            z = false;
                        }
                    } else {
                        bc.d(a, "No CustomAdNetworkHandler set");
                        z = false;
                        oVar = null;
                    }
                }
            } else {
                String str11 = "Do not support binding: " + i;
                z = false;
                oVar = null;
            }
            y.d(str3);
            return new q(oVar, z, true, adUnit2);
        }
        return new q(null, false, false, adUnit);
    }
}
