package sto.android.app.manager;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * created by xc on 2017-12-05.
 */
public class ModifyAudioManager {
    private static final int TYPE_MAX_NORMAL = 6;
    private static final int TYPE_MAX_HEADSET = 6;
    private static final int TYPE_MAX_SPEAKER = 6;
    //private static int sTypeMedia = 7;
    private static final int TYPE_MAX_HEADSPEAKER = 8;
    private static final int TYPE_MAX_EXTAMP = 6;
    private static final int MAX_VOL_SIZE = 6;
    /**
     * normal, headset, handfree.
     */
    private static int sMaxVolMode = 4;
    /**
     * 7 level.
     */
    private static int sMaxVolLevel = 15;
    /**
     * 8 level.
     */
    private static int sMaxVolType = 9;
    /**
     * Data struct size.
     */
    private static int sStructSize = sMaxVolMode * sMaxVolLevel * sMaxVolType
            + TYPE_MAX_NORMAL + TYPE_MAX_HEADSET + TYPE_MAX_SPEAKER
            + TYPE_MAX_HEADSPEAKER + TYPE_MAX_EXTAMP + sMaxVolType;
    /** Get custom data size. */
//	private static final int GET_CUSTOMD_DATASIZE = 5;
    /**
     * Get custom data.
     */
    private static int sSetCustomerData = 0x101;
    /**
     * Set custom data size.
     */
    private static int sGetCustomerData = 0x100;
    /**
     * Get data error dialog id.
     */

    //private static final int CONSTANT_0XFF = 0xFF;

    private static int[] sOffSet = new int[]{sMaxVolMode * sMaxVolLevel * 0,
            sMaxVolMode * sMaxVolLevel * 1, sMaxVolMode * sMaxVolLevel * 2,
            sMaxVolMode * sMaxVolLevel * 3, sMaxVolMode * sMaxVolLevel * 4,
            sMaxVolMode * sMaxVolLevel * 5, sMaxVolMode * sMaxVolLevel * 6,
            sMaxVolMode * sMaxVolLevel * 7, sMaxVolMode * sMaxVolLevel * 8,
            sMaxVolMode * sMaxVolLevel * 9};

    private ModifyAudioManager() {
    }

    /**
     * 修改k2音频参数
     */
    public static void modify() {

        // 获得当前类
        Class audioSystemClass;
        try {
            audioSystemClass = Class.forName("android.media.AudioSystem");

            // 得到这个方法

            Method getAudioData = audioSystemClass.getMethod("getAudioData",
                    int.class, int.class, byte[].class);

            Method setAudioData = audioSystemClass.getMethod("setAudioData",
                    int.class, int.class, byte[].class);

            byte[] mData = new byte[sStructSize];
            Arrays.fill(mData, 0, sStructSize, (byte) 0);
            getAudioData.invoke(null, sGetCustomerData, sStructSize, mData);

            setMaxValue(mData, 2, (byte) 112, false);

            setAudioData.invoke(null, sSetCustomerData, sStructSize, mData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    private static void setMaxValue(byte[] dataPara, int mode, byte val, boolean dual) {
        if (dataPara == null || mode >= sMaxVolMode) {
            // Xlog.d(TAG, "assert! Check the setting value.");
        }
        if (dual && (mode == 3)) {
            dataPara[sOffSet[sMaxVolType] + mode * MAX_VOL_SIZE + 1] = val;
        } else {
            dataPara[sOffSet[sMaxVolType] + mode * MAX_VOL_SIZE] = val;
        }
    }

}
