ZஜZ v2.7.0版本的 MD5 值为:1bef67e030daecb0566ac755017d6792

以下内容为反编译后的 ImageLoader.java 源代码,内容仅作参考


package im.jpgxbngtig.messenger;

import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.SparseArray;
import androidx.exifinterface.media.ExifInterface;
import androidx.recyclerview.widget.ItemTouchHelper;
import com.google.android.exoplayer2.util.MimeTypes;
import com.king.zxing.util.LogUtils;
import im.jpgxbngtig.messenger.FileLoader;
import im.jpgxbngtig.messenger.ImageLoader;
import im.jpgxbngtig.messenger.secretmedia.EncryptedFileInputStream;
import im.jpgxbngtig.tgnet.ConnectionsManager;
import im.jpgxbngtig.tgnet.RequestDelegate;
import im.jpgxbngtig.tgnet.TLObject;
import im.jpgxbngtig.tgnet.TLRPC;
import im.jpgxbngtig.ui.components.AnimatedFileDrawable;
import im.jpgxbngtig.ui.components.RLottieDrawable;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import kotlin.UByte;
import org.json.JSONArray;
import org.json.JSONObject;

public class ImageLoader {
    public static final String AUTOPLAY_FILTER = "g";
    private boolean canForce8888;
    private LruCache<RLottieDrawable> lottieMemCache;
    private LruCache<BitmapDrawable> memCache;
    private static ThreadLocal<byte[]> bytesLocal = new ThreadLocal<>();
    private static ThreadLocal<byte[]> bytesThumbLocal = new ThreadLocal<>();
    private static byte[] header = new byte[12];
    private static byte[] headerThumb = new byte[12];
    private static volatile ImageLoader Instance = null;
    private HashMap<String, Integer> bitmapUseCounts = new HashMap<>();
    private HashMap<String, CacheImage> imageLoadingByUrl = new HashMap<>();
    private HashMap<String, CacheImage> imageLoadingByKeys = new HashMap<>();
    private SparseArray<CacheImage> imageLoadingByTag = new SparseArray<>();
    private HashMap<String, ThumbGenerateInfo> waitingForQualityThumb = new HashMap<>();
    private SparseArray<String> waitingForQualityThumbByTag = new SparseArray<>();
    private LinkedList<HttpImageTask> httpTasks = new LinkedList<>();
    private LinkedList<ArtworkLoadTask> artworkTasks = new LinkedList<>();
    private DispatchQueue cacheOutQueue = new DispatchQueue("cacheOutQueue");
    private DispatchQueue cacheThumbOutQueue = new DispatchQueue("cacheThumbOutQueue");
    private DispatchQueue thumbGeneratingQueue = new DispatchQueue("thumbGeneratingQueue");
    private DispatchQueue imageLoadQueue = new DispatchQueue("imageLoadQueue");
    private HashMap<String, String> replacedBitmaps = new HashMap<>();
    private ConcurrentHashMap<String, Float> fileProgresses = new ConcurrentHashMap<>();
    private HashMap<String, ThumbGenerateTask> thumbGenerateTasks = new HashMap<>();
    private HashMap<String, Integer> forceLoadingImages = new HashMap<>();
    private int currentHttpTasksCount = 0;
    private int currentArtworkTasksCount = 0;
    private ConcurrentHashMap<String, WebFile> testWebFile = new ConcurrentHashMap<>();
    private LinkedList<HttpFileTask> httpFileLoadTasks = new LinkedList<>();
    private HashMap<String, HttpFileTask> httpFileLoadTasksByKeys = new HashMap<>();
    private HashMap<String, Runnable> retryHttpsTasks = new HashMap<>();
    private int currentHttpFileLoadTasksCount = 0;
    private String ignoreRemoval = null;
    private volatile long lastCacheOutTime = 0;
    private int lastImageNum = 0;
    private long lastProgressUpdateTime = 0;
    private File appPath = null;

    public class ThumbGenerateInfo {
        private boolean big;
        private String filter;
        private ArrayList<ImageReceiver> imageReceiverArray;
        private ArrayList<Integer> imageReceiverGuidsArray;
        private TLRPC.Document parentDocument;

        private ThumbGenerateInfo() {
            this.imageReceiverArray = new ArrayList<>();
            this.imageReceiverGuidsArray = new ArrayList<>();
        }
    }

    public class HttpFileTask extends AsyncTask<Void, Void, Boolean> {
        private int currentAccount;
        private String ext;
        private int fileSize;
        private long lastProgressTime;
        private File tempFile;
        private String url;
        private RandomAccessFile fileOutputStream = null;
        private boolean canRetry = true;

        public HttpFileTask(String str, File file, String str2, int i) {
            this.url = str;
            this.tempFile = file;
            this.ext = str2;
            this.currentAccount = i;
        }

