s.java 源代码


package com.applovin.impl.sdk;

import android.content.Context;
import android.net.Uri;
import com.applovin.impl.sdk.utils.StringUtils;
import com.applovin.impl.sdk.utils.Utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class s {

    private final k f7792b;

    private final u f7793c;

    private final String f7791a = "FileManager";

    private final Object f7794d = new Object();

    public s(k kVar) {
        this.f7792b = kVar;
        this.f7793c = kVar.U0();
    }

    private long a() {
        long longValue = ((Long) this.f7792b.A(j0.b.G0)).longValue();
        if (longValue >= 0 && n()) {
            return longValue;
        }
        return -1L;
    }

    private long b(long j6) {
        return j6 / 1048576;
    }

    private void h(long j6, Context context) {
        u uVar;
        String str;
        if (!n()) {
            return;
        }
        long intValue = ((Integer) this.f7792b.A(j0.b.H0)).intValue();
        if (intValue == -1) {
            uVar = this.f7793c;
            str = "Cache has no maximum size set; skipping drop...";
        } else {
            long b6 = b(j6);
            uVar = this.f7793c;
            if (b6 > intValue) {
                uVar.g("FileManager", "Cache has exceeded maximum size; dropping...");
                Iterator<File> it = s(context).iterator();
                while (it.hasNext()) {
                    p(it.next());
                }
                this.f7792b.r().a(k0.g.f23444i);
                return;
            }
            str = "Cache is present but under size limit; not dropping...";
        }
        uVar.g("FileManager", str);
    }

    private boolean l(File file, String str, List<String> list, boolean z6, k0.f fVar) {
        if (file != null && file.exists() && !file.isDirectory()) {
            this.f7793c.g("FileManager", "File exists for " + str);
            if (fVar != null) {
                fVar.e(file.length());
                return true;
            }
            return true;
        }
        ByteArrayOutputStream d6 = d(str, list, z6, fVar);
        if (fVar != null && d6 != null) {
            fVar.c(d6.size());
        }
        return j(d6, file);
    }

    private boolean n() {
        return ((Boolean) this.f7792b.A(j0.b.F0)).booleanValue();
    }

    private boolean o(ByteArrayOutputStream byteArrayOutputStream, File file) {
        k kVar;
        boolean z6;
        this.f7793c.g("FileManager", "Writing resource to filesystem: " + file.getName());
        synchronized (this.f7794d) {
            FileOutputStream fileOutputStream = null;
            try {
                try {
                    FileOutputStream fileOutputStream2 = new FileOutputStream(file);
                    try {
                        byteArrayOutputStream.writeTo(fileOutputStream2);
                        Utils.close(fileOutputStream2, this.f7792b);
                        z6 = true;
                    } catch (IOException e6) {
                        e = e6;
                        fileOutputStream = fileOutputStream2;
                        this.f7793c.h("FileManager", "Unable to write data to file.", e);
                        kVar = this.f7792b;
                        Utils.close(fileOutputStream, kVar);
                        z6 = false;
                        return z6;
                    } catch (Throwable th) {
                        th = th;
                        fileOutputStream = fileOutputStream2;
                        this.f7793c.h("FileManager", "Unknown failure to write file.", th);
                        kVar = this.f7792b;
                        Utils.close(fileOutputStream, kVar);
                        z6 = false;
                        return z6;
                    }
                } catch (Throwable th2) {
                    Utils.close(fileOutputStream, this.f7792b);
                    throw th2;
                }
            } catch (IOException e7) {
                e = e7;
            } catch (Throwable th3) {
                th = th3;
            }
        }
        return z6;
    }

    private boolean p(File file) {
        boolean delete;
        this.f7793c.g("FileManager", "Removing file " + file.getName() + " from filesystem...");
        synchronized (this.f7794d) {
            try {
                try {
                    delete = file.delete();
                } catch (Exception e6) {
                    this.f7793c.h("FileManager", "Failed to remove file " + file.getName() + " from filesystem!", e6);
                    return false;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return delete;
    }

    private long r(Context context) {
        boolean z6;
        long j6;
        boolean z7;
        long a7 = a();
        if (a7 != -1) {
            z6 = true;
        } else {
            z6 = false;
        }
        long seconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
        synchronized (this.f7794d) {
            List<String> l02 = this.f7792b.l0(j0.b.M0);
            j6 = 0;
            for (File file : s(context)) {
                if (z6 && !l02.contains(file.getName()) && seconds - TimeUnit.MILLISECONDS.toSeconds(file.lastModified()) > a7) {
                    this.f7793c.g("FileManager", "File " + file.getName() + " has expired, removing...");
                    z7 = p(file);
                } else {
                    z7 = false;
                }
                if (z7) {
                    this.f7792b.r().a(k0.g.f23443h);
                } else {
                    j6 += file.length();
                }
            }
        }
        return j6;
    }

    private List<File> s(Context context) {
        List<File> asList;
        File t6 = t(context);
        if (t6.isDirectory()) {
            synchronized (this.f7794d) {
                asList = Arrays.asList(t6.listFiles());
            }
            return asList;
        }
        return Collections.emptyList();
    }

    private File t(Context context) {
        return new File(context.getFilesDir(), "al");
    }

    public ByteArrayOutputStream c(File file) {
        FileInputStream fileInputStream;
        if (file == null) {
            return null;
        }
        this.f7793c.g("FileManager", "Reading resource from filesystem: " + file.getName());
        synchronized (this.f7794d) {
            try {
                try {
                    fileInputStream = new FileInputStream(file);
                } catch (Throwable th) {
                    Utils.close("FileManager", this.f7792b);
                    throw th;
                }
            } catch (FileNotFoundException e6) {
                e = e6;
                fileInputStream = null;
            } catch (IOException e7) {
                e = e7;
                fileInputStream = null;
            } catch (Throwable th2) {
                th = th2;
                fileInputStream = null;
            }
            try {
                try {
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    byte[] bArr = new byte[1024];
                    while (true) {
                        int read = fileInputStream.read(bArr, 0, 1024);
                        if (read >= 0) {
                            try {
                                byteArrayOutputStream.write(bArr, 0, read);
                            } catch (Exception unused) {
                                Utils.close(byteArrayOutputStream, this.f7792b);
                                Utils.close(fileInputStream, this.f7792b);
                                return null;
                            }
                        } else {
                            Utils.close(fileInputStream, this.f7792b);
                            return byteArrayOutputStream;
                        }
                    }
                } catch (Throwable th3) {
                    th = th3;
                    this.f7793c.h("FileManager", "Unknown failure to read file.", th);
                    Utils.close(fileInputStream, this.f7792b);
                    return null;
                }
            } catch (FileNotFoundException e8) {
                e = e8;
                this.f7793c.i("FileManager", "File not found. " + e);
                Utils.close(fileInputStream, this.f7792b);
                return null;
            } catch (IOException e9) {
                e = e9;
                this.f7793c.g("FileManager", "Failed to read file: " + file.getName() + e);
                Utils.close(fileInputStream, this.f7792b);
                return null;
            }
        }
    }

    public ByteArrayOutputStream d(String str, List<String> list, boolean z6, k0.f fVar) {
        InputStream inputStream;
        ?? r12 = 0;
        r12 = 0;
        r12 = 0;
        if (z6 && !Utils.isDomainWhitelisted(str, list)) {
            this.f7793c.g("FileManager", "Domain is not whitelisted, skipping precache for url: " + str);
            return null;
        }
        if (((Boolean) this.f7792b.A(j0.b.f23173u2)).booleanValue() && !str.contains("https://")) {
            this.f7793c.k("FileManager", "Plaintext HTTP operation requested; upgrading to HTTPS due to universal SSL setting...");
            str = str.replace("http://", "https://");
        }
        ?? r9 = this.f7793c;
        ?? r10 = "Loading " + str + "...";
        r9.g("FileManager", r10);
        try {
            try {
                r9 = new ByteArrayOutputStream();
                try {
                    r10 = (HttpURLConnection) new URL(str).openConnection();
                } catch (IOException e6) {
                    e = e6;
                    r10 = 0;
                    r9 = r9;
                    inputStream = r10;
                    this.f7793c.h("FileManager", "Error loading " + str, e);
                    fVar.d(e);
                    Utils.close(inputStream, this.f7792b);
                    Utils.close(r9, this.f7792b);
                    Utils.disconnect(r10, this.f7792b);
                    return null;
                } catch (Throwable th) {
                    th = th;
                    r10 = 0;
                }
            } catch (Throwable th2) {
                th = th2;
                r12 = "...";
            }
            try {
                r10.setConnectTimeout(((Integer) this.f7792b.A(j0.b.f23161s2)).intValue());
                r10.setReadTimeout(((Integer) this.f7792b.A(j0.b.f23167t2)).intValue());
                r10.setDefaultUseCaches(true);
                r10.setUseCaches(true);
                r10.setAllowUserInteraction(false);
                r10.setInstanceFollowRedirects(true);
                int responseCode = r10.getResponseCode();
                fVar.b(responseCode);
                if (responseCode >= 200 && responseCode < 300) {
                    inputStream = r10.getInputStream();
                    try {
                        byte[] bArr = new byte[1024];
                        while (true) {
                            int read = inputStream.read(bArr, 0, 1024);
                            if (read >= 0) {
                                try {
                                    r9.write(bArr, 0, read);
                                } catch (Exception unused) {
                                    Utils.close(r9, this.f7792b);
                                    Utils.close(inputStream, this.f7792b);
                                    Utils.close(r9, this.f7792b);
                                    Utils.disconnect(r10, this.f7792b);
                                    return null;
                                }
                            } else {
                                this.f7793c.g("FileManager", "Loaded resource at " + str);
                                Utils.close(inputStream, this.f7792b);
                                Utils.close(r9, this.f7792b);
                                Utils.disconnect(r10, this.f7792b);
                                return r9;
                            }
                        }
                    } catch (IOException e7) {
                        e = e7;
                        this.f7793c.h("FileManager", "Error loading " + str, e);
                        fVar.d(e);
                        Utils.close(inputStream, this.f7792b);
                        Utils.close(r9, this.f7792b);
                        Utils.disconnect(r10, this.f7792b);
                        return null;
                    }
                }
                Utils.close(null, this.f7792b);
                Utils.close(r9, this.f7792b);
                Utils.disconnect(r10, this.f7792b);
                return null;
            } catch (IOException e8) {
                e = e8;
                inputStream = null;
            } catch (Throwable th3) {
                th = th3;
                Utils.close(r12, this.f7792b);
                Utils.close(r9, this.f7792b);
                Utils.disconnect(r10, this.f7792b);
                throw th;
            }
        } catch (IOException e9) {
            e = e9;
            r9 = 0;
            r10 = 0;
        } catch (Throwable th4) {
            th = th4;
            r9 = 0;
            r10 = 0;
        }
    }

    public File e(String str, Context context) {
        File file;
        if (!StringUtils.isValidString(str)) {
            this.f7793c.g("FileManager", "Nothing to look up, skipping...");
            return null;
        }
        this.f7793c.g("FileManager", "Looking up cached resource: " + str);
        if (str.contains("icon")) {
            str = str.replace("/", "_").replace(".", "_");
        }
        synchronized (this.f7794d) {
            File t6 = t(context);
            file = new File(t6, str);
            try {
                t6.mkdirs();
            } catch (Throwable th) {
                this.f7793c.h("FileManager", "Unable to make cache directory at " + t6, th);
                return null;
            }
        }
        return file;
    }

    public String f(Context context, String str, String str2, List<String> list, boolean z6, k0.f fVar) {
        return g(context, str, str2, list, z6, false, fVar);
    }

    public String g(Context context, String str, String str2, List<String> list, boolean z6, boolean z7, k0.f fVar) {
        if (!StringUtils.isValidString(str)) {
            this.f7793c.g("FileManager", "Nothing to cache, skipping...");
            return null;
        }
        String lastPathSegment = Uri.parse(str).getLastPathSegment();
        if (StringUtils.isValidString(lastPathSegment) && StringUtils.isValidString(str2)) {
            lastPathSegment = str2 + lastPathSegment;
        }
        String str3 = lastPathSegment;
        File e6 = e(str3, context);
        if (!l(e6, str, list, z6, fVar)) {
            return null;
        }
        this.f7793c.g("FileManager", "Caching succeeded for file " + str3);
        if (z7) {
            return Uri.fromFile(e6).toString();
        }
        return str3;
    }

    public void i(Context context) {
        if (n() && this.f7792b.u0()) {
            this.f7793c.g("FileManager", "Compacting cache...");
            synchronized (this.f7794d) {
                h(r(context), context);
            }
        }
    }

    public boolean j(ByteArrayOutputStream byteArrayOutputStream, File file) {
        if (file == null) {
            return false;
        }
        this.f7793c.g("FileManager", "Caching " + file.getAbsolutePath() + "...");
        if (byteArrayOutputStream != null && byteArrayOutputStream.size() > 0) {
            if (!o(byteArrayOutputStream, file)) {
                this.f7793c.l("FileManager", "Unable to cache " + file.getAbsolutePath());
                return false;
            }
            this.f7793c.g("FileManager", "Caching completed for " + file);
            return true;
        }
        this.f7793c.k("FileManager", "No data for " + file.getAbsolutePath());
        return false;
    }

    public boolean k(File file, String str, List<String> list, k0.f fVar) {
        return l(file, str, list, true, fVar);
    }

    public void m(Context context) {
        try {
            e(".nomedia", context);
            File file = new File(t(context), ".nomedia");
            if (!file.exists()) {
                this.f7793c.g("FileManager", "Creating .nomedia file at " + file.getAbsolutePath());
                if (file.createNewFile()) {
                    return;
                }
                this.f7793c.l("FileManager", "Failed to create .nomedia file");
            }
        } catch (IOException e6) {
            this.f7793c.h("FileManager", "Failed to create .nomedia file", e6);
        }
    }

    public boolean q(String str, Context context) {
        boolean z6;
        synchronized (this.f7794d) {
            File e6 = e(str, context);
            if (e6 != null && e6.exists() && !e6.isDirectory()) {
                z6 = true;
            } else {
                z6 = false;
            }
        }
        return z6;
    }
}