package com.ironsource.sdk.utils;

import android.content.Context;
import android.os.Build;
import com.ironsource.jl;
import com.ironsource.l9;
import com.ironsource.ls;
import com.ironsource.mediationsdk.logger.IronLog;
import com.ironsource.mg;
import com.ironsource.oe;
import com.ironsource.y8;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/* loaded from: classes4.dex */
public class IronSourceStorageUtils {

    /* renamed from: a  reason: collision with root package name */
    private static final String f3059a = "supersonicads";
    private static ls b;
    private static boolean c;

    private static void a(Context context) {
        ls lsVar = b;
        if (lsVar != null && lsVar.b()) {
            deleteCacheDirectories(context);
        }
        ls lsVar2 = b;
        if (lsVar2 == null || !lsVar2.c()) {
            return;
        }
        deleteFilesDirectories(context);
    }

    private static void a(File file) {
        if (file != null) {
            deleteFolder(b(file).getPath());
        }
    }

    private static boolean a() {
        ls lsVar;
        return Build.VERSION.SDK_INT > 29 && (lsVar = b) != null && lsVar.a();
    }

    private static File b(Context context) {
        oe f = jl.P().f();
        ls lsVar = b;
        return (lsVar == null || !lsVar.d()) ? f.t(context) : f.e(context);
    }

    private static File b(File file) {
        StringBuilder append = new StringBuilder().append(file.getAbsolutePath());
        String str = File.separator;
        return new File(append.append(str).append(f3059a).append(str).toString());
    }

    public static String buildAbsolutePathToDirInCache(String str, String str2) {
        return str2 == null ? str : str + File.separator + str2;
    }

    public static JSONObject buildFilesMap(String str, String str2) {
        Object c2;
        String name;
        File file = new File(str, str2);
        JSONObject jSONObject = new JSONObject();
        File[] listFiles = file.listFiles();
        if (listFiles != null) {
            for (File file2 : listFiles) {
                try {
                    c2 = c(file2);
                } catch (JSONException e) {
                    l9.d().a(e);
                    IronLog.INTERNAL.error(e.toString());
                }
                if (c2 instanceof JSONArray) {
                    name = "files";
                } else if (c2 instanceof JSONObject) {
                    name = file2.getName();
                }
                jSONObject.put(name, c(file2));
            }
        }
        return jSONObject;
    }

    public static JSONObject buildFilesMapOfDirectory(mg mgVar, JSONObject jSONObject) throws Exception {
        String name;
        JSONObject buildFilesMapOfDirectory;
        if (mgVar == null || !mgVar.isDirectory()) {
            return new JSONObject();
        }
        File[] listFiles = mgVar.listFiles();
        if (listFiles == null) {
            return new JSONObject();
        }
        JSONObject jSONObject2 = new JSONObject();
        for (File file : listFiles) {
            mg mgVar2 = new mg(file.getPath());
            if (mgVar2.isFile()) {
                name = mgVar2.getName();
                buildFilesMapOfDirectory = mgVar2.a();
                if (jSONObject.has(name)) {
                    buildFilesMapOfDirectory = SDKUtils.mergeJSONObjects(buildFilesMapOfDirectory, jSONObject.getJSONObject(name));
                }
            } else if (mgVar2.isDirectory()) {
                name = mgVar2.getName();
                buildFilesMapOfDirectory = buildFilesMapOfDirectory(mgVar2, jSONObject);
            }
            jSONObject2.put(name, buildFilesMapOfDirectory);
        }
        return jSONObject2;
    }

    private static File c(Context context) {
        oe f = jl.P().f();
        ls lsVar = b;
        return (lsVar == null || !lsVar.d()) ? f.v(context) : f.k(context);
    }

    private static Object c(File file) {
        File[] listFiles;
        JSONObject jSONObject = new JSONObject();
        JSONArray jSONArray = new JSONArray();
        try {
        } catch (JSONException e) {
            l9.d().a(e);
            IronLog.INTERNAL.error(e.toString());
        }
        if (file.isFile()) {
            jSONArray.put(file.getName());
            return jSONArray;
        }
        for (File file2 : file.listFiles()) {
            if (file2.isDirectory()) {
                jSONObject.put(file2.getName(), c(file2));
            } else {
                jSONArray.put(file2.getName());
                jSONObject.put("files", jSONArray);
            }
        }
        return jSONObject;
    }

    public static void deleteCacheDirectories(Context context) {
        oe f = jl.P().f();
        a(f.e(context));
        a(f.k(context));
    }

    public static synchronized boolean deleteFile(mg mgVar) {
        synchronized (IronSourceStorageUtils.class) {
            if (mgVar.exists()) {
                return mgVar.delete();
            }
            return false;
        }
    }

    public static void deleteFilesDirectories(Context context) {
        oe f = jl.P().f();
        a(f.t(context));
        a(f.v(context));
    }

