package com.droidlogic.app;

import android.graphics.Bitmap;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.util.Log;
import com.droidlogic.app.ScreenControlManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import vendor.amlogic.hardware.screencontrol.c;
import vendor.amlogic.hardware.screencontrol.d;

/* JADX INFO: loaded from: classes.dex */
public class ScreenControlManagerAn16 {
    public static final int REMOTE_EXCEPTION = -65535;
    public static final int SCREENCONTROL_CLIENT_FEATURE_AVC_RECORD = 2;
    public static final int SCREENCONTROL_CLIENT_FEATURE_TS_RECORD = 1;
    private static final int SCREEN_CONTROL_DEATH_COOKIE = 1001;
    private static final String TAG = "ScreenControlManager";
    private static ScreenControlManagerAn16 mInstance;
    private ScreenControlManager.AvcCallbackListener mAvcCbl = null;
    private d mProxy = null;
    private int mCookie = -1;
    private final Object mLock = new Object();
    private final IBinder.DeathRecipient mServerDiedListener = new IBinder.DeathRecipient() { // from class: com.droidlogic.app.ScreenControlManagerAn16.1
        @Override // android.os.IBinder.DeathRecipient
        public void binderDied() {
            Log.d(ScreenControlManagerAn16.TAG, "binderDied: IScreenControlService has died, connect it again!");
            ScreenControlManagerAn16.this.connectToProxy();
        }
    };

    public class CaptureResult {
        private byte[] data;
        private int result;

        public CaptureResult() {
        }

        public byte[] getData() {
            return this.data;
        }

        public int getResult() {
            return this.result;
        }

        public void setData(byte[] bArr) {
            this.data = bArr;
        }

        public void setResult(int i2) {
            this.result = i2;
        }

        public CaptureResult(int i2) {
            this.result = i2;
        }
    }

    public class TSCallbackListener implements ScreenControlManager.AvcCallbackListener {
        long mDuration;
        long mFirstPts;
        FileOutputStream mFos;
        ScreenControlManagerAn16 mScreenControl;
        boolean mStart;

        TSCallbackListener(ScreenControlManagerAn16 screenControlManagerAn16, long j2, String str) {
            this.mFirstPts = -1L;
            this.mDuration = 0L;
            this.mStart = false;
            Log.d(ScreenControlManagerAn16.TAG, "TSCallbackListener");
            this.mScreenControl = screenControlManagerAn16;
            this.mDuration = j2;
            this.mFirstPts = -1L;
            try {
                this.mFos = new FileOutputStream(new File(str));
            } catch (Exception e2) {
                Log.e(ScreenControlManagerAn16.TAG, "FileOutputStream exception: " + e2);
            }
            this.mStart = true;
        }

        @Override // com.droidlogic.app.ScreenControlManager.AvcCallbackListener
        public void onAvcAvailable(byte[] bArr, int i2, long j2) {
            if (!this.mStart) {
                Log.e(ScreenControlManagerAn16.TAG, "onAvcAvailable: already finish.");
                return;
            }
            if (bArr == null) {
                Log.e(ScreenControlManagerAn16.TAG, "onAvcAvailable: data is null");
                return;
            }
            int length = bArr.length;
            Log.d(ScreenControlManagerAn16.TAG, "onAvcAvailable size=" + length + ",frameType=" + i2 + ",pts=" + j2);
            if (length <= 0 || j2 < 0 || i2 < 0) {
                Log.e(ScreenControlManagerAn16.TAG, "onAvcAvailable: param error");
                return;
            }
            if (this.mFirstPts == -1) {
                this.mFirstPts = j2;
            }
            long j3 = j2 - this.mFirstPts;
            try {
                this.mFos.write(bArr);
            } catch (IOException unused) {
                Log.e(ScreenControlManagerAn16.TAG, "write file error");
            }
            if (j3 >= this.mDuration * 1000000) {
                this.mScreenControl.stopRecord();
                release();
            }
        }

        void release() {
            Log.d(ScreenControlManagerAn16.TAG, "TSCallbackListener release");
            try {
                this.mFos.close();
            } catch (Exception e2) {
                Log.e(ScreenControlManagerAn16.TAG, "FileOutputStream exception: " + e2);
            }
            this.mStart = false;
        }
    }

