package ZtlApi;

import android.content.Intent;
import android.media.AudioManager;
import android.os.SystemProperties;
import android.util.Log;
import com.google.android.exoplayer2.util.MimeTypes;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ZtlManager3128 extends ZtlManager {
    static final String SYSTEM_BAR_HIDE = "hide";
    static final String SYSTEM_BAR_SHOW = "show";
    static final String SYSTEM_BAR_STATE = "persist.sys.systemBar";
    private static final String SYS_NODE_VGA_MODE = "/sys/devices/platform/display-subsystem/drm/card0/card0-VGA-1/mode";
    private static final String SYS_NODE_VGA_MODES = "/sys/devices/platform/display-subsystem/drm/card0/card0-VGA-1/modes";
    private String TAG = "Arctan";

    private List<String> readStrListFromFile(String str) throws IOException {
        ArrayList arrayList = new ArrayList();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(str))));
        while (true) {
            String readLine = bufferedReader.readLine();
            if (readLine != null) {
                arrayList.add(readLine);
            } else {
                String str2 = this.TAG;
                Log.d(str2, "readStrListFromFile - " + arrayList.toString());
                return arrayList;
            }
        }
    }

    private String readStrFromFile(String str) throws IOException {
        String str2 = this.TAG;
        Log.d(str2, "readStrFromFile - " + str);
        return new BufferedReader(new InputStreamReader(new FileInputStream(new File(str)))).readLine();
    }

    public void LwlTest(int i) {
        String str = this.TAG;
        Log.d(str, "22LLLLL ----> " + i);
        try {
            readStrListFromFile(SYS_NODE_VGA_MODES);
            readStrFromFile(SYS_NODE_VGA_MODE);
            Log.d(this.TAG, getDisplayMode());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    ZtlManager3128() {
        this.DEBUG_ZTL = getSystemProperty("persist.sys.ztl.debug", "false").equals("true");
    }

    public String getUsbStoragePath() {
        File file = new File("/storage/");
        String str = null;
        try {
            if (!file.exists() || !file.isDirectory()) {
                return null;
            }
            File[] listFiles = file.listFiles();
            if (listFiles.length <= 0) {
                return null;
            }
            str = listFiles[0].getAbsolutePath();
            LOGD("steve : get file path " + str);
            if (!str.contains("USB_DISK")) {
                return str;
            }
            LOGD("steve : open " + str);
            File file2 = new File(str);
            if (!file2.exists() || !file2.isDirectory()) {
                return str;
            }
            File[] listFiles2 = file2.listFiles();
            if (listFiles2.length == 1) {
                String absolutePath = listFiles2[0].getAbsolutePath();
                Log.e("steve : usbPath ", "" + absolutePath);
                return absolutePath;
            }
            for (int i = 0; i < listFiles2.length; i++) {
                if (!listFiles2[i].getAbsolutePath().contains("(")) {
                    String absolutePath2 = listFiles2[i].getAbsolutePath();
                    Log.e("steve : usbPath ", "" + absolutePath2);
                    return absolutePath2;
                }
            }
            return str;
        } catch (Exception e) {
            e.printStackTrace();
            return str;
        }
    }

    public void setSystemDate(int i, int i2, int i3) {
        LOGD("set system Date " + i + "/" + i2 + "/" + i3);
        Intent intent = new Intent("com.ztl.action.setdate");
        intent.putExtra("year", i);
        intent.putExtra("month", i2);
        intent.putExtra("day", i3);
        this.mContext.sendBroadcast(intent);
    }

    public int setAutoDateTime(int i) {
        Intent intent = new Intent("com.ztl.action.autodatetime");
        intent.putExtra("checked", i);
        this.mContext.sendBroadcast(intent);
        return 0;
    }

    public int setRaiseSystemVolume() {
        try {
            ((AudioManager) this.mContext.getSystemService(MimeTypes.BASE_TYPE_AUDIO)).adjustStreamVolume(3, 1, 4);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public int setLowerSystemVolume() {
        try {
            ((AudioManager) this.mContext.getSystemService(MimeTypes.BASE_TYPE_AUDIO)).adjustStreamVolume(3, -1, 4);
            return 0;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public int setSystemBrightness(int i) {
        Log.d("Arctan", "ztl enter set ");
        if (i < 0 || i > 255) {
            LOGD("brightness index 0~255 , please check it");
            return -1;
        }
        try {
            Log.d("Arctan", "before send brodcast");
            Intent intent = new Intent("ZTL.ACTION.SET.SYSTEMBRIGHTNESS");
            intent.putExtra("ztl_brightness", i);
            this.mContext.sendBroadcast(intent);
            return 0;
        } catch (Exception e) {
            try {
                e.printStackTrace();
                return 0;
            } catch (Exception e2) {
                e2.printStackTrace();
                return -1;
            }
        }
    }

    public int getDisplayOrientation() {
        return Integer.valueOf(getSystemProperty("persist.sys.ztlOrientation", "0")).intValue();
    }

    public int Getcurrentgpio(int i) {
        String str;
        execRootCmdSilent("cat /sys/class/gpio/gpio" + i + "/value");
        try {
            str = readStrFromFile("/sys/class/gpio/gpio" + i + "/value");
        } catch (Exception e) {
            e.printStackTrace();
            str = null;
        }
        return Integer.parseInt(str);
    }

    public void setOpenSystemBar() {
        this.mContext.sendBroadcast(new Intent("ZTL.ACTION.OPEN.SYSTEMBAR"));
    }

    public void setCloseSystemBar() {
        this.mContext.sendBroadcast(new Intent("ZTL.ACTION.CLOSE.SYSTEMBAR"));
    }

    public int getSystemBarState() {
        String str = SystemProperties.get(SYSTEM_BAR_STATE);
        if (str.equals(SYSTEM_BAR_SHOW)) {
            return 1;
        }
        return str.equals(SYSTEM_BAR_HIDE) ? 0 : -1;
    }

    public String[] getScreenModes() {
        return getSystemProperty("persist.sys.displaymdoes", "").split(",");
    }

    public void setScreenMode(String str) {
        Intent intent = new Intent("android.ztl.action.SET_SCREEN_MODE");
        intent.putExtra("mode", str);
        this.mContext.sendBroadcast(intent);
    }
}
