package com.tencent.liteav.screencapture;

import android.content.Context;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.view.WindowManager;
import com.tencent.liteav.basic.log.TXCLog;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.smtt.sdk.TbsMediaPlayer$TbsMediaPlayerListener;
import com.tencent.ttpic.util.ActUtil;
import java.lang.ref.WeakReference;
import javax.microedition.khronos.egl.EGLContext;

public class a {
    protected Handler a = null;
    protected volatile HandlerThread b = null;
    protected volatile a c = null;
    protected volatile WeakReference<c> d = null;
    protected volatile int e = 0;
    protected int f = 720;
    protected int g = ActUtil.HEIGHT;
    protected int h = 20;
    protected boolean i = false;
    private Object j = null;
    private boolean k = false;
    private int l = this.f;
    private int m = this.g;
    private Context n = null;
    private b$a o = new b$a() {
        public void a(int i) {
            AppMethodBeat.i(66801);
            a.this.b(i);
            a.this.b(105, a.this.l, a.this.m);
            AppMethodBeat.o(66801);
        }
    };

    public a(Context context, boolean z) {
        AppMethodBeat.i(66744);
        this.n = context;
        this.a = new Handler(Looper.getMainLooper());
        this.k = z;
        if (VERSION.SDK_INT >= 21) {
            b.a().a(context);
        }
        AppMethodBeat.o(66744);
    }

    public int a(int i, int i2, int i3) {
        int rotation;
        AppMethodBeat.i(66745);
        if (this.k) {
            rotation = ((WindowManager) this.n.getSystemService("window")).getDefaultDisplay().getRotation();
            if (rotation == 0 || rotation == 2) {
                if (i > i2) {
                    this.f = i2;
                    this.g = i;
                } else {
                    this.f = i;
                    this.g = i2;
                }
            } else if (i < i2) {
                this.f = i2;
                this.g = i;
            } else {
                this.f = i;
                this.g = i2;
            }
        } else {
            this.f = i;
            this.g = i2;
            rotation = 0;
        }
        TXCLog.d("ScreenCapture", String.format("start screen capture orientation[%d] input size[%d/%d] output size[%d/%d]", new Object[]{Integer.valueOf(rotation), Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(this.f), Integer.valueOf(this.g)}));
        this.h = i3;
        if (VERSION.SDK_INT < 21) {
            a(20000004, null);
            AppMethodBeat.o(66745);
            return 20000004;
        }
        this.l = this.f;
        this.m = this.g;
        a();
        if (this.k) {
            b.a().a(this.o);
        }
        AppMethodBeat.o(66745);
        return 0;
    }

    public void a(Object obj) {
        AppMethodBeat.i(66746);
        b.a().b(this.o);
        this.j = obj;
        b();
        AppMethodBeat.o(66746);
    }

    public void a(final boolean z) {
        AppMethodBeat.i(66747);
        synchronized (this) {
            try {
                if (this.c != null) {
                    this.c.post(new Runnable() {
                        public void run() {
                            a.this.i = z;
                        }
                    });
                } else {
                    this.i = z;
                }
            } finally {
                AppMethodBeat.o(66747);
            }
        }
    }

    public void a(c cVar) {
        AppMethodBeat.i(66748);
        this.d = new WeakReference(cVar);
        AppMethodBeat.o(66748);
    }

    public void a(com.tencent.liteav.basic.d.a aVar) {
        AppMethodBeat.i(66749);
        b.a().a(aVar);
        AppMethodBeat.o(66749);
    }

    public synchronized void a(Runnable runnable) {
        AppMethodBeat.i(66750);
        if (this.c != null) {
            this.c.post(runnable);
        }
        AppMethodBeat.o(66750);
    }

