package com.tencent.mm.plugin.appbrand.media.record;

import android.media.AudioRecord;
import android.os.Looper;
import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.g.a.mb;
import com.tencent.mm.plugin.appbrand.media.record.a.c;
import com.tencent.mm.plugin.appbrand.media.record.a.e;
import com.tencent.mm.plugin.appbrand.media.record.record_imp.RecordParam;
import com.tencent.mm.plugin.appbrand.media.record.record_imp.a;
import com.tencent.mm.sdk.g.d;
import com.tencent.mm.sdk.platformtools.ab;
import com.tencent.mm.sdk.platformtools.ak;
import com.tencent.mm.sdk.platformtools.ap;

public final class g {
    private static Object cli = new Object();
    private static g ilA;
    private ap ikY = null;
    private e ilB = new 4(this);
    public a ilm = null;
    private Object iln = new Object();
    public RecordParam ilo;
    a ilp = a.ilE;
    private c ilq = null;
    private boolean ilr = false;
    private int ils = 0;
    private long ilt = 0;
    private int ilu = 0;
    private ak ilv;
    boolean ilw = false;
    boolean ilx = false;
    private a.a ily = new 1(this);
    private c.a ilz = new 5(this);
    private long mDuration = 0;
    private String mFilePath;
    public boolean mIsRecording = false;
    private long mStartTime = 0;

    static /* synthetic */ void i(g gVar) {
        AppMethodBeat.i(105631);
        gVar.aIp();
        AppMethodBeat.o(105631);
    }

    static /* synthetic */ void k(g gVar) {
        AppMethodBeat.i(105633);
        gVar.stopTimer();
        AppMethodBeat.o(105633);
    }

    private void stopTimer() {
        AppMethodBeat.i(105618);
        ab.i("MicroMsg.Record.AudioRecordMgr", "stopTimer");
        if (this.ikY != null) {
            this.ikY.stopTimer();
        }
        this.ikY = null;
        AppMethodBeat.o(105618);
    }

    static {
        AppMethodBeat.i(105634);
        AppMethodBeat.o(105634);
    }

    private g() {
        AppMethodBeat.i(105619);
        AppMethodBeat.o(105619);
    }

    public static g aIm() {
        g gVar;
        AppMethodBeat.i(105620);
        synchronized (cli) {
            try {
                if (ilA == null) {
                    ilA = new g();
                }
                gVar = ilA;
            } finally {
                while (true) {
                }
                AppMethodBeat.o(105620);
            }
        }
        return gVar;
    }

    public final boolean DA() {
        return this.ilp == a.ilH;
    }

    public final boolean DB() {
        return this.ilp == a.ilI;
    }

    public final boolean EB() {
        AppMethodBeat.i(105621);
        ab.i("MicroMsg.Record.AudioRecordMgr", "stopRecord");
        if (this.ilm == null && this.ilq == null) {
            ab.e("MicroMsg.Record.AudioRecordMgr", "mRecord is null and mAudioEncoder is null, stop fail");
            AppMethodBeat.o(105621);
            return false;
        }
        d.post(new 10(this), "app_brand_stop_record");
        AppMethodBeat.o(105621);
        return true;
    }

