package com.chartboost.sdk.impl;

import android.content.Context;
import com.facebook.cache.disk.DefaultDiskStorage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.json.JSONArray;
import org.json.JSONObject;
/* loaded from: classes2.dex */
public class g5 {

    /* renamed from: a  reason: collision with root package name */
    public final AtomicReference f1702a;
    public final h5 b;

    public h5 a() {
        return this.b;
    }

    public g5(Context context, AtomicReference atomicReference) {
        h5 h5Var = new h5(context.getCacheDir());
        this.b = h5Var;
        this.f1702a = atomicReference;
        try {
            long currentTimeMillis = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(((p9) atomicReference.get()).o);
            File file = new File(h5Var.f1712a, "templates");
            if (file.exists()) {
                a(file.listFiles(), currentTimeMillis);
                a(h5Var);
            }
        } catch (Exception e) {
            c7.b("Exception while cleaning up templates directory at " + this.b.f.getPath(), e);
            e.printStackTrace();
        }
    }

    public File[] c() {
        File b = b();
        if (b != null) {
            return b.listFiles();
        }
        return null;
    }

    public final void a(File[] fileArr, long j) {
        if (fileArr != null) {
            for (File file : fileArr) {
                if (file.isDirectory()) {
                    b(file.listFiles(), j);
                    a(file.listFiles(), file);
                }
            }
        }
    }

    public final void b(File[] fileArr, long j) {
        if (fileArr != null) {
            for (File file : fileArr) {
                if (file.lastModified() < j && !file.delete()) {
                    c7.b("Unable to delete " + file.getPath(), null);
                }
            }
        }
    }

    public JSONObject e() {
        String[] list;
        JSONObject jSONObject = new JSONObject();
        try {
            File file = a().f1712a;
            for (String str : ((p9) this.f1702a.get()).p) {
                if (!str.equals("templates")) {
                    File file2 = new File(file, str);
                    JSONArray jSONArray = new JSONArray();
                    if (file2.exists() && (list = file2.list()) != null) {
                        for (String str2 : list) {
                            if (!str2.equals(".nomedia") && !str2.endsWith(DefaultDiskStorage.FileType.TEMP)) {
                                jSONArray.put(str2);
                            }
                        }
                    }
                    z1.a(jSONObject, str, jSONArray);
                }
            }
        } catch (Exception e) {
            c7.b("getWebViewCacheAssets: " + e, null);
        }
        return jSONObject;
    }

    public boolean c(File file) {
        return file != null && file.exists() && file.length() > 0;
    }

    public File d() {
        return this.b.i;
    }

    public void d(File file) {
        try {
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
            randomAccessFile.seek(0L);
            int read = randomAccessFile.read();
            randomAccessFile.seek(0L);
            randomAccessFile.write(read);
            randomAccessFile.close();
        } catch (FileNotFoundException e) {
            c7.b("File not found when attempting to touch", e);
        } catch (IOException e2) {
            c7.b("IOException when attempting to touch file", e2);
        }
    }

    public long b(File file) {
        long j = 0;
        if (file != null) {
            try {
                if (file.isDirectory()) {
                    File[] listFiles = file.listFiles();
                    if (listFiles != null) {
                        for (File file2 : listFiles) {
                            j += b(file2);
                        }
                        return j;
                    }
                    return 0L;
                }
            } catch (Exception e) {
                c7.b("getFolderSize: " + e, null);
                return 0L;
            }
        }
        if (file != null) {
            return file.length();
        }
        return 0L;
    }

    public boolean a(File file) {
        if (file == null || !file.exists()) {
            return false;
        }
        return file.delete();
    }

    public final void a(File[] fileArr, File file) {
        if (fileArr == null || fileArr.length != 0 || file.delete()) {
            return;
        }
        c7.b("Unable to delete " + file.getPath(), null);
    }

    public File b() {
        return this.b.h;
    }

    public File a(File file, String str) {
        if (file == null || str == null) {
            return null;
        }
        File file2 = new File(file, str);
        if (!file2.exists() || file2.length() <= 0) {
            return null;
        }
        return file2;
    }

    public Boolean a(v vVar) {
        Map d = vVar.d();
        h5 a2 = a();
        if (a2 == null) {
            return Boolean.FALSE;
        }
        File file = a2.f1712a;
        for (c1 c1Var : d.values()) {
            File a3 = c1Var.a(file);
            if (a3 == null) {
                return Boolean.FALSE;
            }
            if (!a3.exists()) {
                c7.b("Asset does not exist: " + c1Var.b, null);
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }

    public final void a(h5 h5Var) {
        File file = new File(h5Var.f1712a, ".adId");
        if (!file.exists() || file.delete()) {
            return;
        }
        c7.b("Unable to delete " + file.getPath(), null);
    }
}