    /* Access modifiers changed, original: protected */
    public void a() {
        AppMethodBeat.i(66751);
        b();
        synchronized (this) {
            try {
                this.b = new HandlerThread("ScreenCaptureGLThread");
                this.b.start();
                this.c = new a(this, this.b.getLooper(), this);
                this.e++;
                this.c.a = this.e;
                this.c.e = this.l;
                this.c.f = this.m;
                this.c.g = this.h <= 0 ? 1 : this.h;
            } finally {
                AppMethodBeat.o(66751);
            }
        }
        a(100);
    }

    /* Access modifiers changed, original: protected */
    public void b() {
        AppMethodBeat.i(66752);
        synchronized (this) {
            try {
                this.e++;
                if (this.c != null) {
                    a((int) TbsMediaPlayer$TbsMediaPlayerListener.MEDIA_EXTRA_SUBTITLE_COUNT, new 3(this, this.c, this.b));
                }
                this.c = null;
                this.b = null;
            } finally {
                while (true) {
                }
                AppMethodBeat.o(66752);
            }
        }
    }

    /* Access modifiers changed, original: protected */
    public c c() {
        AppMethodBeat.i(66753);
        if (this.d == null) {
            AppMethodBeat.o(66753);
            return null;
        }
        c cVar = (c) this.d.get();
        AppMethodBeat.o(66753);
        return cVar;
    }

    /* Access modifiers changed, original: protected */
    public void a(int i, long j) {
        AppMethodBeat.i(66754);
        synchronized (this) {
            try {
                if (this.c != null) {
                    this.c.sendEmptyMessageDelayed(i, j);
                }
            } finally {
                while (true) {
                }
                AppMethodBeat.o(66754);
            }
        }
    }

    /* Access modifiers changed, original: protected */
    public void a(int i) {
        AppMethodBeat.i(66755);
        synchronized (this) {
            try {
                if (this.c != null) {
                    this.c.sendEmptyMessage(i);
                }
            } finally {
                while (true) {
                }
                AppMethodBeat.o(66755);
            }
        }
    }

    /* Access modifiers changed, original: protected */
    public void b(int i, int i2, int i3) {
        AppMethodBeat.i(66756);
        synchronized (this) {
            try {
                if (this.c != null) {
                    Message message = new Message();
                    message.what = i;
                    message.arg1 = i2;
                    message.arg2 = i3;
                    this.c.sendMessage(message);
                }
            } finally {
                while (true) {
                }
                AppMethodBeat.o(66756);
            }
        }
    }

    /* Access modifiers changed, original: protected */
    public void a(int i, Runnable runnable) {
        AppMethodBeat.i(66757);
        synchronized (this) {
            try {
                if (this.c != null) {
                    Message message = new Message();
                    message.what = i;
                    message.obj = runnable;
                    this.c.sendMessage(message);
                }
            } finally {
                while (true) {
                }
                AppMethodBeat.o(66757);
            }
        }
    }

    /* Access modifiers changed, original: protected */
    public void a(int i, EGLContext eGLContext) {
        AppMethodBeat.i(66758);
        c c = c();
        if (c != null) {
            c.a(i, eGLContext);
        }
        AppMethodBeat.o(66758);
    }

    /* Access modifiers changed, original: protected */
    public void a(int i, int i2, int i3, int i4, long j) {
        AppMethodBeat.i(66759);
        c c = c();
        if (c != null) {
            c.a(i, i2, i3, i4, j);
        }
        AppMethodBeat.o(66759);
    }

    /* Access modifiers changed, original: protected */
    public void b(int i) {
        AppMethodBeat.i(66760);
        if (i == 0) {
            this.l = this.f < this.g ? this.f : this.g;
            this.m = this.f < this.g ? this.g : this.f;
        } else {
            this.l = this.f < this.g ? this.g : this.f;
            this.m = this.f < this.g ? this.f : this.g;
        }
        TXCLog.d("ScreenCapture", String.format("reset screen capture angle[%d] output size[%d/%d]", new Object[]{Integer.valueOf(i), Integer.valueOf(this.l), Integer.valueOf(this.m)}));
        AppMethodBeat.o(66760);
    }
}
