-
-
Save shurup312/837c303e381f35ca7e60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tap4fun.spartanwar.utils.network; | |
import android.os.Handler; | |
import com.tap4fun.spartanwar.GameActivity; | |
import java.io.BufferedOutputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.UnsupportedEncodingException; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.ProtocolException; | |
import java.net.URL; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
public class ExtHttpConnection | |
{ | |
private static m p; | |
private HttpURLConnection a; | |
private volatile boolean b = false; | |
private volatile boolean c = false; | |
private String connectionName = ""; | |
private p d = p.a; | |
private o e = o.a; | |
private String f; | |
private int g = 0; | |
private boolean h = false; | |
private boolean i = false; | |
private boolean j = false; | |
private String k = ""; | |
private int l = 0; | |
private Timer m = null; | |
private TimerTask n = null; | |
private volatile boolean o = false; | |
public ExtHttpConnection(String paramString1, String paramString2) | |
{ | |
String str = a(paramString1); | |
try | |
{ | |
this.a = ((HttpURLConnection)new URL(str).openConnection()); | |
this.a.setUseCaches(false); | |
p.b(this.a); | |
this.connectionName = paramString2; | |
return; | |
} | |
catch (MalformedURLException localMalformedURLException) | |
{ | |
com.tap4fun.spartanwar.utils.system.a.a("ExtHttpConnection", localMalformedURLException); | |
jniLuaLError(localMalformedURLException.getMessage()); | |
return; | |
} | |
catch (IOException localIOException) | |
{ | |
com.tap4fun.spartanwar.utils.system.a.a("ExtHttpConnection", localIOException); | |
jniLuaLError(localIOException.getMessage()); | |
} | |
} | |
private static String a(String paramString) | |
{ | |
if ((!paramString.startsWith("http://")) && (!paramString.startsWith("https://")) && (!paramString.startsWith("market://"))) | |
paramString = String.format("http://%s", new Object[] { paramString }); | |
return paramString; | |
} | |
public static void a() | |
{ | |
p = new m(); | |
initJNI(); | |
} | |
private void a(int paramInt1, int paramInt2) | |
{ | |
if (paramInt2 > 0); | |
for (float f1 = paramInt1 * 100 / paramInt2; ; f1 = 0.0F) | |
{ | |
GameActivity.b.c.a(new h(this, f1)); | |
return; | |
} | |
} | |
private void a(int paramInt, InputStream paramInputStream) | |
{ | |
ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream(); | |
byte[] arrayOfByte = new byte[1024]; | |
while (true) | |
{ | |
int i1 = paramInputStream.read(arrayOfByte); | |
if ((i1 == -1) || (this.o)) | |
break; | |
localByteArrayOutputStream.write(arrayOfByte, 0, i1); | |
if (this.i) | |
a(localByteArrayOutputStream.toByteArray(), paramInt); | |
} | |
if (!this.o) | |
{ | |
f(); | |
if (this.j) | |
{ | |
if (this.e.equals(o.a)) | |
b(this.a.getHeaderField("Content-Type")); | |
a(localByteArrayOutputStream.toByteArray()); | |
} | |
} | |
localByteArrayOutputStream.close(); | |
paramInputStream.close(); | |
} | |
private void a(byte[] paramArrayOfByte) | |
{ | |
if (paramArrayOfByte.length == 0) | |
return; | |
switch (e.b[this.e.ordinal()]) | |
{ | |
default: | |
e("Error format"); | |
return; | |
case 1: | |
String str2 = NetUtils.aesDefaultDecrypt(paramArrayOfByte); | |
if (str2.length() == 0) | |
{ | |
e("Connection get null json, maybe AES Decryption Error"); | |
return; | |
} | |
c(str2); | |
return; | |
case 2: | |
try | |
{ | |
str1 = new String(paramArrayOfByte, "UTF-8"); | |
d(str1); | |
return; | |
} | |
catch (UnsupportedEncodingException localUnsupportedEncodingException) | |
{ | |
while (true) | |
{ | |
com.tap4fun.spartanwar.utils.system.a.e("ExtHttpConnection", "UTF-8 encoding is not supported, will use ISO-Latin-1 or ASCII"); | |
String str1 = new String(paramArrayOfByte); | |
} | |
} | |
case 3: | |
} | |
b(paramArrayOfByte); | |
} | |
private void a(byte[] paramArrayOfByte, int paramInt) | |
{ | |
if (paramInt > 0); | |
for (float f1 = 100 * paramArrayOfByte.length / paramInt; ; f1 = 0.0F) | |
{ | |
GameActivity.b.c.a(new g(this, f1, paramArrayOfByte)); | |
return; | |
} | |
} | |
public static void b() | |
{ | |
GameActivity.b.c.a(new d()); | |
p = null; | |
} | |
private void b(int paramInt, InputStream paramInputStream) | |
{ | |
File localFile1 = new File(this.f); | |
File localFile2 = localFile1.getParentFile(); | |
if (!localFile2.exists()) | |
localFile2.mkdirs(); | |
if (!localFile1.exists()) | |
localFile1.createNewFile(); | |
BufferedOutputStream localBufferedOutputStream = new BufferedOutputStream(new FileOutputStream(localFile1)); | |
byte[] arrayOfByte = new byte[10240]; | |
int i1 = 0; | |
while (true) | |
{ | |
int i2 = paramInputStream.read(arrayOfByte); | |
if ((i2 == -1) || (this.o)) | |
break; | |
i1 += i2; | |
localBufferedOutputStream.write(arrayOfByte, 0, i2); | |
if (this.i) | |
a(i1, paramInt); | |
} | |
localBufferedOutputStream.close(); | |
paramInputStream.close(); | |
if (!this.o) | |
{ | |
f(); | |
if ((this.j) && (!this.o)) | |
g(); | |
} | |
} | |
private void b(String paramString) | |
{ | |
if ((paramString.startsWith("application/xml")) || (paramString.startsWith("text/xml"))) | |
{ | |
this.e = o.b; | |
return; | |
} | |
if ((paramString.startsWith("application/json")) || (paramString.startsWith("text/json")) || (paramString.startsWith("application/javascript")) || (paramString.startsWith("text/javascript"))) | |
{ | |
this.e = o.d; | |
return; | |
} | |
if ((paramString.startsWith("image/")) || (paramString.startsWith("audio/")) || (paramString.startsWith("application/octet-stream"))) | |
{ | |
this.e = o.c; | |
return; | |
} | |
this.e = o.b; | |
} | |
private void b(byte[] paramArrayOfByte) | |
{ | |
GameActivity.b.c.a(new k(this, paramArrayOfByte)); | |
} | |
private void c(String paramString) | |
{ | |
GameActivity.b.c.a(new i(this, paramString)); | |
} | |
private void d(String paramString) | |
{ | |
GameActivity.b.c.a(new j(this, paramString)); | |
} | |
private void e() | |
{ | |
if (this.l > 0) | |
{ | |
this.m = new Timer(this.connectionName + "_timeroutTimer", true); | |
this.n = new f(this); | |
this.m.schedule(this.n, this.l); | |
} | |
} | |
private void e(String paramString) | |
{ | |
if (!this.b) | |
com.tap4fun.spartanwar.utils.system.a.a("ExtHttpConnection", "Connection failed without initiative cancellation, errorMsg: " + paramString); | |
GameActivity.b.c.a(new b(this, paramString)); | |
} | |
private void f() | |
{ | |
this.c = true; | |
if (this.n != null) | |
{ | |
this.n.cancel(); | |
this.n = null; | |
} | |
if (this.m != null) | |
{ | |
this.m.cancel(); | |
this.m.purge(); | |
this.m = null; | |
} | |
} | |
private void g() | |
{ | |
GameActivity.b.c.a(new l(this)); | |
} | |
private static native void initJNI(); | |
public static void openURL(String paramString) | |
{ | |
String str = a(paramString); | |
GameActivity.a.post(new c(str)); | |
} | |
private static native void releaseJNI(); | |
public void cancle() | |
{ | |
this.a.disconnect(); | |
Object[] arrayOfObject = new Object[2]; | |
arrayOfObject[0] = this.a.getRequestMethod(); | |
arrayOfObject[1] = this.a.getURL().toExternalForm(); | |
com.tap4fun.spartanwar.utils.system.a.e("ExtHttpConnection", String.format("Canceling connection: Method: %s, URL: %s", arrayOfObject)); | |
this.b = true; | |
} | |
public boolean isCanceled() | |
{ | |
return this.b; | |
} | |
public boolean isFinished() | |
{ | |
return this.c; | |
} | |
public native void jniCallLuaBinaryCallback(int paramInt, byte[] paramArrayOfByte); | |
public native void jniCallLuaDownloadCallback(int paramInt, String paramString); | |
public native void jniCallLuaErrorCallback(int paramInt, String paramString); | |
public native void jniCallLuaJsonCallback(int paramInt, String paramString); | |
public native void jniCallLuaProgressCallback(float paramFloat, byte[] paramArrayOfByte); | |
public native void jniCallLuaTextCallback(int paramInt, String paramString); | |
public native void jniLuaLError(String paramString); | |
public void setHasAuthCallBack(boolean paramBoolean) | |
{ | |
this.h = paramBoolean; | |
} | |
public void setHasCommonCallBack(boolean paramBoolean) | |
{ | |
this.j = paramBoolean; | |
} | |
public void setHasProgressCallBack(boolean paramBoolean) | |
{ | |
this.i = paramBoolean; | |
} | |
public void setHeaderValue(String paramString1, String paramString2) | |
{ | |
this.a.setRequestProperty(paramString1, paramString2); | |
} | |
public void setLocalPath(String paramString) | |
{ | |
this.f = paramString; | |
} | |
public void setRequestBody(String paramString) | |
{ | |
if (this.a.getRequestMethod().equals("POST")) | |
{ | |
this.a.setDoOutput(true); | |
this.a.setFixedLengthStreamingMode(paramString.getBytes().length); | |
this.k = paramString; | |
} | |
} | |
public void setRequestMethod(String paramString) | |
{ | |
try | |
{ | |
this.a.setRequestMethod(paramString.toUpperCase()); | |
return; | |
} | |
catch (ProtocolException localProtocolException) | |
{ | |
localProtocolException.printStackTrace(); | |
} | |
} | |
public void setRequestMode(int paramInt) | |
{ | |
switch (e.a[p.a(paramInt).ordinal()]) | |
{ | |
default: | |
return; | |
case 1: | |
this.d = p.b; | |
return; | |
case 2: | |
} | |
this.d = p.a; | |
} | |
public void setRequsetFormat(int paramInt) | |
{ | |
this.e = o.a(paramInt); | |
} | |
public void setTimeOut(double paramDouble) | |
{ | |
switch (e.a[this.d.ordinal()]) | |
{ | |
default: | |
return; | |
case 2: | |
this.l = (1000 * (int)paramDouble); | |
return; | |
case 1: | |
} | |
this.l = 0; | |
} | |
public void start() | |
{ | |
setHeaderValue("AndroidID", com.tap4fun.spartanwar.utils.system.b.n()); | |
setHeaderValue("DeviceID", com.tap4fun.spartanwar.utils.system.b.o()); | |
new a(this, this.connectionName).start(); | |
e(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment