package com.admarvel.android.ads.internal;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.webkit.URLUtil;
import com.admarvel.android.ads.AdMarvelActivity;
import com.admarvel.android.ads.AdMarvelAd;
import com.admarvel.android.ads.AdMarvelPostitialActivity;
import com.admarvel.android.ads.internal.Utils.Utils;
import com.admarvel.android.ads.internal.util.Logging;
import com.facebook.ads.BuildConfig;
import com.flurry.android.AdCreative;
import com.flurry.android.FlurryFullscreenTakeoverActivity;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
import java.net.URISyntaxException;

/* renamed from: com.admarvel.android.ads.internal.g */
public class AdMarvelRedirectRunnable implements Runnable {
    private final String f434a;
    private final WeakReference<Context> f435b;
    private final AdMarvelAd f436c;
    private final String f437d;
    private final String f438e;
    private final String f439f;
    private final boolean f440g;
    private final boolean f441h;
    private final boolean f442i;

    public AdMarvelRedirectRunnable(String str, Context context, AdMarvelAd adMarvelAd, String str2, String str3, boolean z, boolean z2, boolean z3, String str4) {
        this.f434a = str;
        this.f435b = new WeakReference(context);
        this.f436c = adMarvelAd;
        this.f437d = str2;
        this.f438e = str3;
        this.f440g = z;
        this.f441h = z2;
        this.f442i = z3;
        this.f439f = str4;
    }

