package com.droidlogic.app;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.Rect;
import android.hardware.display.DisplayManager;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceControl;
import android.view.WindowManager;
import com.seewo.sdk.internal.model.SDKParsable;
import com.seewo.sdk.model.SDKShotType;
import java.nio.ByteBuffer;

/* JADX INFO: loaded from: classes.dex */
public class ScreenControlManager implements SDKParsable {
    public static final int AMLOGIC_CAPTURE_OSD_ONLY = 2;
    public static final int AMLOGIC_CAPTURE_VIDEO_AND_OSD = 1;
    public static final int AMLOGIC_CAPTURE_VIDEO_ONLY = 0;
    private static final int SDK_VERSION_ANDROID_11 = 30;
    private static final int SDK_VERSION_ANDROID_13 = 33;
    private static final int SDK_VERSION_ANDROID_14 = 34;
    private static final String TAG = "ScreenControlManager";

    private static class INSTANCE {
        private static final ScreenControlManager I = new ScreenControlManager();

        private INSTANCE() {
        }
    }

    static {
        System.loadLibrary("screencontrol_jni");
    }

    private static Bitmap adjustRotation(Bitmap bitmap, int i2) {
        Matrix matrix = new Matrix();
        matrix.postRotate(i2, bitmap.getWidth() / 2.0f, bitmap.getHeight() / 2.0f);
        Bitmap bitmapCreateBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        new Canvas(bitmapCreateBitmap).drawBitmap(bitmap, matrix, null);
        return bitmapCreateBitmap;
    }

    public static Bitmap captureAmlogic(Context context, int i2, int i3, int i4) {
        int i5 = i4 == SDKShotType.VIDEO_ONLY.ordinal() ? 0 : i4 == SDKShotType.OSD_ONLY.ordinal() ? 2 : 1;
        if (i5 == 2) {
            int i6 = Build.VERSION.SDK_INT;
            return i6 >= 34 ? invokeScreenshotByNative(i2, i3, i5) : i6 >= 33 ? invokeScreenshotSDK33(context, i2, i3) : i6 >= 30 ? invokeScreenshotSDK28(i2, i3) : SurfaceControl.screenshot(i2, i3);
        }
        Log.i(TAG, "captureAmlogic:type " + i5);
        return invokeScreenshotByNative(i2, i3, i5);
    }

    public static void getScreenInfo(Context context, int[] iArr) {
        Point point = new Point();
        ((WindowManager) context.getApplicationContext().getSystemService("window")).getDefaultDisplay().getSize(point);
        iArr[0] = point.x;
        iArr[1] = point.y;
    }

    private static Bitmap invokeScreenshotByNative(int i2, int i3, int i4) {
        Log.i(TAG, "native_ScreenCapBuffer " + i2 + "," + i3 + " type = " + i4);
        byte[] bArrNative_ScreenCapBuffer = INSTANCE.I.native_ScreenCapBuffer(0, 0, i2, i3, i2, i3, i4);
        if (bArrNative_ScreenCapBuffer == null) {
            Log.i(TAG, "invokeScreenshotByNative buffer is null");
            return null;
        }
        Bitmap bitmapCreateBitmap = Bitmap.createBitmap(i2, i3, Bitmap.Config.ARGB_8888);
        bitmapCreateBitmap.copyPixelsFromBuffer(ByteBuffer.wrap(bArrNative_ScreenCapBuffer));
        if (!isReverseScreen()) {
            return bitmapCreateBitmap;
        }
        Bitmap bitmapAdjustRotation = adjustRotation(bitmapCreateBitmap, 180);
        bitmapCreateBitmap.recycle();
        return bitmapAdjustRotation;
    }

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