    private boolean EM() {
        boolean z;
        AppMethodBeat.i(105622);
        ab.i("MicroMsg.Record.AudioRecordMgr", "startRecordInternal");
        if (this.ilm != null) {
            this.ilm.EB();
            this.ilm = null;
            ab.i("MicroMsg.Record.AudioRecordMgr", "mRecorder is not null, stop it, and not callback stop event");
        }
        if (this.ilm == null) {
            this.ilm = new a(this.ilo);
            this.ilm.imh = this.ily;
        }
        a aVar = this.ilm;
        ab.i("MicroMsg.Record.AppBrandRecorder", "startRecord");
        if (aVar.coZ != null) {
            aVar.coZ.EB();
            aVar.coZ = null;
        }
        aVar.img = System.currentTimeMillis();
        ab.i("MicroMsg.Record.AppBrandRecorder", "start time ticket:%d", new Object[]{Long.valueOf(aVar.img)});
        aVar.coZ = new com.tencent.mm.audio.b.c(aVar.sampleRate, aVar.aOn, aVar.cpA);
        if ("mp3".equalsIgnoreCase(aVar.imf.hAQ)) {
            aVar.coZ.gG(40);
        } else {
            aVar.coZ.gG(20);
        }
        aVar.coZ.bq(false);
        aVar.coZ.cpM = aVar.imi;
        com.tencent.mm.audio.b.c cVar = aVar.coZ;
        int i = aVar.imf.imn.ilf;
        cVar.cpu = i;
        ab.i("MicroMsg.MMPcmRecorder", "mAudioSource: ".concat(String.valueOf(i)));
        if (aVar.coZ.EL()) {
            z = true;
        } else {
            z = false;
        }
        ab.i("MicroMsg.Record.AudioRecordMgr", "record start:%b", new Object[]{Boolean.valueOf(z)});
        AppMethodBeat.o(105622);
        return z;
    }

    private boolean aIn() {
        c cVar = null;
        AppMethodBeat.i(105623);
        ab.i("MicroMsg.Record.AudioRecordMgr", "initEncode");
        if (this.ilq != null) {
            this.ilq.close();
            this.ilq = null;
        }
        this.mFilePath = h.cE(this.ilo.hAQ, this.ilo.euD);
        ab.i("MicroMsg.Record.AudioRecordMgr", "mFilePath:%s", new Object[]{this.mFilePath});
        String str = this.ilo.hAQ;
        ab.i("MicroMsg.Record.AudioEncodeFactory", "createEncodeByType:%s", new Object[]{str});
        if (h.Cz(str)) {
            if ("aac".equalsIgnoreCase(str)) {
                cVar = new com.tencent.mm.plugin.appbrand.media.record.a.a();
            } else if ("mp3".equalsIgnoreCase(str)) {
                cVar = new com.tencent.mm.plugin.appbrand.media.record.a.d();
            } else if ("wav".equalsIgnoreCase(str)) {
                cVar = new e();
            }
        }
        this.ilq = cVar;
        if (this.ilq != null) {
            boolean CA = h.CA(this.mFilePath);
            if (CA) {
                try {
                    CA = this.ilq.h(this.mFilePath, this.ilo.sampleRate, this.ilo.imk, this.ilo.iml);
                } catch (Exception e) {
                    ab.printErrStackTrace("MicroMsg.Record.AudioRecordMgr", e, "init encoder fail", new Object[0]);
                    CA = false;
                }
                this.ilq.a(this.ilz);
                AppMethodBeat.o(105623);
                return CA;
            }
            ab.e("MicroMsg.Record.AudioRecordMgr", "prepare cache file fail");
            AppMethodBeat.o(105623);
            return CA;
        }
        AppMethodBeat.o(105623);
        return false;
    }

