package com.bytedance.sdk.openadsdk.utils;

import android.content.Context;
import android.content.MutableContextWrapper;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.webkit.WebView;
import java.lang.ref.SoftReference;
import java.util.LinkedHashMap;
import java.util.Map;

/* JADX INFO: loaded from: classes2.dex */
public class sds {
    private static int fm;
    private static final LinkedHashMap<String, SoftReference<fm>> ro = new LinkedHashMap<String, SoftReference<fm>>(fm, 0.75f, true) { // from class: com.bytedance.sdk.openadsdk.utils.sds.1
        @Override // java.util.LinkedHashMap
        public boolean removeEldestEntry(Map.Entry<String, SoftReference<fm>> entry) {
            fm fmVar;
            com.bytedance.sdk.component.ef.wsy wsyVar;
            if (size() <= sds.fm) {
                return false;
            }
            SoftReference<fm> value = entry.getValue();
            if (value == null || (fmVar = value.get()) == null || (wsyVar = fmVar.fm) == null) {
                return true;
            }
            try {
                com.bytedance.sdk.component.utils.gzf.fm(wsyVar);
                return true;
            } catch (Throwable unused) {
                entry.getKey();
                return true;
            }
        }
    };

    public static class fm {
        com.bytedance.sdk.component.ef.wsy fm;
        Bundle ro;

        public fm(com.bytedance.sdk.component.ef.wsy wsyVar, Bundle bundle) {
            this.fm = wsyVar;
            this.ro = bundle;
        }
    }

    private static void ro() {
        fm fmVar;
        com.bytedance.sdk.component.ef.wsy wsyVar;
        synchronized (ro) {
            while (true) {
                LinkedHashMap<String, SoftReference<fm>> linkedHashMap = ro;
                if (linkedHashMap.size() > fm) {
                    SoftReference<fm> softReferenceRemove = linkedHashMap.remove(linkedHashMap.keySet().iterator().next());
                    if (softReferenceRemove != null && (fmVar = softReferenceRemove.get()) != null && (wsyVar = fmVar.fm) != null) {
                        try {
                            com.bytedance.sdk.component.utils.gzf.fm(wsyVar);
                        } catch (Throwable unused) {
                        }
                    }
                }
            }
        }
    }

    public static void fm(com.bytedance.sdk.openadsdk.core.model.lse lseVar) {
        if (lseVar == null) {
            fm = 0;
        } else {
            fm = Math.max(0, lseVar.wsy());
        }
        ro();
    }

    public static void fm(String str, com.bytedance.sdk.component.ef.wsy wsyVar, Bundle bundle) {
        if (fm <= 0 || TextUtils.isEmpty(str) || wsyVar == null) {
            return;
        }
        LinkedHashMap<String, SoftReference<fm>> linkedHashMap = ro;
        synchronized (linkedHashMap) {
            try {
                fm(wsyVar.getWebView());
                fm(wsyVar, wsyVar.getContext());
                synchronized (linkedHashMap) {
                    linkedHashMap.put(str, new SoftReference<>(new fm(wsyVar, bundle)));
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        linkedHashMap.size();
    }

    public static com.bytedance.sdk.component.ef.wsy fm(Context context, String str) {
        com.bytedance.sdk.component.ef.wsy wsyVar;
        if (fm <= 0 || TextUtils.isEmpty(str)) {
            return null;
        }
        LinkedHashMap<String, SoftReference<fm>> linkedHashMap = ro;
        synchronized (linkedHashMap) {
            try {
                SoftReference<fm> softReferenceRemove = linkedHashMap.remove(str);
                if (softReferenceRemove == null) {
                    return null;
                }
                fm fmVar = softReferenceRemove.get();
                if (fmVar != null && (wsyVar = fmVar.fm) != null) {
                    fm(wsyVar, context);
                    return fmVar.fm;
                }
                return null;
            } finally {
            }
        }
    }

    public static Bundle fm(String str) {
        if (fm <= 0 || TextUtils.isEmpty(str)) {
            return null;
        }
        LinkedHashMap<String, SoftReference<fm>> linkedHashMap = ro;
        synchronized (linkedHashMap) {
            try {
                SoftReference<fm> softReference = linkedHashMap.get(str);
                if (softReference == null) {
                    return null;
                }
                fm fmVar = softReference.get();
                return fmVar != null ? fmVar.ro : null;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    private static void fm(com.bytedance.sdk.component.ef.wsy wsyVar, Context context) {
        if (wsyVar == null || context == null) {
            return;
        }
        try {
            if (wsyVar.getContext() instanceof MutableContextWrapper) {
                ((MutableContextWrapper) wsyVar.getContext()).setBaseContext(context.getApplicationContext());
            }
        } catch (ClassCastException unused) {
        }
        ViewGroup viewGroup = (ViewGroup) wsyVar.getParent();
        if (viewGroup != null) {
            try {
                viewGroup.removeView(wsyVar);
            } catch (Throwable unused2) {
            }
        }
    }

    private static void fm(WebView webView) {
        if (webView != null && fm > 0) {
            try {
                webView.evaluateJavascript("[].forEach.call(document.querySelectorAll('audio,video'), function(audio) { try { audio.pause(); } catch(e) {} })", null);
            } catch (Throwable unused) {
            }
        }
    }
}
