package com.tencent.gme.audio.apiexample;
import android.os.Handler;

import com.gme.TMG.ITMGContext;


public class EnginePollHelper {

    private static EnginePollHelper s_enginePollHelper = null;
    private static boolean s_pollEnabled = true;

    public static void createEnginePollHelper() {
        if (s_enginePollHelper == null) {
            s_enginePollHelper = new EnginePollHelper();
            s_enginePollHelper.startTimer();
        }
    }

    public static void destroyEnginePollHelper() {
        if (s_enginePollHelper != null) {
            s_enginePollHelper.stopTimer();
            s_enginePollHelper = null;
        }
    }

    public static void pauseEnginePollHelper() {
        s_pollEnabled = false;
    }

    public static void resumeEnginePollHelper() {
        s_pollEnabled = true;
    }

    private Handler mhandler = new Handler();
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (s_pollEnabled) {
                if (ITMGContext.GetInstance(null) != null)
                    ITMGContext.GetInstance(null).Poll();
            }
            mhandler.postDelayed(mRunnable, 33);
        }
    };

    EnginePollHelper() {

    }

    private void startTimer() {
        mhandler.postDelayed(mRunnable, 33);
    }

    private void stopTimer() {
        mhandler.removeCallbacks(mRunnable);
    }
}
