package com.common.urlhttp;

import android.text.TextUtils;
import com.clj.fastble.BleManager;
import com.home.base.LedBleActivity;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.protocol.HTTP;

/* loaded from: classes.dex */
class RealRequest {
    private static final String BOUNDARY = UUID.randomUUID().toString();
    private static final String LINE_END = "\r\n";
    private static final String TWO_HYPHENS = "--";

    /* JADX INFO: Access modifiers changed from: package-private */
    public RealResponse getData(String str, Map<String, String> map) {
        HttpURLConnection httpURLConnection = null;
        try {
            httpURLConnection = getHttpURLConnection(str, HttpGet.METHOD_NAME);
            httpURLConnection.setDoInput(true);
            if (map != null) {
                setHeader(httpURLConnection, map);
            }
            httpURLConnection.connect();
            return getRealResponse(httpURLConnection);
        } catch (Exception e) {
            return getExceptonResponse(httpURLConnection, e);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public RealResponse postData(String str, String str2, String str3, Map<String, String> map) {
        HttpURLConnection httpURLConnection = null;
        try {
            httpURLConnection = getHttpURLConnection(str, HttpPost.METHOD_NAME);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setRequestProperty("Authorization", "Bearer " + LedBleActivity.getBaseApp().getUserToken());
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            if (!TextUtils.isEmpty(str3)) {
                httpURLConnection.setRequestProperty("Content-Type", str3);
            }
            if (map != null) {
                setHeader(httpURLConnection, map);
            }
            httpURLConnection.connect();
            if (!TextUtils.isEmpty(str2)) {
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8"));
                bufferedWriter.write(str2);
                bufferedWriter.close();
            }
            return getRealResponse(httpURLConnection);
        } catch (Exception e) {
            return getExceptonResponse(httpURLConnection, e);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public RealResponse uploadFile(String str, File file, List<File> list, Map<String, File> map, String str2, String str3, Map<String, String> map2, Map<String, String> map3, CallBackUtil callBackUtil) {
        HttpURLConnection httpURLConnection;
        HttpURLConnection httpURLConnection2 = null;
        try {
            httpURLConnection = getHttpURLConnection(str, HttpPost.METHOD_NAME);
        } catch (Exception e) {
            e = e;
        }
        try {
            setConnection(httpURLConnection);
            if (map3 != null) {
                setHeader(httpURLConnection, map3);
            }
            httpURLConnection.connect();
            DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
            if (map2 != null) {
                dataOutputStream.write(getParamsString(map2).getBytes());
                dataOutputStream.flush();
            }
            if (file != null) {
                writeFile(file, str2, str3, dataOutputStream, callBackUtil);
            } else if (list != null) {
                Iterator<File> it = list.iterator();
                while (it.hasNext()) {
                    writeFile(it.next(), str2, str3, dataOutputStream, null);
                }
            } else if (map != null) {
                for (String str4 : map.keySet()) {
                    writeFile(map.get(str4), str4, str3, dataOutputStream, null);
                }
            }
            dataOutputStream.write(("\r\n--" + BOUNDARY + TWO_HYPHENS + LINE_END).getBytes());
            dataOutputStream.flush();
            return getRealResponse(httpURLConnection);
        } catch (Exception e2) {
            e = e2;
            httpURLConnection2 = httpURLConnection;
            return getExceptonResponse(httpURLConnection2, e);
        }
    }

    private HttpURLConnection getHttpURLConnection(String str, String str2) throws IOException {
        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(str).openConnection();
        httpURLConnection.setConnectTimeout(BleManager.DEFAULT_SCAN_TIME);
        httpURLConnection.setReadTimeout(15000);
        httpURLConnection.setRequestMethod(str2);
        return httpURLConnection;
    }

    private void setHeader(HttpURLConnection httpURLConnection, Map<String, String> map) {
        if (map != null) {
            for (String str : map.keySet()) {
                httpURLConnection.setRequestProperty(str, map.get(str));
            }
        }
    }

    private void setConnection(HttpURLConnection httpURLConnection) throws ProtocolException {
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setRequestProperty("Connection", HTTP.CONN_KEEP_ALIVE);
        httpURLConnection.setRequestProperty("Charset", "UTF-8");
        httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data; BOUNDARY=" + BOUNDARY);
    }

    private String getParamsString(Map<String, String> map) {
        StringBuffer stringBuffer = new StringBuffer();
        for (String str : map.keySet()) {
            stringBuffer.append(TWO_HYPHENS);
            stringBuffer.append(BOUNDARY);
            stringBuffer.append(LINE_END);
            stringBuffer.append("Content-Disposition: form-data; name=\"" + str + "\"");
            stringBuffer.append("\r\nContent-Type: text/plain\r\n");
            stringBuffer.append("Content-Lenght: " + map.get(str).length());
            stringBuffer.append("\r\n\r\n");
            stringBuffer.append(map.get(str));
            stringBuffer.append(LINE_END);
        }
        return stringBuffer.toString();
    }

    private void writeFile(File file, String str, String str2, DataOutputStream dataOutputStream, final CallBackUtil callBackUtil) throws IOException {
        dataOutputStream.write(getFileParamsString(file, str, str2).getBytes());
        dataOutputStream.flush();
        FileInputStream fileInputStream = new FileInputStream(file);
        final long length = file.length();
        byte[] bArr = new byte[2048];
        long j = 0;
        while (true) {
            int read = fileInputStream.read(bArr);
            if (read != -1) {
                dataOutputStream.write(bArr, 0, read);
                final long j2 = j + read;
                if (callBackUtil != null) {
                    CallBackUtil.mMainHandler.post(new Runnable() { // from class: com.common.urlhttp.RealRequest.1
                        @Override // java.lang.Runnable
                        public void run() {
                            CallBackUtil callBackUtil2 = callBackUtil;
                            float f = ((float) j2) * 100.0f;
                            long j3 = length;
                            callBackUtil2.onProgress(f / ((float) j3), j3);
                        }
                    });
                }
                j = j2;
            } else {
                dataOutputStream.flush();
                fileInputStream.close();
                return;
            }
        }
    }

    private String getFileParamsString(File file, String str, String str2) {
        StringBuffer stringBuffer = new StringBuffer("\r\n--");
        stringBuffer.append(BOUNDARY);
        stringBuffer.append(LINE_END);
        stringBuffer.append("Content-Disposition: form-data; name=\"" + str + "\"; filename=\"" + file.getName() + "\"");
        stringBuffer.append(LINE_END);
        stringBuffer.append("Content-Type: " + str2);
        stringBuffer.append(LINE_END);
        stringBuffer.append("Content-Lenght: " + file.length());
        stringBuffer.append("\r\n\r\n");
        return stringBuffer.toString();
    }

    private RealResponse getRealResponse(HttpURLConnection httpURLConnection) throws IOException {
        RealResponse realResponse = new RealResponse();
        realResponse.code = httpURLConnection.getResponseCode();
        realResponse.contentLength = httpURLConnection.getContentLength();
        realResponse.inputStream = httpURLConnection.getInputStream();
        realResponse.errorStream = httpURLConnection.getErrorStream();
        return realResponse;
    }

    private RealResponse getExceptonResponse(HttpURLConnection httpURLConnection, Exception exc) {
        if (httpURLConnection != null) {
            httpURLConnection.disconnect();
        }
        exc.printStackTrace();
        RealResponse realResponse = new RealResponse();
        realResponse.exception = exc;
        return realResponse;
    }
}