    public static synchronized boolean deleteFolder(String str) {
        boolean z;
        synchronized (IronSourceStorageUtils.class) {
            File file = new File(str);
            if (deleteFolderContentRecursive(file)) {
                z = file.delete();
            }
        }
        return z;
    }

    public static boolean deleteFolderContentRecursive(File file) {
        File[] listFiles = file.listFiles();
        boolean z = true;
        if (listFiles != null) {
            for (File file2 : listFiles) {
                if (file2.isDirectory()) {
                    z &= deleteFolderContentRecursive(file2);
                }
                if (!file2.delete()) {
                    z = false;
                }
            }
        }
        return z;
    }

    public static void ensurePathSafety(File file, String str) throws Exception {
        ls lsVar = b;
        if (lsVar == null || !lsVar.e()) {
            String canonicalPath = new File(str).getCanonicalPath();
            String canonicalPath2 = file.getCanonicalPath();
            if (!canonicalPath2.startsWith(canonicalPath)) {
                throw new Exception(y8.c.u + canonicalPath2);
            }
        }
    }

    public static String getCachedFilesMap(String str, String str2) {
        JSONObject buildFilesMap = buildFilesMap(str, str2);
        try {
            buildFilesMap.put("path", str2);
        } catch (JSONException e) {
            l9.d().a(e);
            IronLog.INTERNAL.error(e.toString());
        }
        return buildFilesMap.toString();
    }

    public static String getDiskCacheDirPath(Context context) {
        File b2;
        if (a() && SDKUtils.isExternalStorageAvailable() && (b2 = b(context)) != null && b2.canWrite()) {
            c = true;
            return b2.getPath();
        }
        return c(context).getPath();
    }

    public static ArrayList<mg> getFilesInFolderRecursive(mg mgVar) {
        if (mgVar == null || !mgVar.isDirectory()) {
            return new ArrayList<>();
        }
        ArrayList<mg> arrayList = new ArrayList<>();
        File[] listFiles = mgVar.listFiles();
        if (listFiles != null) {
            for (File file : listFiles) {
                mg mgVar2 = new mg(file.getPath());
                if (mgVar2.isDirectory()) {
                    arrayList.addAll(getFilesInFolderRecursive(mgVar2));
                }
                if (mgVar2.isFile()) {
                    arrayList.add(mgVar2);
                }
            }
        }
        return arrayList;
    }

    public static String getNetworkStorageDir(Context context) {
        File b2 = b(new File(getDiskCacheDirPath(context)));
        if (!b2.exists()) {
            b2.mkdir();
        }
        return b2.getPath();
    }

    public static long getTotalSizeOfDir(mg mgVar) {
        long totalSizeOfDir;
        long j = 0;
        if (mgVar != null && mgVar.isDirectory()) {
            File[] listFiles = mgVar.listFiles();
            if (listFiles == null) {
                return 0L;
            }
            for (File file : listFiles) {
                mg mgVar2 = new mg(file.getPath());
                if (mgVar2.isFile()) {
                    totalSizeOfDir = mgVar2.length();
                } else if (mgVar2.isDirectory()) {
                    totalSizeOfDir = getTotalSizeOfDir(mgVar2);
                }
                j += totalSizeOfDir;
            }
        }
        return j;
    }

    public static void initializeCacheDirectory(Context context, ls lsVar) {
        b = lsVar;
        a(context);
    }

    public static boolean isPathExist(String str, String str2) {
        return new File(str, str2).exists();
    }

    public static boolean isUxt() {
        return c;
    }

    public static String makeDir(String str) {
        File file = new File(str);
        if (file.exists() || file.mkdirs()) {
            return file.getPath();
        }
        return null;
    }

    public static String readFile(mg mgVar) throws Exception {
        StringBuilder sb = new StringBuilder();
        BufferedReader bufferedReader = new BufferedReader(new FileReader(mgVar));
        while (true) {
            String readLine = bufferedReader.readLine();
            if (readLine == null) {
                bufferedReader.close();
                return sb.toString();
            }
            sb.append(readLine);
            sb.append('\n');
        }
    }

    public static boolean renameFile(String str, String str2) throws Exception {
        return new File(str).renameTo(new File(str2));
    }

    public static int saveFile(byte[] bArr, String str) throws Exception {
        FileOutputStream fileOutputStream = new FileOutputStream(new File(str));
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
        try {
            byte[] bArr2 = new byte[102400];
            int i = 0;
            while (true) {
                int read = byteArrayInputStream.read(bArr2);
                if (read == -1) {
                    return i;
                }
                fileOutputStream.write(bArr2, 0, read);
                i += read;
            }
        } finally {
            fileOutputStream.close();
            byteArrayInputStream.close();
        }
    }
}
