z.java 源代码


package com.moat.analytics.mobile.inm;

import android.app.Activity;
import android.graphics.Rect;
import android.location.Location;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import com.applovin.impl.sdk.utils.JsonUtils;
import com.mbridge.msdk.MBridgeConstans;
import com.x8zs.plugin.android.net.http.Headers;
import java.lang.ref.WeakReference;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.json.JSONObject;

public class z {

    private JSONObject f20041c;

    private Rect f20042d;

    private Rect f20043e;

    private JSONObject f20044f;

    private JSONObject f20045g;

    private Location f20046h;

    private Map<String, Object> f20047i = new HashMap();

    String f20039a = JsonUtils.EMPTY_JSON;

    private c f20040b = new c();

    public static class a {

        int f20048a = 0;

        final Set<Rect> f20049b = new HashSet();

        boolean f20050c = false;

        a() {
        }
    }

    public static class b {

        final View f20051a;

        final Rect f20052b;

        b(View view, b bVar) {
            Rect k6;
            this.f20051a = view;
            if (bVar == null) {
                k6 = z.k(view);
            } else {
                Rect rect = bVar.f20052b;
                k6 = z.b(view, rect.left, rect.top);
            }
            this.f20052b = k6;
        }
    }

    public static class c {

        Rect f20053a = new Rect(0, 0, 0, 0);

        double f20054b = 0.0d;

        double f20055c = 0.0d;

        c() {
        }
    }

    @VisibleForTesting
    static int a(Rect rect, Set<Rect> set) {
        int i6 = 0;
        if (set.isEmpty()) {
            return 0;
        }
        ArrayList<Rect> arrayList = new ArrayList();
        arrayList.addAll(set);
        Collections.sort(arrayList, new Comparator<Rect>() {
            @Override
            public final int compare(Rect rect2, Rect rect3) {
                return Integer.valueOf(rect2.top).compareTo(Integer.valueOf(rect3.top));
            }
        });
        ArrayList arrayList2 = new ArrayList();
        for (Rect rect2 : arrayList) {
            arrayList2.add(Integer.valueOf(rect2.left));
            arrayList2.add(Integer.valueOf(rect2.right));
        }
        Collections.sort(arrayList2);
        int i7 = 0;
        while (i6 < arrayList2.size() - 1) {
            int i8 = i6 + 1;
            if (!((Integer) arrayList2.get(i6)).equals(arrayList2.get(i8))) {
                Rect rect3 = new Rect(((Integer) arrayList2.get(i6)).intValue(), rect.top, ((Integer) arrayList2.get(i8)).intValue(), rect.bottom);
                int i9 = rect.top;
                for (Rect rect4 : arrayList) {
                    if (Rect.intersects(rect4, rect3)) {
                        if (rect4.bottom > i9) {
                            i7 += rect3.width() * (rect4.bottom - Math.max(i9, rect4.top));
                            i9 = rect4.bottom;
                        }
                        if (rect4.bottom != rect3.bottom) {
                        }
                    }
                }
            }
            i6 = i8;
        }
        return i7;
    }

    private static int b(Rect rect) {
        return rect.width() * rect.height();
    }

    private static boolean c(View view) {
        if (view != null && view.isAttachedToWindow()) {
            return true;
        }
        return false;
    }

    private static boolean d(View view) {
        if (view != null && view.hasWindowFocus()) {
            return true;
        }
        return false;
    }

    private static boolean e(View view) {
        if (view != null && view.isShown()) {
            return false;
        }
        return true;
    }

    private static float f(View view) {
        if (view == null) {
            return 0.0f;
        }
        return g(view);
    }

    private static float g(View view) {
        float alpha = view.getAlpha();
        while (view != null && view.getParent() != null && alpha != 0.0d && (view.getParent() instanceof View)) {
            alpha *= ((View) view.getParent()).getAlpha();
            view = (View) view.getParent();
        }
        return alpha;
    }

