revert "fixed redundant thread creation" b9a55799

it breaks Telegram stickers downloading, we'll investigate later
This commit is contained in:
aliveoutside
2022-10-09 15:54:26 +03:00
parent bcfefe777d
commit 9d2878b9dc
3 changed files with 48 additions and 50 deletions
@@ -1,6 +1,7 @@
package ru.vtosters.lite.net;
import android.util.Base64;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -14,30 +15,24 @@ import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Executor;
public final class NetCall {
private final NetRequest req;
private final NetClient cl;
private final Executor executor;
protected NetCall(NetClient cl, NetRequest req, Executor executor) {
protected NetCall(NetClient cl, NetRequest req) {
this.req = req;
this.cl = cl;
this.executor = executor;
}
private static String constructArgs(Map<String, String> args) throws UnsupportedEncodingException {
args = new TreeMap<>(args);
StringBuilder str = new StringBuilder();
String str = "";
for (Map.Entry<String, String> en : args.entrySet()) {
if (str.length() > 0)
str.append("&");
str.append(URLEncoder.encode(en.getKey(), "UTF-8"))
.append("=")
.append(URLEncoder.encode(en.getValue(), "UTF-8"));
if (!str.isEmpty()) str += "&";
str += URLEncoder.encode(en.getKey(), "UTF-8") + "=" + URLEncoder.encode(en.getValue(), "UTF-8");
}
return str.toString();
return str;
}
public NetRequest request() {
@@ -82,13 +77,22 @@ public final class NetCall {
}
public void enqueue(NetCallback cb) {
executor.execute(() -> {
try {
NetResponse resp = execute();
cb.onResponse(NetCall.this, resp);
} catch (IOException e) {
cb.onFailure(NetCall.this, e);
new Thread() {
@Override
public void run() {
try {
NetResponse resp = execute();
cb.onResponse(NetCall.this, resp);
} catch (IOException e) {
cb.onFailure(NetCall.this, e);
}
try {
interrupt();
join();
} catch (Exception e) {
Log.e("NetCall", "Failed to interrupt and join thread", e);
}
}
});
}.start();
}
}
@@ -2,16 +2,12 @@ package ru.vtosters.lite.net;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class NetClient {
private final long timeout;
private final Proxy proxy;
private final PasswordAuthentication authenticator;
private final Executor executor
= Executors.newSingleThreadExecutor();
protected NetClient(Builder b) {
timeout = b.timeout;
@@ -28,7 +24,7 @@ public class NetClient {
}
public NetCall newCall(NetRequest req) {
return new NetCall(this, req, executor);
return new NetCall(this, req);
}
public PasswordAuthentication getAuthenticator() {
@@ -2,77 +2,75 @@ package ru.vtosters.lite.net;
import static ru.vtosters.lite.utils.AccountManagerUtils.getUserToken;
import android.os.AsyncTask;
import com.vk.core.network.Network;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
public class Request {
private static final Executor EXECUTOR =
Executors.newSingleThreadExecutor();
public static void makeRequest(byte[] bArr, RequestCallback callback) {
EXECUTOR.execute(new MakeRequestTask(bArr, callback));
public static void makeRequest(byte[] bArr, RequestCallback RequestCallback) {
new MakeRequestTask(bArr, RequestCallback).execute();
}
public static void makeRequest(String str, RequestCallback callback) {
makeRequest(str.getBytes(), callback);
public static void makeRequest(String str, RequestCallback RequestCallback) {
new MakeRequestTask(str.getBytes(), RequestCallback).execute();
}
public interface RequestCallback {
void onResponse(String str);
}
private static class MakeRequestTask implements Runnable {
private static class MakeRequestTask extends AsyncTask<Void, Void, String> {
private final RequestCallback callback;
private final byte[] url;
private final boolean mIsPut;
private final boolean mIsVTostersRequest;
public MakeRequestTask(byte[] bArr, RequestCallback callback) {
this.mIsVTostersRequest = false;
this.mIsPut = false;
this.callback = callback;
this.url = bArr;
public MakeRequestTask(byte[] bArr, RequestCallback RequestCallback) {
mIsVTostersRequest = false;
mIsPut = false;
callback = RequestCallback;
url = bArr;
}
@Override
public void run() {
public String doInBackground(Void... voidArr) {
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(new String(url)).openConnection();
httpURLConnection.setRequestMethod(mIsPut ? "PUT" : "GET");
if (mIsVTostersRequest) {
httpURLConnection.setRequestProperty("Token", getUserToken());
} else {
}
if (!mIsVTostersRequest) {
httpURLConnection.setRequestProperty("User-Agent", Network.l.c().a());
}
httpURLConnection.setRequestProperty("Content-Type", mIsVTostersRequest
? "Content-Type"
: "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Type", mIsVTostersRequest ? "Content-Type" : "application/x-www-form-urlencoded");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuilder builder = new StringBuilder();
StringBuffer stringBuffer = new StringBuffer();
while (true) {
String readLine = bufferedReader.readLine();
if (readLine != null) {
builder.append(readLine);
stringBuffer.append(readLine);
} else {
bufferedReader.close();
callback.onResponse(builder.toString());
break;
return stringBuffer.toString();
}
}
} catch (Exception e) {
callback.onResponse(e.getLocalizedMessage());
return e.getLocalizedMessage();
}
}
public void onPostExecute(String str) {
super.onPostExecute(str);
callback.onResponse(str);
}
}
}