package com.droidlogic.app;

import Q0.e;
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.mstar.android.tv.TvLanguage;
import com.seewo.sdk.internal.model.SDKParsable;
import com.seewo.sdk.model.SDKShotType;
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 i2, long j2);
    }

    private static class INSTANCE {

        /* JADX INFO: renamed from: I, reason: collision with root package name */
        private static final ScreenControlManager f3153I = new ScreenControlManager();

        private INSTANCE() {
        }
    }

    static {
        if (AmlUtils.isCaptureSoLibraryEffective()) {
            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 = AmlUtils.isCaptureSoLibraryEffective() ? INSTANCE.f3153I.native_ScreenCapBuffer(0, 0, i2, i3, i2, i3, i4) : ScreenControlManagerAn16.getInstance().startScreenCapBuffer(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, TvLanguage.FRISIANEASTERN);
        bitmapCreateBitmap.recycle();
        return bitmapAdjustRotation;
    }

    private static Bitmap invokeScreenshotSDK28(int i2, int i3) {
        Log.i(TAG, "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(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", null).invoke(display.getClass().getMethod("getAddress", null).invoke(display, 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);
            return (Bitmap) objInvoke2.getClass().getMethod("asBitmap", null).invoke(objInvoke2, null);
        } catch (Exception e2) {
            Log.e(TAG, "captureScreen failed", e2);
            return null;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:96:0x0095 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:67:0x009f -> B:82: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 -> L64 java.io.IOException -> L69
            r4.<init>(r1)     // Catch: java.lang.Throwable -> L64 java.io.IOException -> L69
            java.io.BufferedReader r1 = new java.io.BufferedReader     // Catch: java.lang.Throwable -> L5a java.io.IOException -> L5f
            java.io.InputStreamReader r5 = new java.io.InputStreamReader     // Catch: java.lang.Throwable -> L5a java.io.IOException -> L5f
            r5.<init>(r4)     // Catch: java.lang.Throwable -> L5a java.io.IOException -> L5f
            r1.<init>(r5)     // Catch: java.lang.Throwable -> L5a java.io.IOException -> L5f
        L21:
            java.lang.String r2 = r1.readLine()     // Catch: java.lang.Throwable -> L35 java.io.IOException -> L38
            if (r2 == 0) goto L3a
            java.lang.String r2 = r2.trim()     // Catch: java.lang.Throwable -> L35 java.io.IOException -> L38
            java.lang.String r5 = "panel_reverse=1"
            boolean r2 = r2.equalsIgnoreCase(r5)     // Catch: java.lang.Throwable -> L35 java.io.IOException -> L38
            if (r2 == 0) goto L21
            r3 = 1
            goto L3a
        L35:
            r2 = move-exception
            goto L93
        L38:
            r2 = move-exception
            goto L6d
        L3a:
            r1.close()     // Catch: java.lang.Throwable -> L47 java.io.IOException -> L49
            r4.close()     // Catch: java.io.IOException -> L42
            goto L92
        L42:
            r1 = move-exception
            Q0.e.d(r0, r1)
            goto L92
        L47:
            r1 = move-exception
            goto L51
        L49:
            r1 = move-exception
            Q0.e.d(r0, r1)     // Catch: java.lang.Throwable -> L47
            r4.close()     // Catch: java.io.IOException -> L42
            goto L92
        L51:
            r4.close()     // Catch: java.io.IOException -> L55
            goto L59
        L55:
            r2 = move-exception
            Q0.e.d(r0, r2)
        L59:
            throw r1
        L5a:
            r1 = move-exception
            r6 = r2
            r2 = r1
            r1 = r6
            goto L93
        L5f:
            r1 = move-exception
            r6 = r2
            r2 = r1
            r1 = r6
            goto L6d
        L64:
            r1 = move-exception
            r4 = r2
            r2 = r1
            r1 = r4
            goto L93
        L69:
            r1 = move-exception
            r4 = r2
            r2 = r1
            r1 = r4
        L6d:
            Q0.e.d(r0, r2)     // Catch: java.lang.Throwable -> L35
            if (r1 == 0) goto L92
            r1.close()     // Catch: java.lang.Throwable -> L7b java.io.IOException -> L7d
            if (r4 == 0) goto L92
            r4.close()     // Catch: java.io.IOException -> L42
            goto L92
        L7b:
            r1 = move-exception
            goto L87
        L7d:
            r1 = move-exception
            Q0.e.d(r0, r1)     // Catch: java.lang.Throwable -> L7b
            if (r4 == 0) goto L92
            r4.close()     // Catch: java.io.IOException -> L42
            goto L92
        L87:
            if (r4 == 0) goto L91
            r4.close()     // Catch: java.io.IOException -> L8d
            goto L91
        L8d:
            r2 = move-exception
            Q0.e.d(r0, r2)
        L91:
            throw r1
        L92:
            return r3
        L93:
            if (r1 == 0) goto Lba
            r1.close()     // Catch: java.lang.Throwable -> La3 java.io.IOException -> La5
            if (r4 == 0) goto Lba
            r4.close()     // Catch: java.io.IOException -> L9e
            goto Lba
        L9e:
            r1 = move-exception
            Q0.e.d(r0, r1)
            goto Lba
        La3:
            r1 = move-exception
            goto Laf
        La5:
            r1 = move-exception
            Q0.e.d(r0, r1)     // Catch: java.lang.Throwable -> La3
            if (r4 == 0) goto Lba
            r4.close()     // Catch: java.io.IOException -> L9e
            goto Lba
        Laf:
            if (r4 == 0) goto Lb9
            r4.close()     // Catch: java.io.IOException -> Lb5
            goto Lb9
        Lb5:
            r2 = move-exception
            Q0.e.d(r0, r2)
        Lb9:
            throw r1
        Lba:
            throw r2
        */
        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 native void native_SetRecordParameter(String[] strArr, Object[] objArr);

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

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

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

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

    public static void setAvcCallbackListener(AvcCallbackListener avcCallbackListener) {
        ScreenControlManagerAn16.getInstance().setAvcCallbackListener(avcCallbackListener);
    }

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

    public static int startAvcScreenRecord(int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10) {
        e.a(TAG, "startAvcScreenRecord left:" + i2 + ",top:" + i3 + ",right:" + i4 + ",bottom:" + i5 + ",width:" + i6 + ",height:" + i7 + ",frameRate:" + i8 + ",bitRate:" + i9 + ",sourceType:" + i10);
        synchronized (mLock) {
            try {
                if (!AmlUtils.isCaptureSoLibraryEffective()) {
                    return ScreenControlManagerAn16.getInstance().startAvcScreenRecord(i2, i3, i4, i5, i6, i7, i8, i9, i10);
                }
                if (mAvcCallbackListener == null) {
                    return -65535;
                }
                INSTANCE.f3153I.native_StartReceiver(new WeakReference<>(INSTANCE.f3153I));
                return INSTANCE.f3153I.native_startAvcRecord(i2, i3, i4, i5, i6, i7, i8, i9, i10);
            } catch (Throwable th) {
                e.b(TAG, "startAvcScreenRecord: " + th);
                return -65535;
            }
        }
    }

    public static int stopScreenRecord() {
        synchronized (mLock) {
            try {
                try {
                    if (AmlUtils.isCaptureSoLibraryEffective()) {
                        INSTANCE.f3153I.native_ForceStop();
                    } else {
                        ScreenControlManagerAn16.getInstance().stopRecord();
                    }
                } catch (Throwable th) {
                    e.b(TAG, "stopScreenRecord error!:" + th);
                    return -1;
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
        return 0;
    }

    private ScreenControlManager() {
        if (AmlUtils.isCaptureSoLibraryEffective()) {
            native_ConnectScreenControl();
        }
    }
}
