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;
/* loaded from: classes2.dex */
public class tN {
    public static void YFl(ViewGroup viewGroup, com.bytedance.sdk.openadsdk.core.model.Wwa wwa) {
        if (viewGroup == null || wwa == null || TextUtils.isEmpty(wwa.bwE())) {
            return;
        }
        try {
            if (viewGroup.getTag(GA.bwE) != null) {
                return;
            }
            int i = GA.bwE;
            viewGroup.setTag(i, Integer.valueOf(i));
            Drawable YFl2 = YFl(viewGroup.getResources(), wwa);
            if (YFl2 == null) {
                return;
            }
            viewGroup.setForeground(YFl2);
        } catch (Throwable th) {
            com.bytedance.sdk.component.utils.YoT.YFl("add overlay fail", th.getMessage());
        }
    }

    public static void YFl(Activity activity, com.bytedance.sdk.openadsdk.core.model.Wwa wwa) {
        if (activity == null || wwa == null || TextUtils.isEmpty(wwa.bwE())) {
            return;
        }
        try {
            if (activity.getWindow().getDecorView().getTag(GA.bwE) != null) {
                return;
            }
            View decorView = activity.getWindow().getDecorView();
            int i = GA.bwE;
            decorView.setTag(i, Integer.valueOf(i));
            Drawable YFl2 = YFl(activity.getResources(), wwa);
            if (YFl2 == null) {
                return;
            }
            activity.getWindow().getDecorView().setForeground(YFl2);
        } catch (Throwable th) {
            com.bytedance.sdk.component.utils.YoT.YFl("add overlay fail", th.getMessage());
        }
    }

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

    /* loaded from: classes2.dex */
    private static class YFl implements View.OnLayoutChangeListener {
        private int Sg;
        private final Drawable YFl;
        private int tN;

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

        @Override // android.view.View.OnLayoutChangeListener
        public void onLayoutChange(View view, int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8) {
            int i9 = i3 - i;
            int i10 = i4 - i2;
            if (i9 == this.Sg && i10 == this.tN) {
                return;
            }
            this.Sg = i9;
            this.tN = i10;
            this.YFl.setBounds(0, 0, i9, i10);
        }
    }
}