    /* Access modifiers changed, original: final */
    public final void aIo() {
        AppMethodBeat.i(105624);
        ab.i("MicroMsg.Record.AudioRecordMgr", "pause record in runnable");
        boolean EB;
        try {
            if (DA()) {
                ab.e("MicroMsg.Record.AudioRecordMgr", "is paused, don't pause again");
                AppMethodBeat.o(105624);
                return;
            }
            if (this.ilm != null) {
                EB = this.ilm.EB();
                this.ilm = null;
            } else {
                EB = false;
            }
            if (!(this.ilo == null || this.ilo.imo == null)) {
                ab.i("MicroMsg.Record.AudioRecordMgr", "isInterrupted:%b, interrupted:%b", new Object[]{Boolean.valueOf(this.ilx), Boolean.valueOf(this.ilo.imo.aIj())});
                if (this.ilo.imo.aIj() && !this.ilx) {
                    aIr();
                }
            }
            aIq().post(new Runnable() {
                public final void run() {
                    AppMethodBeat.i(105603);
                    g.k(g.this);
                    AppMethodBeat.o(105603);
                }
            });
            ab.i("MicroMsg.Record.AudioRecordMgr", "currentTime:%d, interval:%d, mRealRecordedTime:%d", new Object[]{Long.valueOf(System.currentTimeMillis()), Long.valueOf(System.currentTimeMillis() - this.mStartTime), Integer.valueOf(this.ilu)});
            this.ilu = (int) (((long) this.ilu) + r4);
            this.ilt = this.mDuration - ((long) this.ilu);
            ab.i("MicroMsg.Record.AudioRecordMgr", "mLimitTime:%d, mRealRecordTime:%d", new Object[]{Long.valueOf(this.ilt), Integer.valueOf(this.ilu)});
            ab.i("MicroMsg.Record.AudioRecordMgr", "stop:%b", new Object[]{Boolean.valueOf(EB)});
            if (EB) {
                ab.i("MicroMsg.Record.AudioRecordMgr", "onPause");
                this.ilp = a.ilH;
                this.mIsRecording = false;
                mb mbVar = new mb();
                mbVar.cHS.action = 3;
                mbVar.cHS.state = "pause";
                if (this.ilo != null) {
                    mbVar.cHS.appId = this.ilo.appId;
                }
                com.tencent.mm.sdk.b.a.xxA.a(mbVar, Looper.getMainLooper());
                ab.i("MicroMsg.Record.AudioRecordMgr", "pause record success");
                AppMethodBeat.o(105624);
                return;
            }
            onError(8);
            ab.e("MicroMsg.Record.AudioRecordMgr", "pause record fail");
            AppMethodBeat.o(105624);
        } catch (Exception e) {
            ab.printErrStackTrace("MicroMsg.Record.AudioRecordMgr", e, "_pause", new Object[0]);
            EB = false;
        }
    }

    private void aIp() {
        AppMethodBeat.i(105625);
        ab.i("MicroMsg.Record.AudioRecordMgr", "stop record in runnable");
        boolean EB;
        try {
            if (DB()) {
                ab.e("MicroMsg.Record.AudioRecordMgr", "is stopped, don't stop again");
                AppMethodBeat.o(105625);
                return;
            }
            if (this.ilm != null) {
                EB = this.ilm.EB();
                this.ilm = null;
            } else {
                ab.e("MicroMsg.Record.AudioRecordMgr", "mRecorder is null, has stop record!");
                EB = true;
            }
            this.ilr = true;
            ab.i("MicroMsg.Record.AudioRecordMgr", "mPcmDuration:%d", new Object[]{Integer.valueOf(this.ils)});
            if (this.ilq != null) {
                this.ilq.flush();
                this.ilq.close();
                this.ilq = null;
            }
            aIq().post(new 3(this));
            if (DA()) {
                ab.e("MicroMsg.Record.AudioRecordMgr", "don't calculate time again if it is pause state");
            } else {
                long currentTimeMillis = System.currentTimeMillis();
                ab.i("MicroMsg.Record.AudioRecordMgr", "currentTime:%d, interval:%d, mRealRecordedTime:%d", new Object[]{Long.valueOf(currentTimeMillis), Long.valueOf(System.currentTimeMillis() - this.mStartTime), Integer.valueOf(this.ilu)});
                this.ilu = (int) (((long) this.ilu) + r6);
                this.ilt = this.mDuration - ((long) this.ilu);
                ab.i("MicroMsg.Record.AudioRecordMgr", "mLimitTime:%d, mRealRecordTime:%d", new Object[]{Long.valueOf(this.ilt), Integer.valueOf(this.ilu)});
            }
            ab.i("MicroMsg.Record.AudioRecordMgr", "stop:%b", new Object[]{Boolean.valueOf(EB)});
            if (EB) {
                ab.i("MicroMsg.Record.AudioRecordMgr", "onStop");
                this.ilp = a.ilI;
                this.mIsRecording = false;
                mb mbVar = new mb();
                mbVar.cHS.action = 2;
                mbVar.cHS.state = "stop";
                if (this.ilo != null) {
                    mbVar.cHS.appId = this.ilo.appId;
                }
                ab.i("MicroMsg.Record.AudioRecordMgr", "mPcmDuration:%d, mRealRecordedTime%d", new Object[]{Integer.valueOf(this.ils), Integer.valueOf(this.ilu)});
                mbVar.cHS.duration = Math.min(this.ils, this.ilu);
                mbVar.cHS.filePath = this.mFilePath;
                mbVar.cHS.fileSize = (int) h.CB(this.mFilePath);
                com.tencent.mm.sdk.b.a.xxA.a(mbVar, Looper.getMainLooper());
                ab.i("MicroMsg.Record.AudioRecordMgr", "stop record success");
            } else {
                onError(9);
                ab.e("MicroMsg.Record.AudioRecordMgr", "stop record fail");
            }
            this.ilo = null;
            d.aIg();
            AppMethodBeat.o(105625);
        } catch (Exception e) {
            ab.printErrStackTrace("MicroMsg.Record.AudioRecordMgr", e, "_stop", new Object[0]);
            EB = false;
        }
    }

