package com.ss.android.ugc.effectmanager.common.utils;

import X.0HH;
import X.2FV;
import X.2He;
import X.C08347s1;
import X.C28338jA;
import android.app.ActivityManager;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DeviceUtil {
    public static final Map<String, Object> cachedMap = new ConcurrentHashMap();

    public static class MemoryInfo {
        public long availableSize;
        public long totalSize;

        static {
            Covode.recordClassIndex(125311);
        }

        public long getAvailableSize() {
            return this.availableSize;
        }

        public long getTotalSize() {
            return this.totalSize;
        }
    }

    static {
        Covode.recordClassIndex(125310);
    }

    public static String getCpuModel() {
        Map<String, Object> map = cachedMap;
        Object obj = map.get("cache_key_cpu_model");
        if (obj != null) {
            return (String) obj;
        }
        String cpuModelInternal = getCpuModelInternal();
        map.put("cache_key_cpu_model", cpuModelInternal);
        return cpuModelInternal;
    }

    public static synchronized String getCpuModelInternal() {
        BufferedReader bufferedReader;
        synchronized (DeviceUtil.class) {
            MethodCollector.i(14372);
            BufferedReader bufferedReader2 = null;
            try {
                if (new File("/proc/cpuinfo").exists()) {
                    bufferedReader = new BufferedReader(new FileReader("/proc/cpuinfo"));
                    while (true) {
                        try {
                            String readLine = bufferedReader.readLine();
                            if (readLine == null) {
                                bufferedReader2 = bufferedReader;
                                break;
                            } else if (readLine.contains(":")) {
                                String[] split = readLine.split(":");
                                if (split[0].contains("Hardware")) {
                                    String str = split[1];
                                    closeSafety(bufferedReader);
                                    MethodCollector.o(14372);
                                    return str;
                                }
                            }
                        } catch (Exception e) {
                            e = e;
                            try {
                                0HH.LIZ(e);
                                closeSafety(bufferedReader);
                                String str2 = Build.HARDWARE;
                                MethodCollector.o(14372);
                                return str2;
                            } catch (Throwable th) {
                                th = th;
                                closeSafety(bufferedReader);
                                MethodCollector.o(14372);
                                throw th;
                            }
                        }
                    }
                }
                closeSafety(bufferedReader2);
            } catch (Exception e2) {
                e = e2;
                bufferedReader = null;
                0HH.LIZ(e);
                closeSafety(bufferedReader);
                String str22 = Build.HARDWARE;
                MethodCollector.o(14372);
                return str22;
            } catch (Throwable th2) {
                th = th2;
                bufferedReader = null;
                closeSafety(bufferedReader);
                MethodCollector.o(14372);
                throw th;
            }
            String str222 = Build.HARDWARE;
            MethodCollector.o(14372);
            return str222;
        }
    }

    public static void closeSafety(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (Exception e) {
                0HH.LIZ(e);
            }
        }
    }

    public static MemoryInfo getMemoryInfo(Context context) {
        Map<String, Object> map = cachedMap;
        Object obj = map.get("cache_key_memory_info");
        if (obj != null) {
            return (MemoryInfo) obj;
        }
        MemoryInfo memoryInfoInternal = getMemoryInfoInternal(context);
        map.put("cache_key_memory_info", memoryInfoInternal);
        return memoryInfoInternal;
    }

    public static MemoryInfo getMemoryInfoInternal(Context context) {
        MemoryInfo memoryInfo = new MemoryInfo();
        memoryInfo.totalSize = getTotalMemory(context);
        memoryInfo.availableSize = getAvailableMemory(context);
        return memoryInfo;
    }

    public static long getAvailableMemory(Context context) {
        long j;
        FileInputStream fileInputStream;
        MethodCollector.i(12746);
        try {
            ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
            ActivityManager activityManager = (ActivityManager) INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_utils_DeviceUtil_com_ss_android_ugc_aweme_lancet_GetSystemServiceLancet_getSystemService(context, "activity");
            if (activityManager != null) {
                activityManager.getMemoryInfo(memoryInfo);
                j = memoryInfo.availMem;
                if (j == -1) {
                    FileInputStream fileInputStream2 = null;
                    fileInputStream = new FileInputStream("/proc/meminfo");
                    j = ((long) parseFileForValue("MemAvailable", fileInputStream)) * 1024;
                    closeSafety(fileInputStream);
                }
                MethodCollector.o(12746);
                return j;
            }
        } catch (Exception unused) {
        }
        j = -1;
        FileInputStream fileInputStream22 = null;
        try {
            fileInputStream = new FileInputStream("/proc/meminfo");
            try {
                j = ((long) parseFileForValue("MemAvailable", fileInputStream)) * 1024;
                closeSafety(fileInputStream);
            } catch (Exception unused2) {
                fileInputStream22 = fileInputStream;
            } catch (Throwable th) {
                th = th;
                closeSafety(fileInputStream);
                MethodCollector.o(12746);
                throw th;
            }
        } catch (Exception unused3) {
            closeSafety(fileInputStream22);
            MethodCollector.o(12746);
            return j;
        } catch (Throwable th2) {
            th = th2;
            fileInputStream = null;
            closeSafety(fileInputStream);
            MethodCollector.o(12746);
            throw th;
        }
        MethodCollector.o(12746);
        return j;
    }

    /* JADX WARNING: Removed duplicated region for block: B:13:0x0030 A[Catch:{ Exception -> 0x0040, all -> 0x0047 }] */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static long getTotalMemory(android.content.Context r7) {
        /*
            r6 = 12271(0x2fef, float:1.7195E-41)
            com.bytedance.frameworks.apm.trace.MethodCollector.i(r6)
            r4 = -1
            android.app.ActivityManager$MemoryInfo r1 = new android.app.ActivityManager$MemoryInfo     // Catch:{ Exception -> 0x0020 }
            r1.<init>()     // Catch:{ Exception -> 0x0020 }
            java.lang.String r0 = "activity"
            java.lang.Object r0 = INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_utils_DeviceUtil_com_ss_android_ugc_aweme_lancet_GetSystemServiceLancet_getSystemService(r7, r0)     // Catch:{ Exception -> 0x0020 }
            android.app.ActivityManager r0 = (android.app.ActivityManager) r0     // Catch:{ Exception -> 0x0020 }
            if (r0 == 0) goto L_0x0020
            r0.getMemoryInfo(r1)     // Catch:{ Exception -> 0x0020 }
            long r2 = r1.totalMem     // Catch:{ Exception -> 0x0020 }
            int r0 = (r2 > r4 ? 1 : (r2 == r4 ? 0 : -1))
            if (r0 != 0) goto L_0x0043
            goto L_0x0022
        L_0x0020:
            r2 = -1
        L_0x0022:
            r5 = 0
            java.lang.String r1 = "/proc/meminfo"
            java.io.File r0 = new java.io.File     // Catch:{ Exception -> 0x0040, all -> 0x0047 }
            r0.<init>(r1)     // Catch:{ Exception -> 0x0040, all -> 0x0047 }
            boolean r0 = r0.exists()     // Catch:{ Exception -> 0x0040, all -> 0x0047 }
            if (r0 == 0) goto L_0x0040
            java.io.FileInputStream r4 = new java.io.FileInputStream     // Catch:{ Exception -> 0x0040, all -> 0x0047 }
            r4.<init>(r1)     // Catch:{ Exception -> 0x0040, all -> 0x0047 }
            java.lang.String r0 = "MemTotal"
            int r0 = parseFileForValue(r0, r4)     // Catch:{ Exception -> 0x003f, all -> 0x004a }
            long r2 = (long) r0     // Catch:{ Exception -> 0x003f, all -> 0x004a }
            r0 = 1024(0x400, double:5.06E-321)
            long r2 = r2 * r0
        L_0x003f:
            r5 = r4
        L_0x0040:
            closeSafety(r5)
        L_0x0043:
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r6)
            return r2
        L_0x0047:
            r0 = move-exception
            r4 = r5
            goto L_0x004b
        L_0x004a:
            r0 = move-exception
        L_0x004b:
            closeSafety(r4)
            com.bytedance.frameworks.apm.trace.MethodCollector.o(r6)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.effectmanager.common.utils.DeviceUtil.getTotalMemory(android.content.Context):long");
    }

    public static int extractValue(byte[] bArr, int i) {
        while (i < bArr.length && bArr[i] != 10) {
            if (Character.isDigit(bArr[i])) {
                int i2 = i + 1;
                while (i2 < bArr.length && Character.isDigit(bArr[i2])) {
                    i2++;
                }
                return Integer.parseInt(new String(bArr, 0, i, i2 - i));
            }
            i++;
        }
        return -1;
    }

    public static Object INVOKEVIRTUAL_com_ss_android_ugc_effectmanager_common_utils_DeviceUtil_com_ss_android_ugc_aweme_lancet_GetSystemServiceLancet_getSystemService(Context context, String str) {
        Object obj;
        MethodCollector.i(12486);
        if (Build.VERSION.SDK_INT > 27 || !"clipboard".equals(str)) {
            if (!2FV.LIZIZ && "connectivity".equals(str)) {
                try {
                    new C08347s1().LIZ();
                    2FV.LIZIZ = true;
                    obj = context.getSystemService(str);
                } catch (Throwable unused) {
                }
            }
            obj = context.getSystemService(str);
        } else if (2FV.LIZ) {
            synchronized (ClipboardManager.class) {
                try {
                    obj = context.getSystemService(str);
                    if (Thread.currentThread() != Looper.getMainLooper().getThread()) {
                        Field declaredField = ClipboardManager.class.getDeclaredField("mHandler");
                        declaredField.setAccessible(true);
                        declaredField.set(obj, new 2He((Handler) declaredField.get(obj)));
                    }
                } catch (Exception e) {
                    C28338jA.LIZ(e, "ClipboardManager Handler Reflect Fail");
                } catch (Throwable th) {
                    MethodCollector.o(12486);
                    throw th;
                }
                2FV.LIZ = false;
            }
        } else {
            obj = context.getSystemService(str);
        }
        MethodCollector.o(12486);
        return obj;
    }

    public static int parseFileForValue(String str, FileInputStream fileInputStream) {
        MethodCollector.i(12960);
        byte[] bArr = new byte[1024];
        try {
            int read = fileInputStream.read(bArr);
            int i = 0;
            while (i < read) {
                if (bArr[i] == 10 || i == 0) {
                    if (bArr[i] == 10) {
                        i++;
                    }
                    int i2 = i;
                    while (i2 < read) {
                        int i3 = i2 - i;
                        if (bArr[i2] != str.charAt(i3)) {
                            continue;
                            break;
                        } else if (i3 == str.length() - 1) {
                            int extractValue = extractValue(bArr, i2);
                            MethodCollector.o(12960);
                            return extractValue;
                        } else {
                            i2++;
                        }
                    }
                    continue;
                }
                i++;
            }
        } catch (IOException | NumberFormatException unused) {
        }
        MethodCollector.o(12960);
        return -1;
    }
}
