package com.common.urlhttp;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/* loaded from: classes.dex */
public abstract class CallBackUtil<T> {
    static Handler mMainHandler = new Handler(Looper.getMainLooper());

    /* loaded from: classes.dex */
    public static abstract class CallBackDefault extends CallBackUtil<RealResponse> {
        /* JADX WARN: Can't rename method to resolve collision */
        @Override // com.common.urlhttp.CallBackUtil
        public RealResponse onParseResponse(RealResponse realResponse) {
            return realResponse;
        }
    }

    public abstract void onFailure(int i, String str);

    public abstract T onParseResponse(RealResponse realResponse);

    public void onProgress(float f, long j) {
    }

    public abstract void onResponse(T t);

    /* JADX INFO: Access modifiers changed from: package-private */
    public void onError(final RealResponse realResponse) {
        final String str;
        if (realResponse.inputStream != null) {
            str = getRetString(realResponse.inputStream);
        } else if (realResponse.errorStream != null) {
            str = getRetString(realResponse.errorStream);
        } else if (realResponse.exception != null) {
            str = realResponse.exception.getMessage();
        } else {
            str = "";
        }
        mMainHandler.post(new Runnable() { // from class: com.common.urlhttp.CallBackUtil.1
            @Override // java.lang.Runnable
            public void run() {
                CallBackUtil.this.onFailure(realResponse.code, str);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void onSeccess(RealResponse realResponse) {
        final T onParseResponse = onParseResponse(realResponse);
        mMainHandler.post(new Runnable() { // from class: com.common.urlhttp.CallBackUtil.2
            /* JADX WARN: Multi-variable type inference failed */
            @Override // java.lang.Runnable
            public void run() {
                CallBackUtil.this.onResponse(onParseResponse);
            }
        });
    }

    /* loaded from: classes.dex */
    public static abstract class CallBackString extends CallBackUtil<String> {
        @Override // com.common.urlhttp.CallBackUtil
        public String onParseResponse(RealResponse realResponse) {
            try {
                return CallBackUtil.getRetString(realResponse.inputStream);
            } catch (Exception unused) {
                throw new RuntimeException("failure");
            }
        }
    }

    /* loaded from: classes.dex */
    public static abstract class CallBackBitmap extends CallBackUtil<Bitmap> {
        private int mTargetHeight;
        private int mTargetWidth;

        public CallBackBitmap() {
        }

        public CallBackBitmap(int i, int i2) {
            this.mTargetWidth = i;
            this.mTargetHeight = i2;
        }

        public CallBackBitmap(ImageView imageView) {
            int width = imageView.getWidth();
            int height = imageView.getHeight();
            if (width <= 0 || height <= 0) {
                throw new RuntimeException("无法获取ImageView的width或height");
            }
            this.mTargetWidth = width;
            this.mTargetHeight = height;
        }

        /* JADX WARN: Can't rename method to resolve collision */
        @Override // com.common.urlhttp.CallBackUtil
        public Bitmap onParseResponse(RealResponse realResponse) {
            if (this.mTargetWidth == 0 || this.mTargetHeight == 0) {
                return BitmapFactory.decodeStream(realResponse.inputStream);
            }
            return getZoomBitmap(realResponse.inputStream);
        }

        private Bitmap getZoomBitmap(InputStream inputStream) {
            byte[] bArr;
            try {
                bArr = input2byte(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
                bArr = null;
            }
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(bArr, 0, bArr.length, options);
            int i = options.outWidth;
            int i2 = options.outHeight;
            int floor = (int) Math.floor(i / this.mTargetWidth);
            int floor2 = (int) Math.floor(i2 / this.mTargetHeight);
            options.inSampleSize = (floor > 1 || floor2 > 1) ? Math.max(floor, floor2) : 1;
            options.inJustDecodeBounds = false;
            Bitmap decodeByteArray = BitmapFactory.decodeByteArray(bArr, 0, bArr.length, options);
            if (decodeByteArray != null) {
                return decodeByteArray;
            }
            throw new RuntimeException("Failed to decode stream.");
        }
    }

    public static final byte[] input2byte(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bArr = new byte[100];
        while (true) {
            int read = inputStream.read(bArr, 0, 100);
            if (read > 0) {
                byteArrayOutputStream.write(bArr, 0, read);
            } else {
                return byteArrayOutputStream.toByteArray();
            }
        }
    }

    /* loaded from: classes.dex */
    public static abstract class CallBackFile extends CallBackUtil<File> {
        private final String mDestFileDir;
        private final String mdestFileName;

        public CallBackFile(String str, String str2) {
            this.mDestFileDir = str;
            this.mdestFileName = str2;
        }

        /* JADX WARN: Can't rename method to resolve collision */
        /* JADX WARN: Removed duplicated region for block: B:35:0x006f A[Catch: IOException -> 0x0072, TRY_LEAVE, TryCatch #8 {IOException -> 0x0072, blocks: (B:33:0x006a, B:35:0x006f), top: B:32:0x006a }] */
        /* JADX WARN: Removed duplicated region for block: B:38:0x0074 A[EXC_TOP_SPLITTER, SYNTHETIC] */
        /* JADX WARN: Removed duplicated region for block: B:44:? A[RETURN, SYNTHETIC] */
        /* JADX WARN: Removed duplicated region for block: B:52:0x0080 A[Catch: IOException -> 0x0083, TRY_LEAVE, TryCatch #6 {IOException -> 0x0083, blocks: (B:50:0x007b, B:52:0x0080), top: B:49:0x007b }] */
        /* JADX WARN: Removed duplicated region for block: B:55:0x0085 A[EXC_TOP_SPLITTER, SYNTHETIC] */
        /* JADX WARN: Removed duplicated region for block: B:61:? A[SYNTHETIC] */
        @Override // com.common.urlhttp.CallBackUtil
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct add '--show-bad-code' argument
        */
        public java.io.File onParseResponse(com.common.urlhttp.RealResponse r18) {
            /*
                r17 = this;
                r7 = r17
                r0 = r18
                r1 = 8192(0x2000, float:1.14794E-41)
                byte[] r8 = new byte[r1]
                java.io.InputStream r10 = r0.inputStream     // Catch: java.lang.Throwable -> L60 java.lang.Exception -> L64
                long r11 = r0.contentLength     // Catch: java.lang.Throwable -> L5b java.lang.Exception -> L5e
                java.io.File r0 = new java.io.File     // Catch: java.lang.Throwable -> L5b java.lang.Exception -> L5e
                java.lang.String r1 = r7.mDestFileDir     // Catch: java.lang.Throwable -> L5b java.lang.Exception -> L5e
                r0.<init>(r1)     // Catch: java.lang.Throwable -> L5b java.lang.Exception -> L5e
                boolean r1 = r0.exists()     // Catch: java.lang.Throwable -> L5b java.lang.Exception -> L5e
                if (r1 != 0) goto L1c
                r0.mkdirs()     // Catch: java.lang.Throwable -> L5b java.lang.Exception -> L5e
            L1c:
                java.io.File r13 = new java.io.File     // Catch: java.lang.Throwable -> L5b java.lang.Exception -> L5e
                java.lang.String r1 = r7.mdestFileName     // Catch: java.lang.Throwable -> L5b java.lang.Exception -> L5e
                r13.<init>(r0, r1)     // Catch: java.lang.Throwable -> L5b java.lang.Exception -> L5e
                java.io.FileOutputStream r14 = new java.io.FileOutputStream     // Catch: java.lang.Throwable -> L5b java.lang.Exception -> L5e
                r14.<init>(r13)     // Catch: java.lang.Throwable -> L5b java.lang.Exception -> L5e
                r0 = 0
            L2a:
                int r2 = r10.read(r8)     // Catch: java.lang.Exception -> L59 java.lang.Throwable -> L79
                r3 = -1
                if (r2 == r3) goto L4a
                long r3 = (long) r2     // Catch: java.lang.Exception -> L59 java.lang.Throwable -> L79
                long r15 = r0 + r3
                r0 = 0
                r14.write(r8, r0, r2)     // Catch: java.lang.Exception -> L59 java.lang.Throwable -> L79
                android.os.Handler r0 = com.common.urlhttp.CallBackUtil.CallBackFile.mMainHandler     // Catch: java.lang.Exception -> L59 java.lang.Throwable -> L79
                com.common.urlhttp.CallBackUtil$CallBackFile$1 r5 = new com.common.urlhttp.CallBackUtil$CallBackFile$1     // Catch: java.lang.Exception -> L59 java.lang.Throwable -> L79
                r1 = r5
                r2 = r17
                r3 = r15
                r9 = r5
                r5 = r11
                r1.<init>()     // Catch: java.lang.Exception -> L59 java.lang.Throwable -> L79
                r0.post(r9)     // Catch: java.lang.Exception -> L59 java.lang.Throwable -> L79
                r0 = r15
                goto L2a
            L4a:
                r14.flush()     // Catch: java.lang.Exception -> L59 java.lang.Throwable -> L79
                r14.close()     // Catch: java.io.IOException -> L55
                if (r10 == 0) goto L55
                r10.close()     // Catch: java.io.IOException -> L55
            L55:
                r14.close()     // Catch: java.io.IOException -> L58
            L58:
                return r13
            L59:
                r0 = move-exception
                goto L67
            L5b:
                r0 = move-exception
                r9 = 0
                goto L7b
            L5e:
                r0 = move-exception
                goto L66
            L60:
                r0 = move-exception
                r9 = 0
                r10 = 0
                goto L7b
            L64:
                r0 = move-exception
                r10 = 0
            L66:
                r14 = 0
            L67:
                r0.printStackTrace()     // Catch: java.lang.Throwable -> L79
                r14.close()     // Catch: java.io.IOException -> L72
                if (r10 == 0) goto L72
                r10.close()     // Catch: java.io.IOException -> L72
            L72:
                if (r14 == 0) goto L77
                r14.close()     // Catch: java.io.IOException -> L77
            L77:
                r1 = 0
                return r1
            L79:
                r0 = move-exception
                r9 = r14
            L7b:
                r9.close()     // Catch: java.io.IOException -> L83
                if (r10 == 0) goto L83
                r10.close()     // Catch: java.io.IOException -> L83
            L83:
                if (r9 == 0) goto L88
                r9.close()     // Catch: java.io.IOException -> L88
            L88:
                throw r0
            */
            throw new UnsupportedOperationException("Method not decompiled: com.common.urlhttp.CallBackUtil.CallBackFile.onParseResponse(com.common.urlhttp.RealResponse):java.io.File");
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static String getRetString(InputStream inputStream) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            StringBuilder sb = new StringBuilder();
            while (true) {
                String readLine = bufferedReader.readLine();
                if (readLine != null) {
                    sb.append(readLine + "\n");
                } else {
                    inputStream.close();
                    return sb.toString();
                }
            }
        } catch (Exception unused) {
            return null;
        }
    }
}