    private static Bitmap invokeScreenshotSDK33(Context context, int i2, int i3) {
        try {
            Display display = ((DisplayManager) context.getSystemService("display")).getDisplay(0);
            IBinder iBinder = (IBinder) SurfaceControl.class.getMethod("getPhysicalDisplayToken", Long.TYPE).invoke(null, Class.forName("android.view.DisplayAddress$Physical").getMethod("getPhysicalDisplayId", new Class[0]).invoke(display.getClass().getMethod("getAddress", new Class[0]).invoke(display, new Object[0]), new Object[0]));
            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));
            cls.getMethod("setSize", Integer.TYPE, Integer.TYPE).invoke(objNewInstance, Integer.valueOf(i2), Integer.valueOf(i3));
            Object objInvoke = cls.getMethod("build", new Class[0]).invoke(objNewInstance, new Object[0]);
            Object objInvoke2 = SurfaceControl.class.getMethod("captureDisplay", objInvoke.getClass()).invoke(null, objInvoke);
            return (Bitmap) objInvoke2.getClass().getMethod("asBitmap", new Class[0]).invoke(objInvoke2, new Object[0]);
        } catch (Exception e2) {
            Log.e(TAG, "captureScreen failed", e2);
            return null;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:98:0x0094 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:66:0x009e -> B:83:0x00ba). Please report as a decompilation issue!!! */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    private static boolean isReverseScreen() throws java.lang.Throwable {
        /*
            java.lang.String r0 = "ScreenControlManager"
            java.io.File r1 = new java.io.File
            java.lang.String r2 = "/factorydata/tvconfig/panel/vbyone_1region.ini"
            r1.<init>(r2)
            boolean r2 = r1.exists()
            r3 = 0
            if (r2 != 0) goto L11
            return r3
        L11:
            r2 = 0
            java.io.FileInputStream r4 = new java.io.FileInputStream     // Catch: java.lang.Throwable -> L62 java.io.IOException -> L67
            r4.<init>(r1)     // Catch: java.lang.Throwable -> L62 java.io.IOException -> L67
            java.io.BufferedReader r1 = new java.io.BufferedReader     // Catch: java.lang.Throwable -> L58 java.io.IOException -> L5d
            java.io.InputStreamReader r5 = new java.io.InputStreamReader     // Catch: java.lang.Throwable -> L58 java.io.IOException -> L5d
            r5.<init>(r4)     // Catch: java.lang.Throwable -> L58 java.io.IOException -> L5d
            r1.<init>(r5)     // Catch: java.lang.Throwable -> L58 java.io.IOException -> L5d
        L21:
            java.lang.String r2 = r1.readLine()     // Catch: java.io.IOException -> L56 java.lang.Throwable -> L91
            if (r2 == 0) goto L35
            java.lang.String r2 = r2.trim()     // Catch: java.io.IOException -> L56 java.lang.Throwable -> L91
            java.lang.String r5 = "panel_reverse=1"
            boolean r2 = r2.equalsIgnoreCase(r5)     // Catch: java.io.IOException -> L56 java.lang.Throwable -> L91
            if (r2 == 0) goto L21
            r2 = 1
            r3 = 1
        L35:
            r1.close()     // Catch: java.lang.Throwable -> L43 java.io.IOException -> L45
            r4.close()     // Catch: java.io.IOException -> L3d
            goto L90
        L3d:
            r1 = move-exception
            e.c.d.y.e.d(r0, r1)
            goto L90
        L43:
            r1 = move-exception
            goto L4d
        L45:
            r1 = move-exception
            e.c.d.y.e.d(r0, r1)     // Catch: java.lang.Throwable -> L43
            r4.close()     // Catch: java.io.IOException -> L3d
            goto L90
        L4d:
            r4.close()     // Catch: java.io.IOException -> L51
            goto L55
        L51:
            r2 = move-exception
            e.c.d.y.e.d(r0, r2)
        L55:
            throw r1
        L56:
            r2 = move-exception
            goto L6b
        L58:
            r1 = move-exception
            r6 = r2
            r2 = r1
            r1 = r6
            goto L92
        L5d:
            r1 = move-exception
            r6 = r2
            r2 = r1
            r1 = r6
            goto L6b
        L62:
            r1 = move-exception
            r4 = r2
            r2 = r1
            r1 = r4
            goto L92
        L67:
            r1 = move-exception
            r4 = r2
            r2 = r1
            r1 = r4
        L6b:
            e.c.d.y.e.d(r0, r2)     // Catch: java.lang.Throwable -> L91
            if (r1 == 0) goto L90
            r1.close()     // Catch: java.lang.Throwable -> L79 java.io.IOException -> L7b
            if (r4 == 0) goto L90
            r4.close()     // Catch: java.io.IOException -> L3d
            goto L90
        L79:
            r1 = move-exception
            goto L85
        L7b:
            r1 = move-exception
            e.c.d.y.e.d(r0, r1)     // Catch: java.lang.Throwable -> L79
            if (r4 == 0) goto L90
            r4.close()     // Catch: java.io.IOException -> L3d
            goto L90
        L85:
            if (r4 == 0) goto L8f
            r4.close()     // Catch: java.io.IOException -> L8b
            goto L8f
        L8b:
            r2 = move-exception
            e.c.d.y.e.d(r0, r2)
        L8f:
            throw r1
        L90:
            return r3
        L91:
            r2 = move-exception
        L92:
            if (r1 == 0) goto Lb9
            r1.close()     // Catch: java.lang.Throwable -> La2 java.io.IOException -> La4
            if (r4 == 0) goto Lb9
            r4.close()     // Catch: java.io.IOException -> L9d
            goto Lb9
        L9d:
            r1 = move-exception
            e.c.d.y.e.d(r0, r1)
            goto Lb9
        La2:
            r1 = move-exception
            goto Lae
        La4:
            r1 = move-exception
            e.c.d.y.e.d(r0, r1)     // Catch: java.lang.Throwable -> La2
            if (r4 == 0) goto Lb9
            r4.close()     // Catch: java.io.IOException -> L9d
            goto Lb9
        Lae:
            if (r4 == 0) goto Lb8
            r4.close()     // Catch: java.io.IOException -> Lb4
            goto Lb8
        Lb4:
            r2 = move-exception
            e.c.d.y.e.d(r0, r2)
        Lb8:
            throw r1
        Lb9:
            goto Lbb
        Lba:
            throw r2
        Lbb:
            goto Lba
        */
        throw new UnsupportedOperationException("Method not decompiled: com.droidlogic.app.ScreenControlManager.isReverseScreen():boolean");
    }

    private native void native_ConnectScreenControl();

    private native void native_ForceStop();

    private native int native_ScreenCap(int i2, int i3, int i4, int i5, int i6, int i7, int i8, String str);

    private native byte[] native_ScreenCapBuffer(int i2, int i3, int i4, int i5, int i6, int i7, int i8);

    private native int native_ScreenRecord(int i2, int i3, int i4, int i5, int i6, int i7, String str);

    private ScreenControlManager() {
        native_ConnectScreenControl();
    }
}