    private static boolean h(View view) {
        if (view.isShown() && view.getAlpha() > 0.0d) {
            return true;
        }
        return false;
    }

    private static ArrayDeque<View> i(@NonNull View view) {
        ArrayDeque<View> arrayDeque = new ArrayDeque<>();
        int i6 = 0;
        View view2 = view;
        while (true) {
            if (view2.getParent() == null && view2 != view.getRootView()) {
                break;
            }
            i6++;
            if (i6 > 50) {
                p.a(3, "VisibilityInfo", (Object) null, "Short-circuiting chain retrieval, reached max");
                break;
            }
            arrayDeque.add(view2);
            if (!(view2.getParent() instanceof View)) {
                break;
            }
            view2 = (View) view2.getParent();
        }
        return arrayDeque;
    }

    private static boolean j(View view) {
        if (view.getBackground() != null && view.getBackground().getAlpha() != 0) {
            return false;
        }
        return true;
    }

    public static Rect k(View view) {
        int[] iArr = {Integer.MIN_VALUE, Integer.MIN_VALUE};
        view.getLocationOnScreen(iArr);
        int i6 = iArr[0];
        int i7 = iArr[1];
        return new Rect(i6, i7, view.getWidth() + i6, view.getHeight() + i7);
    }

    private static DisplayMetrics l(View view) {
        Activity activity;
        WeakReference<Activity> weakReference = com.moat.analytics.mobile.inm.a.f19851a;
        if (weakReference != null && (activity = weakReference.get()) != null) {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            activity.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
            return displayMetrics;
        }
        return view.getContext().getResources().getDisplayMetrics();
    }