    private ak aIq() {
        AppMethodBeat.i(105626);
        if (this.ilv == null) {
            this.ilv = new ak(Looper.getMainLooper());
        }
        ak akVar = this.ilv;
        AppMethodBeat.o(105626);
        return akVar;
    }

    /* Access modifiers changed, original: protected|final */
    public final void onError(int i) {
        AppMethodBeat.i(105627);
        ab.i("MicroMsg.Record.AudioRecordMgr", "onError errType:%d", new Object[]{Integer.valueOf(i)});
        if (this.ilp != a.ilJ) {
            j.pj(i);
        }
        this.ilp = a.ilJ;
        this.mIsRecording = false;
        mb mbVar = new mb();
        mbVar.cHS.action = 4;
        mbVar.cHS.state = "error";
        if (this.ilo != null) {
            mbVar.cHS.appId = this.ilo.appId;
        }
        mbVar.cHS.errCode = i;
        mbVar.cHS.aIm = i.ph(i);
        com.tencent.mm.sdk.b.a.xxA.a(mbVar, Looper.getMainLooper());
        AppMethodBeat.o(105627);
    }

    /* Access modifiers changed, original: protected|final */
    public final void aIr() {
        AppMethodBeat.i(105628);
        this.ilx = true;
        ab.i("MicroMsg.Record.AudioRecordMgr", "onInterruptionBegin");
        mb mbVar = new mb();
        mbVar.cHS.action = 6;
        mbVar.cHS.state = "interruptionBegin";
        if (this.ilo != null) {
            mbVar.cHS.appId = this.ilo.appId;
        }
        com.tencent.mm.sdk.b.a.xxA.a(mbVar, Looper.getMainLooper());
        AppMethodBeat.o(105628);
    }

