package com.bytedance.sdk.openadsdk.utils;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;

/* JADX INFO: loaded from: classes2.dex */
public class yz {

    public static class fm implements View.OnLayoutChangeListener {
        private final Drawable fm;

        /* JADX INFO: renamed from: lb, reason: collision with root package name */
        private int f7573lb;
        private int ro;

        public fm(Drawable drawable) {
            this.fm = drawable;
        }

        @Override // android.view.View.OnLayoutChangeListener
        public void onLayoutChange(View view, int i10, int i11, int i12, int i13, int i14, int i15, int i16, int i17) {
            int i18 = i12 - i10;
            int i19 = i13 - i11;
            if (i18 == this.ro && i19 == this.f7573lb) {
                return;
            }
            this.ro = i18;
            this.f7573lb = i19;
            this.fm.setBounds(0, 0, i18, i19);
        }
    }

    public static void fm(ViewGroup viewGroup, com.bytedance.sdk.openadsdk.core.model.lse lseVar) {
        if (viewGroup == null || lseVar == null || TextUtils.isEmpty(lseVar.ai())) {
            return;
        }
        try {
            int i10 = qhl.ynh;
            if (viewGroup.getTag(i10) != null) {
                return;
            }
            viewGroup.setTag(i10, Integer.valueOf(i10));
            Drawable drawableFm = fm(viewGroup.getResources(), lseVar);
            if (drawableFm == null) {
                return;
            }
            viewGroup.setForeground(drawableFm);
        } catch (Throwable th) {
            com.bytedance.sdk.component.utils.wey.lb("add overlay fail", th.getMessage());
        }
    }

    public static void fm(Activity activity, com.bytedance.sdk.openadsdk.core.model.lse lseVar) {
        if (activity == null || lseVar == null || TextUtils.isEmpty(lseVar.ai())) {
            return;
        }
        try {
            View decorView = activity.getWindow().getDecorView();
            int i10 = qhl.ynh;
            if (decorView.getTag(i10) != null) {
                return;
            }
            activity.getWindow().getDecorView().setTag(i10, Integer.valueOf(i10));
            Drawable drawableFm = fm(activity.getResources(), lseVar);
            if (drawableFm == null) {
                return;
            }
            activity.getWindow().getDecorView().setForeground(drawableFm);
        } catch (Throwable th) {
            com.bytedance.sdk.component.utils.wey.lb("add overlay fail", th.getMessage());
        }
    }

    @Nullable
    private static Drawable fm(Resources resources, com.bytedance.sdk.openadsdk.core.model.lse lseVar) {
        try {
            String strAi = lseVar.ai();
            if (TextUtils.isEmpty(strAi)) {
                return null;
            }
            byte[] bArrDecode = Base64.decode(strAi, 0);
            BitmapDrawable bitmapDrawable = new BitmapDrawable(resources, BitmapFactory.decodeByteArray(bArrDecode, 0, bArrDecode.length));
            Shader.TileMode tileMode = Shader.TileMode.REPEAT;
            bitmapDrawable.setTileModeXY(tileMode, tileMode);
            bitmapDrawable.setTargetDensity(resources.getDisplayMetrics());
            return bitmapDrawable;
        } catch (Throwable unused) {
            return null;
        }
    }
}
