package com.insecureshop;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import kotlin.Metadata;
import kotlin.TypeCastException;
import kotlin.jvm.internal.Intrinsics;
/* compiled from: ChooserActivity.kt */
@Metadata(bv = {1, 0, 3}, d1 = {"\u0000&\n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0004\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\u0018\u00002\u00020\u0001B\u0005¢\u0006\u0002\u0010\u0002J\u0010\u0010\u0003\u001a\u0004\u0018\u00010\u00042\u0006\u0010\u0005\u001a\u00020\u0006J\u001c\u0010\u0007\u001a\u0004\u0018\u00010\u00062\u0006\u0010\b\u001a\u00020\u00062\b\u0010\t\u001a\u0004\u0018\u00010\u0004H\u0002J\u0012\u0010\n\u001a\u00020\u000b2\b\u0010\f\u001a\u0004\u0018\u00010\rH\u0014¨\u0006\u000e"}, d2 = {"Lcom/insecureshop/ChooserActivity;", "Landroidx/appcompat/app/AppCompatActivity;", "()V", "getFilename", net.gotev.uploadservice.BuildConfig.FLAVOR, "uri", "Landroid/net/Uri;", "makeTempCopy", "fileUri", "original_filename", "onCreate", net.gotev.uploadservice.BuildConfig.FLAVOR, "savedInstanceState", "Landroid/os/Bundle;", "app_release"}, k = 1, mv = {1, 1, 16})
/* loaded from: classes.dex */
public final class ChooserActivity extends AppCompatActivity {
    private HashMap _$_findViewCache;

    public void _$_clearFindViewByIdCache() {
        HashMap hashMap = this._$_findViewCache;
        if (hashMap != null) {
            hashMap.clear();
        }
    }

    public View _$_findCachedViewById(int i) {
        if (this._$_findViewCache == null) {
            this._$_findViewCache = new HashMap();
        }
        View view = (View) this._$_findViewCache.get(Integer.valueOf(i));
        if (view == null) {
            View findViewById = findViewById(i);
            this._$_findViewCache.put(Integer.valueOf(i), findViewById);
            return findViewById;
        }
        return view;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, androidx.activity.ComponentActivity, androidx.core.app.ComponentActivity, android.app.Activity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_chooser);
        Intent intent = getIntent();
        Intrinsics.checkExpressionValueIsNotNull(intent, "intent");
        if (intent.getExtras() != null) {
            Parcelable parcelableExtra = getIntent().getParcelableExtra("android.intent.extra.STREAM");
            if (parcelableExtra == null) {
                throw new TypeCastException("null cannot be cast to non-null type android.net.Uri");
            }
            Uri fromFile = Uri.fromFile(new File(((Uri) parcelableExtra).toString()));
            Intrinsics.checkExpressionValueIsNotNull(fromFile, "Uri.fromFile(File(uri.toString()))");
            makeTempCopy(fromFile, getFilename(fromFile));
        }
    }

    private final Uri makeTempCopy(Uri uri, String str) {
        try {
            StringBuilder sb = new StringBuilder();
            File externalStorageDirectory = Environment.getExternalStorageDirectory();
            Intrinsics.checkExpressionValueIsNotNull(externalStorageDirectory, "Environment.getExternalStorageDirectory()");
            sb.append(externalStorageDirectory.getAbsolutePath());
            sb.append(File.separator);
            sb.append("insecureshop");
            String sb2 = sb.toString();
            File file = new File(sb2);
            if (!file.exists()) {
                file.mkdirs();
            }
            File file2 = new File(sb2, str);
            file2.createNewFile();
            Uri fromFile = Uri.fromFile(file2);
            InputStream openInputStream = getContentResolver().openInputStream(uri);
            OutputStream openOutputStream = getContentResolver().openOutputStream(fromFile);
            byte[] bArr = new byte[8192];
            while (true) {
                Integer valueOf = openInputStream != null ? Integer.valueOf(openInputStream.read(bArr)) : null;
                if (valueOf != null && valueOf.intValue() == -1) {
                }
                if (valueOf != null) {
                    int intValue = valueOf.intValue();
                    if (openOutputStream != null) {
                        openOutputStream.write(bArr, 0, intValue);
                    }
                }
            }
        } catch (Exception e) {
            Log.e("mytag", "error " + e);
            return null;
        }
    }

    public final String getFilename(Uri uri) {
        Intrinsics.checkParameterIsNotNull(uri, "uri");
        String str = null;
        Context context = getApplicationContext();
        String scheme = uri.getScheme();
        if (Intrinsics.areEqual(scheme, "file")) {
            return uri.getLastPathSegment();
        }
        if (Intrinsics.areEqual(scheme, "content")) {
            Intrinsics.checkExpressionValueIsNotNull(context, "context");
            Cursor query = context.getContentResolver().query(uri, new String[]{"_display_name"}, null, null, null);
            if (query == null || query.getCount() == 0) {
                return str;
            }
            int columnIndexOrThrow = query.getColumnIndexOrThrow("_display_name");
            query.moveToFirst();
            return query.getString(columnIndexOrThrow);
        }
        return str;
    }
}
