package G0;

import H0.q;
import Q0.b;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.IBinder;
import android.os.SystemProperties;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.IWindowManager;
import android.view.SurfaceControl;
import android.view.WindowManager;
import androidx.core.view.ViewCompat;
import androidx.exifinterface.media.ExifInterface;
import com.droidlogic.app.ScreenControlManager;
import com.seewo.sdk.model.SDKShotType;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;

/* JADX INFO: loaded from: classes.dex */
class c {

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private static final SimpleDateFormat f753c = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static final SimpleDateFormat f754d = new SimpleDateFormat("yyyy-MM-dd");

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private Bitmap.CompressFormat f755a = Bitmap.CompressFormat.PNG;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private boolean f756b = true;

    static /* synthetic */ class a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        static final /* synthetic */ int[] f757a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        static final /* synthetic */ int[] f758b;

        static {
            int[] iArr = new int[b.a.values().length];
            f758b = iArr;
            try {
                iArr[b.a.NATIVE.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                f758b[b.a.VIRTUAL_DISPLAY.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            int[] iArr2 = new int[SDKShotType.values().length];
            f757a = iArr2;
            try {
                iArr2[SDKShotType.VIDEO_ONLY.ordinal()] = 1;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                f757a[SDKShotType.OSD_ONLY.ordinal()] = 2;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                f757a[SDKShotType.VIDEO_AND_OSD.ordinal()] = 3;
            } catch (NoSuchFieldError unused5) {
            }
        }
    }

    c() {
    }

    private Bitmap a(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        matrix.postScale(1920.0f / width, 1080.0f / height);
        return m.f814a.d() ? Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true) : Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
    }

    private Bitmap c(int i2, int i3) {
        try {
            Class<?> cls = Class.forName("android.view.SurfaceControl");
            Class cls2 = Integer.TYPE;
            return (Bitmap) cls.getMethod("screenshot", cls2, cls2).invoke(null, Integer.valueOf(i2), Integer.valueOf(i3));
        } catch (Exception e2) {
            Log.e("InternalScreenShotHelper", "screenshot error!", e2);
            return null;
        }
    }

    private Bitmap d(Context context, int i2, int i3, boolean z2, boolean z3) {
        return Build.VERSION.SDK_INT >= 34 ? h(context, i2, i3, z2, z3) : g(context, i2, i3, z2, z3);
    }

    private Bitmap e(int i2, int i3) {
        Log.i("InternalScreenShotHelper", "invokeScreenshotSDK28: ");
        Rect rect = new Rect(0, 0, 0, 0);
        try {
            Class<?> cls = Class.forName("android.view.SurfaceControl");
            Class cls2 = Integer.TYPE;
            return (Bitmap) cls.getDeclaredMethod("screenshot", Rect.class, cls2, cls2, cls2).invoke(null, rect, Integer.valueOf(i2), Integer.valueOf(i3), 0);
        } catch (Exception e2) {
            Log.e("InternalScreenShotHelper", "screenshot error!", e2);
            return null;
        }
    }

    private Bitmap f(Context context, int i2, int i3) {
        SystemProperties.set("persist.sys.screencap.mode", ExifInterface.GPS_MEASUREMENT_2D);
        Display display = ((DisplayManager) context.getSystemService("display")).getDisplay(0);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getRealMetrics(displayMetrics);
        int i4 = displayMetrics.widthPixels;
        int i5 = displayMetrics.heightPixels;
        Bitmap bitmap = null;
        try {
            Object objInvoke = Class.forName("android.view.Display").getDeclaredMethod("getAddress", null).invoke(display, null);
            Class<?> cls = Class.forName("android.view.DisplayAddress$Physical");
            if (cls.isInstance(objInvoke)) {
                Long l2 = (Long) cls.getDeclaredMethod("getPhysicalDisplayId", null).invoke(objInvoke, null);
                l2.longValue();
                Class<?> cls2 = Class.forName("android.view.SurfaceControl");
                IBinder iBinder = (IBinder) cls2.getDeclaredMethod("getPhysicalDisplayToken", Long.TYPE).invoke(null, l2);
                Class<?> cls3 = Class.forName("android.view.SurfaceControl$DisplayCaptureArgs$Builder");
                Object objNewInstance = cls3.getDeclaredConstructor(IBinder.class).newInstance(iBinder);
                Method declaredMethod = cls3.getDeclaredMethod("setSourceCrop", Rect.class);
                Class cls4 = Integer.TYPE;
                Object objInvoke2 = cls3.getDeclaredMethod("build", null).invoke(cls3.getDeclaredMethod("setSize", cls4, cls4).invoke(declaredMethod.invoke(objNewInstance, new Rect(0, 0, i4, i5)), Integer.valueOf(i2), Integer.valueOf(i3)), null);
                Object objInvoke3 = cls2.getDeclaredMethod("captureDisplay", objInvoke2.getClass()).invoke(null, objInvoke2);
                if (objInvoke3 != null) {
                    bitmap = (Bitmap) Class.forName("android.view.SurfaceControl$ScreenshotHardwareBuffer").getDeclaredMethod("asBitmap", null).invoke(objInvoke3, null);
                }
            } else {
                Log.e("InternalScreenShotHelper", "Skipping Screenshot - Default display does not have a physical address: " + display);
            }
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        SystemProperties.set("persist.sys.screencap.mode", "1");
        return bitmap;
    }

    private Bitmap g(Context context, int i2, int i3, boolean z2, boolean z3) {
        try {
            Log.i("InternalScreenShotHelper", "invokeScreenshotSDK33");
            DisplayManager displayManager = (DisplayManager) context.getSystemService("display");
            Display displayB = b(context);
            if (displayB == null) {
                displayB = displayManager.getDisplay(0);
            }
            IBinder iBinder = (IBinder) SurfaceControl.class.getMethod("getPhysicalDisplayToken", Long.TYPE).invoke(null, Class.forName("android.view.DisplayAddress$Physical").getMethod("getPhysicalDisplayId", null).invoke(displayB.getClass().getMethod("getAddress", null).invoke(displayB, null), null));
            Class<?> cls = Class.forName("android.view.SurfaceControl$DisplayCaptureArgs$Builder");
            Object objNewInstance = cls.getConstructor(IBinder.class).newInstance(iBinder);
            cls.getMethod("setSourceCrop", Rect.class).invoke(objNewInstance, new Rect(0, 0, i2, i3));
            Class cls2 = Integer.TYPE;
            cls.getMethod("setSize", cls2, cls2).invoke(objNewInstance, Integer.valueOf(i2), Integer.valueOf(i3));
            Object objInvoke = cls.getMethod("build", null).invoke(objNewInstance, null);
            Object objInvoke2 = SurfaceControl.class.getMethod("captureDisplay", objInvoke.getClass()).invoke(null, objInvoke);
            Bitmap bitmapA = (Bitmap) objInvoke2.getClass().getMethod("asBitmap", null).invoke(objInvoke2, null);
            if (bitmapA != null && z3 && bitmapA.getWidth() > i2) {
                bitmapA = a(bitmapA);
            }
            return (!z2 && z3 && i()) ? j(bitmapA) : bitmapA;
        } catch (Exception e2) {
            Log.e("InternalScreenShotHelper", "captureScreen failed", e2);
            return null;
        }
    }

    private Bitmap h(Context context, int i2, int i3, boolean z2, boolean z3) {
        try {
            Q0.e.f("InternalScreenShotHelper", "invokeScreenshotSDK34");
            Class<?> cls = Class.forName("android.window.ScreenCapture$CaptureArgs$Builder");
            Object objNewInstance = cls.getConstructor(null).newInstance(null);
            cls.getMethod("setSourceCrop", Rect.class).invoke(objNewInstance, new Rect(0, 0, i2, i3));
            Object objInvoke = cls.getMethod("build", null).invoke(objNewInstance, null);
            Method declaredMethod = Class.forName("android.window.ScreenCapture").getDeclaredMethod("createSyncCaptureListener", null);
            declaredMethod.setAccessible(true);
            Object objInvoke2 = declaredMethod.invoke(null, null);
            Method declaredMethod2 = IWindowManager.class.getDeclaredMethod("captureDisplay", Integer.TYPE, Class.forName("android.window.ScreenCapture$CaptureArgs"), Class.forName("android.window.ScreenCapture$ScreenCaptureListener"));
            declaredMethod2.setAccessible(true);
            Method declaredMethod3 = Class.forName("android.view.WindowManagerGlobal").getDeclaredMethod("getWindowManagerService", null);
            declaredMethod3.setAccessible(true);
            Object objInvoke3 = declaredMethod3.invoke(null, null);
            Display displayB = b(context);
            declaredMethod2.invoke(objInvoke3, Integer.valueOf(displayB != null ? displayB.getDisplayId() : 0), objInvoke, objInvoke2);
            Method declaredMethod4 = objInvoke2.getClass().getDeclaredMethod("getBuffer", null);
            declaredMethod4.setAccessible(true);
            Object objInvoke4 = declaredMethod4.invoke(objInvoke2, null);
            Bitmap bitmapA = (Bitmap) objInvoke4.getClass().getMethod("asBitmap", null).invoke(objInvoke4, null);
            if (bitmapA != null && z3 && bitmapA.getWidth() > i2) {
                bitmapA = a(bitmapA);
            }
            return (!z2 && z3 && i()) ? j(bitmapA) : bitmapA;
        } catch (Exception e2) {
            Q0.e.c("InternalScreenShotHelper", "captureScreen failed", e2);
            return null;
        }
    }

    private static boolean i() {
        if (n.f815a.f() != q.ANDROID) {
            m mVar = m.f814a;
            if (mVar.f() && mVar.c() == H0.n.SOLUTION_4_3 && Q0.a.h()) {
                return true;
            }
        }
        return false;
    }

    private static Bitmap j(Bitmap bitmap) {
        RectF rectF = new RectF();
        rectF.left = 240.0f;
        rectF.top = 0.0f;
        rectF.right = 1920.0f - 240.0f;
        rectF.bottom = 1080.0f;
        Bitmap bitmapCreateBitmap = Bitmap.createBitmap(1920, 1080, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmapCreateBitmap);
        canvas.drawColor(ViewCompat.MEASURED_STATE_MASK);
        canvas.drawBitmap(bitmap, (Rect) null, rectF, (Paint) null);
        canvas.setBitmap(null);
        return bitmapCreateBitmap;
    }

    private boolean n() {
        return Q0.d.b() || Q0.a.f() || Q0.a.e();
    }

    private Bitmap o(Context context, int i2, int i3, boolean z2, boolean z3) {
        return p(context, i2, i3, z2, z3, SDKShotType.VIDEO_AND_OSD);
    }

    private Bitmap p(Context context, int i2, int i3, boolean z2, boolean z3, SDKShotType sDKShotType) {
        Bitmap bitmapC;
        int i4 = a.f758b[Q0.b.a().ordinal()];
        if (i4 == 1) {
            return h(context, i2, i3, z2, z3);
        }
        if (i4 == 2) {
            return S0.b.e().h(context);
        }
        if (Q0.b.b() || (!n() && Build.VERSION.SDK_INT > 31)) {
            return sDKShotType != SDKShotType.OSD_ONLY ? S0.b.e().h(context) : d(context, i2, i3, z2, z3);
        }
        b bVar = b.f752a;
        bVar.d(sDKShotType);
        bVar.c();
        if (n()) {
            int i5 = SystemProperties.getInt("persist.sys.shot_type", sDKShotType.ordinal());
            Q0.e.f("InternalScreenShotHelper", "screenshotInner:972 outType:" + i5);
            bitmapC = ScreenControlManager.captureAmlogic(context, i2, i3, i5);
        } else {
            int i6 = Build.VERSION.SDK_INT;
            if (i6 == 31) {
                bitmapC = f(context, i2, i3);
            } else if (i6 > 27) {
                if (Q0.a.g()) {
                    Q0.e.f("InternalScreenShotHelper", "setScreenShotType: ");
                    int i7 = a.f757a[sDKShotType.ordinal()];
                    if (i7 == 1) {
                        q(ExifInterface.GPS_MEASUREMENT_2D);
                    } else if (i7 == 2) {
                        q("1");
                    } else if (i7 == 3) {
                        q("0");
                    }
                }
                bitmapC = e(i2, i3);
            } else {
                bitmapC = c(i2, i3);
            }
        }
        bVar.b();
        bVar.a();
        if (bitmapC != null && z3 && bitmapC.getWidth() > i2) {
            bitmapC = a(bitmapC);
        }
        return (!z2 && z3 && i()) ? j(bitmapC) : bitmapC;
    }

    private void q(String str) {
        try {
            SystemProperties.set("vendor.sys.hwc.vss.source", str);
        } catch (Exception unused) {
            if (Q0.d.c()) {
                return;
            }
            Q0.e.a("InternalScreenShotHelper", "set shot type by service : " + str);
            str.hashCode();
            switch (str) {
                case "0":
                    b.f752a.d(SDKShotType.VIDEO_AND_OSD);
                    break;
                case "1":
                    b.f752a.d(SDKShotType.OSD_ONLY);
                    break;
                case "2":
                    b.f752a.d(SDKShotType.VIDEO_ONLY);
                    break;
            }
        }
    }

    public Display b(Context context) {
        WindowManager windowManager = (WindowManager) context.getSystemService("window");
        if (windowManager == null) {
            return null;
        }
        return windowManager.getDefaultDisplay();
    }

    public Bitmap k(Context context) {
        return l(context, true);
    }

    public Bitmap l(Context context, boolean z2) {
        return m(context, z2, true);
    }

    public Bitmap m(Context context, boolean z2, boolean z3) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return o(context, displayMetrics.widthPixels, displayMetrics.heightPixels, z2, z3);
    }
}