    private static Rect a(DisplayMetrics displayMetrics) {
        return new Rect(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
    }

    private static Rect b(Rect rect, DisplayMetrics displayMetrics) {
        float f6 = displayMetrics.density;
        if (f6 == 0.0f) {
            return rect;
        }
        return new Rect(Math.round(rect.left / f6), Math.round(rect.top / f6), Math.round(rect.right / f6), Math.round(rect.bottom / f6));
    }

    public static Rect a(View view) {
        return view != null ? k(view) : new Rect(0, 0, 0, 0);
    }

    public static Rect b(View view, int i6, int i7) {
        int left = i6 + view.getLeft();
        int top = i7 + view.getTop();
        return new Rect(left, top, view.getWidth() + left, view.getHeight() + top);
    }

    @VisibleForTesting
    static a a(Rect rect, @NonNull View view) {
        a aVar = new a();
        try {
            ArrayDeque<View> i6 = i(view);
            if (i6 != null && !i6.isEmpty()) {
                p.b(2, "VisibilityInfo", view, "starting covering rect search");
                b bVar = null;
                loop0: while (true) {
                    if (i6.isEmpty()) {
                        break;
                    }
                    View pollLast = i6.pollLast();
                    b bVar2 = new b(pollLast, bVar);
                    if (pollLast.getParent() != null && (pollLast.getParent() instanceof ViewGroup)) {
                        ViewGroup viewGroup = (ViewGroup) pollLast.getParent();
                        int childCount = viewGroup.getChildCount();
                        boolean z6 = false;
                        for (int i7 = 0; i7 < childCount; i7++) {
                            if (aVar.f20048a >= 500) {
                                break loop0;
                            }
                            View childAt = viewGroup.getChildAt(i7);
                            if (childAt == pollLast) {
                                z6 = true;
                            } else {
                                aVar.f20048a++;
                                if (a(childAt, pollLast, z6)) {
                                    b(new b(childAt, bVar), rect, aVar);
                                    if (aVar.f20050c) {
                                        return aVar;
                                    }
                                } else {
                                    continue;
                                }
                            }
                        }
                    }
                    bVar = bVar2;
                }
            }
            return aVar;
        } catch (Exception e6) {
            m.a(e6);
        }
        return aVar;
    }

    private static Map<String, String> b(Location location) {
        if (location == null) {
            return null;
        }
        HashMap hashMap = new HashMap();
        hashMap.put("latitude", Double.toString(location.getLatitude()));
        hashMap.put("longitude", Double.toString(location.getLongitude()));
        hashMap.put("timestamp", Long.toString(location.getTime()));
        hashMap.put("horizontalAccuracy", Float.toString(location.getAccuracy()));
        return hashMap;
    }

    private static c a(View view, Rect rect, boolean z6, boolean z7, boolean z8) {
        c cVar = new c();
        int b6 = b(rect);
        if (view != null && z6 && z7 && !z8 && b6 > 0) {
            Rect rect2 = new Rect(0, 0, 0, 0);
            if (a(view, rect2)) {
                int b7 = b(rect2);
                if (b7 < b6) {
                    p.b(2, "VisibilityInfo", null, "Ad is clipped");
                }
                if (view.getRootView() instanceof ViewGroup) {
                    cVar.f20053a = rect2;
                    a a7 = a(rect2, view);
                    if (a7.f20050c) {
                        cVar.f20055c = 1.0d;
                    } else {
                        int a8 = a(rect2, a7.f20049b);
                        if (a8 > 0) {
                            cVar.f20055c = a8 / (b7 * 1.0d);
                        }
                        cVar.f20054b = (b7 - a8) / (b6 * 1.0d);
                    }
                }
            }
        }
        return cVar;
    }

    private static void b(b bVar, Rect rect, a aVar) {
        if (h(bVar.f20051a)) {
            View view = bVar.f20051a;
            boolean z6 = true;
            if (view instanceof ViewGroup) {
                boolean z7 = (ViewGroup.class.equals(view.getClass().getSuperclass()) && j(bVar.f20051a)) ? false : true;
                ViewGroup viewGroup = (ViewGroup) bVar.f20051a;
                int childCount = viewGroup.getChildCount();
                for (int i6 = 0; i6 < childCount; i6++) {
                    int i7 = aVar.f20048a + 1;
                    aVar.f20048a = i7;
                    if (i7 > 500) {
                        return;
                    }
                    b(new b(viewGroup.getChildAt(i6), bVar), rect, aVar);
                    if (aVar.f20050c) {
                        return;
                    }
                }
                z6 = z7;
            }
            if (z6) {
                a(bVar, rect, aVar);
            }
        }
    }

    private static Map<String, String> a(Rect rect) {
        HashMap hashMap = new HashMap();
        hashMap.put("x", String.valueOf(rect.left));
        hashMap.put("y", String.valueOf(rect.top));
        hashMap.put("w", String.valueOf(rect.right - rect.left));
        hashMap.put("h", String.valueOf(rect.bottom - rect.top));
        return hashMap;
    }

    private static Map<String, String> a(Rect rect, DisplayMetrics displayMetrics) {
        return a(b(rect, displayMetrics));
    }

    private static JSONObject a(Location location) {
        Map<String, String> b6 = b(location);
        if (b6 == null) {
            return null;
        }
        return new JSONObject(b6);
    }

    private static void a(b bVar, Rect rect, a aVar) {
        Rect rect2 = bVar.f20052b;
        if (rect2.setIntersect(rect, rect2)) {
            Rect rect3 = new Rect(0, 0, 0, 0);
            if (a(bVar.f20051a, rect3)) {
                Rect rect4 = bVar.f20052b;
                if (rect4.setIntersect(rect3, rect4)) {
                    if (w.a().f20001c) {
                        View view = bVar.f20051a;
                        p.b(2, "VisibilityInfo", view, String.format(Locale.ROOT, "Covered by %s-%s alpha=%f", view.getClass().getName(), rect4.toString(), Float.valueOf(bVar.f20051a.getAlpha())));
                    }
                    aVar.f20049b.add(rect4);
                    if (rect4.contains(rect)) {
                        aVar.f20050c = true;
                    }
                }
            }
        }
    }

    public void a(String str, View view) {
        HashMap hashMap = new HashMap();
        String str2 = JsonUtils.EMPTY_JSON;
        if (view != null) {
            try {
                DisplayMetrics l6 = l(view);
                boolean c6 = c(view);
                boolean d6 = d(view);
                boolean e6 = e(view);
                float f6 = f(view);
                hashMap.put("dr", Float.valueOf(l6.density));
                hashMap.put("dv", Double.valueOf(s.a()));
                hashMap.put("adKey", str);
                boolean z6 = true;
                boolean z7 = false;
                hashMap.put("isAttached", Integer.valueOf(c6 ? 1 : 0));
                hashMap.put("inFocus", Integer.valueOf(d6 ? 1 : 0));
                hashMap.put("isHidden", Integer.valueOf(e6 ? 1 : 0));
                hashMap.put("opacity", Float.valueOf(f6));
                Rect a7 = a(l6);
                Rect a8 = a(view);
                c a9 = a(view, a8, c6, d6, e6);
                if (this.f20041c != null) {
                    double d7 = a9.f20054b;
                    c cVar = this.f20040b;
                    if (d7 == cVar.f20054b) {
                        if (a9.f20053a.equals(cVar.f20053a)) {
                        }
                    }
                }
                this.f20040b = a9;
                this.f20041c = new JSONObject(a(this.f20040b.f20053a, l6));
                z7 = true;
                hashMap.put("coveredPercent", Double.valueOf(a9.f20055c));
                if (this.f20045g == null || !a7.equals(this.f20043e)) {
                    this.f20043e = a7;
                    this.f20045g = new JSONObject(a(a7, l6));
                    z7 = true;
                }
                if (this.f20044f == null || !a8.equals(this.f20042d)) {
                    this.f20042d = a8;
                    this.f20044f = new JSONObject(a(a8, l6));
                    z7 = true;
                }
                Map<String, Object> map = this.f20047i;
                if (map == null || !hashMap.equals(map)) {
                    this.f20047i = hashMap;
                    z7 = true;
                }
                Location b6 = o.a().b();
                if (o.a(b6, this.f20046h)) {
                    z6 = z7;
                } else {
                    this.f20046h = b6;
                }
                if (z6) {
                    JSONObject jSONObject = new JSONObject(this.f20047i);
                    jSONObject.accumulate("screen", this.f20045g);
                    jSONObject.accumulate(MBridgeConstans.DYNAMIC_VIEW_KEY_VIEW, this.f20044f);
                    jSONObject.accumulate("visible", this.f20041c);
                    jSONObject.accumulate("maybe", this.f20041c);
                    jSONObject.accumulate("visiblePercent", Double.valueOf(this.f20040b.f20054b));
                    if (b6 != null) {
                        jSONObject.accumulate(Headers.LOCATION, a(b6));
                    }
                    str2 = jSONObject.toString();
                    this.f20039a = str2;
                }
            } catch (Exception e7) {
                m.a(e7);
                this.f20039a = str2;
            }
        }
    }

    private static boolean a(View view, Rect rect) {
        if (!view.getGlobalVisibleRect(rect)) {
            return false;
        }
        int[] iArr = {Integer.MIN_VALUE, Integer.MIN_VALUE};
        view.getLocationInWindow(iArr);
        int[] iArr2 = {Integer.MIN_VALUE, Integer.MIN_VALUE};
        view.getLocationOnScreen(iArr2);
        rect.offset(iArr2[0] - iArr[0], iArr2[1] - iArr[1]);
        return true;
    }

    private static boolean a(View view, View view2, boolean z6) {
        float z7 = view.getZ();
        float z8 = view2.getZ();
        return z6 ? z7 >= z8 : z7 > z8;
    }
}