package com.ifpdos.certinstaller;

import android.app.KeyguardManager;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.UserManager;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class CertInstallerMain extends PreferenceActivity {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private static Map<String, String> f4429e;

    static {
        HashMap map = new HashMap();
        f4429e = map;
        map.put("application/x-x509-ca-cert", "CERT");
        f4429e.put("application/x-x509-user-cert", "CERT");
        f4429e.put("application/x-x509-server-cert", "CERT");
        f4429e.put("application/x-pem-file", "CERT");
        f4429e.put("application/pkix-cert", "CERT");
        f4429e.put("application/x-pkcs12", "PKCS12");
        f4429e.put("application/x-wifi-config", "wifi-config");
    }

    private boolean a(Bundle bundle) {
        return bundle.size() == 1 && bundle.containsKey("certificate_install_usage");
    }

    private boolean b(Bundle bundle) {
        return bundle.size() == 1 && bundle.containsKey("install_as_uid");
    }

    private boolean c(Bundle bundle) {
        return bundle.size() == 1 && bundle.containsKey("name");
    }

    public static void d(AutoCloseable autoCloseable) {
        if (autoCloseable != null) {
            try {
                autoCloseable.close();
            } catch (RuntimeException e3) {
                throw e3;
            } catch (Exception unused) {
            }
        }
    }

    private void e() {
        Intent intentCreateConfirmDeviceCredentialIntent = ((KeyguardManager) getSystemService(KeyguardManager.class)).createConfirmDeviceCredentialIntent(null, null);
        if (intentCreateConfirmDeviceCredentialIntent == null) {
            k();
        } else {
            startActivityForResult(intentCreateConfirmDeviceCredentialIntent, 3);
        }
    }

    private boolean f(Bundle bundle) {
        return bundle != null && bundle.size() == 1 && "ca".equals(bundle.getString("certificate_install_usage"));
    }

    private boolean g(Bundle bundle) {
        return bundle == null || bundle.isEmpty();
    }

    private static byte[] h(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] bArr = new byte[1024];
        int i3 = 0;
        do {
            int i4 = inputStream.read(bArr);
            if (i4 == -1) {
                return byteArrayOutputStream.toByteArray();
            }
            byteArrayOutputStream.write(bArr, 0, i4);
            i3 += i4;
        } while (i3 <= 10485760);
        throw new IOException("Data file exceeded maximum size.");
    }

    private void i(Intent intent) {
        Intent intent2 = new Intent(this, (Class<?>) CertInstaller.class);
        if (intent.getExtras() != null && intent.getExtras().getString("android.intent.extra.REFERRER") != null) {
            Log.v("CertInstaller", String.format("Removing referrer extra with value %s which was not meant to be included", intent.getBundleExtra("android.intent.extra.REFERRER")));
            intent.removeExtra("android.intent.extra.REFERRER");
        }
        intent2.putExtras(intent);
        intent2.putExtra("android.intent.extra.REFERRER", getLaunchedFromPackage());
        startActivityForResult(intent2, 1);
    }

    private void j(String str, Uri uri) {
        if (str == null) {
            str = getContentResolver().getType(uri);
        }
        String str2 = f4429e.get(str);
        if (str2 == null) {
            Log.e("CertInstaller", "Unknown MIME type: " + str + ". " + Log.getStackTraceString(new Throwable()));
            Toast.makeText(this, R.string.invalid_certificate_title, 1).show();
            return;
        }
        if ("wifi-config".equals(str2)) {
            l(str, uri);
            return;
        }
        InputStream inputStreamOpenInputStream = null;
        try {
            try {
                inputStreamOpenInputStream = getContentResolver().openInputStream(uri);
                byte[] bArrH = h(inputStreamOpenInputStream);
                Intent intent = getIntent();
                intent.putExtra(str2, bArrH);
                i(intent);
            } catch (IOException e3) {
                Log.e("CertInstaller", "Failed to read certificate: " + e3);
                Toast.makeText(this, R.string.cert_read_error, 1).show();
            }
        } finally {
            d(inputStreamOpenInputStream);
        }
    }

    private void k() {
        Intent intent;
        String[] strArr = (String[]) f4429e.keySet().toArray(new String[0]);
        if (b.b(this, "com.seewo.easifinder")) {
            intent = new Intent();
            intent.setComponent(new ComponentName("com.seewo.easifinder", "com.seewo.easifinder.FileRetrieverActivity"));
            intent.setPackage("com.seewo.easifinder");
        } else {
            intent = new Intent("android.intent.action.OPEN_DOCUMENT");
        }
        intent.setType("*/*");
        intent.putExtra("android.intent.extra.MIME_TYPES", strArr);
        intent.putExtra("android.provider.extra.SHOW_ADVANCED", true);
        startActivityForResult(intent, 2);
    }

    private void l(String str, Uri uri) {
        Intent intent = new Intent(this, (Class<?>) WiFiInstaller.class);
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(getContentResolver().openInputStream(uri));
            try {
                byte[] bArrH = h(bufferedInputStream);
                intent.putExtra("wifi-config-file", uri.toString());
                intent.putExtra("wifi-config-data", bArrH);
                intent.putExtra("wifi-config", str);
                startActivityForResult(intent, 1);
                bufferedInputStream.close();
            } finally {
            }
        } catch (IOException e3) {
            Log.e("CertInstaller", "Failed to read wifi config: " + e3);
            Toast.makeText(this, R.string.cert_read_error, 1).show();
        }
    }

    @Override // android.preference.PreferenceActivity, android.app.Activity
    protected void onActivityResult(int i3, int i4, Intent intent) {
        if (i3 == 1) {
            setResult(i4);
            finish();
            return;
        }
        if (i3 == 2) {
            if (i4 == -1) {
                j(null, intent.getData());
                return;
            } else {
                finish();
                return;
            }
        }
        if (i3 != 3) {
            Log.w("CertInstaller", "unknown request code: " + i3);
            return;
        }
        if (i4 == -1) {
            k();
        } else {
            finish();
        }
    }

    @Override // android.preference.PreferenceActivity, android.app.Activity
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setResult(0);
        UserManager userManager = (UserManager) getSystemService("user");
        if (userManager.hasUserRestriction("no_config_credentials") || userManager.isGuestUser()) {
            finish();
            return;
        }
        Intent intent = getIntent();
        String action = intent.getAction();
        if (!"android.credentials.INSTALL".equals(action) && !"android.credentials.INSTALL_AS_USER".equals(action)) {
            if ("android.intent.action.VIEW".equals(action)) {
                j(intent.getType(), intent.getData());
                return;
            }
            return;
        }
        Bundle extras = intent.getExtras();
        String className = intent.getComponent().getClassName();
        String str = getPackageName() + ".InstallCertAsUser";
        if (extras != null && !str.equals(className)) {
            extras.remove("install_as_uid");
        }
        if (!g(extras) && !c(extras) && !b(extras) && !a(extras)) {
            i(intent);
        } else if (f(extras)) {
            e();
        } else {
            k();
        }
    }
}
