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 com.seewo.sdk.util.RLog;
import java.lang.ref.WeakReference;
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;
    static final int MSG_DATA_TYPE_AVC = 1;
    static final int MSG_DATA_TYPE_YUV = 0;
    public static final int REMOTE_EXCEPTION = -65535;
    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 AvcCallbackListener mAvcCallbackListener;
    private static final Object mLock = new Object();

    public interface AvcCallbackListener {
        void onAvcAvailable(byte[] bArr, int i, long j);
    }

    private native void native_ConnectScreenControl();

    private native void native_ForceStop();

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

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

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

    private native void native_SetRecordParameter(String[] strArr, Object[] objArr);

    private native void native_StartReceiver(WeakReference<ScreenControlManager> weakReference);

    private native int native_startAvcRecord(int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9);

    private native int native_startYuvRecord(int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8);

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

    private ScreenControlManager() {
        native_ConnectScreenControl();
    }

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

        private INSTANCE() {
        }
    }

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

    private static Bitmap invokeScreenshotByNative(int i, int i2, int i3) {
        Log.i(TAG, "native_ScreenCapBuffer " + i + "," + i2 + " type = " + i3);
        byte[] bArrNative_ScreenCapBuffer = INSTANCE.I.native_ScreenCapBuffer(0, 0, i, i2, i, i2, i3);
        if (bArrNative_ScreenCapBuffer == null) {
            Log.i(TAG, "invokeScreenshotByNative buffer is null");
            return null;
        }
        Bitmap bitmapCreateBitmap = Bitmap.createBitmap(i, i2, 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 invokeScreenshotSDK33(Context context, int i, int i2) {
        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, i, i2));
            cls.getMethod("setSize", Integer.TYPE, Integer.TYPE).invoke(objNewInstance, Integer.valueOf(i), Integer.valueOf(i2));
            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 e) {
            Log.e(TAG, "captureScreen failed", e);
            return null;
        }
    }

    private static Bitmap invokeScreenshotSDK28(int i, int i2) {
        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(i), Integer.valueOf(i2), 0);
        } catch (Exception e) {
            Log.e(TAG, "screenshot error!", e);
            return null;
        }
    }

    private static Bitmap adjustRotation(Bitmap bitmap, int i) {
        Matrix matrix = new Matrix();
        matrix.postRotate(i, 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;
    }

    /* JADX WARN: Removed duplicated region for block: B:96:0x0094 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:66:0x009e -> B:81:0x00b9). 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 = r2
        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
            com.seewo.sdk.util.RLog.e(r0, r1)
            goto L90
        L43:
            r1 = move-exception
            goto L4d
        L45:
            r1 = move-exception
            com.seewo.sdk.util.RLog.e(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
            com.seewo.sdk.util.RLog.e(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:
            com.seewo.sdk.util.RLog.e(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
            com.seewo.sdk.util.RLog.e(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
            com.seewo.sdk.util.RLog.e(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
            com.seewo.sdk.util.RLog.e(r0, r1)
            goto Lb9
        La2:
            r1 = move-exception
            goto Lae
        La4:
            r1 = move-exception
            com.seewo.sdk.util.RLog.e(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
            com.seewo.sdk.util.RLog.e(r0, r2)
        Lb8:
            throw r1
        Lb9:
            throw r2
        */
        throw new UnsupportedOperationException("Method not decompiled: com.droidlogic.app.ScreenControlManager.isReverseScreen():boolean");
    }

    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;
    }

    public static int startAvcScreenRecord(int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9) {
        RLog.d(TAG, "startAvcScreenRecord left:" + i + ",top:" + i2 + ",right:" + i3 + ",bottom:" + i4 + ",width:" + i5 + ",height:" + i6 + ",frameRate:" + i7 + ",bitRate:" + i8 + ",sourceType:" + i9);
        synchronized (mLock) {
            try {
                if (mAvcCallbackListener == null) {
                    return REMOTE_EXCEPTION;
                }
                INSTANCE.I.native_StartReceiver(new WeakReference<>(INSTANCE.I));
                return INSTANCE.I.native_startAvcRecord(i, i2, i3, i4, i5, i6, i7, i8, i9);
            } catch (Throwable th) {
                RLog.e(TAG, "startAvcScreenRecord: " + th);
                return REMOTE_EXCEPTION;
            }
        }
    }

    public static int stopScreenRecord() {
        synchronized (mLock) {
            try {
                INSTANCE.I.native_ForceStop();
            } catch (Throwable th) {
                RLog.e(TAG, "stopScreenRecord error!:" + th);
                return -1;
            }
        }
        return 0;
    }

    public static int setRecordParameter(String[] strArr, Object[] objArr) {
        synchronized (mLock) {
            try {
                INSTANCE.I.native_SetRecordParameter(strArr, objArr);
            } catch (Throwable th) {
                RLog.e(TAG, "setRecordParameter error!:" + th);
                return -1;
            }
        }
        return 0;
    }

    public static void setAvcCallbackListener(AvcCallbackListener avcCallbackListener) {
        mAvcCallbackListener = avcCallbackListener;
    }

    static int native_proc(Object obj, int i, int i2, long j, Object obj2) {
        RLog.d(TAG, "native_proc type:" + i + " frameType:" + i2 + " pts:" + j);
        if (i != 1) {
            return 0;
        }
        try {
            AvcCallbackListener avcCallbackListener = mAvcCallbackListener;
            if (avcCallbackListener == null) {
                return 0;
            }
            avcCallbackListener.onAvcAvailable((byte[]) obj2, i2, j);
            return 0;
        } catch (Throwable th) {
            RLog.e(TAG, "native_proc error!:" + th);
            return 0;
        }
    }
}
