package n;

import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.view.Choreographer;
import android.view.WindowManager;

/* JADX INFO: loaded from: classes.dex */
public final class I {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private final a f5470a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private final boolean f5471b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private final long f5472c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final long f5473d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private long f5474e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private long f5475f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private long f5476g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private boolean f5477h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private long f5478i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private long f5479j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private long f5480k;

    private static final class a implements Choreographer.FrameCallback, Handler.Callback {

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        private static final a f5481f = new a();

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public volatile long f5482a;

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        private final Handler f5483b;

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        private final HandlerThread f5484c;

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        private Choreographer f5485d;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        private int f5486e;

        private a() {
            HandlerThread handlerThread = new HandlerThread("ChoreographerOwner:Handler");
            this.f5484c = handlerThread;
            handlerThread.start();
            Handler handler = new Handler(handlerThread.getLooper(), this);
            this.f5483b = handler;
            handler.sendEmptyMessage(0);
        }

        private void b() {
            int i2 = this.f5486e + 1;
            this.f5486e = i2;
            if (i2 == 1) {
                this.f5485d.postFrameCallback(this);
            }
        }

        private void c() {
            this.f5485d = Choreographer.getInstance();
        }

        public static a d() {
            return f5481f;
        }

        private void f() {
            int i2 = this.f5486e - 1;
            this.f5486e = i2;
            if (i2 == 0) {
                this.f5485d.removeFrameCallback(this);
                this.f5482a = 0L;
            }
        }

        public void a() {
            this.f5483b.sendEmptyMessage(1);
        }

        @Override // android.view.Choreographer.FrameCallback
        public void doFrame(long j2) {
            this.f5482a = j2;
            this.f5485d.postFrameCallbackDelayed(this, 500L);
        }

        public void e() {
            this.f5483b.sendEmptyMessage(2);
        }

        @Override // android.os.Handler.Callback
        public boolean handleMessage(Message message) {
            int i2 = message.what;
            if (i2 == 0) {
                c();
                return true;
            }
            if (i2 == 1) {
                b();
                return true;
            }
            if (i2 != 2) {
                return false;
            }
            f();
            return true;
        }
    }

    public I(Context context) {
        this(e(context), true);
    }

    private static long b(long j2, long j3, long j4) {
        long j5;
        long j6 = j3 + (((j2 - j3) / j4) * j4);
        if (j2 <= j6) {
            j5 = j6 - j4;
        } else {
            j6 = j4 + j6;
            j5 = j6;
        }
        return j6 - j2 < j2 - j5 ? j6 : j5;
    }

    private static float e(Context context) {
        return ((WindowManager) context.getSystemService("window")).getDefaultDisplay().getRefreshRate();
    }

    private boolean f(long j2, long j3) {
        return Math.abs((j3 - this.f5478i) - (j2 - this.f5479j)) > 20000000;
    }

    public long a(long j2, long j3) {
        long j4;
        long j5;
        long j6 = 1000 * j2;
        if (this.f5477h) {
            if (j2 != this.f5474e) {
                this.f5480k++;
                this.f5475f = this.f5476g;
            }
            long j7 = this.f5480k;
            if (j7 >= 6) {
                j5 = this.f5475f + ((j6 - this.f5479j) / j7);
                if (f(j5, j3)) {
                    this.f5477h = false;
                } else {
                    j4 = (this.f5478i + j5) - this.f5479j;
                }
            } else if (f(j6, j3)) {
                this.f5477h = false;
            }
            j4 = j3;
            j5 = j6;
        } else {
            j4 = j3;
            j5 = j6;
        }
        if (!this.f5477h) {
            this.f5479j = j6;
            this.f5478i = j3;
            this.f5480k = 0L;
            this.f5477h = true;
            g();
        }
        this.f5474e = j2;
        this.f5476g = j5;
        a aVar = this.f5470a;
        return (aVar == null || aVar.f5482a == 0) ? j4 : b(j4, this.f5470a.f5482a, this.f5472c) - this.f5473d;
    }

    public void c() {
        if (this.f5471b) {
            this.f5470a.e();
        }
    }

    public void d() {
        this.f5477h = false;
        if (this.f5471b) {
            this.f5470a.a();
        }
    }

    protected void g() {
    }

    private I(float f2, boolean z2) {
        this.f5471b = z2;
        if (!z2) {
            this.f5470a = null;
            this.f5472c = -1L;
            this.f5473d = -1L;
        } else {
            this.f5470a = a.d();
            long j2 = (long) (1.0E9d / ((double) f2));
            this.f5472c = j2;
            this.f5473d = (j2 * 80) / 100;
        }
    }
}
