package com.google.android.gms.internal.measurement;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.StrictMode;
import android.util.Log;
import defpackage.sma;
import defpackage.xx6;
import defpackage.zs8;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

/* JADX INFO: loaded from: classes2.dex */
public final class zzhg {

    public static class zza {
        private static volatile xx6<zzhh> zza;

        private zza() {
        }

        public static xx6<zzhh> zza(Context context) {
            xx6<zzhh> xx6Var;
            xx6<zzhh> xx6VarZza;
            xx6<zzhh> xx6Var2 = zza;
            if (xx6Var2 != null) {
                return xx6Var2;
            }
            synchronized (zza.class) {
                try {
                    xx6Var = zza;
                    if (xx6Var == null) {
                        new zzhg();
                        if (zzhk.zza(Build.TYPE, Build.TAGS)) {
                            if (zzgs.zza() && !context.isDeviceProtectedStorage()) {
                                context = context.createDeviceProtectedStorageContext();
                            }
                            xx6VarZza = zzhg.zza(context);
                        } else {
                            xx6VarZza = xx6.a();
                        }
                        xx6Var = xx6VarZza;
                        zza = xx6Var;
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            return xx6Var;
        }
    }

    private static zzhh zza(Context context, File file) {
        BufferedReader bufferedReader;
        zs8 zs8Var;
        HashMap map;
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            try {
                zs8Var = new zs8(0);
                map = new HashMap();
            } finally {
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        while (true) {
            String line = bufferedReader.readLine();
            if (line == null) {
                Log.w("HermeticFileOverrides", "Parsed " + String.valueOf(file) + " for Android package " + context.getPackageName());
                zzgy zzgyVar = new zzgy(zs8Var);
                bufferedReader.close();
                return zzgyVar;
            }
            String[] strArrSplit = line.split(" ", 3);
            if (strArrSplit.length != 3) {
                Log.e("HermeticFileOverrides", "Invalid: " + line);
            } else {
                String strZza = zza(strArrSplit[0]);
                String strDecode = Uri.decode(zza(strArrSplit[1]));
                String strDecode2 = (String) map.get(strArrSplit[2]);
                if (strDecode2 == null) {
                    String strZza2 = zza(strArrSplit[2]);
                    strDecode2 = Uri.decode(strZza2);
                    if (strDecode2.length() < 1024 || strDecode2 == strZza2) {
                        map.put(strZza2, strDecode2);
                    }
                }
                zs8 zs8Var2 = (zs8) zs8Var.get(strZza);
                if (zs8Var2 == null) {
                    zs8Var2 = new zs8(0);
                    zs8Var.put(strZza, zs8Var2);
                }
                zs8Var2.put(strDecode, strDecode2);
            }
            throw new RuntimeException(e);
        }
    }

    private static xx6<File> zzb(Context context) {
        try {
            File file = new File(context.getDir("phenotype_hermetic", 0), "overrides.txt");
            return file.exists() ? xx6.d(file) : xx6.a();
        } catch (RuntimeException e) {
            Log.e("HermeticFileOverrides", "no data dir", e);
            return xx6.a();
        }
    }

    @sma
    public static xx6<zzhh> zza(Context context) {
        xx6<zzhh> xx6VarA;
        StrictMode.ThreadPolicy threadPolicyAllowThreadDiskReads = StrictMode.allowThreadDiskReads();
        try {
            StrictMode.allowThreadDiskWrites();
            xx6<File> xx6VarZzb = zzb(context);
            if (xx6VarZzb.c()) {
                xx6VarA = xx6.d(zza(context, (File) xx6VarZzb.b()));
            } else {
                xx6VarA = xx6.a();
            }
            StrictMode.setThreadPolicy(threadPolicyAllowThreadDiskReads);
            return xx6VarA;
        } catch (Throwable th) {
            StrictMode.setThreadPolicy(threadPolicyAllowThreadDiskReads);
            throw th;
        }
    }

    private static final String zza(String str) {
        return new String(str);
    }
}