    public ScreenControlManagerAn16() {
        connectToProxy();
    }

    private byte[] FileDescriptorToByteArray(ParcelFileDescriptor parcelFileDescriptor, int i2) {
        Log.d(TAG, "get the fd:" + parcelFileDescriptor.getFd() + ",size:" + i2);
        try {
            FileChannel channel = new FileInputStream(parcelFileDescriptor.getFileDescriptor()).getChannel();
            if (channel.size() == 0) {
                Log.d(TAG, "FileDescriptorToByteArray: filechannel size is 0");
                return null;
            }
            MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0L, i2);
            Log.d(TAG, "luozw 111 test get the buffer len:" + map.remaining());
            byte[] bArr = new byte[map.remaining()];
            map.get(bArr);
            return bArr;
        } catch (IOException e2) {
            Log.i(TAG, "FileDescriptorToByteArray map fd error:" + e2);
            return null;
        }
    }

    private byte[] FileToByteArray(ParcelFileDescriptor parcelFileDescriptor, int i2) throws Throwable {
        StringBuilder sb;
        FileInputStream fileInputStream;
        byte[] bArr = new byte[i2];
        FileInputStream fileInputStream2 = null;
        try {
            try {
                fileInputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
            } catch (Throwable th) {
                th = th;
            }
        } catch (Exception e2) {
            e = e2;
        }
        try {
            fileInputStream.read(bArr);
            try {
                fileInputStream.close();
            } catch (IOException e3) {
                e = e3;
                sb = new StringBuilder();
                sb.append("FileToByteArray close fd error:");
                sb.append(e);
                Log.i(TAG, sb.toString());
            }
        } catch (Exception e4) {
            e = e4;
            fileInputStream2 = fileInputStream;
            Log.i(TAG, "FileToByteArray open fd error:" + e);
            if (fileInputStream2 != null) {
                try {
                    fileInputStream2.close();
                } catch (IOException e5) {
                    e = e5;
                    sb = new StringBuilder();
                    sb.append("FileToByteArray close fd error:");
                    sb.append(e);
                    Log.i(TAG, sb.toString());
                }
            }
        } catch (Throwable th2) {
            th = th2;
            fileInputStream2 = fileInputStream;
            if (fileInputStream2 != null) {
                try {
                    fileInputStream2.close();
                } catch (IOException e6) {
                    Log.i(TAG, "FileToByteArray close fd error:" + e6);
                }
            }
            throw th;
        }
        return bArr;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void connectToProxy() {
        try {
            IBinder iBinder = (IBinder) Class.forName("android.os.ServiceManager").getMethod("getService", String.class).invoke(null, d.class.getName() + "/default");
            if (iBinder != null) {
                this.mProxy = d.a.E(iBinder);
                iBinder.linkToDeath(this.mServerDiedListener, 0);
                Log.d(TAG, "get the screencontrol service");
            } else {
                Log.e(TAG, "IScreenControlService hal is null. has declared?");
            }
        } catch (Exception e2) {
            Log.e(TAG, "can't get getService method :" + e2.toString());
        }
    }

    public static ScreenControlManagerAn16 getInstance() {
        if (mInstance == null) {
            mInstance = new ScreenControlManagerAn16();
        }
        return mInstance;
    }

    private boolean saveBitmapAsPicture(Bitmap bitmap, File file) {
        boolean zCompress = false;
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            String name = file.getName();
            if (name.contains(".jpeg") || name.contains(".jpg")) {
                zCompress = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
            } else if (name.contains(".png")) {
                zCompress = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            }
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e2) {
            e2.printStackTrace();
        } catch (IOException e3) {
            e3.printStackTrace();
        }
        return zCompress;
    }

    public void setAvcCallbackListener(ScreenControlManager.AvcCallbackListener avcCallbackListener) {
        this.mAvcCbl = avcCallbackListener;
    }

    public void setDisplayRotationAngle(int i2) {
        d dVar;
        Log.d(TAG, "setDisplayRotationAngle angle:" + i2);
        synchronized (this.mLock) {
            try {
                try {
                    if (this.mProxy == null) {
                        connectToProxy();
                    }
                    dVar = this.mProxy;
                } catch (Exception e2) {
                    Log.e(TAG, "startScreenCapBuffer: ScreenControlService is dead!:" + e2);
                }
                if (dVar == null) {
                    Log.e(TAG, "can't get the screencontrol service");
                } else {
                    dVar.f(i2);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public int startAvcScreenRecord(int i2, int i3, int i4, int i5, int i6) {
        return startAvcScreenRecord(0, 0, 0, 0, i2, i3, i4, i5, i6);
    }

    public int startScreenCap(int i2, int i3, int i4, int i5, int i6, int i7, int i8, String str) {
        int result;
        long jNanoTime;
        CaptureResult captureResultStartScreenCapBuffer1;
        Log.d(TAG, "startScreenCap left:" + i2 + ",top:" + i3 + ",right:" + i4 + ",bottom:" + i5 + ",width:" + i6 + ",height:" + i7 + ",sourceType:" + i8 + ",filename:" + str);
        synchronized (this.mLock) {
            result = -1;
            try {
                jNanoTime = System.nanoTime();
                captureResultStartScreenCapBuffer1 = startScreenCapBuffer1(i2, i3, i4, i5, i6, i7, i8);
            } catch (Exception e2) {
                Log.e(TAG, "startScreenCap: ScreenControlService is dead!:" + e2);
            }
            if (captureResultStartScreenCapBuffer1 != null && captureResultStartScreenCapBuffer1.getResult() == 0 && captureResultStartScreenCapBuffer1.getData() != null) {
                Log.d(TAG, "get the RGBA buffer,duration1:" + ((System.nanoTime() - jNanoTime) / 1000000) + " ms");
                Bitmap bitmapCreateBitmap = Bitmap.createBitmap(i6, i7, Bitmap.Config.ARGB_8888);
                bitmapCreateBitmap.copyPixelsFromBuffer(ByteBuffer.wrap(captureResultStartScreenCapBuffer1.getData()));
                saveBitmapAsPicture(bitmapCreateBitmap, new File(str));
                long jNanoTime2 = System.nanoTime();
                bitmapCreateBitmap.recycle();
                Log.d(TAG, "convert jpeg or png file,duration2:" + ((jNanoTime2 - jNanoTime) / 1000000) + " ms");
                result = captureResultStartScreenCapBuffer1.getResult();
            }
        }
        return result;
    }

    public byte[] startScreenCapBuffer(int i2, int i3, int i4, int i5, int i6, int i7, int i8) {
        Log.d(TAG, "startScreenCapBuffer left:" + i2 + ",top:" + i3 + ",right:" + i4 + ",bottom:" + i5 + ",width:" + i6 + ",height:" + i7 + ",sourceType:" + i8);
        synchronized (this.mLock) {
            try {
                try {
                    CaptureResult captureResultStartScreenCapBuffer1 = startScreenCapBuffer1(i2, i3, i4, i5, i6, i7, i8);
                    if (captureResultStartScreenCapBuffer1 != null && captureResultStartScreenCapBuffer1.getResult() == 0 && captureResultStartScreenCapBuffer1.getData() != null) {
                        return captureResultStartScreenCapBuffer1.getData();
                    }
                } catch (Exception e2) {
                    Log.e(TAG, "startScreenCapBuffer: ScreenControlService is dead!:" + e2);
                }
                return null;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public CaptureResult startScreenCapBuffer1(int i2, int i3, int i4, int i5, int i6, int i7, int i8) {
        Log.d(TAG, "startScreenCapBuffer1 left:" + i2 + ",top:" + i3 + ",right:" + i4 + ",bottom:" + i5 + ",width:" + i6 + ",height:" + i7 + ",sourceType:" + i8);
        synchronized (this.mLock) {
            CaptureResult captureResult = null;
            try {
                try {
                    if (this.mProxy == null) {
                        connectToProxy();
                    }
                    if (this.mProxy == null) {
                        Log.e(TAG, "can't get the screencontrol service");
                        return null;
                    }
                    vendor.amlogic.hardware.screencontrol.a aVar = new vendor.amlogic.hardware.screencontrol.a();
                    aVar.f6673b = i2;
                    aVar.f6674g = i3;
                    aVar.f6675r = i4;
                    aVar.f6676x = i5;
                    aVar.f6677y = i6;
                    aVar.f6671H = i7;
                    aVar.f6672I = i8;
                    vendor.amlogic.hardware.screencontrol.b bVarR = this.mProxy.r(aVar);
                    boolean z2 = bVarR == null;
                    Log.d(TAG, "startScreenCapBuffer1: ret = " + bVarR + ", isRetNull = " + z2);
                    if (!z2) {
                        CaptureResult captureResult2 = new CaptureResult(bVarR.f6679g);
                        if (bVarR.f6679g == 0) {
                            int i9 = i6 * i7 * 4;
                            Log.d(TAG, "startScreenCapBuffer1: try to get buffer, size: " + i9);
                            byte[] bArrFileDescriptorToByteArray = FileDescriptorToByteArray(bVarR.f6678b, i9);
                            if (bArrFileDescriptorToByteArray == null) {
                                Log.d(TAG, "startScreenCapBuffer1: read file data");
                                bArrFileDescriptorToByteArray = FileToByteArray(bVarR.f6678b, i9);
                            }
                            captureResult2.setData(bArrFileDescriptorToByteArray);
                        }
                        captureResult = captureResult2;
                    }
                    return captureResult;
                } catch (Exception e2) {
                    Log.e(TAG, "startScreenCapBuffer1: ScreenControlService is dead!:", e2);
                    return null;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public int startScreenRecord(int i2, int i3, int i4, int i5, int i6, int i7, String str) {
        return startScreenRecord(0, 0, 0, 0, i2, i3, i4, i5, i6, i7, str);
    }

    public void stopRecord() {
        try {
            if (this.mProxy == null) {
                connectToProxy();
            }
            d dVar = this.mProxy;
            if (dVar == null) {
                Log.e(TAG, "can't get the screencontrol service");
            } else {
                dVar.y(this.mCookie);
                this.mCookie = -1;
            }
        } catch (Exception e2) {
            Log.e(TAG, "stopRecord: ScreenControlService is dead!:" + e2);
        }
    }

    private class ScreenControlEvent extends c.a {
        private ScreenControlManager.AvcCallbackListener mAvcCb;

        public ScreenControlEvent() {
            this.mAvcCb = null;
        }

        @Override // vendor.amlogic.hardware.screencontrol.c
        public void onAvcDataArouse(ParcelFileDescriptor parcelFileDescriptor, int i2, int i3, long j2) throws Throwable {
            StringBuilder sb;
            FileInputStream fileInputStream;
            Log.i(ScreenControlManagerAn16.TAG, "onAvcDataArouse size=" + i2 + ",frameType=" + i3 + ",pts=" + j2);
            if (this.mAvcCb != null) {
                byte[] bArr = new byte[i2];
                FileInputStream fileInputStream2 = null;
                try {
                    try {
                        fileInputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
                    } catch (Exception e2) {
                        e = e2;
                    }
                } catch (Throwable th) {
                    th = th;
                }
                try {
                    fileInputStream.read(bArr);
                    try {
                        fileInputStream.close();
                        this.mAvcCb.onAvcAvailable(bArr, i3, j2);
                    } catch (IOException e3) {
                        e = e3;
                        sb = new StringBuilder();
                        sb.append("onAvcDataArouse close fd error:");
                        sb.append(e);
                        Log.i(ScreenControlManagerAn16.TAG, sb.toString());
                    }
                } catch (Exception e4) {
                    e = e4;
                    fileInputStream2 = fileInputStream;
                    Log.i(ScreenControlManagerAn16.TAG, "onAvcDataArouse open fd error:" + e);
                    e.printStackTrace();
                    if (fileInputStream2 != null) {
                        try {
                            fileInputStream2.close();
                        } catch (IOException e5) {
                            e = e5;
                            sb = new StringBuilder();
                            sb.append("onAvcDataArouse close fd error:");
                            sb.append(e);
                            Log.i(ScreenControlManagerAn16.TAG, sb.toString());
                        }
                    }
                    this.mAvcCb.onAvcAvailable(bArr, i3, j2);
                } catch (Throwable th2) {
                    th = th2;
                    fileInputStream2 = fileInputStream;
                    if (fileInputStream2 != null) {
                        try {
                            fileInputStream2.close();
                        } catch (IOException e6) {
                            Log.i(ScreenControlManagerAn16.TAG, "onAvcDataArouse close fd error:" + e6);
                            throw th;
                        }
                    }
                    this.mAvcCb.onAvcAvailable(bArr, i3, j2);
                    throw th;
                }
            }
        }

        @Override // vendor.amlogic.hardware.screencontrol.c
        public void onYuvDataArouse(ParcelFileDescriptor parcelFileDescriptor, int i2) throws RemoteException {
            Log.i(ScreenControlManagerAn16.TAG, "onYuvDataArouse size=" + i2);
        }

        public ScreenControlEvent(ScreenControlManager.AvcCallbackListener avcCallbackListener) {
            this.mAvcCb = avcCallbackListener;
        }
    }

    public int startAvcScreenRecord(int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10) {
        Log.d(TAG, "startAvcScreenRecord left:" + i2 + ",top:" + i3 + ",right:" + i4 + ",bottom:" + i5 + ",width:" + i6 + ",height:" + i7 + ",frameRate:" + i8 + ",bitRate:" + i9 + ",sourceType:" + i10);
        synchronized (this.mLock) {
            try {
                try {
                    if (this.mProxy == null) {
                        connectToProxy();
                    }
                    d dVar = this.mProxy;
                    if (dVar == null) {
                        Log.e(TAG, "can't get the screencontrol service");
                        return -1;
                    }
                    if (this.mAvcCbl == null) {
                        Log.e(TAG, "AvcCallbackListener is null");
                        return -1;
                    }
                    this.mCookie = dVar.m(new ScreenControlEvent(this.mAvcCbl), 2);
                    vendor.amlogic.hardware.screencontrol.a aVar = new vendor.amlogic.hardware.screencontrol.a();
                    aVar.f6673b = i2;
                    aVar.f6674g = i3;
                    aVar.f6675r = i4;
                    aVar.f6676x = i5;
                    aVar.f6677y = i6;
                    aVar.f6671H = i7;
                    aVar.f6672I = i10;
                    return this.mProxy.e(this.mCookie, aVar, i8, i9);
                } catch (Exception e2) {
                    Log.e(TAG, "startAvcScreenRecord: ScreenControlService is dead!:" + e2);
                    return -65535;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public int startScreenRecord(int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10, int i11, String str) {
        String str2;
        Log.d(TAG, "startScreenRecord left:" + i2 + ",top:" + i3 + ",right:" + i4 + ",bottom:" + i5 + ",width:" + i6 + ",height:" + i7 + ",sourceType:" + i11 + ",frameRate:" + i8 + ",bitRate:" + i9 + ",limitTimeSec:" + i10 + ",filename:" + str);
        try {
            if (this.mProxy == null) {
                connectToProxy();
            }
            d dVar = this.mProxy;
            if (dVar == null) {
                Log.e(TAG, "can't get the screencontrol service");
                return -1;
            }
            long j2 = i10;
            str2 = TAG;
            try {
                this.mCookie = dVar.m(new ScreenControlEvent(new TSCallbackListener(this, j2, str)), 1);
                vendor.amlogic.hardware.screencontrol.a aVar = new vendor.amlogic.hardware.screencontrol.a();
                aVar.f6673b = i2;
                aVar.f6674g = i3;
                aVar.f6675r = i4;
                aVar.f6676x = i5;
                aVar.f6677y = i6;
                aVar.f6671H = i7;
                aVar.f6672I = i11;
                return this.mProxy.o(this.mCookie, aVar, i8, i9, i10);
            } catch (Exception e2) {
                e = e2;
                Log.e(str2, "startScreenRecord: ScreenControlService is dead!:" + e);
                return -65535;
            }
        } catch (Exception e3) {
            e = e3;
            str2 = TAG;
        }
    }

    public byte[] startScreenCapBuffer(int i2, int i3, int i4) {
        return startScreenCapBuffer(0, 0, 0, 0, i2, i3, i4);
    }

    public int startScreenCap(int i2, int i3, int i4, String str) {
        return startScreenCap(0, 0, 0, 0, i2, i3, i4, str);
    }

    public CaptureResult startScreenCapBuffer1(int i2, int i3, int i4) {
        return startScreenCapBuffer1(0, 0, 0, 0, i2, i3, i4);
    }
}