        private void reportProgress(final float f) {
            long currentTimeMillis = System.currentTimeMillis();
            if (f != 1.0f) {
                long j = this.lastProgressTime;
                if (j != 0 && j >= currentTimeMillis - 500) {
                    return;
                }
            }
            this.lastProgressTime = currentTimeMillis;
            Utilities.stageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.HttpFileTask.this.lambda$reportProgress$1$ImageLoader$HttpFileTask(f);
                }
            });
        }

        public void lambda$reportProgress$1$ImageLoader$HttpFileTask(final float f) {
            ImageLoader.this.fileProgresses.put(this.url, Float.valueOf(f));
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.HttpFileTask.this.lambda$null$0$ImageLoader$HttpFileTask(f);
                }
            });
        }

        public void lambda$null$0$ImageLoader$HttpFileTask(float f) {
            NotificationCenter.getInstance(this.currentAccount).postNotificationName(NotificationCenter.FileLoadProgressChanged, this.url, Float.valueOf(f));
        }

        @Override
        public Boolean doInBackground(Void... voidArr) {
            InputStream inputStream;
            URLConnection uRLConnection;
            InputStream inputStream2;
            List<String> list;
            String str;
            int responseCode;
            boolean z = true;
            boolean z2 = false;
            try {
                uRLConnection = new URL(this.url).openConnection();
                try {
                    uRLConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A5297c Safari/602.1");
                    uRLConnection.setConnectTimeout(5000);
                    uRLConnection.setReadTimeout(5000);
                    if (uRLConnection instanceof HttpURLConnection) {
                        HttpURLConnection httpURLConnection = (HttpURLConnection) uRLConnection;
                        httpURLConnection.setInstanceFollowRedirects(true);
                        int responseCode2 = httpURLConnection.getResponseCode();
                        if (responseCode2 == 302 || responseCode2 == 301 || responseCode2 == 303) {
                            String headerField = httpURLConnection.getHeaderField("Location");
                            String headerField2 = httpURLConnection.getHeaderField("Set-Cookie");
                            uRLConnection = new URL(headerField).openConnection();
                            uRLConnection.setRequestProperty("Cookie", headerField2);
                            uRLConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A5297c Safari/602.1");
                        }
                    }
                    uRLConnection.connect();
                    inputStream2 = uRLConnection.getInputStream();
                    try {
                        this.fileOutputStream = new RandomAccessFile(this.tempFile, "rws");
                    } catch (Throwable th) {
                        inputStream = inputStream2;
                        th = th;
                        if (th instanceof SocketTimeoutException) {
                            if (ApplicationLoader.isNetworkOnline()) {
                                this.canRetry = false;
                            }
                        } else if (th instanceof UnknownHostException) {
                            this.canRetry = false;
                        } else if (th instanceof SocketException) {
                            if (th.getMessage() != null && th.getMessage().contains("ECONNRESET")) {
                                this.canRetry = false;
                            }
                        } else if (th instanceof FileNotFoundException) {
                            this.canRetry = false;
                        }
                        FileLog.e(th);
                        inputStream2 = inputStream;
                        if (this.canRetry) {
                        }
                        return Boolean.valueOf(z2);
                    }
                } catch (Throwable th2) {
                    th = th2;
                    inputStream = null;
                }
            } catch (Throwable th3) {
                th = th3;
                inputStream = null;
                uRLConnection = null;
            }
            if (this.canRetry) {
                try {
                    if ((uRLConnection instanceof HttpURLConnection) && (responseCode = ((HttpURLConnection) uRLConnection).getResponseCode()) != 200 && responseCode != 202 && responseCode != 304) {
                        this.canRetry = false;
                    }
                } catch (Exception e) {
                    FileLog.e(e);
                }
                if (uRLConnection != null) {
                    try {
                        Map<String, List<String>> headerFields = uRLConnection.getHeaderFields();
                        if (headerFields != null && (list = headerFields.get("content-Length")) != null && !list.isEmpty() && (str = list.get(0)) != null) {
                            this.fileSize = Utilities.parseInt(str).intValue();
                        }
                    } catch (Exception e2) {
                        FileLog.e(e2);
                    }
                }
                if (inputStream2 != null) {
                    try {
                        byte[] bArr = new byte[32768];
                        int i = 0;
                        while (true) {
                            if (isCancelled()) {
                                break;
                            }
                            try {
                                int read = inputStream2.read(bArr);
                                if (read <= 0) {
                                    break;
                                }
                                this.fileOutputStream.write(bArr, 0, read);
                                i += read;
                                if (this.fileSize > 0) {
                                    reportProgress(i / this.fileSize);
                                }
                            } catch (Exception e3) {
                                e = e3;
                                z = false;
                            }
                        }
                    } catch (Throwable th4) {
                        th = th4;
                        z = false;
                    }
                    z2 = z;
                }
                try {
                    if (this.fileOutputStream != null) {
                        this.fileOutputStream.close();
                        this.fileOutputStream = null;
                    }
                } catch (Throwable th5) {
                    FileLog.e(th5);
                }
                if (inputStream2 != null) {
                    try {
                        inputStream2.close();
                    } catch (Throwable th6) {
                        FileLog.e(th6);
                    }
                }
            }
            return Boolean.valueOf(z2);
        }

        @Override
        public void onPostExecute(Boolean bool) {
            ImageLoader.this.runHttpFileLoadTasks(this, bool.booleanValue() ? 2 : 1);
        }

        @Override
        protected void onCancelled() {
            ImageLoader.this.runHttpFileLoadTasks(this, 2);
        }
    }

    public class ArtworkLoadTask extends AsyncTask<Void, Void, String> {
        private CacheImage cacheImage;
        private boolean canRetry = true;
        private HttpURLConnection httpConnection;
        private boolean small;

        public ArtworkLoadTask(CacheImage cacheImage) {
            this.cacheImage = cacheImage;
            this.small = Uri.parse(cacheImage.imageLocation.path).getQueryParameter("s") != null;
        }

        @Override
        public String doInBackground(Void... voidArr) {
            InputStream inputStream;
            ByteArrayOutputStream byteArrayOutputStream;
            int read;
            int responseCode;
            try {
                try {
                    HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(this.cacheImage.imageLocation.path.replace("athumb://", "https://")).openConnection();
                    this.httpConnection = httpURLConnection;
                    httpURLConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A5297c Safari/602.1");
                    this.httpConnection.setConnectTimeout(5000);
                    this.httpConnection.setReadTimeout(5000);
                    this.httpConnection.connect();
                    try {
                        if (this.httpConnection != null && (responseCode = this.httpConnection.getResponseCode()) != 200 && responseCode != 202 && responseCode != 304) {
                            this.canRetry = false;
                        }
                    } catch (Exception e) {
                        FileLog.e(e);
                    }
                    InputStream inputStream2 = this.httpConnection.getInputStream();
                    try {
                        ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
                        try {
                            byte[] bArr = new byte[32768];
                            while (!isCancelled() && (read = inputStream2.read(bArr)) > 0) {
                                byteArrayOutputStream2.write(bArr, 0, read);
                            }
                            this.canRetry = false;
                            JSONArray jSONArray = new JSONObject(new String(byteArrayOutputStream2.toByteArray())).getJSONArray("results");
                            if (jSONArray.length() <= 0) {
                                try {
                                    if (this.httpConnection != null) {
                                        this.httpConnection.disconnect();
                                    }
                                } catch (Throwable unused) {
                                }
                                if (inputStream2 != null) {
                                    try {
                                        inputStream2.close();
                                    } catch (Throwable th) {
                                        FileLog.e(th);
                                    }
                                }
                                byteArrayOutputStream2.close();
                            }
                            String string = jSONArray.getJSONObject(0).getString("artworkUrl100");
                            if (this.small) {
                                try {
                                    if (this.httpConnection != null) {
                                        this.httpConnection.disconnect();
                                    }
                                } catch (Throwable unused2) {
                                }
                                if (inputStream2 != null) {
                                    try {
                                        inputStream2.close();
                                    } catch (Throwable th2) {
                                        FileLog.e(th2);
                                    }
                                }
                                try {
                                    byteArrayOutputStream2.close();
                                } catch (Exception unused3) {
                                }
                                return string;
                            }
                            String replace = string.replace("100x100", "600x600");
                            try {
                                if (this.httpConnection != null) {
                                    this.httpConnection.disconnect();
                                }
                            } catch (Throwable unused4) {
                            }
                            if (inputStream2 != null) {
                                try {
                                    inputStream2.close();
                                } catch (Throwable th3) {
                                    FileLog.e(th3);
                                }
                            }
                            try {
                                byteArrayOutputStream2.close();
                            } catch (Exception unused5) {
                            }
                            return replace;
                        } catch (Throwable th4) {
                            inputStream = inputStream2;
                            th = th4;
                            byteArrayOutputStream = byteArrayOutputStream2;
                            try {
                                if (th instanceof SocketTimeoutException) {
                                    if (th instanceof UnknownHostException) {
                                        this.canRetry = false;
                                    } else if (th instanceof SocketException) {
                                        if (th.getMessage() != null && th.getMessage().contains("ECONNRESET")) {
                                            this.canRetry = false;
                                        }
                                    } else if (th instanceof FileNotFoundException) {
                                        this.canRetry = false;
                                    }
                                } else if (ApplicationLoader.isNetworkOnline()) {
                                    this.canRetry = false;
                                }
                                FileLog.e(th);
                                try {
                                    if (this.httpConnection != null) {
                                        this.httpConnection.disconnect();
                                    }
                                } catch (Throwable unused6) {
                                }
                                if (inputStream != null) {
                                    try {
                                        inputStream.close();
                                    } catch (Throwable th5) {
                                        FileLog.e(th5);
                                    }
                                }
                                if (byteArrayOutputStream != null) {
                                    byteArrayOutputStream.close();
                                }
                                return null;
                            } catch (Throwable th6) {
                                try {
                                    if (this.httpConnection != null) {
                                        this.httpConnection.disconnect();
                                    }
                                } catch (Throwable unused7) {
                                }
                                if (inputStream != null) {
                                    try {
                                        inputStream.close();
                                    } catch (Throwable th7) {
                                        FileLog.e(th7);
                                    }
                                }
                                if (byteArrayOutputStream != null) {
                                    try {
                                        byteArrayOutputStream.close();
                                    } catch (Exception unused8) {
                                    }
                                }
                                throw th6;
                            }
                        }
                    } catch (Throwable th8) {
                        byteArrayOutputStream = null;
                        inputStream = inputStream2;
                        th = th8;
                    }
                } catch (Exception unused9) {
                    return null;
                }
            } catch (Throwable th9) {
                th = th9;
                inputStream = null;
                byteArrayOutputStream = null;
                if (th instanceof SocketTimeoutException) {
                }
                FileLog.e(th);
                if (this.httpConnection != null) {
                }
                if (inputStream != null) {
                }
                if (byteArrayOutputStream != null) {
                }
                return null;
            }
        }

        @Override
        public void onPostExecute(String str) {
            if (str != null) {
                this.cacheImage.httpTask = new HttpImageTask(this.cacheImage, 0, str);
                ImageLoader.this.httpTasks.add(this.cacheImage.httpTask);
                ImageLoader.this.runHttpTasks(false);
            } else if (this.canRetry) {
                ImageLoader.this.artworkLoadError(this.cacheImage.url);
            }
            ImageLoader.this.imageLoadQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.ArtworkLoadTask.this.lambda$onPostExecute$0$ImageLoader$ArtworkLoadTask();
                }
            });
        }

        public void lambda$onPostExecute$0$ImageLoader$ArtworkLoadTask() {
            ImageLoader.this.runArtworkTasks(true);
        }

        public void lambda$onCancelled$1$ImageLoader$ArtworkLoadTask() {
            ImageLoader.this.runArtworkTasks(true);
        }

        @Override
        protected void onCancelled() {
            ImageLoader.this.imageLoadQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.ArtworkLoadTask.this.lambda$onCancelled$1$ImageLoader$ArtworkLoadTask();
                }
            });
        }
    }

    public class HttpImageTask extends AsyncTask<Void, Void, Boolean> {
        private CacheImage cacheImage;
        private boolean canRetry = true;
        private RandomAccessFile fileOutputStream;
        private HttpURLConnection httpConnection;
        private int imageSize;
        private long lastProgressTime;
        private String overrideUrl;

        public static void lambda$doInBackground$2(TLObject tLObject, TLRPC.TL_error tL_error) {
        }

        public HttpImageTask(CacheImage cacheImage, int i) {
            this.cacheImage = cacheImage;
            this.imageSize = i;
        }

        public HttpImageTask(CacheImage cacheImage, int i, String str) {
            this.cacheImage = cacheImage;
            this.imageSize = i;
            this.overrideUrl = str;
        }

        private void reportProgress(final float f) {
            long currentTimeMillis = System.currentTimeMillis();
            if (f != 1.0f) {
                long j = this.lastProgressTime;
                if (j != 0 && j >= currentTimeMillis - 500) {
                    return;
                }
            }
            this.lastProgressTime = currentTimeMillis;
            Utilities.stageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.HttpImageTask.this.lambda$reportProgress$1$ImageLoader$HttpImageTask(f);
                }
            });
        }

        public void lambda$reportProgress$1$ImageLoader$HttpImageTask(final float f) {
            ImageLoader.this.fileProgresses.put(this.cacheImage.url, Float.valueOf(f));
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.HttpImageTask.this.lambda$null$0$ImageLoader$HttpImageTask(f);
                }
            });
        }

        public void lambda$null$0$ImageLoader$HttpImageTask(float f) {
            NotificationCenter.getInstance(this.cacheImage.currentAccount).postNotificationName(NotificationCenter.FileLoadProgressChanged, this.cacheImage.url, Float.valueOf(f));
        }

        @Override
        public Boolean doInBackground(Void... voidArr) {
            InputStream inputStream;
            InputStream inputStream2;
            int i;
            WebFile webFile;
            HttpURLConnection httpURLConnection;
            List<String> list;
            String str;
            int responseCode;
            boolean z = true;
            boolean z2 = false;
            if (!isCancelled()) {
                try {
                    String str2 = this.cacheImage.imageLocation.path;
                    if ((str2.startsWith("https://static-maps") || str2.startsWith("https://maps.googleapis")) && (((i = MessagesController.getInstance(this.cacheImage.currentAccount).mapProvider) == 3 || i == 4) && (webFile = (WebFile) ImageLoader.this.testWebFile.get(str2)) != null)) {
                        TLRPC.TL_upload_getWebFile tL_upload_getWebFile = new TLRPC.TL_upload_getWebFile();
                        tL_upload_getWebFile.location = webFile.location;
                        tL_upload_getWebFile.offset = 0;
                        tL_upload_getWebFile.limit = 0;
                        ConnectionsManager.getInstance(this.cacheImage.currentAccount).sendRequest(tL_upload_getWebFile, new RequestDelegate() {
                            @Override
                            public final void run(TLObject tLObject, TLRPC.TL_error tL_error) {
                                ImageLoader.HttpImageTask.lambda$doInBackground$2(tLObject, tL_error);
                            }
                        });
                    }
                    if (this.overrideUrl != null) {
                        str2 = this.overrideUrl;
                    }
                    HttpURLConnection httpURLConnection2 = (HttpURLConnection) new URL(str2).openConnection();
                    this.httpConnection = httpURLConnection2;
                    httpURLConnection2.addRequestProperty("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A5297c Safari/602.1");
                    this.httpConnection.setConnectTimeout(5000);
                    this.httpConnection.setReadTimeout(5000);
                    this.httpConnection.setInstanceFollowRedirects(true);
                } catch (Throwable th) {
                    th = th;
                    inputStream = null;
                }
                if (!isCancelled()) {
                    this.httpConnection.connect();
                    inputStream2 = this.httpConnection.getInputStream();
                    try {
                        this.fileOutputStream = new RandomAccessFile(this.cacheImage.tempFilePath, "rws");
                    } catch (Throwable th2) {
                        inputStream = inputStream2;
                        th = th2;
                        if (th instanceof SocketTimeoutException) {
                            if (ApplicationLoader.isNetworkOnline()) {
                                this.canRetry = false;
                            }
                        } else if (th instanceof UnknownHostException) {
                            this.canRetry = false;
                        } else if (th instanceof SocketException) {
                            if (th.getMessage() != null && th.getMessage().contains("ECONNRESET")) {
                                this.canRetry = false;
                            }
                        } else if (th instanceof FileNotFoundException) {
                            this.canRetry = false;
                        }
                        FileLog.e(th);
                        inputStream2 = inputStream;
                        if (!isCancelled()) {
                        }
                        if (this.fileOutputStream != null) {
                        }
                        if (this.httpConnection != null) {
                        }
                        if (inputStream2 != null) {
                        }
                        if (z2) {
                        }
                        return Boolean.valueOf(z2);
                    }
                    if (!isCancelled()) {
                        try {
                            if (this.httpConnection != null && (responseCode = this.httpConnection.getResponseCode()) != 200 && responseCode != 202 && responseCode != 304) {
                                this.canRetry = false;
                            }
                        } catch (Exception e) {
                            FileLog.e(e);
                        }
                        if (this.imageSize == 0 && (httpURLConnection = this.httpConnection) != null) {
                            try {
                                Map<String, List<String>> headerFields = httpURLConnection.getHeaderFields();
                                if (headerFields != null && (list = headerFields.get("content-Length")) != null && !list.isEmpty() && (str = list.get(0)) != null) {
                                    this.imageSize = Utilities.parseInt(str).intValue();
                                }
                            } catch (Exception e2) {
                                FileLog.e(e2);
                            }
                        }
                        if (inputStream2 != null) {
                            try {
                                byte[] bArr = new byte[8192];
                                int i2 = 0;
                                while (true) {
                                    if (isCancelled()) {
                                        break;
                                    }
                                    try {
                                        int read = inputStream2.read(bArr);
                                        if (read <= 0) {
                                            break;
                                        }
                                        i2 += read;
                                        this.fileOutputStream.write(bArr, 0, read);
                                        if (this.imageSize != 0) {
                                            reportProgress(i2 / this.imageSize);
                                        }
                                    } catch (Exception e3) {
                                        e = e3;
                                    }
                                }
                                z2 = z;
                            } catch (Throwable th3) {
                                th = th3;
                            }
                        }
                    }
                    if (this.fileOutputStream != null) {
                        this.fileOutputStream.close();
                        this.fileOutputStream = null;
                    }
                    if (this.httpConnection != null) {
                        this.httpConnection.disconnect();
                    }
                    if (inputStream2 != null) {
                        try {
                            inputStream2.close();
                        } catch (Throwable th4) {
                            FileLog.e(th4);
                        }
                    }
                    if (z2 && this.cacheImage.tempFilePath != null && !this.cacheImage.tempFilePath.renameTo(this.cacheImage.finalFilePath)) {
                        CacheImage cacheImage = this.cacheImage;
                        cacheImage.finalFilePath = cacheImage.tempFilePath;
                    }
                    return Boolean.valueOf(z2);
                }
            }
            inputStream2 = null;
            if (!isCancelled()) {
            }
            if (this.fileOutputStream != null) {
            }
            if (this.httpConnection != null) {
            }
            if (inputStream2 != null) {
            }
            if (z2) {
                CacheImage cacheImage2 = this.cacheImage;
                cacheImage2.finalFilePath = cacheImage2.tempFilePath;
            }
            return Boolean.valueOf(z2);
        }

        @Override
        public void onPostExecute(final Boolean bool) {
            if (bool.booleanValue() || !this.canRetry) {
                ImageLoader.this.fileDidLoaded(this.cacheImage.url, this.cacheImage.finalFilePath, 0);
            } else {
                ImageLoader.this.httpFileLoadError(this.cacheImage.url);
            }
            Utilities.stageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.HttpImageTask.this.lambda$onPostExecute$4$ImageLoader$HttpImageTask(bool);
                }
            });
            ImageLoader.this.imageLoadQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.HttpImageTask.this.lambda$onPostExecute$5$ImageLoader$HttpImageTask();
                }
            });
        }

        public void lambda$onPostExecute$4$ImageLoader$HttpImageTask(final Boolean bool) {
            ImageLoader.this.fileProgresses.remove(this.cacheImage.url);
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.HttpImageTask.this.lambda$null$3$ImageLoader$HttpImageTask(bool);
                }
            });
        }

        public void lambda$null$3$ImageLoader$HttpImageTask(Boolean bool) {
            if (bool.booleanValue()) {
                NotificationCenter.getInstance(this.cacheImage.currentAccount).postNotificationName(NotificationCenter.fileDidLoad, this.cacheImage.url, this.cacheImage.finalFilePath);
            } else {
                NotificationCenter.getInstance(this.cacheImage.currentAccount).postNotificationName(NotificationCenter.fileDidFailToLoad, this.cacheImage.url, 2);
            }
        }

        public void lambda$onPostExecute$5$ImageLoader$HttpImageTask() {
            ImageLoader.this.runHttpTasks(true);
        }

        public void lambda$onCancelled$6$ImageLoader$HttpImageTask() {
            ImageLoader.this.runHttpTasks(true);
        }

        @Override
        protected void onCancelled() {
            ImageLoader.this.imageLoadQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.HttpImageTask.this.lambda$onCancelled$6$ImageLoader$HttpImageTask();
                }
            });
            Utilities.stageQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.HttpImageTask.this.lambda$onCancelled$8$ImageLoader$HttpImageTask();
                }
            });
        }

        public void lambda$onCancelled$8$ImageLoader$HttpImageTask() {
            ImageLoader.this.fileProgresses.remove(this.cacheImage.url);
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.HttpImageTask.this.lambda$null$7$ImageLoader$HttpImageTask();
                }
            });
        }

        public void lambda$null$7$ImageLoader$HttpImageTask() {
            NotificationCenter.getInstance(this.cacheImage.currentAccount).postNotificationName(NotificationCenter.fileDidFailToLoad, this.cacheImage.url, 1);
        }
    }

    public class ThumbGenerateTask implements Runnable {
        private ThumbGenerateInfo info;
        private int mediaType;
        private File originalPath;

        public ThumbGenerateTask(int i, File file, ThumbGenerateInfo thumbGenerateInfo) {
            this.mediaType = i;
            this.originalPath = file;
            this.info = thumbGenerateInfo;
        }

        private void removeTask() {
            ThumbGenerateInfo thumbGenerateInfo = this.info;
            if (thumbGenerateInfo == null) {
                return;
            }
            final String attachFileName = FileLoader.getAttachFileName(thumbGenerateInfo.parentDocument);
            ImageLoader.this.imageLoadQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.ThumbGenerateTask.this.lambda$removeTask$0$ImageLoader$ThumbGenerateTask(attachFileName);
                }
            });
        }

        public void lambda$removeTask$0$ImageLoader$ThumbGenerateTask(String str) {
        }

        @Override
        public void run() {
            Bitmap createScaledBitmap;
            try {
                if (this.info == null) {
                    removeTask();
                    return;
                }
                final String str = "q_" + this.info.parentDocument.dc_id + "_" + this.info.parentDocument.id;
                File file = new File(FileLoader.getDirectory(4), str + ".jpg");
                if (!file.exists() && this.originalPath.exists()) {
                    int max = this.info.big ? Math.max(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) : Math.min(180, Math.min(AndroidUtilities.displaySize.x, AndroidUtilities.displaySize.y) / 4);
                    Bitmap bitmap = null;
                    if (this.mediaType == 0) {
                        float f = max;
                        bitmap = ImageLoader.loadBitmap(this.originalPath.toString(), null, f, f, false);
                    } else {
                        int i = 2;
                        if (this.mediaType == 2) {
                            String file2 = this.originalPath.toString();
                            if (!this.info.big) {
                                i = 1;
                            }
                            bitmap = ThumbnailUtils.createVideoThumbnail(file2, i);
                        } else if (this.mediaType == 3) {
                            String lowerCase = this.originalPath.toString().toLowerCase();
                            if (lowerCase.endsWith("mp4")) {
                                String file3 = this.originalPath.toString();
                                if (!this.info.big) {
                                    i = 1;
                                }
                                bitmap = ThumbnailUtils.createVideoThumbnail(file3, i);
                            } else if (lowerCase.endsWith(".jpg") || lowerCase.endsWith(".jpeg") || lowerCase.endsWith(".png") || lowerCase.endsWith(".gif")) {
                                float f2 = max;
                                bitmap = ImageLoader.loadBitmap(lowerCase, null, f2, f2, false);
                            }
                        }
                    }
                    if (bitmap == null) {
                        removeTask();
                        return;
                    }
                    int width = bitmap.getWidth();
                    int height = bitmap.getHeight();
                    if (width != 0 && height != 0) {
                        float f3 = width;
                        float f4 = max;
                        float f5 = height;
                        float min = Math.min(f3 / f4, f5 / f4);
                        if (min > 1.0f && (createScaledBitmap = Bitmaps.createScaledBitmap(bitmap, (int) (f3 / min), (int) (f5 / min), true)) != bitmap) {
                            bitmap.recycle();
                            bitmap = createScaledBitmap;
                        }
                        FileOutputStream fileOutputStream = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, this.info.big ? 83 : 60, fileOutputStream);
                        try {
                            fileOutputStream.close();
                        } catch (Exception e) {
                            FileLog.e(e);
                        }
                        final BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
                        final ArrayList arrayList = new ArrayList(this.info.imageReceiverArray);
                        final ArrayList arrayList2 = new ArrayList(this.info.imageReceiverGuidsArray);
                        AndroidUtilities.runOnUIThread(new Runnable() {
                            @Override
                            public final void run() {
                                ImageLoader.ThumbGenerateTask.this.lambda$run$1$ImageLoader$ThumbGenerateTask(str, arrayList, bitmapDrawable, arrayList2);
                            }
                        });
                        return;
                    }
                    removeTask();
                    return;
                }
                removeTask();
            } catch (Throwable th) {
                FileLog.e(th);
                removeTask();
            }
        }

        public void lambda$run$1$ImageLoader$ThumbGenerateTask(String str, ArrayList arrayList, BitmapDrawable bitmapDrawable, ArrayList arrayList2) {
            removeTask();
            if (this.info.filter != null) {
                str = str + "@" + this.info.filter;
            }
            for (int i = 0; i < arrayList.size(); i++) {
                ((ImageReceiver) arrayList.get(i)).setImageBitmapByKey(bitmapDrawable, str, 0, false, ((Integer) arrayList2.get(i)).intValue());
            }
            ImageLoader.this.memCache.put(str, bitmapDrawable);
        }
    }

    public class CacheOutTask implements Runnable {
        private CacheImage cacheImage;
        private boolean isCancelled;
        private Thread runningThread;
        private final Object sync = new Object();

        public CacheOutTask(CacheImage cacheImage) {
            this.cacheImage = cacheImage;
        }

        @Override
        public void run() {
            SecureDocumentKey secureDocumentKey;
            byte[] bArr;
            boolean z;
            ExtendedBitmapDrawable extendedBitmapDrawable;
            boolean z2;
            Long l;
            boolean z3;
            Throwable th;
            float f;
            float f2;
            boolean z4;
            boolean z5;
            boolean z6;
            Bitmap bitmap;
            boolean z7;
            float f3;
            float f4;
            ?? r3;
            boolean z8;
            int i;
            boolean z9;
            int i2;
            FileInputStream fileInputStream;
            Bitmap bitmap2;
            boolean z10;
            int i3;
            boolean z11;
            int i4;
            FileInputStream fileInputStream2;
            int i5;
            int attributeInt;
            Bitmap bitmap3;
            boolean z12;
            boolean z13;
            Bitmap createScaledBitmap;
            Bitmap bitmap4;
            boolean z14;
            float f5;
            ?? r32;
            boolean z15;
            boolean z16;
            ?? r33;
            boolean z17;
            boolean z18;
            boolean z19;
            boolean z20;
            FileInputStream fileInputStream3;
            float min;
            boolean z21;
            boolean z22;
            Long l2;
            ExtendedBitmapDrawable extendedBitmapDrawable2;
            Long l3;
            Throwable th2;
            RandomAccessFile randomAccessFile;
            RandomAccessFile randomAccessFile2;
            String lowerCase;
            byte[] bArr2;
            AnimatedFileDrawable animatedFileDrawable;
            int i6;
            int i7;
            boolean z23;
            boolean z24;
            int[] iArr;
            int i8;
            boolean z25;
            int[] iArr2;
            synchronized (this.sync) {
                this.runningThread = Thread.currentThread();
                Thread.interrupted();
                if (this.isCancelled) {
                    return;
                }
                int i9 = 3;
                int i10 = 1;
                if (this.cacheImage.imageLocation.photoSize instanceof TLRPC.TL_photoStrippedSize) {
                    synchronized (this.sync) {
                        if (this.isCancelled) {
                            return;
                        }
                        TLRPC.TL_photoStrippedSize tL_photoStrippedSize = (TLRPC.TL_photoStrippedSize) this.cacheImage.imageLocation.photoSize;
                        int length = (tL_photoStrippedSize.bytes.length - 3) + Bitmaps.header.length + Bitmaps.footer.length;
                        byte[] bArr3 = (byte[]) ImageLoader.bytesLocal.get();
                        if (bArr3 == null || bArr3.length < length) {
                            bArr3 = null;
                        }
                        if (bArr3 == null) {
                            bArr3 = new byte[length];
                            ImageLoader.bytesLocal.set(bArr3);
                        }
                        System.arraycopy(Bitmaps.header, 0, bArr3, 0, Bitmaps.header.length);
                        System.arraycopy(tL_photoStrippedSize.bytes, 3, bArr3, Bitmaps.header.length, tL_photoStrippedSize.bytes.length - 3);
                        System.arraycopy(Bitmaps.footer, 0, bArr3, (Bitmaps.header.length + tL_photoStrippedSize.bytes.length) - 3, Bitmaps.footer.length);
                        bArr3[164] = tL_photoStrippedSize.bytes[1];
                        bArr3[166] = tL_photoStrippedSize.bytes[2];
                        Bitmap decodeByteArray = BitmapFactory.decodeByteArray(bArr3, 0, length);
                        if (decodeByteArray != null && !TextUtils.isEmpty(this.cacheImage.filter) && this.cacheImage.filter.contains("b")) {
                            Utilities.blurBitmap(decodeByteArray, 3, 1, decodeByteArray.getWidth(), decodeByteArray.getHeight(), decodeByteArray.getRowBytes());
                        }
                        onPostExecute(decodeByteArray != null ? new BitmapDrawable(decodeByteArray) : null);
                        return;
                    }
                }
                if (this.cacheImage.lottieFile) {
                    synchronized (this.sync) {
                        if (this.isCancelled) {
                            return;
                        }
                        int min2 = Math.min(512, AndroidUtilities.dp(170.6f));
                        int min3 = Math.min(512, AndroidUtilities.dp(170.6f));
                        if (this.cacheImage.filter != null) {
                            String[] split = this.cacheImage.filter.split("_");
                            if (split.length >= 2) {
                                float parseFloat = Float.parseFloat(split[0]);
                                float parseFloat2 = Float.parseFloat(split[1]);
                                i8 = Math.min(512, (int) (AndroidUtilities.density * parseFloat));
                                int min4 = Math.min(512, (int) (AndroidUtilities.density * parseFloat2));
                                if (parseFloat > 90.0f || parseFloat2 > 90.0f) {
                                    min3 = min4;
                                } else {
                                    int min5 = Math.min(i8, 160);
                                    min3 = Math.min(min4, 160);
                                    r6 = SharedConfig.getDevicePerfomanceClass() != 2;
                                    i8 = min5;
                                    z25 = true;
                                    if (split.length >= 3) {
                                        if ("nr".equals(split[2])) {
                                            i9 = 2;
                                        }
                                        if (split.length >= 5) {
                                            if ("c1".equals(split[4])) {
                                                iArr2 = new int[]{16219713, 13275258, 16757049, 15582629, 16765248, 16245699, 16768889, 16510934};
                                            } else if ("c2".equals(split[4])) {
                                                iArr2 = new int[]{16219713, 11172960, 16757049, 13150599, 16765248, 14534815, 16768889, 15128242};
                                            } else if ("c3".equals(split[4])) {
                                                iArr2 = new int[]{16219713, 9199944, 16757049, 11371874, 16765248, 12885622, 16768889, 13939080};
                                            } else if ("c4".equals(split[4])) {
                                                iArr2 = new int[]{16219713, 7224364, 16757049, 9591348, 16765248, 10579526, 16768889, 11303506};
                                            } else if ("c5".equals(split[4])) {
                                                iArr2 = new int[]{16219713, 2694162, 16757049, 4663842, 16765248, 5716784, 16768889, 6834492};
                                            }
                                            i6 = min3;
                                            i10 = i9;
                                            iArr = iArr2;
                                            z23 = r6;
                                            z24 = z25;
                                            i7 = i8;
                                            RLottieDrawable rLottieDrawable = new RLottieDrawable(this.cacheImage.finalFilePath, i7, i6, z23, z24, iArr);
                                            rLottieDrawable.setAutoRepeat(i10);
                                            onPostExecute(rLottieDrawable);
                                            return;
                                        }
                                        i6 = min3;
                                        i10 = i9;
                                        z23 = r6;
                                        z24 = z25;
                                        i7 = i8;
                                    }
                                    i9 = 1;
                                    if (split.length >= 5) {
                                    }
                                    i6 = min3;
                                    i10 = i9;
                                    z23 = r6;
                                    z24 = z25;
                                    i7 = i8;
                                }
                            } else {
                                i8 = min2;
                            }
                            z25 = false;
                            if (split.length >= 3) {
                            }
                            i9 = 1;
                            if (split.length >= 5) {
                            }
                            i6 = min3;
                            i10 = i9;
                            z23 = r6;
                            z24 = z25;
                            i7 = i8;
                        } else {
                            i6 = min3;
                            i7 = min2;
                            z23 = false;
                            z24 = false;
                        }
                        iArr = null;
                        RLottieDrawable rLottieDrawable2 = new RLottieDrawable(this.cacheImage.finalFilePath, i7, i6, z23, z24, iArr);
                        rLottieDrawable2.setAutoRepeat(i10);
                        onPostExecute(rLottieDrawable2);
                        return;
                    }
                }
                if (this.cacheImage.animatedFile) {
                    synchronized (this.sync) {
                        if (this.isCancelled) {
                            return;
                        }
                        if (ImageLoader.AUTOPLAY_FILTER.equals(this.cacheImage.filter) && !(this.cacheImage.imageLocation.document instanceof TLRPC.TL_documentEncrypted)) {
                            animatedFileDrawable = new AnimatedFileDrawable(this.cacheImage.finalFilePath, false, this.cacheImage.size, this.cacheImage.imageLocation.document instanceof TLRPC.Document ? this.cacheImage.imageLocation.document : null, this.cacheImage.parentObject, this.cacheImage.currentAccount, false);
                        } else {
                            animatedFileDrawable = new AnimatedFileDrawable(this.cacheImage.finalFilePath, "d".equals(this.cacheImage.filter), 0L, null, null, this.cacheImage.currentAccount, false);
                        }
                        Thread.interrupted();
                        onPostExecute(animatedFileDrawable);
                        return;
                    }
                }
                File file = this.cacheImage.finalFilePath;
                boolean z26 = (this.cacheImage.secureDocument == null && (this.cacheImage.encryptionKeyPath == null || file == null || !file.getAbsolutePath().endsWith(".enc"))) ? false : true;
                if (this.cacheImage.secureDocument != null) {
                    SecureDocumentKey secureDocumentKey2 = this.cacheImage.secureDocument.secureDocumentKey;
                    if (this.cacheImage.secureDocument.secureFile != null && this.cacheImage.secureDocument.secureFile.file_hash != null) {
                        bArr2 = this.cacheImage.secureDocument.secureFile.file_hash;
                    } else {
                        bArr2 = this.cacheImage.secureDocument.fileHash;
                    }
                    bArr = bArr2;
                    secureDocumentKey = secureDocumentKey2;
                } else {
                    secureDocumentKey = null;
                    bArr = null;
                }
                try {
                    if (Build.VERSION.SDK_INT < 19) {
                        try {
                            randomAccessFile2 = new RandomAccessFile(file, "r");
                            try {
                                try {
                                    byte[] bArr4 = this.cacheImage.imageType == 1 ? ImageLoader.headerThumb : ImageLoader.header;
                                    randomAccessFile2.readFully(bArr4, 0, bArr4.length);
                                    lowerCase = new String(bArr4).toLowerCase().toLowerCase();
                                } catch (Exception e) {
                                    e = e;
                                    z = false;
                                    FileLog.e(e);
                                    if (randomAccessFile2 != null) {
                                        randomAccessFile2.close();
                                    }
                                    if (this.cacheImage.imageLocation.path != null) {
                                    }
                                    extendedBitmapDrawable = null;
                                    z2 = false;
                                    l = null;
                                    z3 = true;
                                    BitmapFactory.Options options = new BitmapFactory.Options();
                                    options.inSampleSize = 1;
                                    if (Build.VERSION.SDK_INT < 21) {
                                    }
                                    boolean z27 = ImageLoader.this.canForce8888;
                                    if (this.cacheImage.filter != null) {
                                    }
                                    bitmap = bitmap4;
                                    f4 = f5;
                                    r3 = z14;
                                    boolean z28 = z2;
                                    Long l4 = l;
                                    if (this.cacheImage.imageType == 1) {
                                    }
                                    Thread.interrupted();
                                    if (!z8) {
                                    }
                                    onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                }
                            } catch (Throwable th3) {
                                th2 = th3;
                                randomAccessFile = randomAccessFile2;
                                if (randomAccessFile == null) {
                                    try {
                                        randomAccessFile.close();
                                        throw th2;
                                    } catch (Exception e2) {
                                        FileLog.e(e2);
                                        throw th2;
                                    }
                                }
                                throw th2;
                            }
                        } catch (Exception e3) {
                            e = e3;
                            randomAccessFile2 = null;
                        } catch (Throwable th4) {
                            th2 = th4;
                            randomAccessFile = null;
                            if (randomAccessFile == null) {
                            }
                        }
                        if (lowerCase.startsWith("riff")) {
                            if (lowerCase.endsWith("webp")) {
                                z = true;
                                randomAccessFile2.close();
                                randomAccessFile2.close();
                            }
                        }
                        z = false;
                        randomAccessFile2.close();
                        randomAccessFile2.close();
                    } else {
                        z = false;
                    }
                } catch (Exception e4) {
                    FileLog.e(e4);
                }
                if (this.cacheImage.imageLocation.path != null) {
                    String str = this.cacheImage.imageLocation.path;
                    if (str.startsWith("thumb://")) {
                        int indexOf = str.indexOf(LogUtils.COLON, 8);
                        if (indexOf >= 0) {
                            l3 = Long.valueOf(Long.parseLong(str.substring(8, indexOf)));
                            extendedBitmapDrawable2 = str.substring(indexOf + 1);
                        } else {
                            extendedBitmapDrawable2 = null;
                            l3 = null;
                        }
                        extendedBitmapDrawable = extendedBitmapDrawable2;
                        l = l3;
                        z2 = false;
                    } else {
                        if (str.startsWith("vthumb://")) {
                            int indexOf2 = str.indexOf(LogUtils.COLON, 9);
                            if (indexOf2 >= 0) {
                                l2 = Long.valueOf(Long.parseLong(str.substring(9, indexOf2)));
                                z22 = true;
                            } else {
                                z22 = false;
                                l2 = null;
                            }
                            l = l2;
                            extendedBitmapDrawable = null;
                            z3 = false;
                            z2 = z22;
                            BitmapFactory.Options options2 = new BitmapFactory.Options();
                            options2.inSampleSize = 1;
                            if (Build.VERSION.SDK_INT < 21) {
                                options2.inPurgeable = true;
                            }
                            boolean z272 = ImageLoader.this.canForce8888;
                            if (this.cacheImage.filter != null) {
                                String[] split2 = this.cacheImage.filter.split("_");
                                if (split2.length >= 2) {
                                    f2 = Float.parseFloat(split2[0]) * AndroidUtilities.density;
                                    try {
                                        f5 = f2;
                                        f3 = Float.parseFloat(split2[1]) * AndroidUtilities.density;
                                    } catch (Throwable th5) {
                                        th = th5;
                                        f = 0.0f;
                                        z4 = false;
                                        z5 = false;
                                        z6 = z4;
                                        bitmap = null;
                                        z7 = z6;
                                        FileLog.e(th);
                                        float f6 = f2;
                                        f3 = f;
                                        f4 = f6;
                                        r3 = z7;
                                        boolean z282 = z2;
                                        Long l42 = l;
                                        if (this.cacheImage.imageType == 1) {
                                        }
                                        Thread.interrupted();
                                        if (!z8) {
                                        }
                                        onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                    }
                                } else {
                                    f3 = 0.0f;
                                    f5 = 0.0f;
                                }
                                try {
                                    if (this.cacheImage.filter.contains("b2")) {
                                        r32 = 3;
                                    } else if (this.cacheImage.filter.contains("b1")) {
                                        r32 = 2;
                                    } else {
                                        r32 = this.cacheImage.filter.contains("b") ? 1 : 0;
                                    }
                                    try {
                                        z5 = this.cacheImage.filter.contains("i");
                                        try {
                                            if (this.cacheImage.filter.contains("f")) {
                                                z272 = true;
                                            }
                                            if (z || f5 == 0.0f || f3 == 0.0f) {
                                                z15 = r32 == true ? 1 : 0;
                                                z16 = z5;
                                            } else {
                                                options2.inJustDecodeBounds = true;
                                                try {
                                                    try {
                                                        if (l == null || extendedBitmapDrawable != null) {
                                                            boolean z29 = r32 == true ? 1 : 0;
                                                            z16 = z5;
                                                            if (secureDocumentKey != null) {
                                                                try {
                                                                    RandomAccessFile randomAccessFile3 = new RandomAccessFile(file, "r");
                                                                    int length2 = (int) randomAccessFile3.length();
                                                                    byte[] bArr5 = (byte[]) ImageLoader.bytesLocal.get();
                                                                    if (bArr5 == null || bArr5.length < length2) {
                                                                        bArr5 = null;
                                                                    }
                                                                    if (bArr5 == null) {
                                                                        bArr5 = new byte[length2];
                                                                        ImageLoader.bytesLocal.set(bArr5);
                                                                    }
                                                                    randomAccessFile3.readFully(bArr5, 0, length2);
                                                                    randomAccessFile3.close();
                                                                    EncryptedFileInputStream.decryptBytesWithKeyFile(bArr5, 0, length2, secureDocumentKey);
                                                                    byte[] computeSHA256 = Utilities.computeSHA256(bArr5, 0, length2);
                                                                    if (bArr == null || !Arrays.equals(computeSHA256, bArr)) {
                                                                        z18 = z29;
                                                                        z19 = true;
                                                                    } else {
                                                                        z18 = z29;
                                                                        z19 = false;
                                                                    }
                                                                    int i11 = bArr5[0] & UByte.MAX_VALUE;
                                                                    int i12 = length2 - i11;
                                                                    z20 = z18;
                                                                    if (!z19) {
                                                                        BitmapFactory.decodeByteArray(bArr5, i11, i12, options2);
                                                                        z20 = z18;
                                                                    }
                                                                } catch (Throwable th6) {
                                                                    th = th6;
                                                                    th = th;
                                                                    f = f3;
                                                                    f2 = f5;
                                                                    z5 = z16;
                                                                    z6 = z29;
                                                                    bitmap = null;
                                                                    z7 = z6;
                                                                    FileLog.e(th);
                                                                    float f62 = f2;
                                                                    f3 = f;
                                                                    f4 = f62;
                                                                    r3 = z7;
                                                                    boolean z2822 = z2;
                                                                    Long l422 = l;
                                                                    if (this.cacheImage.imageType == 1) {
                                                                    }
                                                                    Thread.interrupted();
                                                                    if (!z8) {
                                                                    }
                                                                    onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                                                }
                                                            } else {
                                                                z20 = z29;
                                                                if (z26) {
                                                                    fileInputStream3 = new EncryptedFileInputStream(file, this.cacheImage.encryptionKeyPath);
                                                                } else {
                                                                    fileInputStream3 = new FileInputStream(file);
                                                                }
                                                                BitmapFactory.decodeStream(fileInputStream3, null, options2);
                                                                fileInputStream3.close();
                                                            }
                                                        } else {
                                                            if (z2) {
                                                                try {
                                                                    ContentResolver contentResolver = ApplicationLoader.applicationContext.getContentResolver();
                                                                    z21 = r32 == true ? 1 : 0;
                                                                    z16 = z5;
                                                                    MediaStore.Video.Thumbnails.getThumbnail(contentResolver, l.longValue(), 1, options2);
                                                                } catch (Throwable th7) {
                                                                    th = th7;
                                                                    boolean z30 = r32 == true ? 1 : 0;
                                                                    r33 = r32;
                                                                    bitmap = null;
                                                                    th = th;
                                                                    f = f3;
                                                                    f2 = f5;
                                                                    z7 = r33;
                                                                    FileLog.e(th);
                                                                    float f622 = f2;
                                                                    f3 = f;
                                                                    f4 = f622;
                                                                    r3 = z7;
                                                                    boolean z28222 = z2;
                                                                    Long l4222 = l;
                                                                    if (this.cacheImage.imageType == 1) {
                                                                    }
                                                                    Thread.interrupted();
                                                                    if (!z8) {
                                                                    }
                                                                    onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                                                }
                                                            } else {
                                                                z21 = r32 == true ? 1 : 0;
                                                                z16 = z5;
                                                                MediaStore.Images.Thumbnails.getThumbnail(ApplicationLoader.applicationContext.getContentResolver(), l.longValue(), 1, options2);
                                                            }
                                                            z20 = z21;
                                                        }
                                                        float f7 = options2.outWidth;
                                                        float f8 = options2.outHeight;
                                                        if (f5 >= f3 && f7 > f8) {
                                                            min = Math.max(f7 / f5, f8 / f3);
                                                        } else {
                                                            min = Math.min(f7 / f5, f8 / f3);
                                                        }
                                                        if (min < 1.2f) {
                                                            min = 1.0f;
                                                        }
                                                        options2.inJustDecodeBounds = false;
                                                        if (min > 1.0f && (f7 > f5 || f8 > f3)) {
                                                            int i13 = 1;
                                                            do {
                                                                i13 *= 2;
                                                            } while (i13 * 2 < min);
                                                            options2.inSampleSize = i13;
                                                            z15 = z20;
                                                        } else {
                                                            options2.inSampleSize = (int) min;
                                                            z15 = z20;
                                                        }
                                                    } catch (Throwable th8) {
                                                        th = th8;
                                                        r33 = "f";
                                                        z5 = z17;
                                                    }
                                                } catch (Throwable th9) {
                                                    th = th9;
                                                }
                                            }
                                            z5 = z16;
                                            z14 = z15;
                                            bitmap4 = null;
                                        } catch (Throwable th10) {
                                            boolean z31 = r32 == true ? 1 : 0;
                                            th = th10;
                                            f = f3;
                                            f2 = f5;
                                            z6 = r32;
                                        }
                                    } catch (Throwable th11) {
                                        boolean z32 = r32 == true ? 1 : 0;
                                        th = th11;
                                        f = f3;
                                        f2 = f5;
                                        z4 = r32;
                                        z5 = false;
                                        z6 = z4;
                                        bitmap = null;
                                        z7 = z6;
                                        FileLog.e(th);
                                        float f6222 = f2;
                                        f3 = f;
                                        f4 = f6222;
                                        r3 = z7;
                                        boolean z282222 = z2;
                                        Long l42222 = l;
                                        if (this.cacheImage.imageType == 1) {
                                        }
                                        Thread.interrupted();
                                        if (!z8) {
                                        }
                                        onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                    }
                                } catch (Throwable th12) {
                                    th = th12;
                                    f = f3;
                                    f2 = f5;
                                    z4 = false;
                                    z5 = false;
                                    z6 = z4;
                                    bitmap = null;
                                    z7 = z6;
                                    FileLog.e(th);
                                    float f62222 = f2;
                                    f3 = f;
                                    f4 = f62222;
                                    r3 = z7;
                                    boolean z2822222 = z2;
                                    Long l422222 = l;
                                    if (this.cacheImage.imageType == 1) {
                                    }
                                    Thread.interrupted();
                                    if (!z8) {
                                    }
                                    onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                }
                            } else {
                                if (extendedBitmapDrawable != null) {
                                    options2.inJustDecodeBounds = true;
                                    options2.inPreferredConfig = z272 ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
                                    FileInputStream fileInputStream4 = new FileInputStream(file);
                                    Bitmap decodeStream = BitmapFactory.decodeStream(fileInputStream4, null, options2);
                                    try {
                                        fileInputStream4.close();
                                        int i14 = options2.outWidth;
                                        int i15 = options2.outHeight;
                                        options2.inJustDecodeBounds = false;
                                        float max = Math.max(i14 / ItemTouchHelper.Callback.DEFAULT_DRAG_ANIMATION_DURATION, i15 / ItemTouchHelper.Callback.DEFAULT_DRAG_ANIMATION_DURATION);
                                        if (max < 1.0f) {
                                            max = 1.0f;
                                        }
                                        if (max > 1.0f) {
                                            int i16 = 1;
                                            do {
                                                i16 *= 2;
                                            } while (i16 * 2 < max);
                                            options2.inSampleSize = i16;
                                        } else {
                                            options2.inSampleSize = (int) max;
                                        }
                                        bitmap4 = decodeStream;
                                    } catch (Throwable th13) {
                                        th = th13;
                                        bitmap = decodeStream;
                                        f = 0.0f;
                                        f2 = 0.0f;
                                        z7 = false;
                                        z5 = false;
                                        FileLog.e(th);
                                        float f622222 = f2;
                                        f3 = f;
                                        f4 = f622222;
                                        r3 = z7;
                                        boolean z28222222 = z2;
                                        Long l4222222 = l;
                                        if (this.cacheImage.imageType == 1) {
                                        }
                                        Thread.interrupted();
                                        if (!z8) {
                                        }
                                        onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                    }
                                } else {
                                    bitmap4 = null;
                                }
                                f3 = 0.0f;
                                z14 = false;
                                z5 = false;
                                f5 = 0.0f;
                            }
                            bitmap = bitmap4;
                            f4 = f5;
                            r3 = z14;
                            boolean z282222222 = z2;
                            Long l42222222 = l;
                            if (this.cacheImage.imageType == 1) {
                                try {
                                    ImageLoader.this.lastCacheOutTime = System.currentTimeMillis();
                                } catch (Throwable th14) {
                                    th = th14;
                                    z8 = false;
                                }
                                synchronized (this.sync) {
                                    if (this.isCancelled) {
                                        return;
                                    }
                                    if (z) {
                                        RandomAccessFile randomAccessFile4 = new RandomAccessFile(file, "r");
                                        MappedByteBuffer map = randomAccessFile4.getChannel().map(FileChannel.MapMode.READ_ONLY, 0L, file.length());
                                        BitmapFactory.Options options3 = new BitmapFactory.Options();
                                        options3.inJustDecodeBounds = true;
                                        Utilities.loadWebpImage(null, map, map.limit(), options3, true);
                                        bitmap = Bitmaps.createBitmap(options3.outWidth, options3.outHeight, Bitmap.Config.ARGB_8888);
                                        Utilities.loadWebpImage(bitmap, map, map.limit(), null, !options2.inPurgeable);
                                        randomAccessFile4.close();
                                    } else {
                                        if (!options2.inPurgeable && secureDocumentKey == null) {
                                            if (z26) {
                                                fileInputStream = new EncryptedFileInputStream(file, this.cacheImage.encryptionKeyPath);
                                            } else {
                                                fileInputStream = new FileInputStream(file);
                                            }
                                            bitmap = BitmapFactory.decodeStream(fileInputStream, null, options2);
                                            fileInputStream.close();
                                        }
                                        RandomAccessFile randomAccessFile5 = new RandomAccessFile(file, "r");
                                        int length3 = (int) randomAccessFile5.length();
                                        byte[] bArr6 = (byte[]) ImageLoader.bytesThumbLocal.get();
                                        if (bArr6 == null || bArr6.length < length3) {
                                            bArr6 = null;
                                        }
                                        if (bArr6 == null) {
                                            bArr6 = new byte[length3];
                                            ImageLoader.bytesThumbLocal.set(bArr6);
                                        }
                                        randomAccessFile5.readFully(bArr6, 0, length3);
                                        randomAccessFile5.close();
                                        if (secureDocumentKey != null) {
                                            EncryptedFileInputStream.decryptBytesWithKeyFile(bArr6, 0, length3, secureDocumentKey);
                                            byte[] computeSHA2562 = Utilities.computeSHA256(bArr6, 0, length3);
                                            if (bArr != null && Arrays.equals(computeSHA2562, bArr)) {
                                                z9 = false;
                                                i2 = bArr6[0] & UByte.MAX_VALUE;
                                                length3 -= i2;
                                            }
                                            z9 = true;
                                            i2 = bArr6[0] & UByte.MAX_VALUE;
                                            length3 -= i2;
                                        } else {
                                            if (z26) {
                                                EncryptedFileInputStream.decryptBytesWithKeyFile(bArr6, 0, length3, this.cacheImage.encryptionKeyPath);
                                            }
                                            z9 = false;
                                            i2 = 0;
                                        }
                                        if (!z9) {
                                            bitmap = BitmapFactory.decodeByteArray(bArr6, i2, length3, options2);
                                        }
                                    }
                                    if (bitmap == null) {
                                        if (file.length() == 0 || this.cacheImage.filter == null) {
                                            file.delete();
                                        }
                                        z8 = false;
                                    } else {
                                        if (this.cacheImage.filter != null) {
                                            float width = bitmap.getWidth();
                                            float height = bitmap.getHeight();
                                            if (!options2.inPurgeable && f4 != 0.0f && width != f4 && width > f4 + 20.0f) {
                                                Bitmap createScaledBitmap2 = Bitmaps.createScaledBitmap(bitmap, (int) f4, (int) (height / (width / f4)), true);
                                                if (bitmap != createScaledBitmap2) {
                                                    bitmap.recycle();
                                                    bitmap = createScaledBitmap2;
                                                }
                                            }
                                        }
                                        if (z5) {
                                            z8 = Utilities.needInvert(bitmap, options2.inPurgeable ? 0 : 1, bitmap.getWidth(), bitmap.getHeight(), bitmap.getRowBytes()) != 0;
                                        } else {
                                            z8 = false;
                                        }
                                        try {
                                            if (r3 == 1) {
                                                if (bitmap.getConfig() == Bitmap.Config.ARGB_8888) {
                                                    Utilities.blurBitmap(bitmap, 3, options2.inPurgeable ? 0 : 1, bitmap.getWidth(), bitmap.getHeight(), bitmap.getRowBytes());
                                                }
                                            } else if (r3 == 2) {
                                                if (bitmap.getConfig() == Bitmap.Config.ARGB_8888) {
                                                    Utilities.blurBitmap(bitmap, 1, options2.inPurgeable ? 0 : 1, bitmap.getWidth(), bitmap.getHeight(), bitmap.getRowBytes());
                                                }
                                            } else if (r3 == 3) {
                                                if (bitmap.getConfig() == Bitmap.Config.ARGB_8888) {
                                                    Utilities.blurBitmap(bitmap, 7, options2.inPurgeable ? 0 : 1, bitmap.getWidth(), bitmap.getHeight(), bitmap.getRowBytes());
                                                    Utilities.blurBitmap(bitmap, 7, options2.inPurgeable ? 0 : 1, bitmap.getWidth(), bitmap.getHeight(), bitmap.getRowBytes());
                                                    Utilities.blurBitmap(bitmap, 7, options2.inPurgeable ? 0 : 1, bitmap.getWidth(), bitmap.getHeight(), bitmap.getRowBytes());
                                                }
                                            } else if (r3 == 0) {
                                                if (options2.inPurgeable) {
                                                    Utilities.pinBitmap(bitmap);
                                                }
                                            }
                                        } catch (Throwable th15) {
                                            th = th15;
                                            FileLog.e(th);
                                            i = 0;
                                            extendedBitmapDrawable = null;
                                            Thread.interrupted();
                                            if (!z8) {
                                            }
                                            onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                        }
                                    }
                                    i = 0;
                                    extendedBitmapDrawable = null;
                                }
                            } else {
                                int i17 = l42222222 != null ? 0 : 20;
                                try {
                                    if (i17 != 0) {
                                        try {
                                            if (ImageLoader.this.lastCacheOutTime != 0) {
                                                bitmap2 = bitmap;
                                                long j = i17;
                                                if (ImageLoader.this.lastCacheOutTime > System.currentTimeMillis() - j) {
                                                    try {
                                                        if (Build.VERSION.SDK_INT < 21) {
                                                            Thread.sleep(j);
                                                        }
                                                    } catch (Throwable unused) {
                                                        bitmap = bitmap2;
                                                        z10 = false;
                                                        extendedBitmapDrawable = null;
                                                        i3 = 0;
                                                        z8 = z10;
                                                        i = i3;
                                                        Thread.interrupted();
                                                        if (!z8) {
                                                        }
                                                        onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                                    }
                                                }
                                                ImageLoader.this.lastCacheOutTime = System.currentTimeMillis();
                                                synchronized (this.sync) {
                                                    try {
                                                        if (this.isCancelled) {
                                                            return;
                                                        }
                                                        if (!z272 && this.cacheImage.filter != null && r3 == 0 && this.cacheImage.imageLocation.path == null) {
                                                            options2.inPreferredConfig = Bitmap.Config.RGB_565;
                                                            options2.inDither = false;
                                                            if (l42222222 != null || extendedBitmapDrawable != null) {
                                                                bitmap = bitmap2;
                                                            } else if (z282222222) {
                                                                bitmap = MediaStore.Video.Thumbnails.getThumbnail(ApplicationLoader.applicationContext.getContentResolver(), l42222222.longValue(), 1, options2);
                                                            } else {
                                                                bitmap = MediaStore.Images.Thumbnails.getThumbnail(ApplicationLoader.applicationContext.getContentResolver(), l42222222.longValue(), 1, options2);
                                                            }
                                                            if (bitmap != null) {
                                                                try {
                                                                    try {
                                                                        if (z) {
                                                                            RandomAccessFile randomAccessFile6 = new RandomAccessFile(file, "r");
                                                                            MappedByteBuffer map2 = randomAccessFile6.getChannel().map(FileChannel.MapMode.READ_ONLY, 0L, file.length());
                                                                            BitmapFactory.Options options4 = new BitmapFactory.Options();
                                                                            options4.inJustDecodeBounds = true;
                                                                            try {
                                                                                Utilities.loadWebpImage(null, map2, map2.limit(), options4, true);
                                                                                bitmap = Bitmaps.createBitmap(options4.outWidth, options4.outHeight, Bitmap.Config.ARGB_8888);
                                                                                Utilities.loadWebpImage(bitmap, map2, map2.limit(), null, !options2.inPurgeable);
                                                                                randomAccessFile6.close();
                                                                                i5 = 0;
                                                                                extendedBitmapDrawable = null;
                                                                            } catch (Throwable unused2) {
                                                                                extendedBitmapDrawable = null;
                                                                                z10 = false;
                                                                                i3 = 0;
                                                                                z8 = z10;
                                                                                i = i3;
                                                                                Thread.interrupted();
                                                                                if (!z8) {
                                                                                }
                                                                                onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                                                            }
                                                                        } else {
                                                                            try {
                                                                                if (options2.inPurgeable || secureDocumentKey != null) {
                                                                                    extendedBitmapDrawable = null;
                                                                                    RandomAccessFile randomAccessFile7 = new RandomAccessFile(file, "r");
                                                                                    int length4 = (int) randomAccessFile7.length();
                                                                                    byte[] bArr7 = (byte[]) ImageLoader.bytesLocal.get();
                                                                                    if (bArr7 == null || bArr7.length < length4) {
                                                                                        bArr7 = null;
                                                                                    }
                                                                                    if (bArr7 == null) {
                                                                                        bArr7 = new byte[length4];
                                                                                        ImageLoader.bytesLocal.set(bArr7);
                                                                                    }
                                                                                    randomAccessFile7.readFully(bArr7, 0, length4);
                                                                                    randomAccessFile7.close();
                                                                                    if (secureDocumentKey != null) {
                                                                                        EncryptedFileInputStream.decryptBytesWithKeyFile(bArr7, 0, length4, secureDocumentKey);
                                                                                        byte[] computeSHA2563 = Utilities.computeSHA256(bArr7, 0, length4);
                                                                                        if (bArr != null && Arrays.equals(computeSHA2563, bArr)) {
                                                                                            z11 = false;
                                                                                            i4 = bArr7[0] & UByte.MAX_VALUE;
                                                                                            length4 -= i4;
                                                                                        }
                                                                                        z11 = true;
                                                                                        i4 = bArr7[0] & UByte.MAX_VALUE;
                                                                                        length4 -= i4;
                                                                                    } else {
                                                                                        if (z26) {
                                                                                            EncryptedFileInputStream.decryptBytesWithKeyFile(bArr7, 0, length4, this.cacheImage.encryptionKeyPath);
                                                                                        }
                                                                                        z11 = false;
                                                                                        i4 = 0;
                                                                                    }
                                                                                    if (!z11) {
                                                                                        bitmap = BitmapFactory.decodeByteArray(bArr7, i4, length4, options2);
                                                                                    }
                                                                                } else {
                                                                                    if (z26) {
                                                                                        fileInputStream2 = new EncryptedFileInputStream(file, this.cacheImage.encryptionKeyPath);
                                                                                    } else {
                                                                                        fileInputStream2 = new FileInputStream(file);
                                                                                    }
                                                                                    if (this.cacheImage.imageLocation.document instanceof TLRPC.TL_document) {
                                                                                        try {
                                                                                            attributeInt = new ExifInterface(fileInputStream2).getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                                                                                        } catch (Throwable unused3) {
                                                                                        }
                                                                                        if (attributeInt == 3) {
                                                                                            i5 = 180;
                                                                                        } else if (attributeInt != 6) {
                                                                                            if (attributeInt == 8) {
                                                                                                i5 = 270;
                                                                                            }
                                                                                            i5 = 0;
                                                                                        } else {
                                                                                            i5 = 90;
                                                                                        }
                                                                                        try {
                                                                                            fileInputStream2.getChannel().position(0L);
                                                                                        } catch (Throwable unused4) {
                                                                                            i3 = i5;
                                                                                            z10 = false;
                                                                                            extendedBitmapDrawable = null;
                                                                                            z8 = z10;
                                                                                            i = i3;
                                                                                            Thread.interrupted();
                                                                                            if (!z8) {
                                                                                            }
                                                                                            onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                                                                        }
                                                                                    } else {
                                                                                        i5 = 0;
                                                                                    }
                                                                                    extendedBitmapDrawable = null;
                                                                                    try {
                                                                                        bitmap = BitmapFactory.decodeStream(fileInputStream2, null, options2);
                                                                                        fileInputStream2.close();
                                                                                    } catch (Throwable unused5) {
                                                                                        i3 = i5;
                                                                                        z10 = false;
                                                                                        z8 = z10;
                                                                                        i = i3;
                                                                                        Thread.interrupted();
                                                                                        if (!z8) {
                                                                                        }
                                                                                        onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                                                                    }
                                                                                }
                                                                            } catch (Throwable unused6) {
                                                                                extendedBitmapDrawable = null;
                                                                                z10 = false;
                                                                                i3 = 0;
                                                                                z8 = z10;
                                                                                i = i3;
                                                                                Thread.interrupted();
                                                                                if (!z8) {
                                                                                }
                                                                                onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                                                            }
                                                                        }
                                                                        if (bitmap == null) {
                                                                            if (z3 && (file.length() == 0 || this.cacheImage.filter == null)) {
                                                                                file.delete();
                                                                            }
                                                                            z10 = false;
                                                                        } else {
                                                                            try {
                                                                                if (this.cacheImage.filter != null) {
                                                                                    float width2 = bitmap.getWidth();
                                                                                    float height2 = bitmap.getHeight();
                                                                                    if (!options2.inPurgeable && f4 != 0.0f && width2 != f4 && width2 > f4 + 20.0f) {
                                                                                        if (width2 > height2 && f4 > f3) {
                                                                                            createScaledBitmap = Bitmaps.createScaledBitmap(bitmap, (int) f4, (int) (height2 / (width2 / f4)), true);
                                                                                        } else {
                                                                                            createScaledBitmap = Bitmaps.createScaledBitmap(bitmap, (int) (width2 / (height2 / f3)), (int) f3, true);
                                                                                        }
                                                                                        if (bitmap != createScaledBitmap) {
                                                                                            bitmap.recycle();
                                                                                            bitmap = createScaledBitmap;
                                                                                        }
                                                                                    }
                                                                                    if (bitmap != null) {
                                                                                        if (z5) {
                                                                                            Bitmap createScaledBitmap3 = bitmap.getWidth() * bitmap.getHeight() > 22500 ? Bitmaps.createScaledBitmap(bitmap, 100, 100, false) : bitmap;
                                                                                            z13 = Utilities.needInvert(createScaledBitmap3, options2.inPurgeable ? 0 : 1, createScaledBitmap3.getWidth(), createScaledBitmap3.getHeight(), createScaledBitmap3.getRowBytes()) != 0;
                                                                                            if (createScaledBitmap3 != bitmap) {
                                                                                                try {
                                                                                                    createScaledBitmap3.recycle();
                                                                                                } catch (Throwable unused7) {
                                                                                                    z10 = z13;
                                                                                                    i3 = i5;
                                                                                                    z8 = z10;
                                                                                                    i = i3;
                                                                                                    Thread.interrupted();
                                                                                                    if (!z8) {
                                                                                                    }
                                                                                                    onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                                                                                }
                                                                                            }
                                                                                        } else {
                                                                                            z13 = false;
                                                                                        }
                                                                                        if (r3 == 0 || height2 >= 100.0f || width2 >= 100.0f) {
                                                                                            z10 = z13;
                                                                                            bitmap3 = bitmap;
                                                                                            z12 = false;
                                                                                        } else {
                                                                                            if (bitmap.getConfig() == Bitmap.Config.ARGB_8888) {
                                                                                                Utilities.blurBitmap(bitmap, 3, options2.inPurgeable ? 0 : 1, bitmap.getWidth(), bitmap.getHeight(), bitmap.getRowBytes());
                                                                                            }
                                                                                            z10 = z13;
                                                                                            bitmap3 = bitmap;
                                                                                            z12 = true;
                                                                                        }
                                                                                        if (!z12) {
                                                                                            try {
                                                                                                if (options2.inPurgeable) {
                                                                                                    Utilities.pinBitmap(bitmap3);
                                                                                                }
                                                                                            } catch (Throwable unused8) {
                                                                                                bitmap = bitmap3;
                                                                                                i3 = i5;
                                                                                                z8 = z10;
                                                                                                i = i3;
                                                                                                Thread.interrupted();
                                                                                                if (!z8) {
                                                                                                }
                                                                                                onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                                                                            }
                                                                                        }
                                                                                        bitmap = bitmap3;
                                                                                    }
                                                                                }
                                                                                bitmap3 = bitmap;
                                                                                z12 = false;
                                                                                z10 = false;
                                                                                if (!z12) {
                                                                                }
                                                                                bitmap = bitmap3;
                                                                            } catch (Throwable unused9) {
                                                                                i3 = i5;
                                                                                z10 = false;
                                                                                z8 = z10;
                                                                                i = i3;
                                                                                Thread.interrupted();
                                                                                if (!z8) {
                                                                                }
                                                                                onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                                                            }
                                                                        }
                                                                        z8 = z10;
                                                                        i = i5;
                                                                    } catch (Throwable unused10) {
                                                                    }
                                                                } catch (Throwable unused11) {
                                                                    z10 = false;
                                                                    extendedBitmapDrawable = null;
                                                                    i3 = 0;
                                                                    z8 = z10;
                                                                    i = i3;
                                                                    Thread.interrupted();
                                                                    if (!z8) {
                                                                    }
                                                                    onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                                                }
                                                            } else {
                                                                extendedBitmapDrawable = null;
                                                            }
                                                            i5 = 0;
                                                            if (bitmap == null) {
                                                            }
                                                            z8 = z10;
                                                            i = i5;
                                                        }
                                                        options2.inPreferredConfig = Bitmap.Config.ARGB_8888;
                                                        options2.inDither = false;
                                                        if (l42222222 != null) {
                                                        }
                                                        bitmap = bitmap2;
                                                        if (bitmap != null) {
                                                        }
                                                        i5 = 0;
                                                        if (bitmap == null) {
                                                        }
                                                        z8 = z10;
                                                        i = i5;
                                                    } catch (Throwable th16) {
                                                        th = th16;
                                                        extendedBitmapDrawable = null;
                                                        while (true) {
                                                            try {
                                                                try {
                                                                    break;
                                                                } catch (Throwable unused12) {
                                                                    bitmap = bitmap2;
                                                                    z10 = false;
                                                                    i3 = 0;
                                                                    z8 = z10;
                                                                    i = i3;
                                                                    Thread.interrupted();
                                                                    if (!z8) {
                                                                    }
                                                                    onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                                                                }
                                                            } catch (Throwable th17) {
                                                                th = th17;
                                                            }
                                                        }
                                                        throw th;
                                                    }
                                                }
                                            }
                                        } catch (Throwable unused13) {
                                        }
                                    }
                                    ImageLoader.this.lastCacheOutTime = System.currentTimeMillis();
                                    synchronized (this.sync) {
                                    }
                                } catch (Throwable unused14) {
                                    extendedBitmapDrawable = null;
                                }
                                bitmap2 = bitmap;
                            }
                            Thread.interrupted();
                            if (!z8 || i != 0) {
                                onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                            } else {
                                onPostExecute(bitmap != null ? new BitmapDrawable(bitmap) : extendedBitmapDrawable);
                                return;
                            }
                        }
                        if (!str.startsWith("http")) {
                            extendedBitmapDrawable = null;
                            z2 = false;
                            l = null;
                        }
                    }
                    z3 = false;
                    BitmapFactory.Options options22 = new BitmapFactory.Options();
                    options22.inSampleSize = 1;
                    if (Build.VERSION.SDK_INT < 21) {
                    }
                    boolean z2722 = ImageLoader.this.canForce8888;
                    if (this.cacheImage.filter != null) {
                    }
                    bitmap = bitmap4;
                    f4 = f5;
                    r3 = z14;
                    boolean z2822222222 = z2;
                    Long l422222222 = l;
                    if (this.cacheImage.imageType == 1) {
                    }
                    Thread.interrupted();
                    if (!z8) {
                    }
                    onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
                }
                extendedBitmapDrawable = null;
                z2 = false;
                l = null;
                z3 = true;
                BitmapFactory.Options options222 = new BitmapFactory.Options();
                options222.inSampleSize = 1;
                if (Build.VERSION.SDK_INT < 21) {
                }
                boolean z27222 = ImageLoader.this.canForce8888;
                if (this.cacheImage.filter != null) {
                }
                bitmap = bitmap4;
                f4 = f5;
                r3 = z14;
                boolean z28222222222 = z2;
                Long l4222222222 = l;
                if (this.cacheImage.imageType == 1) {
                }
                Thread.interrupted();
                if (!z8) {
                }
                onPostExecute(bitmap == null ? new ExtendedBitmapDrawable(bitmap, z8, i) : extendedBitmapDrawable);
            }
        }

        private void onPostExecute(final Drawable drawable) {
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.CacheOutTask.this.lambda$onPostExecute$1$ImageLoader$CacheOutTask(drawable);
                }
            });
        }

        public void lambda$onPostExecute$1$ImageLoader$CacheOutTask(final Drawable drawable) {
            final String str = null;
            if (drawable instanceof RLottieDrawable) {
                RLottieDrawable rLottieDrawable = (RLottieDrawable) drawable;
                Drawable drawable2 = (Drawable) ImageLoader.this.lottieMemCache.get(this.cacheImage.key);
                if (drawable2 == null) {
                    ImageLoader.this.lottieMemCache.put(this.cacheImage.key, rLottieDrawable);
                    drawable = rLottieDrawable;
                } else {
                    rLottieDrawable.recycle();
                    drawable = drawable2;
                }
                if (drawable != null) {
                    ImageLoader.this.incrementUseCount(this.cacheImage.key);
                    str = this.cacheImage.key;
                }
            } else if (!(drawable instanceof AnimatedFileDrawable)) {
                if (drawable instanceof BitmapDrawable) {
                    BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
                    Drawable drawable3 = (Drawable) ImageLoader.this.memCache.get(this.cacheImage.key);
                    if (drawable3 == null) {
                        ImageLoader.this.memCache.put(this.cacheImage.key, bitmapDrawable);
                        drawable = bitmapDrawable;
                    } else {
                        bitmapDrawable.getBitmap().recycle();
                        drawable = drawable3;
                    }
                    if (drawable != null) {
                        ImageLoader.this.incrementUseCount(this.cacheImage.key);
                        str = this.cacheImage.key;
                    }
                } else {
                    drawable = null;
                }
            }
            ImageLoader.this.imageLoadQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.CacheOutTask.this.lambda$null$0$ImageLoader$CacheOutTask(drawable, str);
                }
            });
        }

        public void lambda$null$0$ImageLoader$CacheOutTask(Drawable drawable, String str) {
            this.cacheImage.setImageAndClear(drawable, str);
        }

        public void cancel() {
            synchronized (this.sync) {
                try {
                    this.isCancelled = true;
                    if (this.runningThread != null) {
                        this.runningThread.interrupt();
                    }
                } catch (Exception unused) {
                }
            }
        }
    }

    public class CacheImage {
        protected boolean animatedFile;
        protected ArtworkLoadTask artworkTask;
        protected CacheOutTask cacheTask;
        protected int currentAccount;
        protected File encryptionKeyPath;
        protected String ext;
        protected String filter;
        protected ArrayList<String> filters;
        protected File finalFilePath;
        protected HttpImageTask httpTask;
        protected ImageLocation imageLocation;
        protected ArrayList<ImageReceiver> imageReceiverArray;
        protected ArrayList<Integer> imageReceiverGuidsArray;
        protected int imageType;
        protected ArrayList<Integer> imageTypes;
        protected String key;
        protected ArrayList<String> keys;
        protected boolean lottieFile;
        protected Object parentObject;
        protected SecureDocument secureDocument;
        protected int size;
        protected File tempFilePath;
        protected String url;

        private CacheImage() {
            this.imageReceiverArray = new ArrayList<>();
            this.imageReceiverGuidsArray = new ArrayList<>();
            this.keys = new ArrayList<>();
            this.filters = new ArrayList<>();
            this.imageTypes = new ArrayList<>();
        }

        public void addImageReceiver(ImageReceiver imageReceiver, String str, String str2, int i, int i2) {
            int indexOf = this.imageReceiverArray.indexOf(imageReceiver);
            if (indexOf >= 0) {
                this.imageReceiverGuidsArray.set(indexOf, Integer.valueOf(i2));
                return;
            }
            this.imageReceiverArray.add(imageReceiver);
            this.imageReceiverGuidsArray.add(Integer.valueOf(i2));
            this.keys.add(str);
            this.filters.add(str2);
            this.imageTypes.add(Integer.valueOf(i));
            ImageLoader.this.imageLoadingByTag.put(imageReceiver.getTag(i), this);
        }

        public void replaceImageReceiver(ImageReceiver imageReceiver, String str, String str2, int i, int i2) {
            int indexOf = this.imageReceiverArray.indexOf(imageReceiver);
            if (indexOf == -1) {
                return;
            }
            if (this.imageTypes.get(indexOf).intValue() != i) {
                ArrayList<ImageReceiver> arrayList = this.imageReceiverArray;
                indexOf = arrayList.subList(indexOf + 1, arrayList.size()).indexOf(imageReceiver);
                if (indexOf == -1) {
                    return;
                }
            }
            this.imageReceiverGuidsArray.set(indexOf, Integer.valueOf(i2));
            this.keys.set(indexOf, str);
            this.filters.set(indexOf, str2);
        }

        public void removeImageReceiver(ImageReceiver imageReceiver) {
            int i = this.imageType;
            int i2 = 0;
            while (i2 < this.imageReceiverArray.size()) {
                ImageReceiver imageReceiver2 = this.imageReceiverArray.get(i2);
                if (imageReceiver2 == null || imageReceiver2 == imageReceiver) {
                    this.imageReceiverArray.remove(i2);
                    this.imageReceiverGuidsArray.remove(i2);
                    this.keys.remove(i2);
                    this.filters.remove(i2);
                    i = this.imageTypes.remove(i2).intValue();
                    if (imageReceiver2 != null) {
                        ImageLoader.this.imageLoadingByTag.remove(imageReceiver2.getTag(i));
                    }
                    i2--;
                }
                i2++;
            }
            if (this.imageReceiverArray.isEmpty()) {
                if (this.imageLocation != null && !ImageLoader.this.forceLoadingImages.containsKey(this.key)) {
                    if (this.imageLocation.location != null) {
                        FileLoader.getInstance(this.currentAccount).cancelLoadFile(this.imageLocation.location, this.ext);
                    } else if (this.imageLocation.document != null) {
                        FileLoader.getInstance(this.currentAccount).cancelLoadFile(this.imageLocation.document);
                    } else if (this.imageLocation.secureDocument != null) {
                        FileLoader.getInstance(this.currentAccount).cancelLoadFile(this.imageLocation.secureDocument);
                    } else if (this.imageLocation.webFile != null) {
                        FileLoader.getInstance(this.currentAccount).cancelLoadFile(this.imageLocation.webFile);
                    }
                }
                if (this.cacheTask != null) {
                    if (i == 1) {
                        ImageLoader.this.cacheThumbOutQueue.cancelRunnable(this.cacheTask);
                    } else {
                        ImageLoader.this.cacheOutQueue.cancelRunnable(this.cacheTask);
                    }
                    this.cacheTask.cancel();
                    this.cacheTask = null;
                }
                if (this.httpTask != null) {
                    ImageLoader.this.httpTasks.remove(this.httpTask);
                    this.httpTask.cancel(true);
                    this.httpTask = null;
                }
                if (this.artworkTask != null) {
                    ImageLoader.this.artworkTasks.remove(this.artworkTask);
                    this.artworkTask.cancel(true);
                    this.artworkTask = null;
                }
                if (this.url != null) {
                    ImageLoader.this.imageLoadingByUrl.remove(this.url);
                }
                if (this.key != null) {
                    ImageLoader.this.imageLoadingByKeys.remove(this.key);
                }
            }
        }

        public void setImageAndClear(final Drawable drawable, final String str) {
            if (drawable != null) {
                final ArrayList arrayList = new ArrayList(this.imageReceiverArray);
                final ArrayList arrayList2 = new ArrayList(this.imageReceiverGuidsArray);
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        ImageLoader.CacheImage.this.lambda$setImageAndClear$0$ImageLoader$CacheImage(drawable, arrayList, arrayList2, str);
                    }
                });
            }
            for (int i = 0; i < this.imageReceiverArray.size(); i++) {
                ImageLoader.this.imageLoadingByTag.remove(this.imageReceiverArray.get(i).getTag(this.imageType));
            }
            this.imageReceiverArray.clear();
            this.imageReceiverGuidsArray.clear();
            if (this.url != null) {
                ImageLoader.this.imageLoadingByUrl.remove(this.url);
            }
            if (this.key != null) {
                ImageLoader.this.imageLoadingByKeys.remove(this.key);
            }
        }

        public void lambda$setImageAndClear$0$ImageLoader$CacheImage(Drawable drawable, ArrayList arrayList, ArrayList arrayList2, String str) {
            int i = 0;
            if (drawable instanceof AnimatedFileDrawable) {
                AnimatedFileDrawable animatedFileDrawable = (AnimatedFileDrawable) drawable;
                boolean z = false;
                while (i < arrayList.size()) {
                    ImageReceiver imageReceiver = (ImageReceiver) arrayList.get(i);
                    AnimatedFileDrawable makeCopy = i == 0 ? animatedFileDrawable : animatedFileDrawable.makeCopy();
                    if (imageReceiver.setImageBitmapByKey(makeCopy, this.key, this.imageType, false, ((Integer) arrayList2.get(i)).intValue())) {
                        if (makeCopy == animatedFileDrawable) {
                            z = true;
                        }
                    } else if (makeCopy != animatedFileDrawable) {
                        makeCopy.recycle();
                    }
                    i++;
                }
                if (!z) {
                    animatedFileDrawable.recycle();
                }
            } else {
                while (i < arrayList.size()) {
                    ((ImageReceiver) arrayList.get(i)).setImageBitmapByKey(drawable, this.key, this.imageTypes.get(i).intValue(), false, ((Integer) arrayList2.get(i)).intValue());
                    i++;
                }
            }
            if (str != null) {
                ImageLoader.this.decrementUseCount(str);
            }
        }
    }

    public static ImageLoader getInstance() {
        ImageLoader imageLoader = Instance;
        if (imageLoader == null) {
            synchronized (ImageLoader.class) {
                imageLoader = Instance;
                if (imageLoader == null) {
                    imageLoader = new ImageLoader();
                    Instance = imageLoader;
                }
            }
        }
        return imageLoader;
    }

    public ImageLoader() {
        this.thumbGeneratingQueue.setPriority(1);
        int memoryClass = ((ActivityManager) ApplicationLoader.applicationContext.getSystemService("activity")).getMemoryClass();
        boolean z = memoryClass >= 192;
        this.canForce8888 = z;
        this.memCache = new LruCache<BitmapDrawable>(Math.min(z ? 30 : 15, memoryClass / 7) * 1024 * 1024) {
            @Override
            public int sizeOf(String str, BitmapDrawable bitmapDrawable) {
                return bitmapDrawable.getBitmap().getByteCount();
            }

            @Override
            public void entryRemoved(boolean z2, String str, BitmapDrawable bitmapDrawable, BitmapDrawable bitmapDrawable2) {
                if (ImageLoader.this.ignoreRemoval == null || !ImageLoader.this.ignoreRemoval.equals(str)) {
                    Integer num = (Integer) ImageLoader.this.bitmapUseCounts.get(str);
                    if (num == null || num.intValue() == 0) {
                        Bitmap bitmap = bitmapDrawable.getBitmap();
                        if (bitmap.isRecycled()) {
                            return;
                        }
                        bitmap.recycle();
                    }
                }
            }
        };
        this.lottieMemCache = new LruCache<RLottieDrawable>(10485760) {
            @Override
            public int sizeOf(String str, RLottieDrawable rLottieDrawable) {
                return rLottieDrawable.getIntrinsicWidth() * rLottieDrawable.getIntrinsicHeight() * 4 * 2;
            }

            @Override
            public void entryRemoved(boolean z2, String str, RLottieDrawable rLottieDrawable, RLottieDrawable rLottieDrawable2) {
                Integer num = (Integer) ImageLoader.this.bitmapUseCounts.get(str);
                if (num == null || num.intValue() == 0) {
                    rLottieDrawable.recycle();
                }
            }
        };
        SparseArray sparseArray = new SparseArray();
        File cacheDir = AndroidUtilities.getCacheDir();
        if (!cacheDir.isDirectory()) {
            try {
                cacheDir.mkdirs();
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
        try {
            new File(cacheDir, ".nomedia").createNewFile();
        } catch (Exception e2) {
            FileLog.e(e2);
        }
        sparseArray.put(4, cacheDir);
        for (int i = 0; i < 3; i++) {
            FileLoader.getInstance(i).setDelegate(new AnonymousClass3(i));
        }
        FileLoader.setMediaDirs(sparseArray);
        AnonymousClass4 anonymousClass4 = new AnonymousClass4();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("android.intent.action.MEDIA_BAD_REMOVAL");
        intentFilter.addAction("android.intent.action.MEDIA_CHECKING");
        intentFilter.addAction("android.intent.action.MEDIA_EJECT");
        intentFilter.addAction("android.intent.action.MEDIA_MOUNTED");
        intentFilter.addAction("android.intent.action.MEDIA_NOFS");
        intentFilter.addAction("android.intent.action.MEDIA_REMOVED");
        intentFilter.addAction("android.intent.action.MEDIA_SHARED");
        intentFilter.addAction("android.intent.action.MEDIA_UNMOUNTABLE");
        intentFilter.addAction("android.intent.action.MEDIA_UNMOUNTED");
        intentFilter.addDataScheme("file");
        try {
            ApplicationLoader.applicationContext.registerReceiver(anonymousClass4, intentFilter);
        } catch (Throwable unused) {
        }
        checkMediaPaths();
    }

    public class AnonymousClass3 implements FileLoader.FileLoaderDelegate {
        final int val$currentAccount;

        AnonymousClass3(int i) {
            this.val$currentAccount = i;
        }

        @Override
        public void fileUploadProgressChanged(final String str, final float f, final boolean z) {
            ImageLoader.this.fileProgresses.put(str, Float.valueOf(f));
            long currentTimeMillis = System.currentTimeMillis();
            if (ImageLoader.this.lastProgressUpdateTime == 0 || ImageLoader.this.lastProgressUpdateTime < currentTimeMillis - 500) {
                ImageLoader.this.lastProgressUpdateTime = currentTimeMillis;
                final int i = this.val$currentAccount;
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        NotificationCenter.getInstance(i).postNotificationName(NotificationCenter.FileUploadProgressChanged, str, Float.valueOf(f), Boolean.valueOf(z));
                    }
                });
            }
        }

        @Override
        public void fileDidUploaded(final String str, final TLRPC.InputFile inputFile, final TLRPC.InputEncryptedFile inputEncryptedFile, final byte[] bArr, final byte[] bArr2, final long j, final boolean z) {
            DispatchQueue dispatchQueue = Utilities.stageQueue;
            final int i = this.val$currentAccount;
            dispatchQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.AnonymousClass3.this.lambda$fileDidUploaded$2$ImageLoader$3(i, str, inputFile, inputEncryptedFile, bArr, bArr2, j, z);
                }
            });
        }

        public void lambda$fileDidUploaded$2$ImageLoader$3(final int i, final String str, final TLRPC.InputFile inputFile, final TLRPC.InputEncryptedFile inputEncryptedFile, final byte[] bArr, final byte[] bArr2, final long j, final boolean z) {
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    NotificationCenter.getInstance(i).postNotificationName(NotificationCenter.FileDidUpload, str, inputFile, inputEncryptedFile, bArr, bArr2, Long.valueOf(j), Boolean.valueOf(z));
                }
            });
            ImageLoader.this.fileProgresses.remove(str);
        }

        @Override
        public void fileDidFailedUpload(final String str, final boolean z) {
            DispatchQueue dispatchQueue = Utilities.stageQueue;
            final int i = this.val$currentAccount;
            dispatchQueue.postRunnable(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.AnonymousClass3.this.lambda$fileDidFailedUpload$4$ImageLoader$3(i, str, z);
                }
            });
        }

        public void lambda$fileDidFailedUpload$4$ImageLoader$3(final int i, final String str, final boolean z) {
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    NotificationCenter.getInstance(i).postNotificationName(NotificationCenter.FileDidFailUpload, str, Boolean.valueOf(z));
                }
            });
            ImageLoader.this.fileProgresses.remove(str);
        }

        @Override
        public void fileDidLoaded(final String str, final File file, final int i) {
            ImageLoader.this.fileProgresses.remove(str);
            final int i2 = this.val$currentAccount;
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.AnonymousClass3.this.lambda$fileDidLoaded$5$ImageLoader$3(file, str, i2, i);
                }
            });
        }

        public void lambda$fileDidLoaded$5$ImageLoader$3(File file, String str, int i, int i2) {
            if (SharedConfig.saveToGallery && ImageLoader.this.appPath != null && file != null && ((str.endsWith(".mp4") || str.endsWith(".jpg")) && file.toString().startsWith(ImageLoader.this.appPath.toString()))) {
                AndroidUtilities.addMediaToGallery(file.toString());
            }
            NotificationCenter.getInstance(i).postNotificationName(NotificationCenter.fileDidLoad, str, file);
            ImageLoader.this.fileDidLoaded(str, file, i2);
        }

        @Override
        public void fileDidFailedLoad(final String str, final int i) {
            ImageLoader.this.fileProgresses.remove(str);
            final int i2 = this.val$currentAccount;
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.AnonymousClass3.this.lambda$fileDidFailedLoad$6$ImageLoader$3(str, i, i2);
                }
            });
        }

        public void lambda$fileDidFailedLoad$6$ImageLoader$3(String str, int i, int i2) {
            ImageLoader.this.fileDidFailedLoad(str, i);
            NotificationCenter.getInstance(i2).postNotificationName(NotificationCenter.fileDidFailToLoad, str, Integer.valueOf(i));
        }

        @Override
        public void fileLoadProgressChanged(final String str, final float f) {
            ImageLoader.this.fileProgresses.put(str, Float.valueOf(f));
            long currentTimeMillis = System.currentTimeMillis();
            if (ImageLoader.this.lastProgressUpdateTime == 0 || ImageLoader.this.lastProgressUpdateTime < currentTimeMillis - 500) {
                ImageLoader.this.lastProgressUpdateTime = currentTimeMillis;
                final int i = this.val$currentAccount;
                AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public final void run() {
                        NotificationCenter.getInstance(i).postNotificationName(NotificationCenter.FileLoadProgressChanged, str, Float.valueOf(f));
                    }
                });
            }
        }
    }

    public class AnonymousClass4 extends BroadcastReceiver {
        AnonymousClass4() {
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("file system changed");
            }
            Runnable runnable = new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.AnonymousClass4.this.lambda$onReceive$0$ImageLoader$4();
                }
            };
            if ("android.intent.action.MEDIA_UNMOUNTED".equals(intent.getAction())) {
                AndroidUtilities.runOnUIThread(runnable, 1000L);
            } else {
                runnable.run();
            }
        }

        public void lambda$onReceive$0$ImageLoader$4() {
            ImageLoader.this.checkMediaPaths();
        }
    }

    public void checkMediaPaths() {
        this.cacheOutQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                ImageLoader.this.lambda$checkMediaPaths$1$ImageLoader();
            }
        });
    }

    public void lambda$checkMediaPaths$1$ImageLoader() {
        final SparseArray<File> createMediaPaths = createMediaPaths();
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                FileLoader.setMediaDirs(createMediaPaths);
            }
        });
    }

    public void addTestWebFile(String str, WebFile webFile) {
        if (str == null || webFile == null) {
            return;
        }
        this.testWebFile.put(str, webFile);
    }

    public void removeTestWebFile(String str) {
        if (str == null) {
            return;
        }
        this.testWebFile.remove(str);
    }

    public SparseArray<File> createMediaPaths() {
        SparseArray<File> sparseArray = new SparseArray<>();
        File cacheDir = AndroidUtilities.getCacheDir();
        if (!cacheDir.isDirectory()) {
            try {
                cacheDir.mkdirs();
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
        try {
            new File(cacheDir, ".nomedia").createNewFile();
        } catch (Exception e2) {
            FileLog.e(e2);
        }
        sparseArray.put(4, cacheDir);
        if (BuildVars.LOGS_ENABLED) {
            FileLog.d("cache path = " + cacheDir);
        }
        try {
            if ("mounted".equals(Environment.getExternalStorageState())) {
                File file = new File(Environment.getExternalStorageDirectory(), "Zixc");
                this.appPath = file;
                file.mkdirs();
                if (this.appPath.isDirectory()) {
                    try {
                        File file2 = new File(this.appPath, "Zixc Images");
                        file2.mkdir();
                        if (file2.isDirectory() && canMoveFiles(cacheDir, file2, 0)) {
                            sparseArray.put(0, file2);
                            if (BuildVars.LOGS_ENABLED) {
                                FileLog.d("image path = " + file2);
                            }
                        }
                    } catch (Exception e3) {
                        FileLog.e(e3);
                    }
                    try {
                        File file3 = new File(this.appPath, "Zixc Video");
                        file3.mkdir();
                        if (file3.isDirectory() && canMoveFiles(cacheDir, file3, 2)) {
                            sparseArray.put(2, file3);
                            if (BuildVars.LOGS_ENABLED) {
                                FileLog.d("video path = " + file3);
                            }
                        }
                    } catch (Exception e4) {
                        FileLog.e(e4);
                    }
                    try {
                        File file4 = new File(this.appPath, "Zixc Audio");
                        file4.mkdir();
                        if (file4.isDirectory() && canMoveFiles(cacheDir, file4, 1)) {
                            new File(file4, ".nomedia").createNewFile();
                            sparseArray.put(1, file4);
                            if (BuildVars.LOGS_ENABLED) {
                                FileLog.d("audio path = " + file4);
                            }
                        }
                    } catch (Exception e5) {
                        FileLog.e(e5);
                    }
                    try {
                        File file5 = new File(this.appPath, "Zixc Documents");
                        file5.mkdir();
                        if (file5.isDirectory() && canMoveFiles(cacheDir, file5, 3)) {
                            new File(file5, ".nomedia").createNewFile();
                            sparseArray.put(3, file5);
                            if (BuildVars.LOGS_ENABLED) {
                                FileLog.d("documents path = " + file5);
                            }
                        }
                    } catch (Exception e6) {
                        FileLog.e(e6);
                    }
                }
            } else if (BuildVars.LOGS_ENABLED) {
                FileLog.d("this Android can't rename files");
            }
            SharedConfig.checkSaveToGalleryFiles();
        } catch (Exception e7) {
            FileLog.e(e7);
        }
        return sparseArray;
    }

    private boolean canMoveFiles(File file, File file2, int i) {
        File file3;
        File file4;
        byte[] bArr;
        RandomAccessFile randomAccessFile;
        RandomAccessFile randomAccessFile2 = null;
        try {
            try {
                if (i == 0) {
                    file4 = new File(file, "000000000_999999_temp.jpg");
                    file3 = new File(file2, "000000000_999999.jpg");
                } else if (i == 3) {
                    file4 = new File(file, "000000000_999999_temp.doc");
                    file3 = new File(file2, "000000000_999999.doc");
                } else if (i == 1) {
                    file4 = new File(file, "000000000_999999_temp.ogg");
                    file3 = new File(file2, "000000000_999999.ogg");
                } else if (i == 2) {
                    file4 = new File(file, "000000000_999999_temp.mp4");
                    file3 = new File(file2, "000000000_999999.mp4");
                } else {
                    file3 = null;
                    file4 = null;
                }
                bArr = new byte[1024];
                file4.createNewFile();
                randomAccessFile = new RandomAccessFile(file4, "rws");
            } catch (Throwable th) {
                th = th;
            }
        } catch (Exception e) {
            e = e;
        }
        try {
            randomAccessFile.write(bArr);
            randomAccessFile.close();
            boolean renameTo = file4.renameTo(file3);
            file4.delete();
            file3.delete();
            return renameTo;
        } catch (Exception e2) {
            e = e2;
            randomAccessFile2 = randomAccessFile;
            FileLog.e(e);
            if (randomAccessFile2 == null) {
                return false;
            }
            try {
                randomAccessFile2.close();
                return false;
            } catch (Exception e3) {
                FileLog.e(e3);
                return false;
            }
        } catch (Throwable th2) {
            th = th2;
            randomAccessFile2 = randomAccessFile;
            if (randomAccessFile2 != null) {
                try {
                    randomAccessFile2.close();
                } catch (Exception e4) {
                    FileLog.e(e4);
                }
            }
            throw th;
        }
    }

    public Float getFileProgress(String str) {
        if (str == null) {
            return null;
        }
        return this.fileProgresses.get(str);
    }

    public String getReplacedKey(String str) {
        if (str == null) {
            return null;
        }
        return this.replacedBitmaps.get(str);
    }

    private void performReplace(String str, String str2) {
        BitmapDrawable bitmapDrawable = this.memCache.get(str);
        this.replacedBitmaps.put(str, str2);
        if (bitmapDrawable != null) {
            BitmapDrawable bitmapDrawable2 = this.memCache.get(str2);
            boolean z = false;
            if (bitmapDrawable2 != null && bitmapDrawable2.getBitmap() != null && bitmapDrawable.getBitmap() != null) {
                Bitmap bitmap = bitmapDrawable2.getBitmap();
                Bitmap bitmap2 = bitmapDrawable.getBitmap();
                if (bitmap.getWidth() > bitmap2.getWidth() || bitmap.getHeight() > bitmap2.getHeight()) {
                    z = true;
                }
            }
            if (!z) {
                this.ignoreRemoval = str;
                this.memCache.remove(str);
                this.memCache.put(str2, bitmapDrawable);
                this.ignoreRemoval = null;
            } else {
                this.memCache.remove(str);
            }
        }
        Integer num = this.bitmapUseCounts.get(str);
        if (num != null) {
            this.bitmapUseCounts.put(str2, num);
            this.bitmapUseCounts.remove(str);
        }
    }

    public void incrementUseCount(String str) {
        Integer num = this.bitmapUseCounts.get(str);
        if (num == null) {
            this.bitmapUseCounts.put(str, 1);
        } else {
            this.bitmapUseCounts.put(str, Integer.valueOf(num.intValue() + 1));
        }
    }

    public boolean decrementUseCount(String str) {
        Integer num = this.bitmapUseCounts.get(str);
        if (num == null) {
            return true;
        }
        if (num.intValue() == 1) {
            this.bitmapUseCounts.remove(str);
            return true;
        }
        this.bitmapUseCounts.put(str, Integer.valueOf(num.intValue() - 1));
        return false;
    }

    public void removeImage(String str) {
        this.bitmapUseCounts.remove(str);
        this.memCache.remove(str);
    }

    public boolean isInMemCache(String str, boolean z) {
        return z ? this.lottieMemCache.get(str) != null : this.memCache.get(str) != null;
    }

    public void clearMemory() {
        this.memCache.evictAll();
        this.lottieMemCache.evictAll();
    }

    private void removeFromWaitingForThumb(int i, ImageReceiver imageReceiver) {
        String str = this.waitingForQualityThumbByTag.get(i);
        if (str != null) {
            ThumbGenerateInfo thumbGenerateInfo = this.waitingForQualityThumb.get(str);
            if (thumbGenerateInfo != null) {
                int indexOf = thumbGenerateInfo.imageReceiverArray.indexOf(imageReceiver);
                if (indexOf >= 0) {
                    thumbGenerateInfo.imageReceiverArray.remove(indexOf);
                    thumbGenerateInfo.imageReceiverGuidsArray.remove(indexOf);
                }
                if (thumbGenerateInfo.imageReceiverArray.isEmpty()) {
                    this.waitingForQualityThumb.remove(str);
                }
            }
            this.waitingForQualityThumbByTag.remove(i);
        }
    }

    public void cancelLoadingForImageReceiver(final ImageReceiver imageReceiver, final boolean z) {
        if (imageReceiver == null) {
            return;
        }
        this.imageLoadQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                ImageLoader.this.lambda$cancelLoadingForImageReceiver$2$ImageLoader(z, imageReceiver);
            }
        });
    }

    public void lambda$cancelLoadingForImageReceiver$2$ImageLoader(boolean z, ImageReceiver imageReceiver) {
        int i = 0;
        while (true) {
            int i2 = 3;
            if (i >= 3) {
                return;
            }
            if (i > 0 && !z) {
                return;
            }
            if (i == 0) {
                i2 = 1;
            } else if (i == 1) {
                i2 = 0;
            }
            int tag = imageReceiver.getTag(i2);
            if (tag != 0) {
                if (i == 0) {
                    removeFromWaitingForThumb(tag, imageReceiver);
                }
                CacheImage cacheImage = this.imageLoadingByTag.get(tag);
                if (cacheImage != null) {
                    cacheImage.removeImageReceiver(imageReceiver);
                }
            }
            i++;
        }
    }

    public BitmapDrawable getAnyImageFromMemory(String str) {
        ArrayList<String> filterKeys;
        BitmapDrawable bitmapDrawable = this.memCache.get(str);
        if (bitmapDrawable != null || (filterKeys = this.memCache.getFilterKeys(str)) == null || filterKeys.isEmpty()) {
            return bitmapDrawable;
        }
        return this.memCache.get(str + "@" + filterKeys.get(0));
    }

    public BitmapDrawable getImageFromMemory(TLObject tLObject, String str, String str2) {
        String str3 = null;
        if (tLObject == null && str == null) {
            return null;
        }
        if (str != null) {
            str3 = Utilities.MD5(str);
        } else if (tLObject instanceof TLRPC.FileLocation) {
            TLRPC.FileLocation fileLocation = (TLRPC.FileLocation) tLObject;
            str3 = fileLocation.volume_id + "_" + fileLocation.local_id;
        } else if (tLObject instanceof TLRPC.Document) {
            TLRPC.Document document = (TLRPC.Document) tLObject;
            str3 = document.dc_id + "_" + document.id;
        } else if (tLObject instanceof SecureDocument) {
            SecureDocument secureDocument = (SecureDocument) tLObject;
            str3 = secureDocument.secureFile.dc_id + "_" + secureDocument.secureFile.id;
        } else if (tLObject instanceof WebFile) {
            str3 = Utilities.MD5(((WebFile) tLObject).url);
        }
        if (str2 != null) {
            str3 = str3 + "@" + str2;
        }
        return this.memCache.get(str3);
    }

    public void lambda$replaceImageInCache$3$ImageLoader(String str, String str2, ImageLocation imageLocation) {
        ArrayList<String> filterKeys = this.memCache.getFilterKeys(str);
        if (filterKeys != null) {
            for (int i = 0; i < filterKeys.size(); i++) {
                String str3 = filterKeys.get(i);
                String str4 = str + "@" + str3;
                String str5 = str2 + "@" + str3;
                performReplace(str4, str5);
                NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.didReplacedPhotoInMemCache, str4, str5, imageLocation);
            }
            return;
        }
        performReplace(str, str2);
        NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.didReplacedPhotoInMemCache, str, str2, imageLocation);
    }

    public void replaceImageInCache(final String str, final String str2, final ImageLocation imageLocation, boolean z) {
        if (z) {
            AndroidUtilities.runOnUIThread(new Runnable() {
                @Override
                public final void run() {
                    ImageLoader.this.lambda$replaceImageInCache$3$ImageLoader(str, str2, imageLocation);
                }
            });
        } else {
            lambda$replaceImageInCache$3$ImageLoader(str, str2, imageLocation);
        }
    }

    public void putImageToCache(BitmapDrawable bitmapDrawable, String str) {
        this.memCache.put(str, bitmapDrawable);
    }

    private void generateThumb(int i, File file, ThumbGenerateInfo thumbGenerateInfo) {
        if ((i != 0 && i != 2 && i != 3) || file == null || thumbGenerateInfo == null) {
            return;
        }
        if (this.thumbGenerateTasks.get(FileLoader.getAttachFileName(thumbGenerateInfo.parentDocument)) == null) {
            this.thumbGeneratingQueue.postRunnable(new ThumbGenerateTask(i, file, thumbGenerateInfo));
        }
    }

    public void cancelForceLoadingForImageReceiver(ImageReceiver imageReceiver) {
        final String imageKey;
        if (imageReceiver == null || (imageKey = imageReceiver.getImageKey()) == null) {
            return;
        }
        this.imageLoadQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                ImageLoader.this.lambda$cancelForceLoadingForImageReceiver$4$ImageLoader(imageKey);
            }
        });
    }

    public void lambda$cancelForceLoadingForImageReceiver$4$ImageLoader(String str) {
        this.forceLoadingImages.remove(str);
    }

    private void createLoadOperationForImageReceiver(final ImageReceiver imageReceiver, final String str, final String str2, final String str3, final ImageLocation imageLocation, final String str4, final int i, final int i2, final int i3, final int i4, final int i5) {
        if (imageReceiver == null || str2 == null || str == null || imageLocation == null) {
            return;
        }
        int tag = imageReceiver.getTag(i3);
        if (tag == 0) {
            tag = this.lastImageNum;
            imageReceiver.setTag(tag, i3);
            int i6 = this.lastImageNum + 1;
            this.lastImageNum = i6;
            if (i6 == Integer.MAX_VALUE) {
                this.lastImageNum = 0;
            }
        }
        final int i7 = tag;
        final boolean isNeedsQualityThumb = imageReceiver.isNeedsQualityThumb();
        final Object parentObject = imageReceiver.getParentObject();
        final TLRPC.Document qulityThumbDocument = imageReceiver.getQulityThumbDocument();
        final boolean isShouldGenerateQualityThumb = imageReceiver.isShouldGenerateQualityThumb();
        final int currentAccount = imageReceiver.getCurrentAccount();
        final boolean z = i3 == 0 && imageReceiver.isCurrentKeyQuality();
        this.imageLoadQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                ImageLoader.this.lambda$createLoadOperationForImageReceiver$5$ImageLoader(i4, str2, str, i7, imageReceiver, str4, i3, i5, imageLocation, z, parentObject, qulityThumbDocument, isNeedsQualityThumb, isShouldGenerateQualityThumb, i2, i, str3, currentAccount);
            }
        });
    }

    public void lambda$createLoadOperationForImageReceiver$5$ImageLoader(int i, String str, String str2, int i2, ImageReceiver imageReceiver, String str3, int i3, int i4, ImageLocation imageLocation, boolean z, Object obj, TLRPC.Document document, boolean z2, boolean z3, int i5, int i6, String str4, int i7) {
        ?? r9;
        int i8;
        boolean z4;
        String str5;
        boolean z5;
        boolean z6;
        int i9;
        File file;
        String str6;
        boolean z7;
        int i10;
        File file2;
        File file3;
        boolean z8;
        File file4;
        File file5;
        int i11;
        boolean z9;
        int i12;
        boolean z10;
        File file6;
        boolean z11;
        boolean z12;
        File file7;
        int i13;
        File file8;
        boolean z13;
        CacheImage cacheImage;
        CacheImage cacheImage2;
        boolean z14;
        boolean z15;
        TLRPC.Document document2 = document;
        if (i != 2) {
            CacheImage cacheImage3 = this.imageLoadingByUrl.get(str);
            CacheImage cacheImage4 = this.imageLoadingByKeys.get(str2);
            CacheImage cacheImage5 = this.imageLoadingByTag.get(i2);
            if (cacheImage5 != null) {
                if (cacheImage5 == cacheImage4) {
                    cacheImage = cacheImage3;
                    cacheImage2 = cacheImage4;
                    z4 = true;
                    r9 = 1;
                    i8 = 0;
                } else if (cacheImage5 == cacheImage3) {
                    cacheImage = cacheImage3;
                    if (cacheImage4 == null) {
                        cacheImage2 = cacheImage4;
                        z15 = true;
                        i8 = 0;
                        cacheImage5.replaceImageReceiver(imageReceiver, str2, str3, i3, i4);
                    } else {
                        cacheImage2 = cacheImage4;
                        z15 = true;
                        i8 = 0;
                    }
                    z4 = true;
                    r9 = z15;
                } else {
                    cacheImage = cacheImage3;
                    cacheImage2 = cacheImage4;
                    z14 = true;
                    i8 = 0;
                    cacheImage5.removeImageReceiver(imageReceiver);
                }
                if (!z4 && cacheImage2 != null) {
                    cacheImage2.addImageReceiver(imageReceiver, str2, str3, i3, i4);
                    z4 = true;
                }
                if (!z4 && cacheImage != null) {
                    cacheImage.addImageReceiver(imageReceiver, str2, str3, i3, i4);
                    z4 = true;
                }
            } else {
                cacheImage = cacheImage3;
                cacheImage2 = cacheImage4;
                z14 = true;
                i8 = 0;
            }
            z4 = false;
            r9 = z14;
            if (!z4) {
                cacheImage2.addImageReceiver(imageReceiver, str2, str3, i3, i4);
                z4 = true;
            }
            if (!z4) {
                cacheImage.addImageReceiver(imageReceiver, str2, str3, i3, i4);
                z4 = true;
            }
        } else {
            r9 = 1;
            i8 = 0;
            z4 = false;
        }
        if (!z4) {
            if (imageLocation.path != null) {
                String str7 = imageLocation.path;
                if (str7.startsWith("http") || str7.startsWith("athumb")) {
                    z13 = false;
                    file = null;
                } else if (str7.startsWith("thumb://")) {
                    int indexOf = str7.indexOf(LogUtils.COLON, 8);
                    if (indexOf >= 0) {
                        file = new File(str7.substring(indexOf + r9));
                        z13 = true;
                    }
                    file = null;
                    z13 = true;
                } else {
                    if (str7.startsWith("vthumb://")) {
                        int indexOf2 = str7.indexOf(LogUtils.COLON, 9);
                        if (indexOf2 >= 0) {
                            file = new File(str7.substring(indexOf2 + r9));
                        }
                        file = null;
                    } else {
                        file = new File(str7);
                    }
                    z13 = true;
                }
                z6 = z13;
                str5 = "athumb";
                z5 = false;
            } else {
                if (i == 0 && z) {
                    if (obj instanceof MessageObject) {
                        MessageObject messageObject = (MessageObject) obj;
                        TLRPC.Document document3 = messageObject.getDocument();
                        str6 = messageObject.messageOwner.attachPath;
                        document2 = document3;
                        file2 = FileLoader.getPathToMessage(messageObject.messageOwner);
                        i10 = messageObject.getFileType();
                        z7 = false;
                    } else if (document2 != null) {
                        File pathToAttach = FileLoader.getPathToAttach(document2, r9);
                        int i14 = MessageObject.isVideoDocument(document) ? 2 : 3;
                        file2 = pathToAttach;
                        str6 = null;
                        i10 = i14;
                        z7 = true;
                    } else {
                        str6 = null;
                        z7 = false;
                        i10 = 0;
                        file2 = null;
                        document2 = null;
                    }
                    if (document2 != null) {
                        if (z2) {
                            File directory = FileLoader.getDirectory(4);
                            file3 = file2;
                            StringBuilder sb = new StringBuilder();
                            str5 = "athumb";
                            sb.append("q_");
                            sb.append(document2.dc_id);
                            sb.append("_");
                            sb.append(document2.id);
                            sb.append(".jpg");
                            file4 = new File(directory, sb.toString());
                            if (file4.exists()) {
                                z8 = true;
                                if (!TextUtils.isEmpty(str6)) {
                                    file5 = new File(str6);
                                }
                                file5 = null;
                                if (file5 == null) {
                                    file5 = file3;
                                }
                                if (file4 != null) {
                                    String attachFileName = FileLoader.getAttachFileName(document2);
                                    ThumbGenerateInfo thumbGenerateInfo = this.waitingForQualityThumb.get(attachFileName);
                                    if (thumbGenerateInfo == null) {
                                        thumbGenerateInfo = new ThumbGenerateInfo();
                                        thumbGenerateInfo.parentDocument = document2;
                                        thumbGenerateInfo.filter = str3;
                                        thumbGenerateInfo.big = z7;
                                        this.waitingForQualityThumb.put(attachFileName, thumbGenerateInfo);
                                    }
                                    if (!thumbGenerateInfo.imageReceiverArray.contains(imageReceiver)) {
                                        thumbGenerateInfo.imageReceiverArray.add(imageReceiver);
                                        thumbGenerateInfo.imageReceiverGuidsArray.add(Integer.valueOf(i4));
                                    }
                                    this.waitingForQualityThumbByTag.put(i2, attachFileName);
                                    if (file5.exists() && z3) {
                                        generateThumb(i10, file5, thumbGenerateInfo);
                                        return;
                                    }
                                    return;
                                }
                                z5 = z8;
                                file = file4;
                                z6 = true;
                            }
                        } else {
                            file3 = file2;
                            str5 = "athumb";
                        }
                        z8 = false;
                        file4 = null;
                        if (!TextUtils.isEmpty(str6)) {
                        }
                        file5 = null;
                        if (file5 == null) {
                        }
                        if (file4 != null) {
                        }
                    } else {
                        str5 = "athumb";
                        z5 = false;
                        z6 = true;
                    }
                } else {
                    str5 = "athumb";
                    z5 = false;
                    z6 = false;
                }
                i9 = 2;
                file = null;
                if (i != i9) {
                    boolean isEncrypted = imageLocation.isEncrypted();
                    CacheImage cacheImage6 = new CacheImage();
                    if (!z) {
                        if (MessageObject.isGifDocument(imageLocation.webFile) || MessageObject.isGifDocument(imageLocation.document) || MessageObject.isRoundVideoDocument(imageLocation.document)) {
                            cacheImage6.animatedFile = true;
                        } else if (imageLocation.path != null) {
                            String str8 = imageLocation.path;
                            if (!str8.startsWith("vthumb") && !str8.startsWith("thumb")) {
                                String httpUrlExtension = getHttpUrlExtension(str8, "jpg");
                                if (httpUrlExtension.equals("mp4") || httpUrlExtension.equals("gif")) {
                                    cacheImage6.animatedFile = true;
                                }
                            }
                        }
                    }
                    if (file == null) {
                        if (imageLocation.photoSize instanceof TLRPC.TL_photoStrippedSize) {
                            i11 = i5;
                            z11 = true;
                        } else {
                            if (imageLocation.secureDocument != null) {
                                cacheImage6.secureDocument = imageLocation.secureDocument;
                                z6 = cacheImage6.secureDocument.secureFile.dc_id == Integer.MIN_VALUE;
                                file8 = new File(FileLoader.getDirectory(4), str);
                                i11 = i5;
                            } else {
                                i11 = i5;
                                if (!AUTOPLAY_FILTER.equals(str3) && (i11 != 0 || i6 <= 0 || imageLocation.path != null || isEncrypted)) {
                                    file8 = new File(FileLoader.getDirectory(4), str);
                                    if (file8.exists()) {
                                        z5 = true;
                                    } else if (i11 == 2) {
                                        file8 = new File(FileLoader.getDirectory(4), str + ".enc");
                                        z5 = z5;
                                    }
                                    if (imageLocation.document != null) {
                                        cacheImage6.lottieFile = "application/x-tgsticker".equals(imageLocation.document.mime_type);
                                    }
                                }
                                boolean z16 = z5;
                                if (imageLocation.document != null) {
                                    TLRPC.Document document4 = imageLocation.document;
                                    if (document4 instanceof TLRPC.TL_documentEncrypted) {
                                        file7 = new File(FileLoader.getDirectory(4), str);
                                        z12 = z6;
                                    } else if (MessageObject.isVideoDocument(document4)) {
                                        z12 = z6;
                                        file7 = new File(FileLoader.getDirectory(2), str);
                                    } else {
                                        z12 = z6;
                                        file7 = new File(FileLoader.getDirectory(3), str);
                                    }
                                    if (AUTOPLAY_FILTER.equals(str3) && !file7.exists()) {
                                        file7 = new File(FileLoader.getDirectory(4), document4.dc_id + "_" + document4.id + ".temp");
                                    }
                                    cacheImage6.lottieFile = "application/x-tgsticker".equals(document4.mime_type);
                                    i13 = document4.size;
                                    z5 = z16;
                                    z11 = z12;
                                    file = file7;
                                    if (AUTOPLAY_FILTER.equals(str3)) {
                                        i12 = i3;
                                        z9 = z5;
                                        z10 = z11;
                                    } else {
                                        cacheImage6.animatedFile = true;
                                        cacheImage6.size = i13;
                                        i12 = i3;
                                        z9 = z5;
                                        z10 = true;
                                    }
                                } else {
                                    boolean z17 = z6;
                                    if (imageLocation.webFile != null) {
                                        file6 = new File(FileLoader.getDirectory(3), str);
                                    } else {
                                        file6 = new File(FileLoader.getDirectory(i8), str);
                                    }
                                    z5 = z16;
                                    z11 = z17;
                                    file = file6;
                                }
                            }
                            file = file8;
                            z11 = z6;
                        }
                        i13 = 0;
                        if (AUTOPLAY_FILTER.equals(str3)) {
                        }
                    } else {
                        i11 = i5;
                        z9 = z5;
                        i12 = i3;
                        z10 = z6;
                    }
                    cacheImage6.imageType = i12;
                    cacheImage6.key = str2;
                    cacheImage6.filter = str3;
                    cacheImage6.imageLocation = imageLocation;
                    cacheImage6.ext = str4;
                    cacheImage6.currentAccount = i7;
                    cacheImage6.parentObject = obj;
                    if (imageLocation.lottieAnimation) {
                        cacheImage6.lottieFile = true;
                    }
                    if (i11 == 2) {
                        cacheImage6.encryptionKeyPath = new File(FileLoader.getInternalCacheDir(), str + ".enc.key");
                    }
                    String str9 = str5;
                    cacheImage6.addImageReceiver(imageReceiver, str2, str3, i3, i4);
                    if (z10 || z9 || file.exists()) {
                        cacheImage6.finalFilePath = file;
                        cacheImage6.imageLocation = imageLocation;
                        cacheImage6.cacheTask = new CacheOutTask(cacheImage6);
                        this.imageLoadingByKeys.put(str2, cacheImage6);
                        if (i != 0) {
                            this.cacheThumbOutQueue.postRunnable(cacheImage6.cacheTask);
                            return;
                        } else {
                            this.cacheOutQueue.postRunnable(cacheImage6.cacheTask);
                            return;
                        }
                    }
                    cacheImage6.url = str;
                    this.imageLoadingByUrl.put(str, cacheImage6);
                    if (imageLocation.path != null) {
                        String MD5 = Utilities.MD5(imageLocation.path);
                        cacheImage6.tempFilePath = new File(FileLoader.getDirectory(4), MD5 + "_temp.jpg");
                        cacheImage6.finalFilePath = file;
                        if (imageLocation.path.startsWith(str9)) {
                            cacheImage6.artworkTask = new ArtworkLoadTask(cacheImage6);
                            this.artworkTasks.add(cacheImage6.artworkTask);
                            runArtworkTasks(false);
                            return;
                        } else {
                            cacheImage6.httpTask = new HttpImageTask(cacheImage6, i6);
                            this.httpTasks.add(cacheImage6.httpTask);
                            runHttpTasks(false);
                            return;
                        }
                    }
                    if (imageLocation.location != null) {
                        FileLoader.getInstance(i7).loadFile(imageLocation, obj, str4, i != 0 ? 2 : 1, (i11 != 0 || (i6 > 0 && imageLocation.key == null)) ? i11 : 1);
                    } else if (imageLocation.document != null) {
                        FileLoader.getInstance(i7).loadFile(imageLocation.document, obj, i != 0 ? 2 : 1, i11);
                    } else if (imageLocation.secureDocument != null) {
                        FileLoader.getInstance(i7).loadFile(imageLocation.secureDocument, i != 0 ? 2 : 1);
                    } else if (imageLocation.webFile != null) {
                        FileLoader.getInstance(i7).loadFile(imageLocation.webFile, i != 0 ? 2 : 1, i11);
                    }
                    if (imageReceiver.isForceLoding()) {
                        this.forceLoadingImages.put(cacheImage6.key, 0);
                        return;
                    }
                    return;
                }
            }
            i9 = 2;
            if (i != i9) {
            }
        }
    }

    public void loadImageForImageReceiver(ImageReceiver imageReceiver) {
        boolean z;
        String imageKey;
        boolean z2;
        String thumbKey;
        boolean z3;
        Object parentObject;
        ImageLocation thumbLocation;
        ImageLocation imageLocation;
        boolean z4;
        String ext;
        ImageLocation imageLocation2;
        int i;
        String str;
        String str2;
        String str3;
        String str4;
        String str5;
        String str6;
        boolean z5;
        int i2;
        boolean z6;
        String str7;
        String str8;
        BitmapDrawable bitmapDrawable;
        ImageLocation imageLocation3;
        BitmapDrawable bitmapDrawable2;
        BitmapDrawable bitmapDrawable3;
        if (imageReceiver == null) {
            return;
        }
        String mediaKey = imageReceiver.getMediaKey();
        int newGuid = imageReceiver.getNewGuid();
        if (mediaKey != null) {
            ImageLocation mediaLocation = imageReceiver.getMediaLocation();
            if (mediaLocation != null && (MessageObject.isAnimatedStickerDocument(mediaLocation.document) || mediaLocation.lottieAnimation)) {
                bitmapDrawable3 = this.lottieMemCache.get(mediaKey);
            } else {
                bitmapDrawable3 = this.memCache.get(mediaKey);
                if (bitmapDrawable3 != null) {
                    this.memCache.moveToFront(mediaKey);
                }
            }
            Drawable drawable = bitmapDrawable3;
            if (drawable != null) {
                cancelLoadingForImageReceiver(imageReceiver, true);
                imageReceiver.setImageBitmapByKey(drawable, mediaKey, 3, true, newGuid);
                if (!imageReceiver.isForcePreview()) {
                    return;
                }
                z = true;
                imageKey = imageReceiver.getImageKey();
                if (!z && imageKey != null) {
                    imageLocation3 = imageReceiver.getImageLocation();
                    if (imageLocation3 == null && (MessageObject.isAnimatedStickerDocument(imageLocation3.document) || imageLocation3.lottieAnimation)) {
                        bitmapDrawable2 = this.lottieMemCache.get(imageKey);
                    } else {
                        bitmapDrawable2 = this.memCache.get(imageKey);
                        if (bitmapDrawable2 != null) {
                            this.memCache.moveToFront(imageKey);
                        }
                    }
                    if (bitmapDrawable2 != null) {
                        cancelLoadingForImageReceiver(imageReceiver, true);
                        imageReceiver.setImageBitmapByKey(bitmapDrawable2, imageKey, 0, true, newGuid);
                        if (!imageReceiver.isForcePreview() && mediaKey == null) {
                            return;
                        }
                        z2 = true;
                        thumbKey = imageReceiver.getThumbKey();
                        if (thumbKey != null) {
                            ImageLocation thumbLocation2 = imageReceiver.getThumbLocation();
                            if (thumbLocation2 != null && (MessageObject.isAnimatedStickerDocument(thumbLocation2.document) || thumbLocation2.lottieAnimation)) {
                                bitmapDrawable = this.lottieMemCache.get(imageKey);
                            } else {
                                bitmapDrawable = this.memCache.get(thumbKey);
                                if (bitmapDrawable != null) {
                                    this.memCache.moveToFront(thumbKey);
                                }
                            }
                            Drawable drawable2 = bitmapDrawable;
                            if (drawable2 != null) {
                                imageReceiver.setImageBitmapByKey(drawable2, thumbKey, 1, true, newGuid);
                                cancelLoadingForImageReceiver(imageReceiver, false);
                                if (z2 && imageReceiver.isForcePreview()) {
                                    return;
                                }
                                z3 = true;
                                parentObject = imageReceiver.getParentObject();
                                TLRPC.Document qulityThumbDocument = imageReceiver.getQulityThumbDocument();
                                thumbLocation = imageReceiver.getThumbLocation();
                                String thumbFilter = imageReceiver.getThumbFilter();
                                ImageLocation mediaLocation2 = imageReceiver.getMediaLocation();
                                String mediaFilter = imageReceiver.getMediaFilter();
                                imageLocation = imageReceiver.getImageLocation();
                                String imageFilter = imageReceiver.getImageFilter();
                                if (imageLocation == null && imageReceiver.isNeedsQualityThumb() && imageReceiver.isCurrentKeyQuality()) {
                                    if (!(parentObject instanceof MessageObject)) {
                                        imageLocation = ImageLocation.getForDocument(((MessageObject) parentObject).getDocument());
                                    } else if (qulityThumbDocument != null) {
                                        imageLocation = ImageLocation.getForDocument(qulityThumbDocument);
                                    }
                                    z4 = true;
                                    ext = imageReceiver.getExt();
                                    if (ext == null) {
                                        ext = "jpg";
                                    }
                                    imageLocation2 = mediaLocation2;
                                    String str9 = null;
                                    String str10 = null;
                                    String str11 = null;
                                    String str12 = null;
                                    i = 0;
                                    boolean z7 = false;
                                    while (i < 2) {
                                        ImageLocation imageLocation4 = i == 0 ? imageLocation : imageLocation2;
                                        if (imageLocation4 != null) {
                                            String key = imageLocation4.getKey(parentObject, imageLocation2 != null ? imageLocation2 : imageLocation);
                                            if (key != null) {
                                                z6 = z2;
                                                if (imageLocation4.path != null) {
                                                    str7 = key + "." + getHttpUrlExtension(imageLocation4.path, "jpg");
                                                } else if (imageLocation4.photoSize instanceof TLRPC.TL_photoStrippedSize) {
                                                    str7 = key + "." + ext;
                                                } else {
                                                    if (imageLocation4.location != null) {
                                                        str7 = key + "." + ext;
                                                        if (imageReceiver.getExt() == null && imageLocation4.location.key == null) {
                                                            i2 = newGuid;
                                                            if (imageLocation4.location.volume_id == -2147483648L) {
                                                            }
                                                            z5 = z4;
                                                        } else {
                                                            i2 = newGuid;
                                                        }
                                                        z5 = z4;
                                                        z7 = true;
                                                    } else {
                                                        i2 = newGuid;
                                                        if (imageLocation4.webFile != null) {
                                                            str7 = key + "." + getHttpUrlExtension(imageLocation4.webFile.url, FileLoader.getMimeTypePart(imageLocation4.webFile.mime_type));
                                                        } else if (imageLocation4.secureDocument != null) {
                                                            str7 = key + "." + ext;
                                                        } else if (imageLocation4.document != null) {
                                                            if (i == 0 && z4) {
                                                                key = "q_" + key;
                                                            }
                                                            String documentFileName = FileLoader.getDocumentFileName(imageLocation4.document);
                                                            String str13 = "";
                                                            if (documentFileName != null) {
                                                                int lastIndexOf = documentFileName.lastIndexOf(46);
                                                                z5 = z4;
                                                                if (lastIndexOf != -1) {
                                                                    str8 = documentFileName.substring(lastIndexOf);
                                                                    if (str8.length() <= 1) {
                                                                        str13 = str8;
                                                                    } else if (MimeTypes.VIDEO_MP4.equals(imageLocation4.document.mime_type)) {
                                                                        str13 = ".mp4";
                                                                    } else if ("video/x-matroska".equals(imageLocation4.document.mime_type)) {
                                                                        str13 = ".mkv";
                                                                    }
                                                                    str7 = key + str13;
                                                                    z7 = (!MessageObject.isVideoDocument(imageLocation4.document) || MessageObject.isGifDocument(imageLocation4.document) || MessageObject.isRoundVideoDocument(imageLocation4.document) || MessageObject.canPreviewDocument(imageLocation4.document)) ? false : true;
                                                                }
                                                            } else {
                                                                z5 = z4;
                                                            }
                                                            str8 = "";
                                                            if (str8.length() <= 1) {
                                                            }
                                                            str7 = key + str13;
                                                            z7 = (!MessageObject.isVideoDocument(imageLocation4.document) || MessageObject.isGifDocument(imageLocation4.document) || MessageObject.isRoundVideoDocument(imageLocation4.document) || MessageObject.canPreviewDocument(imageLocation4.document)) ? false : true;
                                                        } else {
                                                            z5 = z4;
                                                            str7 = null;
                                                        }
                                                        z5 = z4;
                                                    }
                                                    if (i != 0) {
                                                        str9 = str7;
                                                        str12 = key;
                                                    } else {
                                                        str10 = str7;
                                                        str11 = key;
                                                    }
                                                    if (imageLocation4 == thumbLocation) {
                                                        if (i == 0) {
                                                            imageLocation = null;
                                                            str9 = null;
                                                            str12 = null;
                                                        } else {
                                                            str10 = null;
                                                            imageLocation2 = null;
                                                            str11 = null;
                                                        }
                                                    }
                                                    i++;
                                                    z2 = z6;
                                                    newGuid = i2;
                                                    z4 = z5;
                                                }
                                                z5 = z4;
                                                i2 = newGuid;
                                                if (i != 0) {
                                                }
                                                if (imageLocation4 == thumbLocation) {
                                                }
                                                i++;
                                                z2 = z6;
                                                newGuid = i2;
                                                z4 = z5;
                                            }
                                        }
                                        z5 = z4;
                                        i2 = newGuid;
                                        z6 = z2;
                                        i++;
                                        z2 = z6;
                                        newGuid = i2;
                                        z4 = z5;
                                    }
                                    int i3 = newGuid;
                                    boolean z8 = z2;
                                    if (thumbLocation == null) {
                                        ImageLocation strippedLocation = imageReceiver.getStrippedLocation();
                                        if (strippedLocation == null) {
                                            strippedLocation = imageLocation2 != null ? imageLocation2 : imageLocation;
                                        }
                                        str = thumbLocation.getKey(parentObject, strippedLocation);
                                        if (thumbLocation.path != null) {
                                            str6 = str + "." + getHttpUrlExtension(thumbLocation.path, "jpg");
                                        } else if (thumbLocation.photoSize instanceof TLRPC.TL_photoStrippedSize) {
                                            str6 = str + "." + ext;
                                        } else if (thumbLocation.location != null) {
                                            str6 = str + "." + ext;
                                        } else {
                                            str2 = null;
                                        }
                                        str2 = str6;
                                    } else {
                                        str = null;
                                        str2 = null;
                                    }
                                    str3 = str11;
                                    if (str3 != null && mediaFilter != null) {
                                        str3 = str3 + "@" + mediaFilter;
                                    }
                                    String str14 = str3;
                                    str4 = str12;
                                    if (str4 != null && imageFilter != null) {
                                        str4 = str4 + "@" + imageFilter;
                                    }
                                    String str15 = str4;
                                    if (str != null && thumbFilter != null) {
                                        str = str + "@" + thumbFilter;
                                    }
                                    String str16 = str;
                                    if (imageLocation == null && imageLocation.path != null) {
                                        createLoadOperationForImageReceiver(imageReceiver, str16, str2, ext, thumbLocation, thumbFilter, 0, 1, 1, z3 ? 2 : 1, i3);
                                        createLoadOperationForImageReceiver(imageReceiver, str15, str9, ext, imageLocation, imageFilter, imageReceiver.getSize(), 1, 0, 0, i3);
                                        return;
                                    }
                                    ImageLocation imageLocation5 = imageLocation;
                                    String str17 = ext;
                                    if (imageLocation2 == null) {
                                        int cacheType = imageReceiver.getCacheType();
                                        int i4 = (cacheType == 0 && z7) ? 1 : cacheType;
                                        int i5 = i4 == 0 ? 1 : i4;
                                        if (z3) {
                                            str5 = mediaFilter;
                                        } else {
                                            str5 = mediaFilter;
                                            createLoadOperationForImageReceiver(imageReceiver, str16, str2, str17, thumbLocation, thumbFilter, 0, i5, 1, z3 ? 2 : 1, i3);
                                        }
                                        if (!z8) {
                                            createLoadOperationForImageReceiver(imageReceiver, str15, str9, str17, imageLocation5, imageFilter, 0, 1, 0, 0, i3);
                                        }
                                        createLoadOperationForImageReceiver(imageReceiver, str14, str10, str17, imageLocation2, str5, imageReceiver.getSize(), i4, 3, 0, i3);
                                        return;
                                    }
                                    int cacheType2 = imageReceiver.getCacheType();
                                    int i6 = (cacheType2 == 0 && z7) ? 1 : cacheType2;
                                    createLoadOperationForImageReceiver(imageReceiver, str16, str2, str17, thumbLocation, thumbFilter, 0, i6 == 0 ? 1 : i6, 1, z3 ? 2 : 1, i3);
                                    createLoadOperationForImageReceiver(imageReceiver, str15, str9, str17, imageLocation5, imageFilter, imageReceiver.getSize(), i6, 0, 0, i3);
                                    return;
                                }
                                z4 = false;
                                ext = imageReceiver.getExt();
                                if (ext == null) {
                                }
                                imageLocation2 = mediaLocation2;
                                String str92 = null;
                                String str102 = null;
                                String str112 = null;
                                String str122 = null;
                                i = 0;
                                boolean z72 = false;
                                while (i < 2) {
                                }
                                int i32 = newGuid;
                                boolean z82 = z2;
                                if (thumbLocation == null) {
                                }
                                str3 = str112;
                                if (str3 != null) {
                                    str3 = str3 + "@" + mediaFilter;
                                }
                                String str142 = str3;
                                str4 = str122;
                                if (str4 != null) {
                                    str4 = str4 + "@" + imageFilter;
                                }
                                String str152 = str4;
                                if (str != null) {
                                    str = str + "@" + thumbFilter;
                                }
                                String str162 = str;
                                if (imageLocation == null) {
                                }
                                ImageLocation imageLocation52 = imageLocation;
                                String str172 = ext;
                                if (imageLocation2 == null) {
                                }
                            }
                        }
                        z3 = false;
                        parentObject = imageReceiver.getParentObject();
                        TLRPC.Document qulityThumbDocument2 = imageReceiver.getQulityThumbDocument();
                        thumbLocation = imageReceiver.getThumbLocation();
                        String thumbFilter2 = imageReceiver.getThumbFilter();
                        ImageLocation mediaLocation22 = imageReceiver.getMediaLocation();
                        String mediaFilter2 = imageReceiver.getMediaFilter();
                        imageLocation = imageReceiver.getImageLocation();
                        String imageFilter2 = imageReceiver.getImageFilter();
                        if (imageLocation == null) {
                            if (!(parentObject instanceof MessageObject)) {
                            }
                            z4 = true;
                            ext = imageReceiver.getExt();
                            if (ext == null) {
                            }
                            imageLocation2 = mediaLocation22;
                            String str922 = null;
                            String str1022 = null;
                            String str1122 = null;
                            String str1222 = null;
                            i = 0;
                            boolean z722 = false;
                            while (i < 2) {
                            }
                            int i322 = newGuid;
                            boolean z822 = z2;
                            if (thumbLocation == null) {
                            }
                            str3 = str1122;
                            if (str3 != null) {
                            }
                            String str1422 = str3;
                            str4 = str1222;
                            if (str4 != null) {
                            }
                            String str1522 = str4;
                            if (str != null) {
                            }
                            String str1622 = str;
                            if (imageLocation == null) {
                            }
                            ImageLocation imageLocation522 = imageLocation;
                            String str1722 = ext;
                            if (imageLocation2 == null) {
                            }
                        }
                        z4 = false;
                        ext = imageReceiver.getExt();
                        if (ext == null) {
                        }
                        imageLocation2 = mediaLocation22;
                        String str9222 = null;
                        String str10222 = null;
                        String str11222 = null;
                        String str12222 = null;
                        i = 0;
                        boolean z7222 = false;
                        while (i < 2) {
                        }
                        int i3222 = newGuid;
                        boolean z8222 = z2;
                        if (thumbLocation == null) {
                        }
                        str3 = str11222;
                        if (str3 != null) {
                        }
                        String str14222 = str3;
                        str4 = str12222;
                        if (str4 != null) {
                        }
                        String str15222 = str4;
                        if (str != null) {
                        }
                        String str16222 = str;
                        if (imageLocation == null) {
                        }
                        ImageLocation imageLocation5222 = imageLocation;
                        String str17222 = ext;
                        if (imageLocation2 == null) {
                        }
                    }
                }
                z2 = z;
                thumbKey = imageReceiver.getThumbKey();
                if (thumbKey != null) {
                }
                z3 = false;
                parentObject = imageReceiver.getParentObject();
                TLRPC.Document qulityThumbDocument22 = imageReceiver.getQulityThumbDocument();
                thumbLocation = imageReceiver.getThumbLocation();
                String thumbFilter22 = imageReceiver.getThumbFilter();
                ImageLocation mediaLocation222 = imageReceiver.getMediaLocation();
                String mediaFilter22 = imageReceiver.getMediaFilter();
                imageLocation = imageReceiver.getImageLocation();
                String imageFilter22 = imageReceiver.getImageFilter();
                if (imageLocation == null) {
                }
                z4 = false;
                ext = imageReceiver.getExt();
                if (ext == null) {
                }
                imageLocation2 = mediaLocation222;
                String str92222 = null;
                String str102222 = null;
                String str112222 = null;
                String str122222 = null;
                i = 0;
                boolean z72222 = false;
                while (i < 2) {
                }
                int i32222 = newGuid;
                boolean z82222 = z2;
                if (thumbLocation == null) {
                }
                str3 = str112222;
                if (str3 != null) {
                }
                String str142222 = str3;
                str4 = str122222;
                if (str4 != null) {
                }
                String str152222 = str4;
                if (str != null) {
                }
                String str162222 = str;
                if (imageLocation == null) {
                }
                ImageLocation imageLocation52222 = imageLocation;
                String str172222 = ext;
                if (imageLocation2 == null) {
                }
            }
        }
        z = false;
        imageKey = imageReceiver.getImageKey();
        if (!z) {
            imageLocation3 = imageReceiver.getImageLocation();
            if (imageLocation3 == null) {
            }
            bitmapDrawable2 = this.memCache.get(imageKey);
            if (bitmapDrawable2 != null) {
            }
            if (bitmapDrawable2 != null) {
            }
        }
        z2 = z;
        thumbKey = imageReceiver.getThumbKey();
        if (thumbKey != null) {
        }
        z3 = false;
        parentObject = imageReceiver.getParentObject();
        TLRPC.Document qulityThumbDocument222 = imageReceiver.getQulityThumbDocument();
        thumbLocation = imageReceiver.getThumbLocation();
        String thumbFilter222 = imageReceiver.getThumbFilter();
        ImageLocation mediaLocation2222 = imageReceiver.getMediaLocation();
        String mediaFilter222 = imageReceiver.getMediaFilter();
        imageLocation = imageReceiver.getImageLocation();
        String imageFilter222 = imageReceiver.getImageFilter();
        if (imageLocation == null) {
        }
        z4 = false;
        ext = imageReceiver.getExt();
        if (ext == null) {
        }
        imageLocation2 = mediaLocation2222;
        String str922222 = null;
        String str1022222 = null;
        String str1122222 = null;
        String str1222222 = null;
        i = 0;
        boolean z722222 = false;
        while (i < 2) {
        }
        int i322222 = newGuid;
        boolean z822222 = z2;
        if (thumbLocation == null) {
        }
        str3 = str1122222;
        if (str3 != null) {
        }
        String str1422222 = str3;
        str4 = str1222222;
        if (str4 != null) {
        }
        String str1522222 = str4;
        if (str != null) {
        }
        String str1622222 = str;
        if (imageLocation == null) {
        }
        ImageLocation imageLocation522222 = imageLocation;
        String str1722222 = ext;
        if (imageLocation2 == null) {
        }
    }

    public void httpFileLoadError(final String str) {
        this.imageLoadQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                ImageLoader.this.lambda$httpFileLoadError$6$ImageLoader(str);
            }
        });
    }

    public void lambda$httpFileLoadError$6$ImageLoader(String str) {
        CacheImage cacheImage = this.imageLoadingByUrl.get(str);
        if (cacheImage == null) {
            return;
        }
        HttpImageTask httpImageTask = cacheImage.httpTask;
        cacheImage.httpTask = new HttpImageTask(httpImageTask.cacheImage, httpImageTask.imageSize);
        this.httpTasks.add(cacheImage.httpTask);
        runHttpTasks(false);
    }

    public void artworkLoadError(final String str) {
        this.imageLoadQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                ImageLoader.this.lambda$artworkLoadError$7$ImageLoader(str);
            }
        });
    }

    public void lambda$artworkLoadError$7$ImageLoader(String str) {
        CacheImage cacheImage = this.imageLoadingByUrl.get(str);
        if (cacheImage == null) {
            return;
        }
        cacheImage.artworkTask = new ArtworkLoadTask(cacheImage.artworkTask.cacheImage);
        this.artworkTasks.add(cacheImage.artworkTask);
        runArtworkTasks(false);
    }

    public void fileDidLoaded(final String str, final File file, final int i) {
        this.imageLoadQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                ImageLoader.this.lambda$fileDidLoaded$8$ImageLoader(str, i, file);
            }
        });
    }

    public void lambda$fileDidLoaded$8$ImageLoader(String str, int i, File file) {
        ThumbGenerateInfo thumbGenerateInfo = this.waitingForQualityThumb.get(str);
        if (thumbGenerateInfo != null && thumbGenerateInfo.parentDocument != null) {
            generateThumb(i, file, thumbGenerateInfo);
            this.waitingForQualityThumb.remove(str);
        }
        CacheImage cacheImage = this.imageLoadingByUrl.get(str);
        if (cacheImage == null) {
            return;
        }
        this.imageLoadingByUrl.remove(str);
        ArrayList arrayList = new ArrayList();
        for (int i2 = 0; i2 < cacheImage.imageReceiverArray.size(); i2++) {
            String str2 = cacheImage.keys.get(i2);
            String str3 = cacheImage.filters.get(i2);
            int intValue = cacheImage.imageTypes.get(i2).intValue();
            ImageReceiver imageReceiver = cacheImage.imageReceiverArray.get(i2);
            int intValue2 = cacheImage.imageReceiverGuidsArray.get(i2).intValue();
            CacheImage cacheImage2 = this.imageLoadingByKeys.get(str2);
            if (cacheImage2 == null) {
                cacheImage2 = new CacheImage();
                cacheImage2.secureDocument = cacheImage.secureDocument;
                cacheImage2.currentAccount = cacheImage.currentAccount;
                cacheImage2.finalFilePath = file;
                cacheImage2.key = str2;
                cacheImage2.imageLocation = cacheImage.imageLocation;
                cacheImage2.imageType = intValue;
                cacheImage2.ext = cacheImage.ext;
                cacheImage2.encryptionKeyPath = cacheImage.encryptionKeyPath;
                cacheImage2.cacheTask = new CacheOutTask(cacheImage2);
                cacheImage2.filter = str3;
                cacheImage2.animatedFile = cacheImage.animatedFile;
                cacheImage2.lottieFile = cacheImage.lottieFile;
                this.imageLoadingByKeys.put(str2, cacheImage2);
                arrayList.add(cacheImage2.cacheTask);
            }
            cacheImage2.addImageReceiver(imageReceiver, str2, str3, intValue, intValue2);
        }
        for (int i3 = 0; i3 < arrayList.size(); i3++) {
            CacheOutTask cacheOutTask = (CacheOutTask) arrayList.get(i3);
            if (cacheOutTask.cacheImage.imageType == 1) {
                this.cacheThumbOutQueue.postRunnable(cacheOutTask);
            } else {
                this.cacheOutQueue.postRunnable(cacheOutTask);
            }
        }
    }

    public void fileDidFailedLoad(final String str, int i) {
        if (i == 1) {
            return;
        }
        this.imageLoadQueue.postRunnable(new Runnable() {
            @Override
            public final void run() {
                ImageLoader.this.lambda$fileDidFailedLoad$9$ImageLoader(str);
            }
        });
    }

    public void lambda$fileDidFailedLoad$9$ImageLoader(String str) {
        CacheImage cacheImage = this.imageLoadingByUrl.get(str);
        if (cacheImage != null) {
            cacheImage.setImageAndClear(null, null);
        }
    }

    public void runHttpTasks(boolean z) {
        if (z) {
            this.currentHttpTasksCount--;
        }
        while (this.currentHttpTasksCount < 4 && !this.httpTasks.isEmpty()) {
            HttpImageTask poll = this.httpTasks.poll();
            if (poll != null) {
                poll.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null, null, null);
                this.currentHttpTasksCount++;
            }
        }
    }

    public void runArtworkTasks(boolean z) {
        if (z) {
            this.currentArtworkTasksCount--;
        }
        while (this.currentArtworkTasksCount < 4 && !this.artworkTasks.isEmpty()) {
            try {
                this.artworkTasks.poll().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null, null, null);
                this.currentArtworkTasksCount++;
            } catch (Throwable unused) {
                runArtworkTasks(false);
            }
        }
    }

    public boolean isLoadingHttpFile(String str) {
        return this.httpFileLoadTasksByKeys.containsKey(str);
    }

    public static String getHttpFileName(String str) {
        return Utilities.MD5(str);
    }

    public static File getHttpFilePath(String str, String str2) {
        String httpUrlExtension = getHttpUrlExtension(str, str2);
        return new File(FileLoader.getDirectory(4), Utilities.MD5(str) + "." + httpUrlExtension);
    }

    public void loadHttpFile(String str, String str2, int i) {
        if (str == null || str.length() == 0 || this.httpFileLoadTasksByKeys.containsKey(str)) {
            return;
        }
        String httpUrlExtension = getHttpUrlExtension(str, str2);
        File file = new File(FileLoader.getDirectory(4), Utilities.MD5(str) + "_temp." + httpUrlExtension);
        file.delete();
        HttpFileTask httpFileTask = new HttpFileTask(str, file, httpUrlExtension, i);
        this.httpFileLoadTasks.add(httpFileTask);
        this.httpFileLoadTasksByKeys.put(str, httpFileTask);
        runHttpFileLoadTasks(null, 0);
    }

    public void cancelLoadHttpFile(String str) {
        HttpFileTask httpFileTask = this.httpFileLoadTasksByKeys.get(str);
        if (httpFileTask != null) {
            httpFileTask.cancel(true);
            this.httpFileLoadTasksByKeys.remove(str);
            this.httpFileLoadTasks.remove(httpFileTask);
        }
        Runnable runnable = this.retryHttpsTasks.get(str);
        if (runnable != null) {
            AndroidUtilities.cancelRunOnUIThread(runnable);
        }
        runHttpFileLoadTasks(null, 0);
    }

    public void runHttpFileLoadTasks(final HttpFileTask httpFileTask, final int i) {
        AndroidUtilities.runOnUIThread(new Runnable() {
            @Override
            public final void run() {
                ImageLoader.this.lambda$runHttpFileLoadTasks$11$ImageLoader(httpFileTask, i);
            }
        });
    }

    public void lambda$runHttpFileLoadTasks$11$ImageLoader(HttpFileTask httpFileTask, int i) {
        if (httpFileTask != null) {
            this.currentHttpFileLoadTasksCount--;
        }
        if (httpFileTask != null) {
            if (i == 1) {
                if (!httpFileTask.canRetry) {
                    this.httpFileLoadTasksByKeys.remove(httpFileTask.url);
                    NotificationCenter.getInstance(httpFileTask.currentAccount).postNotificationName(NotificationCenter.httpFileDidFailedLoad, httpFileTask.url, 0);
                } else {
                    final HttpFileTask httpFileTask2 = new HttpFileTask(httpFileTask.url, httpFileTask.tempFile, httpFileTask.ext, httpFileTask.currentAccount);
                    Runnable runnable = new Runnable() {
                        @Override
                        public final void run() {
                            ImageLoader.this.lambda$null$10$ImageLoader(httpFileTask2);
                        }
                    };
                    this.retryHttpsTasks.put(httpFileTask.url, runnable);
                    AndroidUtilities.runOnUIThread(runnable, 1000L);
                }
            } else if (i == 2) {
                this.httpFileLoadTasksByKeys.remove(httpFileTask.url);
                File file = new File(FileLoader.getDirectory(4), Utilities.MD5(httpFileTask.url) + "." + httpFileTask.ext);
                if (!httpFileTask.tempFile.renameTo(file)) {
                    file = httpFileTask.tempFile;
                }
                NotificationCenter.getInstance(httpFileTask.currentAccount).postNotificationName(NotificationCenter.httpFileDidLoad, httpFileTask.url, file.toString());
            }
        }
        while (this.currentHttpFileLoadTasksCount < 2 && !this.httpFileLoadTasks.isEmpty()) {
            this.httpFileLoadTasks.poll().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, null, null, null);
            this.currentHttpFileLoadTasksCount++;
        }
    }

    public void lambda$null$10$ImageLoader(HttpFileTask httpFileTask) {
        this.httpFileLoadTasks.add(httpFileTask);
        runHttpFileLoadTasks(null, 0);
    }

    public static boolean shouldSendImageAsDocument(String str, Uri uri) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        if (str == null && uri != null && uri.getScheme() != null) {
            if (uri.getScheme().contains("file")) {
                str = uri.getPath();
            } else {
                try {
                    str = AndroidUtilities.getPath(uri);
                } catch (Throwable th) {
                    FileLog.e(th);
                }
            }
        }
        if (str != null) {
            BitmapFactory.decodeFile(str, options);
        } else if (uri != null) {
            try {
                InputStream openInputStream = ApplicationLoader.applicationContext.getContentResolver().openInputStream(uri);
                BitmapFactory.decodeStream(openInputStream, null, options);
                openInputStream.close();
            } catch (Throwable th2) {
                FileLog.e(th2);
                return false;
            }
        }
        float f = options.outWidth;
        float f2 = options.outHeight;
        return f / f2 > 10.0f || f2 / f > 10.0f;
    }

    public static Bitmap loadBitmap(String str, Uri uri, float f, float f2, boolean z) {
        InputStream openInputStream;
        float max;
        String path;
        int attributeInt;
        Matrix matrix;
        Bitmap decodeStream;
        Bitmap createBitmap;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        if (str == null && uri != null && uri.getScheme() != null) {
            if (uri.getScheme().contains("file")) {
                str = uri.getPath();
            } else {
                try {
                    str = AndroidUtilities.getPath(uri);
                } catch (Throwable th) {
                    FileLog.e(th);
                }
            }
        }
        Bitmap bitmap = null;
        if (str != null) {
            BitmapFactory.decodeFile(str, options);
        } else if (uri != null) {
            try {
                InputStream openInputStream2 = ApplicationLoader.applicationContext.getContentResolver().openInputStream(uri);
                BitmapFactory.decodeStream(openInputStream2, null, options);
                openInputStream2.close();
                openInputStream = ApplicationLoader.applicationContext.getContentResolver().openInputStream(uri);
                float f3 = options.outWidth / f;
                float f4 = options.outHeight / f2;
                max = !z ? Math.max(f3, f4) : Math.min(f3, f4);
                if (max < 1.0f) {
                    max = 1.0f;
                }
                options.inJustDecodeBounds = false;
                options.inSampleSize = (int) max;
                if (options.inSampleSize % 2 != 0) {
                    int i = 1;
                    while (true) {
                        int i2 = i * 2;
                        if (i2 >= options.inSampleSize) {
                            break;
                        }
                        i = i2;
                    }
                    options.inSampleSize = i;
                }
                options.inPurgeable = Build.VERSION.SDK_INT < 21;
                if (str == null) {
                    path = str;
                } else {
                    path = uri != null ? AndroidUtilities.getPath(uri) : null;
                }
                if (path != null) {
                    try {
                        attributeInt = new ExifInterface(path).getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                        matrix = new Matrix();
                    } catch (Throwable unused) {
                    }
                    if (attributeInt == 3) {
                        matrix.postRotate(180.0f);
                    } else {
                        if (attributeInt != 6) {
                            if (attributeInt == 8) {
                                matrix.postRotate(270.0f);
                            }
                            if (str == null) {
                                try {
                                    bitmap = BitmapFactory.decodeFile(str, options);
                                    if (bitmap == null) {
                                        return bitmap;
                                    }
                                    if (options.inPurgeable) {
                                        Utilities.pinBitmap(bitmap);
                                    }
                                    Bitmap createBitmap2 = Bitmaps.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                                    if (createBitmap2 == bitmap) {
                                        return bitmap;
                                    }
                                    bitmap.recycle();
                                    return createBitmap2;
                                } catch (Throwable th2) {
                                    FileLog.e(th2);
                                    getInstance().clearMemory();
                                    if (bitmap == null) {
                                        try {
                                            bitmap = BitmapFactory.decodeFile(str, options);
                                            if (bitmap != null && options.inPurgeable) {
                                                Utilities.pinBitmap(bitmap);
                                            }
                                        } catch (Throwable th3) {
                                            FileLog.e(th3);
                                            return bitmap;
                                        }
                                    }
                                    if (bitmap == null) {
                                        return bitmap;
                                    }
                                    Bitmap createBitmap3 = Bitmaps.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                                    if (createBitmap3 == bitmap) {
                                        return bitmap;
                                    }
                                    bitmap.recycle();
                                    return createBitmap3;
                                }
                            }
                            try {
                            } catch (Throwable th4) {
                                FileLog.e(th4);
                            }
                            if (uri == null) {
                                return null;
                            }
                            try {
                                decodeStream = BitmapFactory.decodeStream(openInputStream, null, options);
                            } catch (Throwable th5) {
                                th = th5;
                            }
                            if (decodeStream != null) {
                                try {
                                    if (options.inPurgeable) {
                                        Utilities.pinBitmap(decodeStream);
                                    }
                                    createBitmap = Bitmaps.createBitmap(decodeStream, 0, 0, decodeStream.getWidth(), decodeStream.getHeight(), matrix, true);
                                } catch (Throwable th6) {
                                    th = th6;
                                    bitmap = decodeStream;
                                    try {
                                        FileLog.e(th);
                                        openInputStream.close();
                                        return bitmap;
                                    } catch (Throwable th7) {
                                        try {
                                            openInputStream.close();
                                        } catch (Throwable th8) {
                                            FileLog.e(th8);
                                        }
                                        throw th7;
                                    }
                                }
                                if (createBitmap != decodeStream) {
                                    decodeStream.recycle();
                                    bitmap = createBitmap;
                                    openInputStream.close();
                                    return bitmap;
                                }
                            }
                            bitmap = decodeStream;
                            openInputStream.close();
                            return bitmap;
                        }
                        matrix.postRotate(90.0f);
                    }
                    if (str == null) {
                    }
                }
                matrix = null;
                if (str == null) {
                }
            } catch (Throwable th9) {
                FileLog.e(th9);
                return null;
            }
        }
        openInputStream = null;
        float f32 = options.outWidth / f;
        float f42 = options.outHeight / f2;
        if (!z) {
        }
        if (max < 1.0f) {
        }
        options.inJustDecodeBounds = false;
        options.inSampleSize = (int) max;
        if (options.inSampleSize % 2 != 0) {
        }
        options.inPurgeable = Build.VERSION.SDK_INT < 21;
        if (str == null) {
        }
        if (path != null) {
        }
        matrix = null;
        if (str == null) {
        }
    }

    public static void fillPhotoSizeWithBytes(TLRPC.PhotoSize photoSize) {
        if (photoSize != null) {
            if (photoSize.bytes == null || photoSize.bytes.length == 0) {
                try {
                    RandomAccessFile randomAccessFile = new RandomAccessFile(FileLoader.getPathToAttach(photoSize, true), "r");
                    if (((int) randomAccessFile.length()) < 20000) {
                        photoSize.bytes = new byte[(int) randomAccessFile.length()];
                        randomAccessFile.readFully(photoSize.bytes, 0, photoSize.bytes.length);
                    }
                } catch (Throwable th) {
                    FileLog.e(th);
                }
            }
        }
    }

    private static TLRPC.PhotoSize scaleAndSaveImageInternal(TLRPC.PhotoSize photoSize, Bitmap bitmap, int i, int i2, float f, float f2, float f3, int i3, boolean z, boolean z2, boolean z3) throws Exception {
        TLRPC.TL_fileLocationToBeDeprecated tL_fileLocationToBeDeprecated;
        Bitmap createScaledBitmap = (f3 > 1.0f || z2) ? Bitmaps.createScaledBitmap(bitmap, i, i2, true) : bitmap;
        if (photoSize == null || !(photoSize.location instanceof TLRPC.TL_fileLocationToBeDeprecated)) {
            tL_fileLocationToBeDeprecated = new TLRPC.TL_fileLocationToBeDeprecated();
            tL_fileLocationToBeDeprecated.volume_id = -2147483648L;
            tL_fileLocationToBeDeprecated.dc_id = Integer.MIN_VALUE;
            tL_fileLocationToBeDeprecated.local_id = SharedConfig.getLastLocalId();
            tL_fileLocationToBeDeprecated.file_reference = new byte[0];
            photoSize = new TLRPC.TL_photoSize();
            photoSize.location = tL_fileLocationToBeDeprecated;
            photoSize.w = createScaledBitmap.getWidth();
            photoSize.h = createScaledBitmap.getHeight();
            if (photoSize.w <= 100 && photoSize.h <= 100) {
                photoSize.type = "s";
            } else if (photoSize.w <= 320 && photoSize.h <= 320) {
                photoSize.type = "m";
            } else if (photoSize.w <= 800 && photoSize.h <= 800) {
                photoSize.type = "x";
            } else if (photoSize.w <= 1280 && photoSize.h <= 1280) {
                photoSize.type = "y";
            } else {
                photoSize.type = "w";
            }
        } else {
            tL_fileLocationToBeDeprecated = (TLRPC.TL_fileLocationToBeDeprecated) photoSize.location;
        }
        FileOutputStream fileOutputStream = new FileOutputStream(new File(FileLoader.getDirectory(tL_fileLocationToBeDeprecated.volume_id == -2147483648L ? 4 : 0), tL_fileLocationToBeDeprecated.volume_id + "_" + tL_fileLocationToBeDeprecated.local_id + ".jpg"));
        if (z3) {
            createScaledBitmap.compress(Bitmap.CompressFormat.PNG, i3, fileOutputStream);
        } else {
            createScaledBitmap.compress(Bitmap.CompressFormat.JPEG, i3, fileOutputStream);
        }
        if (z) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            if (z3) {
                createScaledBitmap.compress(Bitmap.CompressFormat.PNG, i3, byteArrayOutputStream);
            } else {
                createScaledBitmap.compress(Bitmap.CompressFormat.JPEG, i3, byteArrayOutputStream);
            }
            photoSize.bytes = byteArrayOutputStream.toByteArray();
            photoSize.size = photoSize.bytes.length;
            byteArrayOutputStream.close();
        } else {
            photoSize.size = (int) fileOutputStream.getChannel().size();
        }
        fileOutputStream.close();
        if (createScaledBitmap != bitmap) {
            createScaledBitmap.recycle();
        }
        return photoSize;
    }

    public static TLRPC.PhotoSize SaveImageWithOriginalInternal(TLRPC.PhotoSize photoSize, String str, boolean z) throws Exception {
        TLRPC.TL_fileLocationToBeDeprecated tL_fileLocationToBeDeprecated;
        if (photoSize == null || !(photoSize.location instanceof TLRPC.TL_fileLocationToBeDeprecated)) {
            tL_fileLocationToBeDeprecated = new TLRPC.TL_fileLocationToBeDeprecated();
            tL_fileLocationToBeDeprecated.volume_id = -2147483648L;
            tL_fileLocationToBeDeprecated.dc_id = Integer.MIN_VALUE;
            tL_fileLocationToBeDeprecated.local_id = SharedConfig.getLastLocalId();
            tL_fileLocationToBeDeprecated.file_reference = new byte[0];
            photoSize = new TLRPC.TL_photoSize();
            photoSize.location = tL_fileLocationToBeDeprecated;
            BitmapFactory.Options options = new BitmapFactory.Options();
            BitmapFactory.decodeFile(str, options);
            photoSize.w = options.outWidth;
            photoSize.h = options.outHeight;
            if (photoSize.w <= 100 && photoSize.h <= 100) {
                photoSize.type = "s";
            } else if (photoSize.w <= 320 && photoSize.h <= 320) {
                photoSize.type = "m";
            } else if (photoSize.w <= 800 && photoSize.h <= 800) {
                photoSize.type = "x";
            } else if (photoSize.w <= 1280 && photoSize.h <= 1280) {
                photoSize.type = "y";
            } else {
                photoSize.type = "w";
            }
        } else {
            tL_fileLocationToBeDeprecated = (TLRPC.TL_fileLocationToBeDeprecated) photoSize.location;
        }
        File file = new File(FileLoader.getDirectory(tL_fileLocationToBeDeprecated.volume_id == -2147483648L ? 4 : 0), tL_fileLocationToBeDeprecated.volume_id + "_" + tL_fileLocationToBeDeprecated.local_id + ".jpg");
        FileInputStream fileInputStream = new FileInputStream(str);
        AndroidUtilities.copyFile(fileInputStream, file);
        if (z) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            photoSize.bytes = byteArrayOutputStream.toByteArray();
            photoSize.size = photoSize.bytes.length;
            byteArrayOutputStream.close();
        } else {
            photoSize.size = (int) fileInputStream.getChannel().size();
        }
        fileInputStream.close();
        return photoSize;
    }

    public static TLRPC.PhotoSize scaleAndSaveImage(Bitmap bitmap, float f, float f2, int i, boolean z) {
        return scaleAndSaveImage(null, bitmap, f, f2, i, z, 0, 0, false);
    }

    public static TLRPC.PhotoSize scaleAndSaveImage(TLRPC.PhotoSize photoSize, Bitmap bitmap, float f, float f2, int i, boolean z) {
        return scaleAndSaveImage(photoSize, bitmap, f, f2, i, z, 0, 0, false);
    }

    public static TLRPC.PhotoSize scaleAndSaveImage(Bitmap bitmap, float f, float f2, int i, boolean z, int i2, int i3) {
        return scaleAndSaveImage(null, bitmap, f, f2, i, z, i2, i3, false);
    }

    public static TLRPC.PhotoSize scaleAndSaveImage(Bitmap bitmap, float f, float f2, int i, boolean z, boolean z2) {
        return scaleAndSaveImage(null, bitmap, f, f2, i, z, 0, 0, z2);
    }

    public static TLRPC.PhotoSize scaleAndSaveImage(Bitmap bitmap, float f, float f2, int i, boolean z, int i2, int i3, boolean z2) {
        return scaleAndSaveImage(null, bitmap, f, f2, i, z, i2, i3, z2);
    }

    public static TLRPC.PhotoSize scaleAndSaveImage(TLRPC.PhotoSize photoSize, Bitmap bitmap, float f, float f2, int i, boolean z, int i2, int i3, boolean z2) {
        float f3;
        boolean z3;
        int i4;
        int i5;
        float max;
        if (bitmap == null) {
            return null;
        }
        float width = bitmap.getWidth();
        float height = bitmap.getHeight();
        if (width != 0.0f && height != 0.0f) {
            float max2 = Math.max(width / f, height / f2);
            if (i2 != 0 && i3 != 0) {
                float f4 = i2;
                if (width < f4 || height < i3) {
                    if (width >= f4 || height <= i3) {
                        if (width > f4) {
                            float f5 = i3;
                            if (height < f5) {
                                max = height / f5;
                            }
                        }
                        max = Math.max(width / f4, height / i3);
                    } else {
                        max = width / f4;
                    }
                    f3 = max;
                    z3 = true;
                    i4 = (int) (width / f3);
                    i5 = (int) (height / f3);
                    if (i5 != 0 && i4 != 0) {
                        try {
                            return scaleAndSaveImageInternal(photoSize, bitmap, i4, i5, width, height, f3, i, z, z3, z2);
                        } catch (Throwable th) {
                            FileLog.e(th);
                            getInstance().clearMemory();
                            System.gc();
                            try {
                                return scaleAndSaveImageInternal(photoSize, bitmap, i4, i5, width, height, f3, i, z, z3, z2);
                            } catch (Throwable th2) {
                                FileLog.e(th2);
                            }
                        }
                    }
                }
            }
            f3 = max2;
            z3 = false;
            i4 = (int) (width / f3);
            i5 = (int) (height / f3);
            if (i5 != 0) {
                return scaleAndSaveImageInternal(photoSize, bitmap, i4, i5, width, height, f3, i, z, z3, z2);
            }
        }
        return null;
    }

    public static String getHttpUrlExtension(String str, String str2) {
        String lastPathSegment = Uri.parse(str).getLastPathSegment();
        if (!TextUtils.isEmpty(lastPathSegment) && lastPathSegment.length() > 1) {
            str = lastPathSegment;
        }
        int lastIndexOf = str.lastIndexOf(46);
        String substring = lastIndexOf != -1 ? str.substring(lastIndexOf + 1) : null;
        return (substring == null || substring.length() == 0 || substring.length() > 4) ? str2 : substring;
    }

    public static void saveMessageThumbs(TLRPC.Message message) {
        TLRPC.PhotoSize photoSize;
        int i = 0;
        TLRPC.PhotoSize photoSize2 = null;
        if (message.media instanceof TLRPC.TL_messageMediaPhoto) {
            int size = message.media.photo.sizes.size();
            for (int i2 = 0; i2 < size; i2++) {
                photoSize = message.media.photo.sizes.get(i2);
                if (photoSize instanceof TLRPC.TL_photoCachedSize) {
                    photoSize2 = photoSize;
                    break;
                }
            }
        } else if (message.media instanceof TLRPC.TL_messageMediaDocument) {
            int size2 = message.media.document.thumbs.size();
            for (int i3 = 0; i3 < size2; i3++) {
                photoSize = message.media.document.thumbs.get(i3);
                if (photoSize instanceof TLRPC.TL_photoCachedSize) {
                    photoSize2 = photoSize;
                    break;
                }
            }
        } else if ((message.media instanceof TLRPC.TL_messageMediaWebPage) && message.media.webpage.photo != null) {
            int size3 = message.media.webpage.photo.sizes.size();
            for (int i4 = 0; i4 < size3; i4++) {
                photoSize = message.media.webpage.photo.sizes.get(i4);
                if (photoSize instanceof TLRPC.TL_photoCachedSize) {
                    photoSize2 = photoSize;
                    break;
                }
            }
        }
        if (photoSize2 == null || photoSize2.bytes == null || photoSize2.bytes.length == 0) {
            return;
        }
        if (photoSize2.location == null || (photoSize2.location instanceof TLRPC.TL_fileLocationUnavailable)) {
            photoSize2.location = new TLRPC.TL_fileLocationToBeDeprecated();
            photoSize2.location.volume_id = -2147483648L;
            photoSize2.location.local_id = SharedConfig.getLastLocalId();
        }
        boolean z = true;
        File pathToAttach = FileLoader.getPathToAttach(photoSize2, true);
        if (MessageObject.shouldEncryptPhotoOrVideo(message)) {
            pathToAttach = new File(pathToAttach.getAbsolutePath() + ".enc");
        } else {
            z = false;
        }
        if (!pathToAttach.exists()) {
            if (z) {
                try {
                    RandomAccessFile randomAccessFile = new RandomAccessFile(new File(FileLoader.getInternalCacheDir(), pathToAttach.getName() + ".key"), "rws");
                    long length = randomAccessFile.length();
                    byte[] bArr = new byte[32];
                    byte[] bArr2 = new byte[16];
                    if (length > 0 && length % 48 == 0) {
                        randomAccessFile.read(bArr, 0, 32);
                        randomAccessFile.read(bArr2, 0, 16);
                    } else {
                        Utilities.random.nextBytes(bArr);
                        Utilities.random.nextBytes(bArr2);
                        randomAccessFile.write(bArr);
                        randomAccessFile.write(bArr2);
                    }
                    randomAccessFile.close();
                    Utilities.aesCtrDecryptionByteArray(photoSize2.bytes, bArr, bArr2, 0, photoSize2.bytes.length, 0);
                } catch (Exception e) {
                    FileLog.e(e);
                }
            }
            RandomAccessFile randomAccessFile2 = new RandomAccessFile(pathToAttach, "rws");
            randomAccessFile2.write(photoSize2.bytes);
            randomAccessFile2.close();
        }
        TLRPC.TL_photoSize tL_photoSize = new TLRPC.TL_photoSize();
        tL_photoSize.w = photoSize2.w;
        tL_photoSize.h = photoSize2.h;
        tL_photoSize.location = photoSize2.location;
        tL_photoSize.size = photoSize2.size;
        tL_photoSize.type = photoSize2.type;
        if (message.media instanceof TLRPC.TL_messageMediaPhoto) {
            int size4 = message.media.photo.sizes.size();
            while (i < size4) {
                if (message.media.photo.sizes.get(i) instanceof TLRPC.TL_photoCachedSize) {
                    message.media.photo.sizes.set(i, tL_photoSize);
                    return;
                }
                i++;
            }
            return;
        }
        if (message.media instanceof TLRPC.TL_messageMediaDocument) {
            int size5 = message.media.document.thumbs.size();
            while (i < size5) {
                if (message.media.document.thumbs.get(i) instanceof TLRPC.TL_photoCachedSize) {
                    message.media.document.thumbs.set(i, tL_photoSize);
                    return;
                }
                i++;
            }
            return;
        }
        if (message.media instanceof TLRPC.TL_messageMediaWebPage) {
            int size6 = message.media.webpage.photo.sizes.size();
            while (i < size6) {
                if (message.media.webpage.photo.sizes.get(i) instanceof TLRPC.TL_photoCachedSize) {
                    message.media.webpage.photo.sizes.set(i, tL_photoSize);
                    return;
                }
                i++;
            }
        }
    }

    public static void saveMessagesThumbs(ArrayList<TLRPC.Message> arrayList) {
        if (arrayList == null || arrayList.isEmpty()) {
            return;
        }
        for (int i = 0; i < arrayList.size(); i++) {
            saveMessageThumbs(arrayList.get(i));
        }
    }
}