package a0;

import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;
import java.io.IOException;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class s extends n implements Handler.Callback {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private Uri f2014b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private f f2015c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private u f2016d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private boolean f2017e;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final Handler f2019g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private final HandlerThread f2020h;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private boolean f2018f = true;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private IOException f2021i = null;

    public s(String str, Uri uri, Map map) {
        this.f2014b = uri;
        if ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme()) || "android.resource".equals(uri.getScheme())) {
            this.f2015c = new k(str, null, null, 8000, 8000, true);
            if (map != null) {
                for (Map.Entry entry : map.entrySet()) {
                    Log.i("NetworkFDS", "setRequestProperty key[" + ((String) entry.getKey()) + "] value[" + ((String) entry.getValue()) + "]");
                    ((k) this.f2015c).i((String) entry.getKey(), (String) entry.getValue());
                }
            }
        } else if ("ftp".equals(uri.getScheme())) {
            this.f2015c = new o(null);
        } else {
            Log.e("NetworkFDS", "unKnown NetworkFrameworkDataSource");
        }
        b0.x xVar = new b0.x("NetworkFrameworkDataSource:Handler", -16);
        this.f2020h = xVar;
        xVar.start();
        this.f2019g = new Handler(xVar.getLooper(), this);
    }

    private synchronized void i() {
        while (!this.f2017e) {
            try {
                wait();
            } catch (InterruptedException unused) {
                Thread.currentThread().interrupt();
            }
        }
    }

    private void k() {
        try {
            long jMaxMemory = (Runtime.getRuntime().maxMemory() * 3) / 4;
            long jMin = Math.min(jMaxMemory, 62914560L);
            Log.i("NetworkFDS", "dalvikMax:" + jMaxMemory + ", ReadbackableDataCache size:" + jMin);
            u uVar = new u(this.f2014b, this.f2015c, (int) jMin);
            this.f2016d = uVar;
            uVar.c();
        } catch (IOException e2) {
            this.f2018f = false;
            this.f2021i = e2;
            Log.e("NetworkFDS", "new ReadbackableDataCache Fail. " + e2.toString());
        }
        synchronized (this) {
            this.f2017e = true;
            notifyAll();
        }
    }

    private void m() {
        HandlerThread handlerThread = this.f2020h;
        if (handlerThread != null) {
            handlerThread.quit();
        }
        u uVar = this.f2016d;
        if (uVar != null) {
            uVar.e();
        }
        this.f2021i = null;
    }

    @Override // a0.n
    public void a() {
        this.f2019g.sendEmptyMessage(2);
    }

    @Override // a0.n
    public long c() {
        return this.f2016d.b();
    }

    @Override // a0.n
    public int e(long j2, byte[] bArr, int i2, int i3) {
        try {
            return this.f2016d.d(j2, bArr, i2, i3);
        } catch (IOException e2) {
            Log.e("NetworkFDS", "readv23 error. " + e2.toString());
            return -1;
        }
    }

    protected void finalize() throws Throwable {
        try {
            m();
        } finally {
            super.finalize();
        }
    }

    @Override // android.os.Handler.Callback
    public boolean handleMessage(Message message) {
        try {
            int i2 = message.what;
            if (i2 == 1) {
                k();
                return true;
            }
            if (i2 != 2) {
                return false;
            }
            m();
            return true;
        } catch (IOException e2) {
            Log.e("NetworkFDS", "Prepare internal error: ", e2);
            this.f2021i = e2;
            return true;
        } catch (RuntimeException e3) {
            Log.e("NetworkFDS", "Internal runtime error: ", e3);
            return true;
        }
    }

    public void j() throws IOException {
        this.f2019g.sendEmptyMessage(1);
        i();
        if (this.f2018f) {
            return;
        }
        if (this.f2021i == null) {
            throw new IOException("NetworkFrameworkDataSource prepare fail");
        }
        Log.e("NetworkFDS", "throw exception");
        throw new IOException(this.f2021i);
    }
}