    static /* synthetic */ void g(g gVar) {
        AppMethodBeat.i(105629);
        try {
            ab.i("MicroMsg.Record.AudioRecordMgr", "_start in runnable");
            if (gVar.aIn()) {
                gVar.ilr = false;
                gVar.ils = 0;
                if (!gVar.ilw) {
                    gVar.ilw = true;
                    if (!(gVar.ilo == null || gVar.ilo.imo == null)) {
                        gVar.ilo.imo.a(gVar.ilB);
                        gVar.ilo.imo.aIk();
                    }
                }
                if (gVar.EM()) {
                    int i;
                    c cVar = gVar.ilq;
                    a aVar = gVar.ilm;
                    if (aVar.coZ != null) {
                        com.tencent.mm.audio.b.c cVar2 = aVar.coZ;
                        if (cVar2.cpS > 0) {
                            i = cVar2.cpS;
                        } else {
                            int minBufferSize = AudioRecord.getMinBufferSize(cVar2.mSampleRate, cVar2.cpQ, 2);
                            ab.i("MicroMsg.MMPcmRecorder", "getDefaultMinBufferSize minBufSize:%d", new Object[]{Integer.valueOf(minBufferSize)});
                            if (minBufferSize == -2 || minBufferSize == -1) {
                                i = 0;
                            } else {
                                cVar2.cpS = minBufferSize * cVar2.cpz;
                                i = cVar2.cpS;
                            }
                        }
                    } else {
                        i = 0;
                    }
                    cVar.pl(i);
                    gVar.ilq.t(gVar.ilo.imm);
                    gVar.ilu = 0;
                    gVar.mDuration = (long) gVar.ilo.duration;
                    gVar.mStartTime = System.currentTimeMillis();
                    gVar.ilt = gVar.mDuration;
                    ab.i("MicroMsg.Record.AudioRecordMgr", "mDuration:%d, mCurrentTime:%d", new Object[]{Long.valueOf(gVar.mDuration), Long.valueOf(gVar.mStartTime)});
                    gVar.aIq().post(new 11(gVar));
                    ab.i("MicroMsg.Record.AudioRecordMgr", "onStart");
                    gVar.ilp = a.ilF;
                    gVar.mIsRecording = true;
                    mb mbVar = new mb();
                    mbVar.cHS.action = 0;
                    mbVar.cHS.state = "start";
                    if (gVar.ilo != null) {
                        mbVar.cHS.appId = gVar.ilo.appId;
                    }
                    com.tencent.mm.sdk.b.a.xxA.a(mbVar, Looper.getMainLooper());
                    ab.i("MicroMsg.Record.AudioRecordMgr", "start record success");
                    AppMethodBeat.o(105629);
                    return;
                }
                gVar.onError(6);
                ab.e("MicroMsg.Record.AudioRecordMgr", "start record fail");
                AppMethodBeat.o(105629);
                return;
            }
            gVar.onError(2);
            ab.e("MicroMsg.Record.AudioRecordMgr", "init encoder fail");
            AppMethodBeat.o(105629);
        } catch (Exception e) {
            ab.printErrStackTrace("MicroMsg.Record.AudioRecordMgr", e, "_start", new Object[0]);
            gVar.onError(4);
            AppMethodBeat.o(105629);
        }
    }

    static /* synthetic */ void h(g gVar) {
        AppMethodBeat.i(105630);
        try {
            ab.i("MicroMsg.Record.AudioRecordMgr", "_resume in runnable");
            if (gVar.ilq == null) {
                gVar.onError(3);
                ab.e("MicroMsg.Record.AudioRecordMgr", "resume record fail");
                AppMethodBeat.o(105630);
            } else if (gVar.ilt <= 0) {
                gVar.aIp();
                ab.e("MicroMsg.Record.AudioRecordMgr", "resume record fail, record time reach max time, to stop record");
                AppMethodBeat.o(105630);
            } else if (gVar.EM()) {
                gVar.mStartTime = System.currentTimeMillis();
                ab.i("MicroMsg.Record.AudioRecordMgr", "mLimitTime:%d, currentTime:%d", new Object[]{Long.valueOf(gVar.ilt), Long.valueOf(gVar.mStartTime)});
                gVar.aIq().post(new 12(gVar));
                ab.i("MicroMsg.Record.AudioRecordMgr", "onResume");
                gVar.ilp = a.ilG;
                gVar.mIsRecording = true;
                mb mbVar = new mb();
                mbVar.cHS.action = 1;
                mbVar.cHS.state = "resume";
                if (gVar.ilo != null) {
                    mbVar.cHS.appId = gVar.ilo.appId;
                }
                com.tencent.mm.sdk.b.a.xxA.a(mbVar, Looper.getMainLooper());
                ab.i("MicroMsg.Record.AudioRecordMgr", "resume record success");
                AppMethodBeat.o(105630);
            } else {
                gVar.onError(7);
                ab.e("MicroMsg.Record.AudioRecordMgr", "resume record fail");
                AppMethodBeat.o(105630);
            }
        } catch (Exception e) {
            ab.printErrStackTrace("MicroMsg.Record.AudioRecordMgr", e, "_resume", new Object[0]);
            gVar.onError(5);
            AppMethodBeat.o(105630);
        }
    }
}