    public void run() {
        Context context = (Context) this.f435b.get();
        if (context != null) {
            Intent intent;
            if (this.f440g) {
                try {
                    String offlinekeyUrl;
                    if (this.f434a != null && this.f441h && !Utils.m515g(this.f434a)) {
                        if (URLUtil.isNetworkUrl(this.f434a) && Utils.m495a(context, this.f434a, this.f442i)) {
                            if (this.f437d.equals(AdCreative.kFormatBanner)) {
                                if (AdMarvelWebView.m386a(this.f438e) != null) {
                                    AdMarvelWebView.m386a(this.f438e).m207a(this.f436c, this.f434a);
                                    return;
                                }
                                return;
                            } else if (this.f437d.equals("interstitial")) {
                                intent = new Intent();
                                intent.setAction("com.admarvel.adreceiver.LISTENER");
                                intent.putExtra("WEBVIEW_GUID", this.f439f);
                                intent.putExtra(FlurryFullscreenTakeoverActivity.EXTRA_KEY_URL, this.f434a);
                                intent.putExtra("callback", "click");
                                context.sendBroadcast(intent);
                                return;
                            }
                        }
                        offlinekeyUrl = this.f436c.getOfflinekeyUrl();
                        if (Utils.m495a(context, offlinekeyUrl.substring(0, offlinekeyUrl.lastIndexOf("/")) + "/" + this.f434a, this.f442i)) {
                            if (this.f437d.equals(AdCreative.kFormatBanner)) {
                                if (AdMarvelWebView.m386a(this.f438e) != null) {
                                    AdMarvelWebView.m386a(this.f438e).m207a(this.f436c, this.f434a);
                                    return;
                                }
                                return;
                            } else if (this.f437d.equals("interstitial")) {
                                intent = new Intent();
                                intent.setAction("com.admarvel.adreceiver.LISTENER");
                                intent.putExtra("WEBVIEW_GUID", this.f439f);
                                intent.putExtra(FlurryFullscreenTakeoverActivity.EXTRA_KEY_URL, this.f434a);
                                intent.putExtra("callback", "click");
                                context.sendBroadcast(intent);
                                return;
                            }
                        }
                    } else if (!(this.f434a == null || Utils.m515g(this.f434a) || !Utils.m495a(context, this.f434a, this.f442i))) {
                        if (!this.f437d.equals("native")) {
                            new Utils(context).m552c(this.f436c.getXml());
                        }
                        if (this.f437d.equals(AdCreative.kFormatBanner)) {
                            if (AdMarvelWebView.m386a(this.f438e) != null) {
                                AdMarvelWebView.m386a(this.f438e).m207a(this.f436c, this.f434a);
                                return;
                            }
                            return;
                        } else if (this.f437d.equals("interstitial")) {
                            intent = new Intent();
                            intent.setAction("com.admarvel.adreceiver.LISTENER");
                            intent.putExtra("WEBVIEW_GUID", this.f439f);
                            intent.putExtra(FlurryFullscreenTakeoverActivity.EXTRA_KEY_URL, this.f434a);
                            intent.putExtra("callback", "click");
                            context.sendBroadcast(intent);
                            return;
                        }
                    }
                    if (this.f434a != null && Utils.m481a(this.f434a, "admarvelsdk") != Utils.NONE) {
                        if (this.f437d.equals(AdCreative.kFormatBanner)) {
                            if (AdMarvelWebView.m386a(this.f438e) != null) {
                                AdMarvelWebView.m386a(this.f438e).m207a(this.f436c, Utils.m486a(this.f434a, "admarvelsdk", BuildConfig.FLAVOR, Utils.m481a(this.f434a, "admarvelsdk"), context));
                            }
                        } else if (this.f437d.equals("interstitial")) {
                            intent = new Intent();
                            intent.setAction("com.admarvel.adreceiver.LISTENER");
                            intent.putExtra("WEBVIEW_GUID", this.f439f);
                            intent.putExtra(FlurryFullscreenTakeoverActivity.EXTRA_KEY_URL, Utils.m486a(this.f434a, "admarvelsdk", BuildConfig.FLAVOR, Utils.m481a(this.f434a, "admarvelsdk"), context));
                            intent.putExtra("callback", "click");
                            context.sendBroadcast(intent);
                        }
                        if (!this.f437d.equals("native")) {
                            new Utils(context).m552c(this.f436c.getXml());
                            return;
                        }
                        return;
                    } else if (this.f434a != null && Utils.m481a(this.f434a, "admarvelinternal") != Utils.NONE) {
                        if (this.f437d.equals(AdCreative.kFormatBanner)) {
                            if (AdMarvelWebView.m386a(this.f438e) != null) {
                                AdMarvelWebView.m386a(this.f438e).m207a(this.f436c, Utils.m486a(this.f434a, "admarvelinternal", BuildConfig.FLAVOR, Utils.m481a(this.f434a, "admarvelinternal"), context));
                            }
                        } else if (this.f437d.equals("interstitial")) {
                            intent = new Intent();
                            intent.setAction("com.admarvel.adreceiver.LISTENER");
                            intent.putExtra("WEBVIEW_GUID", this.f439f);
                            intent.putExtra(FlurryFullscreenTakeoverActivity.EXTRA_KEY_URL, Utils.m486a(this.f434a, "admarvelinternal", BuildConfig.FLAVOR, Utils.m481a(this.f434a, "admarvelinternal"), context));
                            intent.putExtra("callback", "click");
                            context.sendBroadcast(intent);
                        }
                        if (!this.f437d.equals("native")) {
                            new Utils(context).m552c(this.f436c.getXml());
                            return;
                        }
                        return;
                    } else if (Utils.m524l(context) && this.f434a != null && Utils.m481a(this.f434a, "admarvelvideo") != Utils.NONE) {
                        offlinekeyUrl = Utils.m486a(this.f434a, "admarvelvideo", "http://", Utils.m481a(this.f434a, "admarvelvideo"), context);
                        r2 = new Intent("android.intent.action.VIEW");
                        r2.addFlags(268435456);
                        r2.setDataAndType(Uri.parse(offlinekeyUrl), "video/*");
                        if (Utils.m493a(context, r2)) {
                            context.startActivity(r2);
                        }
                        if (!this.f437d.equals("native")) {
                            new Utils(context).m552c(this.f436c.getXml());
                        }
                    } else if (Utils.m524l(context) && this.f434a != null && Utils.m481a(this.f434a, "admarvelexternal") != Utils.NONE) {
                        intent = new Intent("android.intent.action.VIEW", Uri.parse(Utils.m486a(this.f434a, "admarvelexternal", BuildConfig.FLAVOR, Utils.m481a(this.f434a, "admarvelexternal"), context)));
                        intent.addFlags(268435456);
                        if (Utils.m493a(context, intent)) {
                            context.startActivity(intent);
                        }
                        if (!this.f437d.equals("native")) {
                            new Utils(context).m552c(this.f436c.getXml());
                        }
                    } else if (Utils.m524l(context) && this.f434a != null && Utils.m481a(this.f434a, "intent") != Utils.NONE) {
                        try {
                            intent = Intent.parseUri(this.f434a, 1);
                            if (context.getPackageManager().resolveActivity(intent, AccessibilityNodeInfoCompat.ACTION_CUT) != null) {
                                context.startActivity(intent);
                            } else {
                                offlinekeyUrl = intent.getStringExtra("browser_fallback_url");
                                r2 = new Intent("android.intent.action.VIEW");
                                r2.setData(Uri.parse(offlinekeyUrl));
                                context.startActivity(r2);
                            }
                        } catch (URISyntaxException e) {
                            e.printStackTrace();
                        } catch (Exception e2) {
                            e2.printStackTrace();
                        }
                    } else if ((!Utils.m524l(context) || this.f434a == null || Utils.m481a(this.f434a, "admarvelcustomvideo") == Utils.NONE) && Utils.m524l(context) && this.f434a != null && this.f434a.length() > 0) {
                        Intent intent2;
                        if (this.f437d.equals(AdCreative.kFormatBanner)) {
                            intent = this.f442i ? new Intent(context, AdMarvelPostitialActivity.class) : new Intent(context, AdMarvelActivity.class);
                            intent.addFlags(268435456);
                            if (this.f442i) {
                                intent.addFlags(GravityCompat.RELATIVE_LAYOUT_DIRECTION);
                            }
                            intent.putExtra(FlurryFullscreenTakeoverActivity.EXTRA_KEY_URL, this.f434a);
                            intent.putExtra("isInterstitial", false);
                            intent.putExtra("isInterstitialClick", false);
                            intent2 = intent;
                        } else {
                            intent = new Intent(context, AdMarvelActivity.class);
                            intent.addFlags(268435456);
                            intent.putExtra(FlurryFullscreenTakeoverActivity.EXTRA_KEY_URL, this.f434a);
                            intent.putExtra("isInterstitial", false);
                            if (this.f437d.equals("native")) {
                                intent.putExtra("isInterstitialClick", false);
                                intent2 = intent;
                            } else {
                                intent.putExtra("isInterstitialClick", true);
                                intent2 = intent;
                            }
                        }
                        intent2.putExtra("GUID", this.f438e);
                        intent2.putExtra("WEBVIEW_GUID", this.f439f);
                        if (!this.f437d.equals("native")) {
                            byte[] toByteArray;
                            Object obj;
                            this.f436c.removeNonStringEntriesTargetParam();
                            byte[] bArr = new byte[0];
                            try {
                                OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                                ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
                                objectOutputStream.writeObject(this.f436c);
                                objectOutputStream.close();
                                toByteArray = byteArrayOutputStream.toByteArray();
                            } catch (IOException e3) {
                                e3.printStackTrace();
                                toByteArray = bArr;
                            }
                            AdMarvelAdAssests adMarvelAdAssests = new AdMarvelAdAssests();
                            if (toByteArray.length > 461000) {
                                if (AdMarvelActivity.f123a != null) {
                                    adMarvelAdAssests.m58a(this.f436c);
                                    adMarvelAdAssests.m63b(true);
                                    obj = 1;
                                }
                                obj = null;
                            } else {
                                intent2.putExtra("serialized_admarvelad", toByteArray);
                                obj = null;
                            }
                            if (this.f436c.getXml().length() > 461000) {
                                adMarvelAdAssests.m61a(true);
                                adMarvelAdAssests.m60a(this.f436c.getXml());
                                obj = 1;
                            } else {
                                intent2.putExtra("xml", this.f436c.getXml());
                            }
                            if (obj != null) {
                                AdMarvelActivity.f123a.clear();
                                AdMarvelActivity.f123a.put("admarveladAssests" + this.f438e, adMarvelAdAssests);
                            }
                            if (this.f436c.getSource() != null) {
                                intent2.putExtra("source", this.f436c.getSource());
                            }
                        }
                        context.startActivity(intent2);
                        if (!this.f437d.equals("native")) {
                            new Utils(context).m552c(this.f436c.getXml());
                        }
                    }
                } catch (Exception e22) {
                    Logging.log(e22.getMessage() + "Exception in RedirectRunnable ");
                }
            }
            if (this.f437d.equals(AdCreative.kFormatBanner)) {
                if (AdMarvelWebView.m386a(this.f438e) != null) {
                    AdMarvelWebView.m386a(this.f438e).m207a(this.f436c, this.f434a);
                }
            } else if (this.f437d.equals("interstitial")) {
                intent = new Intent();
                intent.setAction("com.admarvel.adreceiver.LISTENER");
                intent.putExtra("WEBVIEW_GUID", this.f439f);
                intent.putExtra(FlurryFullscreenTakeoverActivity.EXTRA_KEY_URL, this.f434a);
                intent.putExtra("callback", "click");
                context.sendBroadcast(intent);
            }
        }
    }
}
