package org.cocos2dx.lib; import android.util.Log; import com.lzy.okgo.cookie.SerializableCookie; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.URI; import java.net.URISyntaxException; import java.util.LinkedList; import java.util.Map; import java.util.Queue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class Cocos2dxDownloader { private static ConcurrentHashMap<String, Boolean> _resumingSupport = new ConcurrentHashMap<>(); private int _countOfMaxProcessingTasks; private OkHttpClient _httpClient = null; private int _id; private int _runningTaskCount = 0; private ConcurrentHashMap<Integer, Call> _taskMap = new ConcurrentHashMap<>(); private Queue<Runnable> _taskQueue = new LinkedList(); private String _tempFileNameSuffix; public static void abort(Cocos2dxDownloader cocos2dxDownloader, final int i) { Cocos2dxHelper.getActivity().runOnUiThread(new Runnable() { /* class org.cocos2dx.lib.Cocos2dxDownloader.AnonymousClass4 */ public void run() { for (Map.Entry entry : Cocos2dxDownloader.this._taskMap.entrySet()) { Object key = entry.getKey(); Call call = (Call) entry.getValue(); if (call != null && Integer.parseInt(key.toString()) == i) { call.cancel(); Cocos2dxDownloader.this._taskMap.remove(Integer.valueOf(i)); Cocos2dxDownloader.this.runNextTaskIfExists(); return; } } } }); } public static void cancelAllRequests(Cocos2dxDownloader cocos2dxDownloader) { Cocos2dxHelper.getActivity().runOnUiThread(new Runnable() { /* class org.cocos2dx.lib.Cocos2dxDownloader.AnonymousClass5 */ public void run() { for (Map.Entry entry : Cocos2dxDownloader.this._taskMap.entrySet()) { Call call = (Call) entry.getValue(); if (call != null) { call.cancel(); } } } }); } public static Cocos2dxDownloader createDownloader(int i, int i2, String str, int i3) { Cocos2dxDownloader cocos2dxDownloader = new Cocos2dxDownloader(); cocos2dxDownloader._id = i; cocos2dxDownloader._httpClient = (i2 > 0 ? new OkHttpClient().newBuilder().followRedirects(true).followSslRedirects(true).callTimeout((long) i2, TimeUnit.SECONDS) : new OkHttpClient().newBuilder().followRedirects(true).followSslRedirects(true)).build(); cocos2dxDownloader._tempFileNameSuffix = str; cocos2dxDownloader._countOfMaxProcessingTasks = i3; return cocos2dxDownloader; } public static void createTask(final Cocos2dxDownloader cocos2dxDownloader, final int i, final String str, final String str2, final String[] strArr) { cocos2dxDownloader.enqueueTask(new Runnable() { /* class org.cocos2dx.lib.Cocos2dxDownloader.AnonymousClass3 */ /* renamed from: a reason: collision with root package name */ String f3770a = null; String b = null; File c = null; File d = null; long e = 0; /* JADX WARNING: Removed duplicated region for block: B:41:0x0138 */ /* JADX WARNING: Removed duplicated region for block: B:42:0x0154 */ public void run() { Call call; if (str2.length() > 0) { try { Log.e(SerializableCookie.DOMAIN, "domain============= " + str); this.f3770a = new URI("https://shehona.cn/creator/344qp/version.manifest").getHost(); this.c = new File(str2 + cocos2dxDownloader._tempFileNameSuffix); if (!this.c.isDirectory()) { File parentFile = this.c.getParentFile(); if (parentFile.isDirectory() || parentFile.mkdirs()) { this.d = new File(str2); if (!this.d.isDirectory()) { long length = this.c.length(); this.b = this.f3770a.startsWith("www.") ? this.f3770a.substring(4) : this.f3770a; if (length > 0) { if (!Cocos2dxDownloader._resumingSupport.containsKey(this.b) || !((Boolean) Cocos2dxDownloader._resumingSupport.get(this.b)).booleanValue()) { try { PrintWriter printWriter = new PrintWriter(this.c); printWriter.print(""); printWriter.close(); } catch (FileNotFoundException unused) { } } else { this.e = length; } } } } } } catch (URISyntaxException e2) { e2.printStackTrace(); } catch (NullPointerException e3) { e3.printStackTrace(); } call = null; if (call != null) { final String str = "Can't create DownloadTask for " + str; Cocos2dxHelper.runOnGLThread(new Runnable() { /* class org.cocos2dx.lib.Cocos2dxDownloader.AnonymousClass3.AnonymousClass2 */ public void run() { cocos2dxDownloader.nativeOnFinish(cocos2dxDownloader._id, i, 0, str, null); } }); return; } cocos2dxDownloader._taskMap.put(Integer.valueOf(i), call); return; } Request.Builder url = new Request.Builder().url(str); for (int i2 = 0; i2 < strArr.length / 2; i2++) { int i3 = i2 * 2; url.addHeader(strArr[i3], strArr[i3 + 1]); } if (this.e > 0) { url.addHeader("RANGE", "bytes=" + this.e + "-"); } call = cocos2dxDownloader._httpClient.newCall(url.build()); call.enqueue(new Callback() { /* class org.cocos2dx.lib.Cocos2dxDownloader.AnonymousClass3.AnonymousClass1 */ @Override // okhttp3.Callback public void onFailure(Call call, IOException iOException) { cocos2dxDownloader.onFinish(i, 0, iOException.toString(), null); } /* JADX WARNING: Removed duplicated region for block: B:43:0x0102 A[Catch:{ IOException -> 0x0122 }] */ /* JADX WARNING: Removed duplicated region for block: B:44:0x0116 A[Catch:{ IOException -> 0x0122 }] */ /* JADX WARNING: Removed duplicated region for block: B:76:0x01bc */ /* JADX WARNING: Removed duplicated region for block: B:79:0x01c4 */ /* JADX WARNING: Removed duplicated region for block: B:84:0x01d8 A[SYNTHETIC, Splitter:B:84:0x01d8] */ /* JADX WARNING: Removed duplicated region for block: B:89:0x01e0 A[Catch:{ IOException -> 0x01dc }] */ /* JADX WARNING: Removed duplicated region for block: B:97:? A[RETURN, SYNTHETIC] */ @Override // okhttp3.Callback public void onResponse(Call call, Response response) throws IOException { InputStream inputStream; Throwable th; FileOutputStream fileOutputStream; Throwable th2; IOException e; FileOutputStream fileOutputStream2; String str; ConcurrentHashMap concurrentHashMap; String str2; boolean z; byte[] bArr = new byte[4096]; FileOutputStream fileOutputStream3 = null; try { if (response.code() >= 200) { if (response.code() <= 206) { long contentLength = response.body().contentLength(); if (str2.length() > 0 && !Cocos2dxDownloader._resumingSupport.containsKey(AnonymousClass3.this.b)) { if (contentLength > 0) { concurrentHashMap = Cocos2dxDownloader._resumingSupport; str2 = AnonymousClass3.this.b; z = true; } else { concurrentHashMap = Cocos2dxDownloader._resumingSupport; str2 = AnonymousClass3.this.b; z = false; } concurrentHashMap.put(str2, z); } long j = AnonymousClass3.this.e; inputStream = response.body().byteStream(); try { if (str2.length() > 0) { FileOutputStream fileOutputStream4 = AnonymousClass3.this.e > 0 ? new FileOutputStream(AnonymousClass3.this.c, true) : new FileOutputStream(AnonymousClass3.this.c, false); while (true) { try { int read = inputStream.read(bArr); if (read == -1) { break; } long j2 = (long) read; long j3 = j + j2; fileOutputStream4.write(bArr, 0, read); fileOutputStream = fileOutputStream4; try { cocos2dxDownloader.onProgress(i, j2, j3, contentLength); j = j3; fileOutputStream4 = fileOutputStream; } catch (IOException e2) { e = e2; try { e.printStackTrace(); cocos2dxDownloader.onFinish(i, 0, e.toString(), null); if (inputStream != null) { inputStream.close(); } if (fileOutputStream == null) { fileOutputStream.close(); return; } return; } catch (Throwable th3) { th2 = th3; th = th2; fileOutputStream3 = fileOutputStream; if (inputStream != null) { try { inputStream.close(); } catch (IOException e3) { Log.e("Cocos2dxDownloader", e3.toString()); throw th; } } if (fileOutputStream3 != null) { fileOutputStream3.close(); } throw th; } } } catch (IOException e4) { e = e4; fileOutputStream = fileOutputStream4; e.printStackTrace(); cocos2dxDownloader.onFinish(i, 0, e.toString(), null); if (inputStream != null) { } if (fileOutputStream == null) { } } catch (Throwable th4) { th2 = th4; fileOutputStream = fileOutputStream4; th = th2; fileOutputStream3 = fileOutputStream; if (inputStream != null) { } if (fileOutputStream3 != null) { } throw th; } } fileOutputStream2 = fileOutputStream4; fileOutputStream2.flush(); if (AnonymousClass3.this.d.exists()) { if (AnonymousClass3.this.d.isDirectory()) { str = null; if (str == null) { cocos2dxDownloader.onFinish(i, 0, null, null); cocos2dxDownloader.runNextTaskIfExists(); } else { cocos2dxDownloader.onFinish(i, 0, str, null); } } else if (!AnonymousClass3.this.d.delete()) { str = "Can't remove old file:" + AnonymousClass3.this.d.getAbsolutePath(); if (str == null) { } } } AnonymousClass3.this.c.renameTo(AnonymousClass3.this.d); str = null; if (str == null) { } } else { int i = -1; ByteArrayOutputStream byteArrayOutputStream = contentLength > 0 ? new ByteArrayOutputStream((int) contentLength) : new ByteArrayOutputStream(4096); while (true) { int read2 = inputStream.read(bArr); if (read2 == i) { break; } long j4 = (long) read2; long j5 = j + j4; byteArrayOutputStream.write(bArr, 0, read2); cocos2dxDownloader.onProgress(i, j4, j5, contentLength); j = j5; i = i; } cocos2dxDownloader.onFinish(i, 0, null, byteArrayOutputStream.toByteArray()); cocos2dxDownloader.runNextTaskIfExists(); fileOutputStream2 = null; } if (inputStream != null) { try { inputStream.close(); } catch (IOException e5) { Log.e("Cocos2dxDownloader", e5.toString()); return; } } if (fileOutputStream2 != null) { fileOutputStream2.close(); return; } return; } catch (IOException e6) { e = e6; fileOutputStream = null; e.printStackTrace(); cocos2dxDownloader.onFinish(i, 0, e.toString(), null); if (inputStream != null) { } if (fileOutputStream == null) { } } catch (Throwable th5) { th = th5; if (inputStream != null) { } if (fileOutputStream3 != null) { } throw th; } } } cocos2dxDownloader.onFinish(i, -2, response.message(), null); } catch (IOException e7) { e = e7; inputStream = null; fileOutputStream = null; e.printStackTrace(); cocos2dxDownloader.onFinish(i, 0, e.toString(), null); if (inputStream != null) { } if (fileOutputStream == null) { } } catch (Throwable th6) { th = th6; inputStream = null; if (inputStream != null) { } if (fileOutputStream3 != null) { } throw th; } } }); if (call != null) { } } }); } private void enqueueTask(Runnable runnable) { synchronized (this._taskQueue) { if (this._runningTaskCount < this._countOfMaxProcessingTasks) { Cocos2dxHelper.getActivity().runOnUiThread(runnable); this._runningTaskCount++; } else { this._taskQueue.add(runnable); } } } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void onFinish(final int i, final int i2, final String str, final byte[] bArr) { if (this._taskMap.get(Integer.valueOf(i)) != null) { this._taskMap.remove(Integer.valueOf(i)); this._runningTaskCount--; Cocos2dxHelper.runOnGLThread(new Runnable() { /* class org.cocos2dx.lib.Cocos2dxDownloader.AnonymousClass2 */ public void run() { Cocos2dxDownloader.this.nativeOnFinish(Cocos2dxDownloader.this._id, i, i2, str, bArr); } }); runNextTaskIfExists(); } } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void onProgress(final int i, final long j, final long j2, final long j3) { Cocos2dxHelper.runOnGLThread(new Runnable() { /* class org.cocos2dx.lib.Cocos2dxDownloader.AnonymousClass1 */ public void run() { Cocos2dxDownloader.this.nativeOnProgress(Cocos2dxDownloader.this._id, i, j, j2, j3); } }); } /* access modifiers changed from: private */ /* access modifiers changed from: public */ private void runNextTaskIfExists() { synchronized (this._taskQueue) { while (this._runningTaskCount < this._countOfMaxProcessingTasks && this._taskQueue.size() > 0) { Cocos2dxHelper.getActivity().runOnUiThread(this._taskQueue.poll()); this._runningTaskCount++; } } } /* access modifiers changed from: package-private */ public native void nativeOnFinish(int i, int i2, int i3, String str, byte[] bArr); /* access modifiers changed from: package-private */ public native void nativeOnProgress(int i, int i2, long j, long j2, long j3); }