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 androidx.collection.t0;
import ck.g;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

/* loaded from: classes3.dex */
public final class zzhg {

    /* loaded from: classes3.dex */
    public static class zza {
        private static volatile g zza;

        private zza() {
        }

        public static g zza(Context context) {
            g zza2;
            g gVar = zza;
            if (gVar == null) {
                synchronized (zza.class) {
                    gVar = zza;
                    if (gVar == null) {
                        new zzhg();
                        if (zzhk.zza(Build.TYPE, Build.TAGS)) {
                            if (zzgs.zza() && !context.isDeviceProtectedStorage()) {
                                context = context.createDeviceProtectedStorageContext();
                            }
                            zza2 = zzhg.zza(context);
                        } else {
                            zza2 = g.a();
                        }
                        zza = zza2;
                        gVar = zza2;
                    }
                }
            }
            return gVar;
        }
    }

    private static zzhh zza(Context context, File file) {
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            try {
                t0 t0Var = new t0();
                HashMap hashMap = new HashMap();
                while (true) {
                    String readLine = bufferedReader.readLine();
                    if (readLine != null) {
                        String[] split = readLine.split(" ", 3);
                        if (split.length != 3) {
                            Log.e("HermeticFileOverrides", "Invalid: " + readLine);
                        } else {
                            String zza2 = zza(split[0]);
                            String decode = Uri.decode(zza(split[1]));
                            String str = (String) hashMap.get(split[2]);
                            if (str == null) {
                                String zza3 = zza(split[2]);
                                str = Uri.decode(zza3);
                                if (str.length() < 1024 || str == zza3) {
                                    hashMap.put(zza3, str);
                                }
                            }
                            t0 t0Var2 = (t0) t0Var.get(zza2);
                            if (t0Var2 == null) {
                                t0Var2 = new t0();
                                t0Var.put(zza2, t0Var2);
                            }
                            t0Var2.put(decode, str);
                        }
                    } else {
                        Log.w("HermeticFileOverrides", "Parsed " + String.valueOf(file) + " for Android package " + context.getPackageName());
                        zzgy zzgyVar = new zzgy(t0Var);
                        bufferedReader.close();
                        return zzgyVar;
                    }
                }
            } finally {
            }
        } catch (IOException e10) {
            throw new RuntimeException(e10);
        }
    }

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

    static g zza(Context context) {
        g a10;
        StrictMode.ThreadPolicy allowThreadDiskReads = StrictMode.allowThreadDiskReads();
        try {
            StrictMode.allowThreadDiskWrites();
            g zzb = zzb(context);
            if (zzb.c()) {
                a10 = g.d(zza(context, (File) zzb.b()));
            } else {
                a10 = g.a();
            }
            return a10;
        } finally {
            StrictMode.setThreadPolicy(allowThreadDiskReads);
        }
    }

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